napistu.network.clustering

Functions for clustering an igraph.Graph’s vertices.

Public Functions

run_infomap(graph, source_attr, target_attr, weight_attr, directed, two_level, num_trials, seed, teleportation_probability, use_subprocess)

Run Infomap community detection on an igraph graph and return module assignments.

Functions

run_infomap(graph[, weight_attr, directed, ...])

Run Infomap community detection on an igraph graph and return module assignments.

napistu.network.clustering._run_infomap_local(edges: list, directed: bool, two_level: bool, num_trials: int, seed: int, teleportation_probability: float, markov_time: float) dict

Run Infomap in the current process.

Will conflict with PyTorch and other libraries that ship their own OpenMP runtime. Use _run_infomap_subprocess if this is a concern.

Parameters:
  • edges (list) – List of (source, target, weight) tuples.

  • directed (bool) – Whether to treat the graph as directed.

  • two_level (bool) – Whether to optimize a flat two-level partition.

  • num_trials (int) – Number of independent optimization runs.

  • seed (int) – Random seed.

  • teleportation_probability (float) – Probability of teleporting to a random node at each step.

Returns:

Dictionary mapping integer node_id to integer module_id.

Return type:

dict

napistu.network.clustering._run_infomap_subprocess(edges: list, n_vertices: int, directed: bool, two_level: bool, num_trials: int, seed: int, teleportation_probability: float, markov_time: float) dict

Run Infomap in an isolated subprocess to avoid OpenMP conflicts with PyTorch and other libraries that ship their own OpenMP runtime.

Parameters:
  • edges (list) – List of (source, target, weight) tuples.

  • n_vertices (int) – Number of vertices in the graph.

  • directed (bool) – Whether to treat the graph as directed.

  • two_level (bool) – Whether to optimize a flat two-level partition.

  • num_trials (int) – Number of independent optimization runs.

  • seed (int) – Random seed.

  • teleportation_probability (float) – Probability of teleporting to a random node at each step.

  • markov_time (float) – Markov time for the random walk.

Returns:

Dictionary mapping integer node_id to integer module_id.

Return type:

dict

napistu.network.clustering.run_infomap(graph: Graph, weight_attr: str = 'weight', directed: bool | None = None, two_level: bool = True, num_trials: int = 5, seed: int = 123, teleportation_probability: float = 0.15, markov_time: float = 1.0, use_subprocess: bool = True) Series

Run Infomap community detection on an igraph graph and return module assignments.

Parameters:
  • graph (ig.Graph) – Input graph.

  • weight_attr (str) – Edge attribute name for edge weights. Default “weight”. Larger weights indicate stronger associations and will be traversed more frequently by the random walker, biasing nodes toward the same module.

  • directed (bool, optional) – Whether to treat the graph as directed. Defaults to graph.is_directed().

  • two_level (bool) – If True, optimize a flat two-level partition rather than a deep hierarchy. Recommended for pathway-level interpretability. Default True.

  • num_trials (int) – Number of independent optimization runs. Best solution is kept. Default 5.

  • seed (int) – Random seed for reproducibility. Default 123.

  • teleportation_probability (float) – Probability of teleporting to a random node at each step (analogous to 1 - damping in PageRank). Default 0.15.

  • markov_time (float) – Markov time for the random walk. Default 1.0. Larger values produce coarser modules; smaller values produce finer modules.

  • use_subprocess (bool) – If True, run Infomap in an isolated subprocess to avoid OpenMP conflicts with PyTorch and other libraries that ship their own OpenMP runtime (libiomp5 vs libomp). Default True.

Returns:

  • pd.Series – Series indexed by vertex name (or integer id if no name attribute), containing integer module assignments. Isolated vertices (no edges) will have None as their module assignment.

  • Dependencies

  • ————

  • Requires the infomap package and, on macOS, libomp – brew install libomp pip install infomap

  • Note (a missing libomp will cause a kernel crash rather than a Python)

  • exception. If your notebook crashes on import or instantiation, install

  • libomp and restart the kernel before reimporting.