diff options
author | Mathilde LĂ©val <9384853+mathilde-leval@users.noreply.github.com> | 2021-08-03 18:09:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-03 12:09:22 -0400 |
commit | d475ffe7b24633934b5e566974fe7e72a9400cf4 (patch) | |
tree | 9fedb8e89ffb89a98829af8a4f0dd54f07248af5 | |
parent | 2eb274e39f712047cebf5666ee9caf2ba2e51ee4 (diff) | |
download | networkx-d475ffe7b24633934b5e566974fe7e72a9400cf4.tar.gz |
modularity_max: provide labels to get_edge_data (#4965)
* modularity_max: provide labels to get_edge_data
* test greedy mod communities relabeled separately
* Minor style changes + add note to test.
Co-authored-by: Mathilde Leval <mleval@csod.com>
Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r-- | networkx/algorithms/community/modularity_max.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/community/tests/test_modularity_max.py | 9 |
2 files changed, 12 insertions, 1 deletions
diff --git a/networkx/algorithms/community/modularity_max.py b/networkx/algorithms/community/modularity_max.py index cc9d5d98..f35bafc4 100644 --- a/networkx/algorithms/community/modularity_max.py +++ b/networkx/algorithms/community/modularity_max.py @@ -92,7 +92,9 @@ def greedy_modularity_communities(G, weight=None, resolution=1): a = [k[i] * q0 for i in range(N)] dq_dict = { i: { - j: 2 * q0 * G.get_edge_data(i, j).get(weight, 1.0) + j: 2 + * q0 + * G.get_edge_data(label_for_node[i], label_for_node[j]).get(weight, 1.0) - 2 * resolution * k[i] * k[j] * q0 * q0 for j in [node_for_label[u] for u in G.neighbors(label_for_node[i])] if j != i diff --git a/networkx/algorithms/community/tests/test_modularity_max.py b/networkx/algorithms/community/tests/test_modularity_max.py index 23103d4b..6709fbbd 100644 --- a/networkx/algorithms/community/tests/test_modularity_max.py +++ b/networkx/algorithms/community/tests/test_modularity_max.py @@ -24,6 +24,15 @@ def test_modularity_communities(func): assert set(func(G)) == expected +def test_greedy_modularity_communities_relabeled(): + # Test for gh-4966 + G = nx.balanced_tree(2, 2) + mapping = {0: "a", 1: "b", 2: "c", 3: "d", 4: "e", 5: "f", 6: "g", 7: "h"} + G = nx.relabel_nodes(G, mapping) + expected = [frozenset({"e", "d", "a", "b"}), frozenset({"c", "f", "g"})] + assert greedy_modularity_communities(G) == expected + + def test_modularity_communities_weighted(): G = nx.balanced_tree(2, 3) for (a, b) in G.edges: |