-
Notifications
You must be signed in to change notification settings - Fork 0
/
community_utils_NetworkX.py
137 lines (120 loc) · 3.5 KB
/
community_utils_NetworkX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from collections import defaultdict
import itertools
import networkx as nx
# Copyright(C) 2011 by
# Ben Edwards <bedwards@cs.unm.edu>
# Aric Hagberg <hagberg@lanl.gov>
# All rights reserved.
# BSD license.
__author__="""\n""".join(['Ben Edwards (bedwards@cs.unm.edu)',
'Aric Hagberg (hagberg@lanl.gov)'])
def is_partition(N,C):
"""Determines whether C partitions N
Parameters
----------
G : Container
C : list of sets
Returns
-------
is_partition : boolean
Whether C is a partition of G
Examples
--------
>>> G = nx.barbell_graph(3,0)
>>> nx.is_partition(G.nodes(),[set(range(3)),set(range(3,6))])
True
>>> nx.is_partition(G.nodes(),[set(range(3)),set(range(2,6))])
False
References
----------
.. [1] M. E. J Newman 'Networks: An Introduction', pages 359
Oxford University Press 2011.
"""
for n in N:
aff = affiliation(n,C)
if not len(aff) == 1:
return False
return True
def affiliation(n,C):
"""Return the affiliation of n for a given
community C
Parameters
----------
n : object
C : list of sets
Community structure to search in
Returns
-------
aff : list of ints
Index of affiliation of n
Examples
--------
>>> C = [set(['a','b']),set(['a'])]
>>> nx.affiliation('a',C)
[0,1]
>>> nx.affiliation('b',C)
[0]
"""
aff = []
i = 0
for c in C:
if n in c:
aff.append(i)
i+=1
return aff
def draw_partition(G,C,*args,**kwargs):
"""Not for release, just for testing"""
try:
import nx_opengl
draw = nx_opengl.draw_opengl
except ImportError:
draw = nx.draw
colors = ['r','b','g','c','m','k','y']
nc = []
for n in G:
aff = nx.affiliation(n,C)[0]
nc.append(colors[aff % 7])
draw(G,*args,node_color=nc,**kwargs)
def affiliation_dict(community):
"""Return dictionary mapping node to a list of community labels.
The community labels are arbitrary.
"""
aff=defaultdict(list)
for i,partition in enumerate(community):
for n in partition:
aff[n].append(i)
return aff
def community_sets(affiliation):
"""Return list of community sets from affiliation dictionary
"""
communities=defaultdict(set)
for n,cc in affiliation.items():
try: # overlapping communites, dict value is list
for c in cc:
communities[c].add(n)
except TypeError: # non-overlapping, dict value is single item
communities[cc].add(n)
return list(communities.values())
def unique_community(G,community):
"""Return True if community partitions G into unique sets.
"""
community_size=sum(len(c) for c in community)
# communitity size must have same number of nodes as G
if len(G)==community_size:
return True
# check that the set of nodes in the communities is the same as G
if set(G)==set.union(*community):
return True
else:
return False
def overlapping_community(G,community):
"""Return True if community partitions G into overlapping sets.
"""
community_size=sum(len(c) for c in community)
# community size must be larger to be overlapping
if not len(G) < community_size:
return False
# check that the set of nodes in the communities is the same as G
if not set(G)==set.union(*community):
return False
return True