Network¶
Network is the first-class input builder. Construct it from a graph
library with a from_* constructor, or build it incrementally with the
add_* verbs, then partition it with infomap.run() or
Network.run().
- class infomap.Network(core=None)¶
Bases:
objectA first-class Infomap network input builder.
Build a network with the fluent
add_*/set_*verbs (each returnsself), then run it viarun()or the functionalinfomap.run(). Both return an immutableResult.Networkis an in-memory builder: it runs silently and does not write output files (its engine is created with--silent --no-file-output, and that cannot be turned off per run). Read results off the returnedResult. If you need Infomap to write.tree/.cluoutput files, useInfomapinstead.Examples
Build a small network and run it directly:
>>> from infomap import Network >>> net = ( ... Network() ... .add_link(1, 2) ... .add_link(1, 3) ... .add_link(2, 3) ... .add_link(3, 4) ... .add_link(4, 5) ... .add_link(4, 6) ... .add_link(5, 6) ... ) >>> result = net.run(options={"silent": True}) >>> result.num_top_modules 2 >>> for node_id, module_id in sorted(result.modules().items()): ... print(node_id, module_id) 1 1 2 1 3 1 4 2 5 2 6 2
A
Networkcan also be handed toinfomap.run():>>> from infomap import run >>> result = run(net, silent=True) >>> result.num_top_modules 2
- Parameters:
core (optional) – Internal engine handle to build onto. If
None, a default silent, no-file-outputCoreis created and owned by thisNetwork. When composed byinfomap.Infomap, the shared, options-configuredCoreis passed in.
- classmethod from_edge_index(edge_index, *, edge_weight=None, num_nodes=None, directed=True, node_ids=None)¶
Build a
Networkfrom a PyG-style edge index.Loads
edge_indexvia the same adapterInfomap.add_edge_index()uses. The{internal_id: label}mapping is stored onnode_id_to_label.
- classmethod from_file(path, *, accumulate=True)¶
Build a
Networkby reading a network file.- Parameters:
path (str or os.PathLike) – Path to an Infomap-readable network file.
accumulate (bool, optional) – Accumulate onto already added nodes and links. Default
True.
- classmethod from_igraph(g, *, edge_weights=None, vertex_weights=None, node_id='node_id', layer_id='layer_id', multilayer_inter_intra_format=True, meta_attribute=None)¶
Build a
Networkfrom a python-igraph graph.Loads
gvia the same adapterInfomap.add_igraph_graph()uses. The{internal_id: label}mapping is stored onnode_id_to_label. Passmeta_attributeto use a vertex attribute as Infomap meta data (values are encoded to integers, so string categories work).
- classmethod from_networkx(g, *, weight='weight', node_id='node_id', layer_id='layer_id', multilayer_inter_intra_format=True, meta_attribute=None)¶
Build a
Networkfrom a NetworkX graph.Loads
gvia the same adapterInfomap.add_networkx_graph()uses. The{internal_id: label}mapping is stored onnode_id_to_label. Passmeta_attributeto use a node attribute as Infomap meta data (values are encoded to integers, so string categories work).
- classmethod from_scipy_sparse_matrix(A, *, directed=False, weighted=True, node_ids=None)¶
Build a
Networkfrom a SciPy sparse adjacency matrix.Loads
Avia the same adapterInfomap.add_scipy_sparse_matrix()uses. The{internal_id: label}mapping is stored onnode_id_to_label.
- add_link(source_id, target_id, weight=1.0)¶
Add a link.
- add_links(links)¶
Add several links.
- Parameters:
links (iterable of tuples or numpy.ndarray) – Iterable of tuples of int of the form
(source_id, target_id, [weight]). NumPy arrays must be 2-dimensional with 2 or 3 columns.
- add_multilayer_inter_link(source_layer_id, node_id, target_layer_id, weight=1.0)¶
Add an inter-layer link.
- add_multilayer_inter_links(links)¶
Add several inter-layer links.
- Parameters:
links (iterable of tuples) – Iterable of tuples of the form
(source_layer_id, node_id, target_layer_id, [weight]). NumPy arrays must be 2-dimensional with 3 or 4 columns.
- add_multilayer_intra_link(layer_id, source_node_id, target_node_id, weight=1.0)¶
Add an intra-layer link.
- add_multilayer_intra_links(links)¶
Add several intra-layer links.
- Parameters:
links (iterable of tuples) – Iterable of tuples of the form
(layer_id, source_node_id, target_node_id, [weight]). NumPy arrays must be 2-dimensional with 3 or 4 columns.
- add_multilayer_link(source_multilayer_node, target_multilayer_node, weight=1.0)¶
Add a multilayer link.
- Parameters:
source_multilayer_node (tuple of int, or MultilayerNode) –
(layer_id, node_id).target_multilayer_node (tuple of int, or MultilayerNode) –
(layer_id, node_id).weight (float, optional)
- add_multilayer_links(links)¶
Add several multilayer links.
- Parameters:
links (iterable of tuples) – Iterable of tuples of the form
(source_node, target_node, [weight]). NumPy arrays must be 2-dimensional with 4 or 5 columns of the form(source_layer_id, source_node_id, target_layer_id, target_node_id, [weight]).
- add_node(node_id, name=None, teleportation_weight=None)¶
Add a node.
- add_nodes(nodes)¶
Add nodes.
- add_state_node(state_id, node_id)¶
Add a state node.
- add_state_nodes(state_nodes)¶
Add state nodes.
- Parameters:
state_nodes (iterable of tuples or dict of int: int) – Iterable of tuples of the form
(state_id, node_id)or dict of the form{state_id: node_id}.
- read_file(filename, accumulate=True)¶
Read network data from file.
- remove_links(links)¶
Remove several links.
- Parameters:
links (iterable of tuples) – Iterable of tuples of the form
(source_id, target_id).
- run(*, options=None, args=None, initial_partition=None)¶
Run Infomap on this network and return a
Result.- Parameters:
options (Options, mapping, or None, optional) – Configuration rendered to Infomap CLI flags for this run.
args (str, optional) – Raw Infomap arguments prepended before the rendered options.
initial_partition (mapping, optional) – Initial module assignment for this run only, keyed by internal node id (
{node_or_state_id: module_id}) or, for a multilayer network,{(layer_id, node_id): module_id}. The internal ids are the ones you built the network with (the values ofnode_id_to_label).
- Returns:
An immutable snapshot of the run, bound to this
Network.- Return type:
- set_meta_data(node_id, meta_category=None)¶
Set integer meta data for one node, or for many at once.
- set_names(names)¶
Set names to several nodes at once.
- Parameters:
names (iterable of tuples or dict of int: str) – Iterable of tuples on the form
(node_id, name)or dict of the form{node_id: name}.
- property bipartite_start_id¶
Get or set the bipartite start id.
- Returns:
The node id where the second node type starts.
- Return type:
- property network¶
The underlying SWIG state network.
Exposed so the graph adapters (which reach for
infomap.network.add_multilayer_node/add_multilayer_state_link) work identically when building onto aNetworkor anInfomap.