Skip to content

Commit

Permalink
⬆️ support for python-infomap>=1.7.1
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRossetti committed Oct 15, 2021
1 parent 6df619e commit 04fb848
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cdlib/algorithms/bipartite_clustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ def infomap_bipartite(g_original: object, flags: str = "") -> BiNodeClustering:
im = imp.Infomap(flags)
im.bipartite_start_id = min(Y.keys())

im.add_nodes(g1.nodes)
if int(imp.__version__.replace(".", "")) >= 171:
n_dict = {i: str(n) for i, n in enumerate(g1.nodes)}
im.add_nodes(n_dict)
else:
im.add_nodes(g1.nodes)

for source, target, data in g1.edges(data=True):
if "weight" in data:
Expand Down
6 changes: 5 additions & 1 deletion cdlib/algorithms/crisp_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,11 @@ def infomap(g_original: object, flags: str = "") -> NodeClustering:
with pipes():
im = imp.Infomap(flags)

im.add_nodes(g1.nodes)
if int(imp.__version__.replace(".", "")) >= 171:
n_dict = {i: str(n) for i, n in enumerate(g1.nodes)}
im.add_nodes(n_dict)
else:
im.add_nodes(g1.nodes)

for source, target, data in g1.edges(data=True):
if "weight" in data:
Expand Down

4 comments on commit 04fb848

@antoneri
Copy link

Choose a reason for hiding this comment

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

Is this related to this commit? mapequation/infomap@e30870f

Did it break on your end or are you leveraging the new API?

@GiulioRossetti
Copy link
Owner Author

Choose a reason for hiding this comment

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

Hi, the issue is due to a misalignment of pypi and conda versions for infomap.
The pypi version (the last time I checked) was aligned to the commit you linked, the conda not yet.

@danieledler
Copy link
Contributor

Choose a reason for hiding this comment

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

Another comment. For the Infomap algorithm, you create a new graph with the nodes relabeled using consecutive integers.

As Infomap 1.x supports non-consecutive integers, you may skip this step if the node types are ints to save some performance on the marginal. However, in that case, you can't generate node ids with enumerate as above, as those ids may not match the edges, but if you already have integer ids, it should work to add the nodes directly.

For string types, you can maybe use that method, or avoid it by remapping nodes and edges as you add them, which Infomap does in the add_networkx_graph method. You can use that method directly also to simplify these steps.

@danieledler
Copy link
Contributor

@danieledler danieledler commented on 04fb848 Dec 6, 2021

Choose a reason for hiding this comment

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

Oh, yet another comment :)

The int(imp.__version__.replace(".", "")) code to check version is a bit dangerous. If we had for example a version 1.6.10, which is less than 1.7.1 for semantic versioning, you would get 1610 which is larger than 171.

There are methods in setuptools or packaging that you can use (from stackoverflow).

--Edit--
Tip from @antoneri, tuples of ints have correct comparisons where (1.6.10) < (1.7.1). So an alternative may be to check if tuple(map(int, infomap.__version__. split("."))) >= (1,7,1).

Please sign in to comment.