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: object

A first-class Infomap network input builder.

Build a network with the fluent add_*/set_* verbs (each returns self), then run it via run() or the functional infomap.run(). Both return an immutable Result.

Network is 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 returned Result. If you need Infomap to write .tree/.clu output files, use Infomap instead.

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 Network can also be handed to infomap.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-output Core is created and owned by this Network. When composed by infomap.Infomap, the shared, options-configured Core is passed in.

classmethod from_edge_index(edge_index, *, edge_weight=None, num_nodes=None, directed=True, node_ids=None)

Build a Network from a PyG-style edge index.

Loads edge_index via the same adapter Infomap.add_edge_index() uses. The {internal_id: label} mapping is stored on node_id_to_label.

classmethod from_file(path, *, accumulate=True)

Build a Network by 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 Network from a python-igraph graph.

Loads g via the same adapter Infomap.add_igraph_graph() uses. The {internal_id: label} mapping is stored on node_id_to_label. Pass meta_attribute to 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 Network from a NetworkX graph.

Loads g via the same adapter Infomap.add_networkx_graph() uses. The {internal_id: label} mapping is stored on node_id_to_label. Pass meta_attribute to 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 Network from a SciPy sparse adjacency matrix.

Loads A via the same adapter Infomap.add_scipy_sparse_matrix() uses. The {internal_id: label} mapping is stored on node_id_to_label.

Add a link.

Parameters:
  • source_id (int)

  • target_id (int)

  • weight (float, optional)

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 an inter-layer link.

Parameters:
  • source_layer_id (int)

  • node_id (int)

  • target_layer_id (int)

  • weight (float, optional)

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 an intra-layer link.

Parameters:
  • layer_id (int)

  • source_node_id (int)

  • target_node_id (int)

  • weight (float, optional)

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 a multilayer link.

Parameters:

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.

Parameters:
  • node_id (int)

  • name (str, optional)

  • teleportation_weight (float, optional) – Used for teleporting between layers in multilayer networks.

add_nodes(nodes)

Add nodes.

Parameters:

nodes (iterable of tuples or iterable of int or dict) – Iterable of tuples on the form (node_id, [name], [teleportation_weight]).

add_state_node(state_id, node_id)

Add a state node.

Parameters:
  • state_id (int)

  • node_id (int) – Id of the physical node the state node should be added to.

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.

Parameters:
  • filename (str)

  • accumulate (bool, optional) – If the network data should be accumulated to already added nodes and links. Default True.

Remove a link.

Parameters:
  • source_id (int)

  • target_id (int)

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 of node_id_to_label).

Returns:

An immutable snapshot of the run, bound to this Network.

Return type:

Result

set_meta_data(node_id, meta_category=None)

Set integer meta data for one node, or for many at once.

Parameters:
  • node_id (int or mapping) – A node id, or a {node_id: meta_category} mapping to assign meta data to several nodes in one call.

  • meta_category (int, optional) – The meta category, when node_id is a single node id (ignored when node_id is a mapping).

set_name(node_id, name)

Set the name of a node.

Parameters:
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:

int

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 a Network or an Infomap.

node_id_to_label: dict[int, Any]

{internal_id: label} mapping set by the library constructors (from_networkx/from_igraph/from_scipy_sparse_matrix/from_edge_index). Bare annotation: declares the instance attribute for type-checkers without creating it at runtime (no behaviour change).

The number of links.

Returns:

The number of links.

Return type:

int

property num_nodes

The number of state nodes if a higher-order network, else physical.

Returns:

The number of nodes.

Return type:

int