Skip to content

Commit

Permalink
fix(py): Use inter/intra multilayer format by default
Browse files Browse the repository at this point in the history
Support adding multilayer nodes next to intra-layer links, supporting disconnected nodes.
  • Loading branch information
danieledler committed Jun 20, 2024
1 parent 1467768 commit 0ed9af3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
13 changes: 9 additions & 4 deletions examples/python/infomap-networkx.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def draw_network(G):

draw_network(G)

print("A state network...")
print("\nA state network...")
G = nx.Graph()
G.add_node("a", phys_id=1)
G.add_node("b", phys_id=2)
Expand All @@ -94,6 +94,7 @@ def draw_network(G):
im = Infomap(silent=True)
im.add_networkx_graph(G)
im.run()
print("#node_id module_id flow state_id")
for node in im.nodes:
print(node.node_id, node.module_id, node.flow, node.state_id)
# 1 1 0.16666666666666666 0
Expand All @@ -104,19 +105,23 @@ def draw_network(G):
# 5 2 0.16666666666666666 5


print("A multilayer network...")
print("\nA multilayer network...")
G = nx.Graph()
G.add_node(11, phys_id=1, layer_id=1)
G.add_node(21, phys_id=2, layer_id=1)
G.add_node(22, phys_id=2, layer_id=2)
G.add_node(23, phys_id=3, layer_id=2)
G.add_edge(11, 21)
G.add_edge(22, 23)
im = Infomap(silent=True)
im = Infomap(silent=False)
# Add multilayer_inter_intra_format=False for full multilayer format
im.add_networkx_graph(G)
im.run()
print("#node_id module_id flow state_id layer_id")
for node in im.nodes:
print(node.node_id, node.module_id, node.flow, node.state_id, node.layer_id)
print(
node.node_id, node.module_id, f"{node.flow:.2f}", node.state_id, node.layer_id
)
# 1 1 0.25 0 1
# 2 1 0.25 1 1
# 2 2 0.25 2 2
Expand Down
35 changes: 12 additions & 23 deletions interfaces/python/infomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def _construct_args(
args += " --variable-markov-time"
if variable_markov_damping != 1.0:
args += " --variable-markov-damping {}".format(variable_markov_damping)
args += " --variable-markov-damping {}".format(variable_markov_damping)

if preferred_number_of_modules is not None:
args += " --preferred-number-of-modules {}".format(preferred_number_of_modules)
Expand Down Expand Up @@ -1017,12 +1018,6 @@ def add_multilayer_intra_link(
"""
if self.num_nodes != 0:
raise RuntimeError(
"""Using the multilayer intra/inter api and explicitly adding nodes using add_node is unsupported.
If you want to set node names, use set_name."""
)

return super().addMultilayerIntraLink(
layer_id, source_node_id, target_node_id, weight
)
Expand Down Expand Up @@ -1068,12 +1063,6 @@ def add_multilayer_inter_link(
weight : float, optional
"""
if self.num_nodes != 0:
raise RuntimeError(
"""Using the multilayer intra/inter api and explicitly adding nodes using add_node is unsupported.
If you want to set node names, use set_name."""
)

return super().addMultilayerInterLink(
source_layer_id, node_id, target_layer_id, weight
)
Expand Down Expand Up @@ -1149,7 +1138,7 @@ def add_networkx_graph(
weight="weight",
phys_id="phys_id",
layer_id="layer_id",
multilayer_inter_intra_format=False,
multilayer_inter_intra_format=True,
):
"""Add NetworkX graph
Expand Down Expand Up @@ -1198,13 +1187,13 @@ def add_networkx_graph(
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
>>> im.run()
>>> for node in im.nodes:
... print(node.node_id, node.module_id, node.flow, node.state_id)
1 1 0.16666666666666666 0
2 1 0.16666666666666666 1
3 1 0.16666666666666666 2
1 2 0.16666666666666666 3
4 2 0.16666666666666666 4
5 2 0.16666666666666666 5
... print(node.state_id, node.node_id, node.module_id, node.flow)
0 1 1 0.16666666666666666
1 2 1 0.16666666666666666
2 3 1 0.16666666666666666
3 1 2 0.16666666666666666
4 4 2 0.16666666666666666
5 5 2 0.16666666666666666
Usage with a multilayer network
Expand All @@ -1221,9 +1210,9 @@ def add_networkx_graph(
>>> mapping = im.add_networkx_graph(G)
>>> im.run()
>>> for node in im.nodes:
... print(node.node_id, node.module_id, node.flow, node.state_id, node.layer_id)
1 1 0.25 0 1
2 1 0.25 1 1
... print(node.state_id, node.module_id, f"{node.flow:.2f}", node.node_id, node.layer_id)
0 1 0.25 1 1
1 1 0.25 2 1
2 2 0.25 2 2
3 2 0.25 3 2
Expand Down
6 changes: 2 additions & 4 deletions src/core/InfomapBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ void InfomapBase::run(const std::string& parameters)
}
#endif

m_network.postProcessInputData();
if (m_network.numNodes() == 0) {
m_network.postProcessInputData();
if (m_network.numNodes() == 0) {
m_network.readInputData(networkFile);
}
m_network.readInputData(networkFile);
}

if (!metaDataFile.empty()) {
Expand Down

1 comment on commit 0ed9af3

@danieledler
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #287.

Please sign in to comment.