Quick start

Import the package with:

import infomap

A simple example:

import networkx as nx
import infomap

graph = nx.karate_club_graph()
communities = infomap.find_communities(
    graph,
    seed=123,
    num_trials=20,
)

print(communities)

For direct control over Infomap-specific options and result access:

from infomap import Infomap

# Command line flags can be provided as a string
im = Infomap("--two-level --directed")

# Add weight as optional third argument
im.add_link(0, 1)
im.add_link(0, 2)
im.add_link(0, 3)
im.add_link(1, 0)
im.add_link(1, 2)
im.add_link(2, 1)
im.add_link(2, 0)
im.add_link(3, 0)
im.add_link(3, 4)
im.add_link(3, 5)
im.add_link(4, 3)
im.add_link(4, 5)
im.add_link(5, 4)
im.add_link(5, 3)

# Run the search
im.run()

print(f"Found {im.num_top_modules} modules with codelength: {im.codelength}")

print("Result")
print("\n#node module")
for node in im.tree:
    if node.is_leaf:
        print(node.node_id, node.module_id)

For interactive exploration, start:

infomap-shell

This opens a Python shell with im = Infomap(pretty=True) ready to use. Run options() to inspect the generated InfomapOptions reference and summary() to print the current network or result state.