Skip to content

Commit

Permalink
Merge pull request easybuilders#21702 from PetrKralCZ/20241018150828_…
Browse files Browse the repository at this point in the history
…new_pr_networkx31

fix the zero division bug of `networkx-3.1`
  • Loading branch information
jfgrimm authored Oct 18, 2024
2 parents 677cf0f + 312b92d commit 1fe07ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 5 additions & 1 deletion easybuild/easyconfigs/n/networkx/networkx-3.1-gfbf-2023a.eb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ and study of the structure, dynamics, and functions of complex networks."""
toolchain = {'name': 'gfbf', 'version': '2023a'}

sources = [SOURCE_TAR_GZ]
checksums = ['de346335408f84de0eada6ff9fafafff9bcda11f0a0dfaa931133debb146ab61']
patches = ['networkx-3.1_zero_division.patch']
checksums = [
{'networkx-3.1.tar.gz': 'de346335408f84de0eada6ff9fafafff9bcda11f0a0dfaa931133debb146ab61'},
{'networkx-3.1_zero_division.patch': 'fb225e9942f18c87c67b49093b86e9f949e043fdf2f3c1ed467b6d3ad857aa35'},
]

dependencies = [
('Python', '3.11.3'),
Expand Down
42 changes: 42 additions & 0 deletions easybuild/easyconfigs/n/networkx/networkx-3.1_zero_division.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Fixes the zero division bug `ZeroDivisionError: division by zero`
patch source: https://github.com/networkx/networkx/pull/6791

From 6e47bef10ba7c17514b0cbd2ec27c1f58ca21200 Mon Sep 17 00:00:00 2001
From: juanis2112 <[email protected]>
Date: Sat, 15 Jul 2023 14:22:02 -0500
Subject: [PATCH] Fix empty graph zero division error for louvain

---
networkx/algorithms/community/louvain.py | 3 +++
networkx/algorithms/community/tests/test_louvain.py | 7 +++++++
2 files changed, 10 insertions(+)

diff --git a/networkx/algorithms/community/louvain.py b/networkx/algorithms/community/louvain.py
index ca71c0c30cb..94a8f7c98b9 100644
--- a/networkx/algorithms/community/louvain.py
+++ b/networkx/algorithms/community/louvain.py
@@ -163,6 +163,9 @@ def louvain_partitions(
"""

partition = [{u} for u in G.nodes()]
+ if nx.is_empty(G):
+ yield partition
+ return
mod = modularity(G, partition, resolution=resolution, weight=weight)
is_directed = G.is_directed()
if G.is_multigraph():
diff --git a/networkx/algorithms/community/tests/test_louvain.py b/networkx/algorithms/community/tests/test_louvain.py
index ed5c2a38db6..e4942dfeb58 100644
--- a/networkx/algorithms/community/tests/test_louvain.py
+++ b/networkx/algorithms/community/tests/test_louvain.py
@@ -185,3 +185,10 @@ def test_threshold():
mod2 = nx.community.modularity(G, partition2)

assert mod1 < mod2
+
+
+def test_empty_graph():
+ G = nx.Graph()
+ G.add_nodes_from(range(5))
+ expected = [{0}, {1}, {2}, {3}, {4}]
+ assert nx.community.louvain_communities(G) == expected

0 comments on commit 1fe07ff

Please sign in to comment.