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 nodenameplus the module/path/flow attributes. Edges come from the partitioned network, with their weights asweight. The attribute values keep their native types: module ids areint, flows and weights arefloat, and the tree path is a colon-joinedstrsuch as"1:3".- Parameters:
result (Result) – A result returned by
infomap.run()orInfomap.run().module_attribute (str or None, optional) – Node attribute for the top-level module id. Default
"infomap_module". UseNoneto omit.path_attribute (str or None, optional) – Node attribute for the colon-joined tree path. Default
"infomap_path". UseNoneto omit.include_hierarchy (bool, optional) – Also write per-level
infomap_level_{n}attributes. DefaultTrue.flow_attribute (str or None, optional) – Node attribute for the node flow. Default
"flow". UseNoneto omit.
- Returns:
DiGraphwhen the partitioned network is directed, elseGraph.- Return type:
See also
infomap.Result.to_networkxThe same conversion as a
Resultmethod.annotate_networkx_graphAnnotate 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
nameplus the module/path/flow attributes. Edges come from the partitioned network, with their weights asweight. The attribute values keep their native types, as into_networkx().- Parameters:
result (Result) – A result returned by
infomap.run()orInfomap.run().module_attribute – See
to_networkx().path_attribute – See
to_networkx().include_hierarchy – See
to_networkx().flow_attribute – See
to_networkx().
- Returns:
Directed when the partitioned network is directed, else undirected.
- Return type:
igraph.Graph
See also
infomap.Result.to_igraphThe same conversion as a
Resultmethod.annotate_igraph_graphAnnotate 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
Infomapinstance, or theResultit returned (the result’s engine is used). Raises ifrun()has not been called.node_mapping (mapping, optional) – Mapping from internal Infomap (state) ids to graph node labels, as returned by the
add_*_graphadapters. 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". UseNoneto omit.path_attribute (str or None, optional) – Node attribute for the colon-joined tree path. Default
"infomap_path". UseNoneto omit.include_hierarchy (bool, optional) – Also write per-level
infomap_level_{n}attributes (and the path attribute). DefaultTrue.flow_attribute (str or None, optional) – Node attribute for the node flow. Default
None(omitted).copy (bool, optional) – Annotate a copy of
graphinstead of modifying it in place. DefaultTrue.strict (bool, optional) – Raise
ValueErrorwhen the graph nodes and the Infomap assignments do not match exactly. IfFalse, only the matching nodes are annotated and a warning is emitted. DefaultTrue.
- Returns:
The annotated graph (a copy when
copy=True, otherwisegraph).- Return type:
- 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
Infomapinstance, or theResultit returned (the result’s engine is used). Raises ifrun()has not been called.module_attribute (str or None, optional) – Vertex attribute for the top-level module id. Default
"infomap_module". UseNoneto omit.path_attribute (str or None, optional) – Vertex attribute for the colon-joined tree path. Default
"infomap_path". UseNoneto omit.include_hierarchy (bool, optional) – Also write per-level
infomap_level_{n}attributes (and the path attribute). DefaultTrue.flow_attribute (str or None, optional) – Vertex attribute for the node flow. Default
None(omitted).copy (bool, optional) – Annotate a copy of
graphinstead of modifying it in place. DefaultTrue.strict (bool, optional) – Raise
ValueErrorwhen the graph vertices and the Infomap assignments do not match exactly. IfFalse, only the matching vertices are annotated and a warning is emitted. DefaultTrue.
- Returns:
The annotated graph (a copy when
copy=True, otherwisegraph).- 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:
graph (networkx.Graph or igraph.Graph) – The graph to annotate and write.
im (Infomap or Result) – A run
Infomapinstance, or theResultit returned (the result’s engine is used).path (str or pathlib.Path) – Output file path.
**options – Passed through to
annotate_networkx_graph()orannotate_igraph_graph()(e.g.module_attribute,flow_attribute,strict). The special keywriter_options(a dict) is instead passed to the underlying GraphML writer.
- 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
Infomapinstance, or theResultit 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 keywriter_options(a dict) is instead passed tonetworkx.write_gexf.
- Return type:
None
- Raises:
NotImplementedError – If
graphis a python-igraph graph: python-igraph has no native GEXF writer. Usewrite_graphml()or pass a NetworkX graph.