-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomsplot.py
272 lines (225 loc) · 8.09 KB
/
comsplot.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import random
import itertools as it
from typing import Optional, Dict, List, TypeVar, Tuple, Callable
from statistics import mean, stdev
import igraph as ig
from igraph.drawing.colors import Palette
import networkx as nx
import matplotlib.pyplot as plt
from adjustText import adjust_text
from renard.pipeline.character_unification import Character
def get_all_coms(graphs: List[nx.Graph]) -> List[int]:
all_coms = set()
for G in graphs:
for node in G.nodes:
for com in G.nodes[node].get("dcoms", []):
all_coms.add(com)
return list(sorted(all_coms))
T = TypeVar("T")
K = TypeVar("K")
def alist_get(alist: List[Tuple[K, T]], key: K) -> T:
for akey, avalue in alist:
if akey == key:
return avalue
raise IndexError
class CategoricalPalette(Palette):
def __init__(self, coms: List[int]) -> None:
super().__init__(len(coms))
if len(coms) > 0:
rp = ig.RainbowPalette(n=len(coms))
self._colors = [(com, rp._get(i)) for i, com in enumerate(coms)]
else:
self._colors = []
def _get(self, v: int) -> tuple:
return self._colors[v][1]
def subpalette(self, coms: List[int]) -> "CategoricalPalette":
# some useless computations but that's fine
cp = CategoricalPalette(coms)
cp._colors = [(com, alist_get(self._colors, com)) for com in coms]
return cp
@staticmethod
def with_hints(
G: nx.Graph,
hints: List[Tuple[str, Tuple[float, float, float, float]]],
) -> "CategoricalPalette":
"""
:param G: a graph, annotated with :func:`annotate_kclique_coms_`
:param hints: a list of tuple of the form (NAME COLOR)
:return: a palette that hopefully respects ``hints``
"""
coms = get_all_coms([G])
palette = CategoricalPalette(coms)
for name, color in hints:
for node in G.nodes:
if name in node.names:
char_coms = G.nodes[node]["dcoms"]
palette._colors[char_coms[0]] = (char_coms[0], color)
break
return palette
def igraph_plot(
G: nx.Graph,
ax=None,
com_palette: Optional[CategoricalPalette] = None,
layout_dict: Optional[Dict[Character, List[int]]] = None,
name_style: Optional[Callable[[Character], str]] = None,
):
if ax is None:
_, ax = plt.subplots()
all_coms = get_all_coms([G])
if com_palette is None:
com_palette = ig.RainbowPalette(n=len(all_coms))
# when converting from nx to igraph, node types are changed. In
# igraph, it seems nodes can only be int. The nx node itself is
# converted to the node attribute "_nx_name".
g = ig.Graph.from_networkx(G)
if name_style is None:
name_style = lambda char: char.most_frequent_name()
g.vs["label"] = [name_style(char) for char in g.vs["_nx_name"]]
if layout_dict:
layout = ig.layout.Layout(coords=[layout_dict[v["_nx_name"]] for v in g.vs])
else:
layout = g.layout_kamada_kawai()
if len(all_coms) == 0:
ig.plot(g, layout=layout, target=ax)
return
clusters = [[] for _ in all_coms]
for v_i, v in enumerate(g.vs):
character = v["_nx_name"]
coms = G.nodes[character].get("dcoms", [])
for com in coms:
com_i = all_coms.index(com)
clusters[com_i].append(v_i)
cover = ig.VertexCover(g, clusters)
# see
# https://python.igraph.org/en/0.11.6/api/igraph.Graph.html#__plot__
# for all options
ig.plot(
cover,
layout=layout,
target=ax,
vertex_size=15,
# COMPLETE HACK! ig.plot does not take 'palette' into account
# in that case (why?). So we retorts to
# 'mark_groups'. However, 'mark_groups' should take a dict but
# call iteritems() on it (not good in python 3), so using
# 'items()' change the handling by
# 'igraph.clustering._handle_mark_groups_arg_for_clustering'
mark_groups={i: com_palette._get(i) for i, _ in enumerate(all_coms)}.items(), # type: ignore
)
def zp_scatter(
G: nx.Graph,
coms: List[set],
name_style: Optional[Callable[[Character], str]] = None,
):
if name_style is None:
name_style = lambda c: c.most_frequent_name()
degrees = {n: d for n, d in G.degree}
labels = []
comids = []
pscores = []
zscores = []
for com_i, com in enumerate(coms):
for node in com:
labels.append(name_style(node))
comids.append(com_i)
# pscores
for node in com:
s = 0
for ocom in coms:
s += (
len([n for n in G.neighbors(node) if n in ocom]) / degrees[node]
) ** 2
pscores.append(1 - s)
# zscore
kscores = []
for node in com:
kscores.append(len([n for n in G.neighbors(node) if n in com]))
mean_k = mean(kscores)
stdev_k = stdev(kscores)
zscores += [(k - mean_k) / stdev_k if stdev_k > 0 else 0 for k in kscores]
com_palette = ig.RainbowPalette(n=len(coms))
fig, ax = plt.subplots(figsize=(15, 12))
ax.scatter(pscores, zscores, c=[com_palette.get(comid) for comid in comids])
texts = []
for label, x, y in zip(labels, pscores, zscores):
texts.append(ax.text(x, y, label))
adjust_text(texts, arrowprops={"arrowstyle": "->"}, force_text=(0.01, 0.01))
ax.set_xlabel("Participation coefficient P")
ax.set_ylabel("Within-module degree z")
plt.legend()
def zp_scatter_star(
G: nx.Graph,
coms: List[set],
name_style: Optional[Callable[[Character], str]] = None,
lang: str = "fra",
):
if name_style is None:
name_style = lambda c: c.most_frequent_name()
labels = []
pscores = []
zscores = []
def com_degree(node, com: set) -> int:
return len([n for n in G.neighbors(node) if n in com])
for node in G.nodes:
# find the node community, which is the union of all the
# community he belongs to
node_com = set()
for com in coms:
if node in com:
node_com = node_com.union(com)
if len(node_com) == 0:
zscores.append(0)
else:
k = len([n for n in G.neighbors(node) if n in node_com])
# compute number of links for nodes in node_com
kscores = []
for onode in node_com:
kscores.append(len([n for n in G.neighbors(onode) if n in node_com]))
# compute zscore
mean_k = mean(kscores)
stdev_k = stdev(kscores)
zscores.append((k - mean_k) / stdev_k if stdev_k > 0 else 0)
s = 0
for com in coms:
s += (
com_degree(node, com) / sum(com_degree(node, ocom) for ocom in coms)
) ** 2
pscores.append(1 - s)
labels.append(name_style(node))
fig, ax = plt.subplots(figsize=(15, 12))
ax.scatter(pscores, zscores)
tested_pos = set()
def find_textpos(
pos: Tuple[float, float], xoff: float, yoff: float
) -> Tuple[float, float]:
if not pos in tested_pos:
tested_pos.add(pos)
return pos
return find_textpos(
random.choice(
list(
it.product(
[pos[0], pos[0] + xoff, pos[0] - xoff],
[pos[1], pos[1] + yoff, pos[1] - yoff],
)
)
),
xoff,
yoff,
)
for label, x, y in zip(labels, pscores, zscores):
textpos = find_textpos((x, y + 0.05), 0.0, 0.1)
ax.annotate(
label,
(x, y),
textpos,
ha="center",
# arrowprops={"arrowstyle": "-"} if textpos != (x, y + 0.1) else None,
)
if lang == "fra":
ax.set_xlabel("Coefficient de participation $P^*$")
ax.set_ylabel("Degré intra-module $z^*$")
elif lang == "eng":
ax.set_xlabel("Participation coefficient $P^*$")
ax.set_ylabel("Intra-module degree $z^*$")
plt.legend()