rm CondaPkg environment
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
"""Functions for computing and measuring community structure.
|
||||
|
||||
The functions in this class are not imported into the top-level
|
||||
:mod:`networkx` namespace. You can access these functions by importing
|
||||
the :mod:`networkx.algorithms.community` module, then accessing the
|
||||
The ``community`` subpackage can be accessed by using :mod:`networkx.community`, then accessing the
|
||||
functions as attributes of ``community``. For example::
|
||||
|
||||
>>> from networkx.algorithms import community
|
||||
>>> import networkx as nx
|
||||
>>> G = nx.barbell_graph(5, 1)
|
||||
>>> communities_generator = community.girvan_newman(G)
|
||||
>>> communities_generator = nx.community.girvan_newman(G)
|
||||
>>> top_level_communities = next(communities_generator)
|
||||
>>> next_level_communities = next(communities_generator)
|
||||
>>> sorted(map(sorted, next_level_communities))
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -79,7 +79,7 @@ def asyn_fluidc(G, k, max_iter=100, seed=None):
|
||||
communities = {n: i for i, n in enumerate(vertices[:k])}
|
||||
density = {}
|
||||
com_to_numvertices = {}
|
||||
for vertex in communities.keys():
|
||||
for vertex in communities:
|
||||
com_to_numvertices[communities[vertex]] = 1
|
||||
density[communities[vertex]] = max_density
|
||||
# Set up control variables and start iterating
|
||||
|
||||
@@ -27,14 +27,13 @@ def k_clique_communities(G, k, cliques=None):
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from networkx.algorithms.community import k_clique_communities
|
||||
>>> G = nx.complete_graph(5)
|
||||
>>> K5 = nx.convert_node_labels_to_integers(G, first_label=2)
|
||||
>>> G.add_edges_from(K5.edges())
|
||||
>>> c = list(k_clique_communities(G, 4))
|
||||
>>> c = list(nx.community.k_clique_communities(G, 4))
|
||||
>>> sorted(list(c[0]))
|
||||
[0, 1, 2, 3, 4, 5, 6]
|
||||
>>> list(k_clique_communities(G, 6))
|
||||
>>> list(nx.community.k_clique_communities(G, 6))
|
||||
[]
|
||||
|
||||
References
|
||||
|
||||
@@ -69,7 +69,6 @@ def asyn_lpa_communities(G, weight=None, seed=None):
|
||||
seed.shuffle(nodes)
|
||||
|
||||
for node in nodes:
|
||||
|
||||
if not G[node]:
|
||||
continue
|
||||
|
||||
@@ -155,7 +154,7 @@ def _color_network(G):
|
||||
|
||||
Returns a dict keyed by color to a set of nodes with that color.
|
||||
"""
|
||||
coloring = dict() # color => set(node)
|
||||
coloring = {} # color => set(node)
|
||||
colors = nx.coloring.greedy_color(G)
|
||||
for node, color in colors.items():
|
||||
if color in coloring:
|
||||
|
||||
@@ -83,9 +83,8 @@ def louvain_communities(
|
||||
Examples
|
||||
--------
|
||||
>>> import networkx as nx
|
||||
>>> import networkx.algorithms.community as nx_comm
|
||||
>>> G = nx.petersen_graph()
|
||||
>>> nx_comm.louvain_communities(G, seed=123)
|
||||
>>> nx.community.louvain_communities(G, seed=123)
|
||||
[{0, 4, 5, 7, 9}, {1, 2, 3, 6, 8}]
|
||||
|
||||
Notes
|
||||
@@ -218,8 +217,8 @@ def _one_level(G, m, partition, resolution=1, is_directed=False, seed=None):
|
||||
if is_directed:
|
||||
in_degrees = dict(G.in_degree(weight="weight"))
|
||||
out_degrees = dict(G.out_degree(weight="weight"))
|
||||
Stot_in = [deg for deg in in_degrees.values()]
|
||||
Stot_out = [deg for deg in out_degrees.values()]
|
||||
Stot_in = list(in_degrees.values())
|
||||
Stot_out = list(out_degrees.values())
|
||||
# Calculate weights for both in and out neighbours
|
||||
nbrs = {}
|
||||
for u in G:
|
||||
@@ -230,7 +229,7 @@ def _one_level(G, m, partition, resolution=1, is_directed=False, seed=None):
|
||||
nbrs[u][n] += wt
|
||||
else:
|
||||
degrees = dict(G.degree(weight="weight"))
|
||||
Stot = [deg for deg in degrees.values()]
|
||||
Stot = list(degrees.values())
|
||||
nbrs = {u: {v: data["weight"] for v, data in G[u].items() if v != u} for u in G}
|
||||
rand_nodes = list(G.nodes)
|
||||
seed.shuffle(rand_nodes)
|
||||
@@ -332,7 +331,7 @@ def _gen_graph(G, partition):
|
||||
com1 = node2com[node1]
|
||||
com2 = node2com[node2]
|
||||
temp = H.get_edge_data(com1, com2, {"weight": 0})["weight"]
|
||||
H.add_edge(com1, com2, **{"weight": wt + temp})
|
||||
H.add_edge(com1, com2, weight=wt + temp)
|
||||
return H
|
||||
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ def _split_n_from(n, min_size_of_first_part):
|
||||
|
||||
|
||||
def lukes_partitioning(G, max_size, node_weight=None, edge_weight=None):
|
||||
|
||||
"""Optimal partitioning of a weighted tree using the Lukes algorithm.
|
||||
|
||||
This algorithm partitions a connected, acyclic graph featuring integer
|
||||
@@ -126,7 +125,7 @@ def lukes_partitioning(G, max_size, node_weight=None, edge_weight=None):
|
||||
def _a_parent_of_leaves_only(gr):
|
||||
tleaves = set(_leaves(gr))
|
||||
for n in set(gr.nodes) - tleaves:
|
||||
if all([x in tleaves for x in nx.descendants(gr, n)]):
|
||||
if all(x in tleaves for x in nx.descendants(gr, n)):
|
||||
return n
|
||||
|
||||
@lru_cache(CLUSTER_EVAL_CACHE_SIZE)
|
||||
@@ -146,15 +145,14 @@ def lukes_partitioning(G, max_size, node_weight=None, edge_weight=None):
|
||||
assert len(ccx) == 1
|
||||
return ccx[0]
|
||||
|
||||
def _concatenate_or_merge(partition_1, partition_2, x, i, ref_weigth):
|
||||
|
||||
def _concatenate_or_merge(partition_1, partition_2, x, i, ref_weight):
|
||||
ccx = _pivot(partition_1, x)
|
||||
cci = _pivot(partition_2, i)
|
||||
merged_xi = ccx.union(cci)
|
||||
|
||||
# We first check if we can do the merge.
|
||||
# If so, we do the actual calculations, otherwise we concatenate
|
||||
if _weight_of_cluster(frozenset(merged_xi)) <= ref_weigth:
|
||||
if _weight_of_cluster(frozenset(merged_xi)) <= ref_weight:
|
||||
cp1 = list(filter(lambda x: x != ccx, partition_1))
|
||||
cp2 = list(filter(lambda x: x != cci, partition_2))
|
||||
|
||||
@@ -167,13 +165,13 @@ def lukes_partitioning(G, max_size, node_weight=None, edge_weight=None):
|
||||
# INITIALIZATION -----------------------
|
||||
leaves = set(_leaves(t_G))
|
||||
for lv in leaves:
|
||||
t_G.nodes[lv][PKEY] = dict()
|
||||
t_G.nodes[lv][PKEY] = {}
|
||||
slot = safe_G.nodes[lv][node_weight]
|
||||
t_G.nodes[lv][PKEY][slot] = [{lv}]
|
||||
t_G.nodes[lv][PKEY][0] = [{lv}]
|
||||
|
||||
for inner in [x for x in t_G.nodes if x not in leaves]:
|
||||
t_G.nodes[inner][PKEY] = dict()
|
||||
t_G.nodes[inner][PKEY] = {}
|
||||
slot = safe_G.nodes[inner][node_weight]
|
||||
t_G.nodes[inner][PKEY][slot] = [{inner}]
|
||||
|
||||
@@ -183,7 +181,7 @@ def lukes_partitioning(G, max_size, node_weight=None, edge_weight=None):
|
||||
weight_of_x = safe_G.nodes[x_node][node_weight]
|
||||
best_value = 0
|
||||
best_partition = None
|
||||
bp_buffer = dict()
|
||||
bp_buffer = {}
|
||||
x_descendants = nx.descendants(t_G, x_node)
|
||||
for i_node in x_descendants:
|
||||
for j in range(weight_of_x, max_size + 1):
|
||||
|
||||
@@ -287,9 +287,8 @@ def greedy_modularity_communities(
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from networkx.algorithms.community import greedy_modularity_communities
|
||||
>>> G = nx.karate_club_graph()
|
||||
>>> c = greedy_modularity_communities(G)
|
||||
>>> c = nx.community.greedy_modularity_communities(G)
|
||||
>>> sorted(c[0])
|
||||
[8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
|
||||
|
||||
@@ -388,10 +387,8 @@ def naive_greedy_modularity_communities(G, resolution=1, weight=None):
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> from networkx.algorithms.community import \
|
||||
... naive_greedy_modularity_communities
|
||||
>>> G = nx.karate_club_graph()
|
||||
>>> c = naive_greedy_modularity_communities(G)
|
||||
>>> c = nx.community.naive_greedy_modularity_communities(G)
|
||||
>>> sorted(c[0])
|
||||
[8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
|
||||
|
||||
@@ -401,7 +398,7 @@ def naive_greedy_modularity_communities(G, resolution=1, weight=None):
|
||||
modularity
|
||||
"""
|
||||
# First create one community for each node
|
||||
communities = list(frozenset([u]) for u in G.nodes())
|
||||
communities = [frozenset([u]) for u in G.nodes()]
|
||||
# Track merges
|
||||
merges = []
|
||||
# Greedily merge communities until no improvement is possible
|
||||
|
||||
@@ -202,11 +202,10 @@ def modularity(G, communities, weight="weight", resolution=1):
|
||||
|
||||
Examples
|
||||
--------
|
||||
>>> import networkx.algorithms.community as nx_comm
|
||||
>>> G = nx.barbell_graph(3, 0)
|
||||
>>> nx_comm.modularity(G, [{0, 1, 2}, {3, 4, 5}])
|
||||
>>> nx.community.modularity(G, [{0, 1, 2}, {3, 4, 5}])
|
||||
0.35714285714285715
|
||||
>>> nx_comm.modularity(G, nx_comm.label_propagation_communities(G))
|
||||
>>> nx.community.modularity(G, nx.community.label_propagation_communities(G))
|
||||
0.35714285714285715
|
||||
|
||||
References
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,7 +1,8 @@
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx import Graph, NetworkXError
|
||||
from networkx.algorithms.community.asyn_fluid import asyn_fluidc
|
||||
from networkx.algorithms.community import asyn_fluidc
|
||||
|
||||
|
||||
def test_exceptions():
|
||||
|
||||
@@ -5,7 +5,6 @@ module.
|
||||
from operator import itemgetter
|
||||
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import girvan_newman
|
||||
|
||||
|
||||
def set_of_sets(iterable):
|
||||
@@ -29,14 +28,14 @@ class TestGirvanNewman:
|
||||
|
||||
def test_no_edges(self):
|
||||
G = nx.empty_graph(3)
|
||||
communities = list(girvan_newman(G))
|
||||
communities = list(nx.community.girvan_newman(G))
|
||||
assert len(communities) == 1
|
||||
validate_communities(communities[0], [{0}, {1}, {2}])
|
||||
|
||||
def test_undirected(self):
|
||||
# Start with the graph .-.-.-.
|
||||
G = nx.path_graph(4)
|
||||
communities = list(girvan_newman(G))
|
||||
communities = list(nx.community.girvan_newman(G))
|
||||
assert len(communities) == 3
|
||||
# After one removal, we get the graph .-. .-.
|
||||
validate_communities(communities[0], [{0, 1}, {2, 3}])
|
||||
@@ -50,7 +49,7 @@ class TestGirvanNewman:
|
||||
|
||||
def test_directed(self):
|
||||
G = nx.DiGraph(nx.path_graph(4))
|
||||
communities = list(girvan_newman(G))
|
||||
communities = list(nx.community.girvan_newman(G))
|
||||
assert len(communities) == 3
|
||||
validate_communities(communities[0], [{0, 1}, {2, 3}])
|
||||
validate_possible_communities(
|
||||
@@ -62,7 +61,7 @@ class TestGirvanNewman:
|
||||
G = nx.path_graph(4)
|
||||
G.add_edge(0, 0)
|
||||
G.add_edge(2, 2)
|
||||
communities = list(girvan_newman(G))
|
||||
communities = list(nx.community.girvan_newman(G))
|
||||
assert len(communities) == 3
|
||||
validate_communities(communities[0], [{0, 1}, {2, 3}])
|
||||
validate_possible_communities(
|
||||
@@ -78,7 +77,7 @@ class TestGirvanNewman:
|
||||
def heaviest(G):
|
||||
return max(G.edges(data="weight"), key=itemgetter(2))[:2]
|
||||
|
||||
communities = list(girvan_newman(G, heaviest))
|
||||
communities = list(nx.community.girvan_newman(G, heaviest))
|
||||
assert len(communities) == 3
|
||||
validate_communities(communities[0], [{0}, {1, 2, 3}])
|
||||
validate_communities(communities[1], [{0}, {1}, {2, 3}])
|
||||
|
||||
@@ -3,16 +3,15 @@ from itertools import combinations
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import k_clique_communities
|
||||
|
||||
|
||||
def test_overlapping_K5():
|
||||
G = nx.Graph()
|
||||
G.add_edges_from(combinations(range(5), 2)) # Add a five clique
|
||||
G.add_edges_from(combinations(range(2, 7), 2)) # Add another five clique
|
||||
c = list(k_clique_communities(G, 4))
|
||||
c = list(nx.community.k_clique_communities(G, 4))
|
||||
assert c == [frozenset(range(7))]
|
||||
c = set(k_clique_communities(G, 5))
|
||||
c = set(nx.community.k_clique_communities(G, 5))
|
||||
assert c == {frozenset(range(5)), frozenset(range(2, 7))}
|
||||
|
||||
|
||||
@@ -20,7 +19,7 @@ def test_isolated_K5():
|
||||
G = nx.Graph()
|
||||
G.add_edges_from(combinations(range(0, 5), 2)) # Add a five clique
|
||||
G.add_edges_from(combinations(range(5, 10), 2)) # Add another five clique
|
||||
c = set(k_clique_communities(G, 5))
|
||||
c = set(nx.community.k_clique_communities(G, 5))
|
||||
assert c == {frozenset(range(5)), frozenset(range(5, 10))}
|
||||
|
||||
|
||||
@@ -29,7 +28,7 @@ class TestZacharyKarateClub:
|
||||
self.G = nx.karate_club_graph()
|
||||
|
||||
def _check_communities(self, k, expected):
|
||||
communities = set(k_clique_communities(self.G, k))
|
||||
communities = set(nx.community.k_clique_communities(self.G, k))
|
||||
assert communities == expected
|
||||
|
||||
def test_k2(self):
|
||||
@@ -89,4 +88,4 @@ class TestZacharyKarateClub:
|
||||
|
||||
def test_bad_k():
|
||||
with pytest.raises(nx.NetworkXError):
|
||||
list(k_clique_communities(nx.Graph(), 1))
|
||||
list(nx.community.k_clique_communities(nx.Graph(), 1))
|
||||
|
||||
@@ -3,10 +3,6 @@ from itertools import chain, combinations
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import (
|
||||
asyn_lpa_communities,
|
||||
label_propagation_communities,
|
||||
)
|
||||
|
||||
|
||||
def test_directed_not_supported():
|
||||
@@ -16,15 +12,15 @@ def test_directed_not_supported():
|
||||
test.add_edge("a", "b")
|
||||
test.add_edge("a", "c")
|
||||
test.add_edge("b", "d")
|
||||
result = label_propagation_communities(test)
|
||||
result = nx.community.label_propagation_communities(test)
|
||||
|
||||
|
||||
def test_iterator_vs_iterable():
|
||||
G = nx.empty_graph("a")
|
||||
assert list(label_propagation_communities(G)) == [{"a"}]
|
||||
for community in label_propagation_communities(G):
|
||||
assert list(nx.community.label_propagation_communities(G)) == [{"a"}]
|
||||
for community in nx.community.label_propagation_communities(G):
|
||||
assert community == {"a"}
|
||||
pytest.raises(TypeError, next, label_propagation_communities(G))
|
||||
pytest.raises(TypeError, next, nx.community.label_propagation_communities(G))
|
||||
|
||||
|
||||
def test_one_node():
|
||||
@@ -34,7 +30,7 @@ def test_one_node():
|
||||
# The expected communities are:
|
||||
ground_truth = {frozenset(["a"])}
|
||||
|
||||
communities = label_propagation_communities(test)
|
||||
communities = nx.community.label_propagation_communities(test)
|
||||
result = {frozenset(c) for c in communities}
|
||||
assert result == ground_truth
|
||||
|
||||
@@ -53,7 +49,7 @@ def test_unconnected_communities():
|
||||
# The expected communities are:
|
||||
ground_truth = {frozenset(["a", "c", "d"]), frozenset(["b", "e", "f"])}
|
||||
|
||||
communities = label_propagation_communities(test)
|
||||
communities = nx.community.label_propagation_communities(test)
|
||||
result = {frozenset(c) for c in communities}
|
||||
assert result == ground_truth
|
||||
|
||||
@@ -103,7 +99,7 @@ def test_connected_communities():
|
||||
}
|
||||
ground_truth = (ground_truth1, ground_truth2)
|
||||
|
||||
communities = label_propagation_communities(test)
|
||||
communities = nx.community.label_propagation_communities(test)
|
||||
result = {frozenset(c) for c in communities}
|
||||
assert result in ground_truth
|
||||
|
||||
@@ -114,8 +110,8 @@ def test_termination():
|
||||
test1 = nx.karate_club_graph()
|
||||
test2 = nx.caveman_graph(2, 10)
|
||||
test2.add_edges_from([(0, 20), (20, 10)])
|
||||
asyn_lpa_communities(test1)
|
||||
asyn_lpa_communities(test2)
|
||||
nx.community.asyn_lpa_communities(test1)
|
||||
nx.community.asyn_lpa_communities(test2)
|
||||
|
||||
|
||||
class TestAsynLpaCommunities:
|
||||
@@ -128,7 +124,7 @@ class TestAsynLpaCommunities:
|
||||
instances, each element of which is a node in the graph.
|
||||
|
||||
"""
|
||||
communities = asyn_lpa_communities(G)
|
||||
communities = nx.community.asyn_lpa_communities(G)
|
||||
result = {frozenset(c) for c in communities}
|
||||
assert result == expected
|
||||
|
||||
@@ -151,7 +147,7 @@ class TestAsynLpaCommunities:
|
||||
def test_seed_argument(self):
|
||||
G = nx.Graph(["ab", "ac", "bc", "de", "df", "fe"])
|
||||
ground_truth = {frozenset("abc"), frozenset("def")}
|
||||
communities = asyn_lpa_communities(G, seed=1)
|
||||
communities = nx.community.asyn_lpa_communities(G, seed=1)
|
||||
result = {frozenset(c) for c in communities}
|
||||
assert result == ground_truth
|
||||
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import (
|
||||
is_partition,
|
||||
louvain_communities,
|
||||
louvain_partitions,
|
||||
modularity,
|
||||
partition_quality,
|
||||
)
|
||||
|
||||
|
||||
def test_modularity_increase():
|
||||
@@ -13,10 +6,10 @@ def test_modularity_increase():
|
||||
250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
|
||||
)
|
||||
partition = [{u} for u in G.nodes()]
|
||||
mod = modularity(G, partition)
|
||||
partition = louvain_communities(G)
|
||||
mod = nx.community.modularity(G, partition)
|
||||
partition = nx.community.louvain_communities(G)
|
||||
|
||||
assert modularity(G, partition) > mod
|
||||
assert nx.community.modularity(G, partition) > mod
|
||||
|
||||
|
||||
def test_valid_partition():
|
||||
@@ -24,11 +17,11 @@ def test_valid_partition():
|
||||
250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
|
||||
)
|
||||
H = G.to_directed()
|
||||
partition = louvain_communities(G)
|
||||
partition2 = louvain_communities(H)
|
||||
partition = nx.community.louvain_communities(G)
|
||||
partition2 = nx.community.louvain_communities(H)
|
||||
|
||||
assert is_partition(G, partition)
|
||||
assert is_partition(H, partition2)
|
||||
assert nx.community.is_partition(G, partition)
|
||||
assert nx.community.is_partition(H, partition2)
|
||||
|
||||
|
||||
def test_karate_club_partition():
|
||||
@@ -39,14 +32,14 @@ def test_karate_club_partition():
|
||||
{23, 25, 27, 28, 24, 31},
|
||||
{32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
|
||||
]
|
||||
partition = louvain_communities(G, seed=2, weight=None)
|
||||
partition = nx.community.louvain_communities(G, seed=2, weight=None)
|
||||
|
||||
assert part == partition
|
||||
|
||||
|
||||
def test_partition_iterator():
|
||||
G = nx.path_graph(15)
|
||||
parts_iter = louvain_partitions(G, seed=42)
|
||||
parts_iter = nx.community.louvain_partitions(G, seed=42)
|
||||
first_part = next(parts_iter)
|
||||
first_copy = [s.copy() for s in first_part]
|
||||
|
||||
@@ -100,10 +93,10 @@ def test_directed_partition():
|
||||
H.add_edges_from(H_edges)
|
||||
|
||||
G_expected_partition = [{0, 1, 2}, {3, 4}, {5}, {6}, {8, 7}, {9, 10}]
|
||||
G_partition = louvain_communities(G, seed=123, weight=None)
|
||||
G_partition = nx.community.louvain_communities(G, seed=123, weight=None)
|
||||
|
||||
H_expected_partition = [{2, 3, 4, 5}, {8, 1, 6, 7}, {9, 10, 11}]
|
||||
H_partition = louvain_communities(H, seed=123, weight=None)
|
||||
H_partition = nx.community.louvain_communities(H, seed=123, weight=None)
|
||||
|
||||
assert G_partition == G_expected_partition
|
||||
assert H_partition == H_expected_partition
|
||||
@@ -121,9 +114,9 @@ def test_none_weight_param():
|
||||
{23, 25, 27, 28, 24, 31},
|
||||
{32, 33, 8, 14, 15, 18, 20, 22, 26, 29, 30},
|
||||
]
|
||||
partition1 = louvain_communities(G, weight=None, seed=2)
|
||||
partition2 = louvain_communities(G, weight="foo", seed=2)
|
||||
partition3 = louvain_communities(G, weight="weight", seed=2)
|
||||
partition1 = nx.community.louvain_communities(G, weight=None, seed=2)
|
||||
partition2 = nx.community.louvain_communities(G, weight="foo", seed=2)
|
||||
partition3 = nx.community.louvain_communities(G, weight="weight", seed=2)
|
||||
|
||||
assert part == partition1
|
||||
assert part != partition2
|
||||
@@ -139,15 +132,15 @@ def test_quality():
|
||||
I = nx.MultiGraph(G)
|
||||
J = nx.MultiDiGraph(H)
|
||||
|
||||
partition = louvain_communities(G)
|
||||
partition2 = louvain_communities(H)
|
||||
partition3 = louvain_communities(I)
|
||||
partition4 = louvain_communities(J)
|
||||
partition = nx.community.louvain_communities(G)
|
||||
partition2 = nx.community.louvain_communities(H)
|
||||
partition3 = nx.community.louvain_communities(I)
|
||||
partition4 = nx.community.louvain_communities(J)
|
||||
|
||||
quality = partition_quality(G, partition)[0]
|
||||
quality2 = partition_quality(H, partition2)[0]
|
||||
quality3 = partition_quality(I, partition3)[0]
|
||||
quality4 = partition_quality(J, partition4)[0]
|
||||
quality = nx.community.partition_quality(G, partition)[0]
|
||||
quality2 = nx.community.partition_quality(H, partition2)[0]
|
||||
quality3 = nx.community.partition_quality(I, partition3)[0]
|
||||
quality4 = nx.community.partition_quality(J, partition4)[0]
|
||||
|
||||
assert quality >= 0.65
|
||||
assert quality2 >= 0.65
|
||||
@@ -163,9 +156,9 @@ def test_multigraph():
|
||||
G.add_edge(0, 9, foo=20)
|
||||
H.add_edge(0, 9, foo=20)
|
||||
|
||||
partition1 = louvain_communities(G, seed=1234)
|
||||
partition2 = louvain_communities(H, seed=1234)
|
||||
partition3 = louvain_communities(H, weight="foo", seed=1234)
|
||||
partition1 = nx.community.louvain_communities(G, seed=1234)
|
||||
partition2 = nx.community.louvain_communities(H, seed=1234)
|
||||
partition3 = nx.community.louvain_communities(H, weight="foo", seed=1234)
|
||||
|
||||
assert partition1 == partition2 != partition3
|
||||
|
||||
@@ -175,9 +168,9 @@ def test_resolution():
|
||||
250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
|
||||
)
|
||||
|
||||
partition1 = louvain_communities(G, resolution=0.5, seed=12)
|
||||
partition2 = louvain_communities(G, seed=12)
|
||||
partition3 = louvain_communities(G, resolution=2, seed=12)
|
||||
partition1 = nx.community.louvain_communities(G, resolution=0.5, seed=12)
|
||||
partition2 = nx.community.louvain_communities(G, seed=12)
|
||||
partition3 = nx.community.louvain_communities(G, resolution=2, seed=12)
|
||||
|
||||
assert len(partition1) <= len(partition2) <= len(partition3)
|
||||
|
||||
@@ -186,9 +179,9 @@ def test_threshold():
|
||||
G = nx.LFR_benchmark_graph(
|
||||
250, 3, 1.5, 0.009, average_degree=5, min_community=20, seed=10
|
||||
)
|
||||
partition1 = louvain_communities(G, threshold=0.3, seed=2)
|
||||
partition2 = louvain_communities(G, seed=2)
|
||||
mod1 = modularity(G, partition1)
|
||||
mod2 = modularity(G, partition2)
|
||||
partition1 = nx.community.louvain_communities(G, threshold=0.3, seed=2)
|
||||
partition2 = nx.community.louvain_communities(G, seed=2)
|
||||
mod1 = nx.community.modularity(G, partition1)
|
||||
mod2 = nx.community.modularity(G, partition2)
|
||||
|
||||
assert mod1 < mod2
|
||||
|
||||
@@ -3,7 +3,6 @@ from itertools import product
|
||||
import pytest
|
||||
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import lukes_partitioning
|
||||
|
||||
EWL = "e_weight"
|
||||
NWL = "n_weight"
|
||||
@@ -11,7 +10,6 @@ NWL = "n_weight"
|
||||
|
||||
# first test from the Lukes original paper
|
||||
def paper_1_case(float_edge_wt=False, explicit_node_wt=True, directed=False):
|
||||
|
||||
# problem-specific constants
|
||||
limit = 3
|
||||
|
||||
@@ -42,7 +40,9 @@ def paper_1_case(float_edge_wt=False, explicit_node_wt=True, directed=False):
|
||||
# partitioning
|
||||
clusters_1 = {
|
||||
frozenset(x)
|
||||
for x in lukes_partitioning(example_1, limit, node_weight=wtu, edge_weight=EWL)
|
||||
for x in nx.community.lukes_partitioning(
|
||||
example_1, limit, node_weight=wtu, edge_weight=EWL
|
||||
)
|
||||
}
|
||||
|
||||
return clusters_1
|
||||
@@ -50,7 +50,6 @@ def paper_1_case(float_edge_wt=False, explicit_node_wt=True, directed=False):
|
||||
|
||||
# second test from the Lukes original paper
|
||||
def paper_2_case(explicit_edge_wt=True, directed=False):
|
||||
|
||||
# problem specific constants
|
||||
byte_block_size = 32
|
||||
|
||||
@@ -94,7 +93,7 @@ def paper_2_case(explicit_edge_wt=True, directed=False):
|
||||
# partitioning
|
||||
clusters_2 = {
|
||||
frozenset(x)
|
||||
for x in lukes_partitioning(
|
||||
for x in nx.community.lukes_partitioning(
|
||||
example_2, byte_block_size, node_weight=NWL, edge_weight=wtu
|
||||
)
|
||||
}
|
||||
@@ -128,11 +127,10 @@ def test_mandatory_tree():
|
||||
not_a_tree = nx.complete_graph(4)
|
||||
|
||||
with pytest.raises(nx.NotATree):
|
||||
lukes_partitioning(not_a_tree, 5)
|
||||
nx.community.lukes_partitioning(not_a_tree, 5)
|
||||
|
||||
|
||||
def test_mandatory_integrality():
|
||||
|
||||
byte_block_size = 32
|
||||
|
||||
ex_1_broken = nx.DiGraph()
|
||||
@@ -149,6 +147,6 @@ def test_mandatory_integrality():
|
||||
ex_1_broken.nodes[5][NWL] = 2
|
||||
|
||||
with pytest.raises(TypeError):
|
||||
lukes_partitioning(
|
||||
nx.community.lukes_partitioning(
|
||||
ex_1_broken, byte_block_size, node_weight=NWL, edge_weight=EWL
|
||||
)
|
||||
|
||||
@@ -96,7 +96,7 @@ def test_greedy_modularity_communities_directed():
|
||||
)
|
||||
def test_modularity_communities_weighted(func):
|
||||
G = nx.balanced_tree(2, 3)
|
||||
for (a, b) in G.edges:
|
||||
for a, b in G.edges:
|
||||
if ((a == 1) or (a == 2)) and (b != 0):
|
||||
G[a][b]["weight"] = 10.0
|
||||
else:
|
||||
|
||||
@@ -3,27 +3,26 @@
|
||||
"""
|
||||
|
||||
import networkx as nx
|
||||
from networkx.algorithms.community import is_partition
|
||||
|
||||
|
||||
def test_is_partition():
|
||||
G = nx.empty_graph(3)
|
||||
assert is_partition(G, [{0, 1}, {2}])
|
||||
assert is_partition(G, ({0, 1}, {2}))
|
||||
assert is_partition(G, ([0, 1], [2]))
|
||||
assert is_partition(G, [[0, 1], [2]])
|
||||
assert nx.community.is_partition(G, [{0, 1}, {2}])
|
||||
assert nx.community.is_partition(G, ({0, 1}, {2}))
|
||||
assert nx.community.is_partition(G, ([0, 1], [2]))
|
||||
assert nx.community.is_partition(G, [[0, 1], [2]])
|
||||
|
||||
|
||||
def test_not_covering():
|
||||
G = nx.empty_graph(3)
|
||||
assert not is_partition(G, [{0}, {1}])
|
||||
assert not nx.community.is_partition(G, [{0}, {1}])
|
||||
|
||||
|
||||
def test_not_disjoint():
|
||||
G = nx.empty_graph(3)
|
||||
assert not is_partition(G, [{0, 1}, {1, 2}])
|
||||
assert not nx.community.is_partition(G, [{0, 1}, {1, 2}])
|
||||
|
||||
|
||||
def test_not_node():
|
||||
G = nx.empty_graph(3)
|
||||
assert not is_partition(G, [{0, 1}, {3}])
|
||||
assert not nx.community.is_partition(G, [{0, 1}, {3}])
|
||||
|
||||
Reference in New Issue
Block a user