Export helpers

In-memory converters

to_networkx() and to_igraph() build a graph from a Result, annotated with the partition (infomap_module, infomap_path, per-level ids, and flow), ready for the graph library’s own writer.

infomap.to_networkx(result: Result, *, module_attribute: str | None = 'infomap_module', path_attribute: str | None = 'infomap_path', include_hierarchy: bool = True, flow_attribute: str | None = 'flow') networkx.Graph

Build a NetworkX graph from a Result.

Nodes are the result’s (state) nodes, keyed by state_id, carrying the Infomap node name plus the module/path/flow attributes. Edges come from the partitioned network, with their weights as weight. The attribute values keep their native types: module ids are int, flows and weights are float, and the tree path is a colon-joined str such as "1:3".

Parameters:
  • result (Result) – A result returned by infomap.run() or Infomap.run().

  • module_attribute (str or None, optional) – Node attribute for the top-level module id. Default "infomap_module". Use None to omit.

  • path_attribute (str or None, optional) – Node attribute for the colon-joined tree path. Default "infomap_path". Use None to omit.

  • include_hierarchy (bool, optional) – Also write per-level infomap_level_{n} attributes. Default True.

  • flow_attribute (str or None, optional) – Node attribute for the node flow. Default "flow". Use None to omit.

Returns:

DiGraph when the partitioned network is directed, else Graph.

Return type:

networkx.Graph or networkx.DiGraph

See also

infomap.Result.to_networkx

The same conversion as a Result method.

annotate_networkx_graph

Annotate an existing graph in place instead (string-valued attributes).

infomap.to_igraph(result: Result, *, module_attribute: str | None = 'infomap_module', path_attribute: str | None = 'infomap_path', include_hierarchy: bool = True, flow_attribute: str | None = 'flow') igraph.Graph

Build a python-igraph graph from a Result.

Vertices are the result’s (state) nodes in the order the result yields them, carrying the Infomap node name plus the module/path/flow attributes. Edges come from the partitioned network, with their weights as weight. The attribute values keep their native types, as in to_networkx().

Parameters:
Returns:

Directed when the partitioned network is directed, else undirected.

Return type:

igraph.Graph

See also

infomap.Result.to_igraph

The same conversion as a Result method.

annotate_igraph_graph

Annotate an existing graph in place instead (string-valued attributes).

File writers

These helpers accept a post-run stateful Infomap instance or the Result it returned. They live in the public infomap.io namespace; infomap.export is a back-compatibility alias for infomap.io.export.

infomap.io.export.annotate_networkx_graph(graph: networkx.Graph, im: Infomap | Result, *, node_mapping: Mapping[Any, Any] | None = None, module_attribute: str | None = 'infomap_module', path_attribute: str | None = 'infomap_path', include_hierarchy: bool = True, flow_attribute: str | None = None, copy: bool = True, strict: bool = True) networkx.Graph

Return a NetworkX graph annotated with Infomap result attributes.

Writes each node’s module assignment (and optionally its tree path, per-level module ids, and flow) as string-valued node attributes, suitable for GraphML/GEXF export.

Parameters:
  • graph (networkx.Graph) – The graph to annotate.

  • im (Infomap or Result) – A run Infomap instance, or the Result it returned (the result’s engine is used). Raises if run() has not been called.

  • node_mapping (mapping, optional) – Mapping from internal Infomap (state) ids to graph node labels, as returned by the add_*_graph adapters. If omitted, graph nodes are assumed to be the internal ids.

  • module_attribute (str or None, optional) – Node attribute for the top-level module id. Default "infomap_module". Use None to omit.

  • path_attribute (str or None, optional) – Node attribute for the colon-joined tree path. Default "infomap_path". Use None to omit.

  • include_hierarchy (bool, optional) – Also write per-level infomap_level_{n} attributes (and the path attribute). Default True.

  • flow_attribute (str or None, optional) – Node attribute for the node flow. Default None (omitted).

  • copy (bool, optional) – Annotate a copy of graph instead of modifying it in place. Default True.

  • strict (bool, optional) – Raise ValueError when the graph nodes and the Infomap assignments do not match exactly. If False, only the matching nodes are annotated and a warning is emitted. Default True.

Returns:

The annotated graph (a copy when copy=True, otherwise graph).

Return type:

networkx.Graph

infomap.io.export.annotate_igraph_graph(graph: igraph.Graph, im: Infomap | Result, *, module_attribute: str | None = 'infomap_module', path_attribute: str | None = 'infomap_path', include_hierarchy: bool = True, flow_attribute: str | None = None, copy: bool = True, strict: bool = True) igraph.Graph

Return a python-igraph graph annotated with Infomap result attributes.

Writes each vertex’s module assignment (and optionally its tree path, per-level module ids, and flow) as string-valued vertex attributes, suitable for GraphML export. Vertices are matched to Infomap state ids by igraph vertex index, as assigned by infomap.Infomap.add_igraph_graph().

Parameters:
  • graph (igraph.Graph) – The graph to annotate.

  • im (Infomap or Result) – A run Infomap instance, or the Result it returned (the result’s engine is used). Raises if run() has not been called.

  • module_attribute (str or None, optional) – Vertex attribute for the top-level module id. Default "infomap_module". Use None to omit.

  • path_attribute (str or None, optional) – Vertex attribute for the colon-joined tree path. Default "infomap_path". Use None to omit.

  • include_hierarchy (bool, optional) – Also write per-level infomap_level_{n} attributes (and the path attribute). Default True.

  • flow_attribute (str or None, optional) – Vertex attribute for the node flow. Default None (omitted).

  • copy (bool, optional) – Annotate a copy of graph instead of modifying it in place. Default True.

  • strict (bool, optional) – Raise ValueError when the graph vertices and the Infomap assignments do not match exactly. If False, only the matching vertices are annotated and a warning is emitted. Default True.

Returns:

The annotated graph (a copy when copy=True, otherwise graph).

Return type:

igraph.Graph

infomap.io.export.write_graphml(graph: networkx.Graph | igraph.Graph, im: Infomap | Result, path: str | Path, **options: Any) None

Write a GraphML file with Infomap result attributes on nodes.

Annotates graph (without modifying it) and writes it as GraphML. Accepts both NetworkX and python-igraph graphs; the graph type selects the annotate helper and writer.

Parameters:
Return type:

None

infomap.io.export.write_gexf(graph: networkx.Graph, im: Infomap | Result, path: str | Path, **options: Any) None

Write a GEXF file with Infomap result attributes on NetworkX nodes.

Annotates graph (without modifying it) and writes it as GEXF via NetworkX.

Parameters:
  • graph (networkx.Graph) – The graph to annotate and write.

  • im (Infomap or Result) – A run Infomap instance, or the Result it returned (the result’s engine is used).

  • path (str or pathlib.Path) – Output file path.

  • **options – Passed through to annotate_networkx_graph() (e.g. module_attribute, flow_attribute, strict). The special key writer_options (a dict) is instead passed to networkx.write_gexf.

Return type:

None

Raises:

NotImplementedError – If graph is a python-igraph graph: python-igraph has no native GEXF writer. Use write_graphml() or pass a NetworkX graph.