Integrations¶
Scanpy / AnnData¶
- infomap.tl.infomap(adata: Any, *, adjacency: Any = None, directed: bool = False, use_weights: bool = True, key_added: str = 'infomap', neighbors_key: str | None = None, obsp: str | None = None, copy: bool = False, args: str | None = None, **infomap_options: Any) Any¶
Run Infomap on an AnnData observation graph.
This function follows Scanpy
tlconventions: by default it readsadata.obsp["connectivities"], writes categorical module labels toadata.obs[key_added], and stores run metadata inadata.uns[key_added]. Scanpy itself is not imported.- Parameters:
adata (AnnData) – An AnnData-like object with
.obs,.obsp,.uns, and.obs_names.adjacency (sparse matrix, optional) – Square observation-by-observation adjacency matrix to use instead of reading one from
adata.obsp. Mutually exclusive withneighbors_keyandobsp.directed (bool, optional) – Treat the adjacency as directed. Default
False.use_weights (bool, optional) – Use the adjacency values as link weights. If
False, every nonzero entry is treated as weight 1. DefaultTrue.key_added (str, optional) – Key under which module labels are written to
adata.obsand run metadata toadata.uns. Default"infomap".neighbors_key (str, optional) – Read the adjacency from the
obspkey named byadata.uns[neighbors_key]["connectivities_key"]. Mutually exclusive withadjacencyandobsp.obsp (str, optional) – Read the adjacency from
adata.obsp[obsp]. Default"connectivities". Mutually exclusive withadjacencyandneighbors_key.copy (bool, optional) – Operate on (and return) a copy of
adatainstead of modifying it in place. DefaultFalse.args (str, optional) – Raw Infomap CLI arguments passed to
Infomap.**infomap_options – Keyword arguments passed to
Infomap. By default,silent=Trueandno_file_output=Trueare used unless explicitly overridden.
- Returns:
The annotated copy when
copy=True, otherwiseNoneandadatais modified in place. In both cases,adata.obs[key_added]holds the module labels as a categorical (string categories sorted by integer value) andadata.uns[key_added]holds a dict with the runparams, the number of top modulesn_modules, and thecodelength.- Return type:
AnnData or None
GraphRAG¶
- infomap.tl.graphrag.read_graphrag(entities, relationships, *, entity_id_col='id', entity_title_col='title', source_col='source', target_col='target', weight_col='weight', relationship_id_col='id', endpoint_col='title') GraphRAGGraph¶
Read GraphRAG-style entity and relationship Parquet tables.
Assigns a 1-based Infomap node id to every entity (and to any relationship endpoint that does not appear in the entity table) and converts the relationships to source/target/weight arrays ready for
infomap.Infomap.add_links().- Parameters:
entities (str, pathlib.Path, or pandas.DataFrame) – Entity table, or a path to a Parquet file containing it.
relationships (str, pathlib.Path, or pandas.DataFrame) – Relationship table, or a path to a Parquet file containing it.
entity_id_col (str, optional) – Entity table column with unique entity ids. Default
"id".entity_title_col (str, optional) – Entity table column with entity titles. Default
"title".source_col (str, optional) – Relationship table column with source endpoints. Default
"source".target_col (str, optional) – Relationship table column with target endpoints. Default
"target".weight_col (str or None, optional) – Relationship table column with link weights, or
Nonefor unweighted links. Default"weight".relationship_id_col (str or None, optional) – Relationship table column with relationship ids. If missing or
None, positional indices are used. Default"id".endpoint_col (str, optional) – Which entity column the relationship endpoints reference:
"title"(default, requires unique titles) or"id".
- Returns:
The tables together with the link arrays and the mappings between Infomap node ids and entity ids/titles.
- Return type:
- infomap.tl.graphrag.run_graphrag_communities(*, input_dir, output_dir=None, args=None, entities_name='entities.parquet', relationships_name='relationships.parquet', entity_id_col='id', entity_title_col='title', source_col='source', target_col='target', weight_col='weight', relationship_id_col='id', endpoint_col='title', silent=True, seed=123, num_trials=5, **infomap_options) GraphRAGRunResult¶
Read GraphRAG tables, run Infomap, and write community outputs.
End-to-end convenience wrapper around
read_graphrag(), an Infomap run, andwrite_graphrag_communities().- Parameters:
input_dir (str or pathlib.Path) – Directory containing the entity and relationship Parquet files.
output_dir (str or pathlib.Path, optional) – Where to write
infomap_nodes.parquet,communities.parquet, and the run summaryinfomap_run.json. IfNone, nothing is written and only the in-memory result is returned.args (str, optional) – Raw Infomap CLI arguments passed to
Infomap.entities_name (str, optional) – Entity file name inside
input_dir. Default"entities.parquet".relationships_name (str, optional) – Relationship file name inside
input_dir. Default"relationships.parquet".entity_id_col (str, optional) – Column names passed to
read_graphrag().entity_title_col (str, optional) – Column names passed to
read_graphrag().source_col (str, optional) – Column names passed to
read_graphrag().target_col (str, optional) – Column names passed to
read_graphrag().weight_col (str, optional) – Column names passed to
read_graphrag(); see there for details.relationship_id_col (str, optional) – Column names passed to
read_graphrag(); see there for details.endpoint_col (str, optional) – Column names passed to
read_graphrag(); see there for details.silent (bool, optional) – Suppress Infomap console output. Default
True.seed (int, optional) – Random number generator seed. Default
123.num_trials (int, optional) – Number of independent trials; the best solution is kept. Default
5.**infomap_options – Additional keyword arguments passed to
Infomap.
- Returns:
The Infomap instance, the parsed graph, the output directory, and the nodes/communities tables.
- Return type:
- Raises:
TypeError – If
options=is passed; useargs=for raw CLI arguments.ValueError – If
summary_jsonis passed; it is managed throughoutput_dir.
- infomap.tl.graphrag.write_graphrag_communities(im, *, graph: GraphRAGGraph, output)¶
Write GraphRAG-compatible community tables from a finished run.
- Parameters:
im (Infomap) – An
Infomapinstance that has been run on the links ofgraph.graph (GraphRAGGraph) – The graph returned by
read_graphrag()for the same network.output (str or pathlib.Path) – Output directory, or a
.parquetpath for the communities table (its parent directory is then used for the nodes table). Writesinfomap_nodes.parquetandcommunities.parquet.
- Returns:
The
(nodes, communities)tables that were written.- Return type:
- Raises:
ValueError – If
outputisNone.
- class infomap.tl.graphrag.GraphRAGGraph(entities: Any, relationships: Any, sources: Any, targets: Any, weights: Any | None, entity_id_to_node_id: dict[Any, int], node_id_to_entity_id: dict[int, Any], node_id_to_entity_title: dict[int, Any], endpoint_to_node_id: dict[Any, int], entity_title_to_node_id: dict[Any, int], relationship_ids: list[Any], entity_id_col: str)¶
GraphRAG-style tables converted to Infomap node ids.
The mapping fields are advanced plumbing for callers that need to join Infomap node ids back to the original GraphRAG entity and relationship tables.
- class infomap.tl.graphrag.GraphRAGRunResult(infomap: Any, graph: GraphRAGGraph, output_dir: Path | None, nodes: Any, communities: Any)¶
Result object returned by
run_graphrag_communities().
Distributed trials¶
- infomap.merge.merge_trial_results(patterns: Sequence[str], out_name: str, formats: Sequence[str] = ('tree', 'clu'), require_complete: bool = False) MergeSummary¶
Merge distributed Infomap trial-results shards into final output.
- Parameters:
patterns (sequence of str) – Shard result file paths or glob patterns (each may be comma-separated).
out_name (str) – Output basename;
<out_name>.tree/<out_name>.cluare written.formats (sequence of str, optional) – Which output formats to write. Only
treeandcluare supported (the merge has no network, so link-bearing formats cannot be produced).require_complete (bool, optional) – If True, raise when any global trial index in
[0, max]is missing. DefaultFalse.
- Returns:
Summary with the winning
trialindex,codelength, the resolved winner tree path, the number of shards/trials, anymissingindices, and the list ofoutputswritten.- Return type:
- class infomap.merge.MergeSummary¶
Summary returned by
merge_trial_results().
- exception infomap.merge.MergeError¶
Raised when shard files are missing, inconsistent, or unmergeable.