Result¶
Result is the immutable snapshot returned by infomap.run() and
Infomap.run(). Scalar metrics are properties; node and tree collections
are methods with defaults (modules(depth=1), nodes(), to_dataframe()).
- class infomap.Result(engine: Infomap, *, generation: int)¶
Immutable snapshot of an Infomap run.
Returned by
Infomap.run()(and the functionalinfomap.run()) and used to back the legacy on-instance accessors. Holds eager O(1) scalars; tree-derived metrics (theeffective_num_*modules) and node/tree/dataframe data are materialized lazily and guarded against the engine being re-run.Read scalars as properties and collections as methods (with defaults):
Examples
Run Infomap and read the partition off the
Result:>>> from infomap import Infomap >>> im = Infomap(silent=True) >>> im.add_links(((1, 2), (1, 3), (2, 3), (4, 5), (4, 6), (5, 6), (3, 4))) >>> result = im.run() >>> 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
Per-node views via
nodes()(a physical-node default; passstates=Truefor state nodes):>>> for node in sorted(result.nodes(), key=lambda n: n.node_id): ... print(node.node_id, node.module_id, f"{node.flow:.4f}") 1 1 0.1429 2 1 0.1429 3 1 0.2143 4 2 0.2143 5 2 0.1429 6 2 0.1429
A
Resultis a snapshot, not a live handle: re-running the sameInfomaprebuilds the C++ result tree, so node-level access on the oldResultraises. Eager scalars (codelength,num_top_modules, …) stay valid.- effective_num_modules(depth: int = 1) float¶
The flow-weighted effective number of modules at
depth.Measured as the perplexity of the module flow distribution. Equivalent to the legacy
Infomap.get_effective_num_modules(depth).- Parameters:
depth (int, optional) – The module level.
1(default) is the top (coarsest) level;-1the bottom (leaf-module) level.
- leaf_modules()¶
A view of the leaf modules (bottom modules containing leaf nodes), depth first from the root.
Equivalent to the legacy
Infomap.leaf_modulesiterator.- Returns:
Yields each leaf module (the live
InfomapLeafModuleIteratorpositions). The iterator is generation-guarded: consuming it after the bound engine re-runs raises instead of walking a rebuilt tree.- Return type:
generator
- links(data: str = 'weight')¶
A view of the partitioned links and their weights or flow.
Equivalent to the legacy
Infomap.get_links(data). The sources and targets are state ids for a state or multilayer network.- Parameters:
data (str, optional) – The kind of value to return, one of
"weight"or"flow". Default"weight".- Returns:
(source, target, weight_or_flow)tuples. The generator is generation-guarded liketree(): consuming it after the bound engine re-runs raises instead of reading the rebuilt engine.- Return type:
- modules(depth: int = 1, *, states: bool = False) dict¶
Map
node_id(orstate_idwhenstates) tomodule_id.Equivalent to the legacy
Infomap.get_modules(depth, states): for a higher-order (multilayer/memory) network, requesting physical-node modules withoutstatesis ambiguous and raises, mirroring the C++getModulesguard.
- multilevel_modules(*, states: bool = False) dict¶
Map
node_id(orstate_idwhenstates) to a tuple ofmodule_id, one per level from the top down.Equivalent to the legacy
Infomap.get_multilevel_modules(states).
- nodes(depth: int = 1, *, states: bool = False)¶
Iterate leaf
TreeNodeviews, depth first from the root.
- to_dataframe(columns: Sequence[str] | None = None, *, states: bool = False, level: int = 1, index: str | bool | None = None, sort: bool | str | Sequence[str] = False, depth_level: int | None = None) Any¶
A pandas DataFrame of the leaf nodes.
Byte-identical to the legacy
Infomap.to_dataframe. Default columns("node_id", "module_id", "flow", "path", "name");"community"is an alias for"module_id";"name"is resolved via the physical node name map (falling back to the integernode_id). The opt-in"state_name"column resolves the per-state-node name for a higher-order network (falling back to the physical name, thennode_id); seestate_names.
- tree(depth: int = 1, *, states: bool = False)¶
A view of the hierarchical tree, iterating over the modules as well as the leaf nodes, depth first from the root.
Equivalent to the legacy
Infomap.get_tree(depth, states). For a higher-order (multilayer/memory) network the physical tree is used unlessstatesis requested, mirroring the legacy semantics.- Parameters:
- Returns:
Yields each node in the tree (the live
InfomapIterator/InfomapIteratorPhysicalpositions), depth first from the root. The iterator is generation-guarded: consuming it after the bound engine re-runs raises instead of walking a rebuilt C++ tree.- Return type:
generator
- property effective_num_leaf_modules: float¶
The flow-weighted effective number of leaf modules.
Measured as the perplexity of the leaf-module flow distribution. Computed lazily on first access (see
effective_num_modules()).
- property effective_num_top_modules: float¶
The flow-weighted effective number of top modules.
Measured as the perplexity of the top-module flow distribution. Computed lazily on first access (see
effective_num_modules()).
- property max_depth: int¶
Alias of
num_levels.
TreeNode is the lightweight, immutable per-node view yielded by
Result.nodes().
- class infomap.TreeNode(*, node_id: int, state_id: int, module_id: int, flow: float, depth: int, layer_id: int, child_index: int, modular_centrality: float, path: tuple, name: Any, state_name: Any)¶
A lightweight, immutable view over a single leaf node of a
Result.Yielded by
Result.nodes(). A plain Python object: attribute access reads from the snapshot data theResultalready materialized, without touching the underlying C++ engine, so it stays valid after a re-run.- depth¶
The depth of the node in the tree (number of levels below the root; equals
len(path)).- Type:
- path¶
The tree path from the root as a tuple of one-based child indices (the colon-separated path in tree output files).