im.add_state_node(4, 2) im.add_state_node(5, 4) im.add_link(0, 1) im.add_link(1, 2) im.add_link(3, 2) im.add_link(4, 5) im.run() print( f"Found {im.num_top_modules} modules with codelength {im.codelength:.8f} bits" ) print("\n#node_id module") for node, module in im.get_modules(states=True).items(): print(node, module) print("\nState nodes:") print("#state_id node_id module_id") for node in im.nodes: print(node.state_id, node.node_id, node.module_id) print( "\nPhysical nodes (merging state nodes with same physical node id within modules):" ) print("#node_id module_id") for node in im.physical_tree: if node.is_leaf: print(node.node_id, node.module_id)
textcoords="offset points", horizontalalignment="center", verticalalignment="center", xytext=[0, 2], color=cmap_dark(communities[n]), ) plt.axis("off") pathlib.Path("output").mkdir(exist_ok=True) print("Writing network figure to output/karate.png") plt.savefig("output/karate.png") # plt.show() G = nx.karate_club_graph() print("Building Infomap network from a NetworkX graph...") im = Infomap(two_level=True, silent=True, num_trials=20) im.add_networkx_graph(G) print("Find communities with Infomap...") im.run() print( f"Found {im.num_top_modules} modules with codelength {im.codelength:.8f} bits" ) nx.set_node_attributes(G, im.get_modules(), "community") draw_network(G)
# Add weight as an optional third argument im.add_link(1, 2) im.add_link(1, 3) im.add_link(2, 3) im.add_link(3, 4) im.add_link(4, 5) im.add_link(4, 6) im.add_link(5, 6) im.run() print( f"Found {im.num_top_modules} modules with codelength {im.codelength:.8f} bits" ) modules = im.get_modules() print("Modify the network and test partition...") # Do some modification to the network im.add_link(1, 5) # Note that removing links will not remove nodes if they become unconnected im.remove_link(5, 6) # Run again with the optimal partition from the original network as initial solution # Set no_infomap to skip optimization and just calculate the codelength im.run(initial_partition=modules, no_infomap=True) print( f"Found {im.num_top_modules} modules with codelength {im.codelength:.8f} bits" )