/
test_gefura.py
259 lines (220 loc) · 7.76 KB
/
test_gefura.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
import itertools
import networkx as nx
import pytest
from gefura import global_gefura, local_gefura
# Format: (list of edges, node to gefura value dict, kwargs, ID)
# We follow the convention that nodes that start with the same character
# belong to the same group.
global_gefura_data = [
pytest.param(
[
("a1", "a2"),
("a1", "a3"),
("a2", "a3"),
("a3", "b1"),
("a2", "b2"),
("b1", "b2"),
("b2", "c1"),
("b1", "c3"),
("b2", "c2"),
("c2", "c3"),
],
{
"a1": 0,
"a2": 13 / 48,
"a3": 17 / 96,
"b1": 29 / 90,
"b2": 5 / 9,
"c1": 0,
"c2": 5 / 96,
"c3": 5 / 96,
},
{},
id="3 groups",
),
pytest.param(
[
("a1", "b1"),
("b1", "b2"),
("b2", "c1"),
("c1", "c2"),
("c2", "b3"),
("b3", "a2"),
],
{
"a1": 0,
"a2": 0,
"b1": 4 / 12,
"b2": 6 / 12,
"b3": 4 / 12,
"c1": 6 / 11,
"c2": 5 / 11,
},
{},
id="line graph, 3 groups",
),
pytest.param(
[("b1", "a1"), ("a1", "a2"), ("a1", "b2"), ("a2", "a3"), ("a3", "b2")],
{"a1": 2.5, "a2": 0.5, "a3": 0.5, "b1": 0, "b2": 0.5},
{"normalized": False},
id="2 groups, unnormalized",
),
pytest.param(
[("a1", "a2"), ("a2", "b1"), ("b1", "b2"), ("b2", "b3")],
{"a1": 0, "a2": 1, "b1": 1, "b2": 0.5, "b3": 0},
{},
id="line graph, 2 groups",
),
]
def group_nodes_by_first_char(G):
return [set(grp[1]) for grp in itertools.groupby(sorted(G), key=lambda x: x[0])]
@pytest.mark.parametrize("edges,expected,kwargs", global_gefura_data)
def test_global_gefura(edges, expected, kwargs):
G = nx.Graph()
G.add_edges_from(edges)
groups = group_nodes_by_first_char(G)
assert global_gefura(G, groups, **kwargs) == pytest.approx(expected)
def test_singleton_groups():
G = nx.Graph()
G.add_edge(1, 2)
groups = [{1}, {2}]
expected = {1: 0.0, 2: 0.0}
assert global_gefura(G, groups, normalized=False) == pytest.approx(expected)
# Normalization should not throw ZeroDivisionError
assert global_gefura(G, groups) == pytest.approx(expected)
class TestDiGraph:
def setup(self):
edges = [
("a1", "a2"),
("a1", "b2"),
("a2", "a1"),
("a2", "b1"),
("b1", "a1"),
("b1", "b2"),
]
self.G = nx.DiGraph()
self.G.add_edges_from(edges)
self.groups = [{"a1", "a2"}, {"b1", "b2"}]
def test_global(self):
known_vals_unnormalized = {"a1": 1.5, "a2": 1, "b1": 0.5, "b2": 0}
known_vals_normalized = {"a1": 0.375, "a2": 0.25, "b1": 0.125, "b2": 0}
gamma = global_gefura(self.G, self.groups, normalized=False)
assert gamma == pytest.approx(known_vals_unnormalized)
assert global_gefura(self.G, self.groups) == pytest.approx(
known_vals_normalized
)
def test_local(self):
known_vals_unnormalized_out = {"a1": 0.5, "a2": 1, "b1": 0, "b2": 0}
known_vals_unnormalized_in = {"a1": 1, "a2": 0, "b1": 0.5, "b2": 0}
known_vals_unnormalized_all = {"a1": 1.5, "a2": 1, "b1": 0.5, "b2": 0}
known_vals_normalized_out = {"a1": 0.25, "a2": 0.5, "b1": 0, "b2": 0}
known_vals_normalized_in = {"a1": 0.5, "a2": 0, "b1": 0.25, "b2": 0}
known_vals_normalized_all = {"a1": 0.375, "a2": 0.25, "b1": 0.125, "b2": 0}
gamma_out = local_gefura(self.G, self.groups, normalized=False)
assert gamma_out == pytest.approx(known_vals_unnormalized_out)
gamma_in = local_gefura(self.G, self.groups, normalized=False, direction="in")
assert gamma_in == pytest.approx(known_vals_unnormalized_in)
gamma_all = local_gefura(self.G, self.groups, normalized=False, direction="all")
assert gamma_all == pytest.approx(known_vals_unnormalized_all)
gamma_out = local_gefura(self.G, self.groups)
assert gamma_out == pytest.approx(known_vals_normalized_out)
gamma_in = local_gefura(self.G, self.groups, direction="in")
assert gamma_in == pytest.approx(known_vals_normalized_in)
gamma_all = local_gefura(self.G, self.groups, direction="all")
assert gamma_all == pytest.approx(known_vals_normalized_all)
class TestWeightedGraph:
def setup(self):
edges = [
("a1", "a2", 1),
("a2", "b2", 3),
("a1", "b1", 1),
("b1", "b3", 2),
("b2", "b3", 1),
]
self.G = nx.Graph()
self.G.add_weighted_edges_from(edges)
self.groups = [{"a1", "a2"}, {"b1", "b2", "b3"}]
def test_global(self):
known_vals = {"a1": 0.5, "a2": 1 / 6, "b1": 0.5, "b2": 0.125, "b3": 0.125}
gamma = global_gefura(self.G, self.groups, weight="weight")
assert gamma == pytest.approx(known_vals)
def test_global_ignore_weights(self):
known_vals = {"a1": 1 / 3, "a2": 1 / 3, "b1": 0.25, "b2": 0.25, "b3": 0}
gamma = global_gefura(self.G, self.groups)
assert gamma == pytest.approx(known_vals)
def test_local(self):
known_vals = dict(a1=1.5, a2=0.5, b1=2, b2=0.5, b3=0.5)
gamma = local_gefura(self.G, self.groups, weight="weight", normalized=False)
assert gamma == pytest.approx(known_vals)
class TestLocal:
def setup(self):
edges = [
("a1", "b1"),
("a2", "b1"),
("b1", "b2"),
("b2", "c1"),
("b2", "c2"),
("a1", "c1"),
]
self.G = nx.Graph()
self.G.add_edges_from(edges)
self.groups = [{"a1", "a2"}, {"b1", "b2"}, {"c1", "c2"}]
def test_normalized(self):
known_gamma = {
"a1": 0.125,
"a2": 0,
"b1": 0.375,
"b2": 0.375,
"c1": 0.125,
"c2": 0,
}
gamma = local_gefura(self.G, self.groups)
assert gamma == pytest.approx(known_gamma)
def test_unnormalized(self):
known_gamma = {"a1": 0.5, "a2": 0, "b1": 1.5, "b2": 1.5, "c1": 0.5, "c2": 0}
gamma = local_gefura(self.G, self.groups, normalized=False)
assert gamma == pytest.approx(known_gamma)
def test_local_line_graph():
edges = [
("a3", "a2"),
("a2", "c1"),
("c1", "b1"),
("b1", "a1"),
("a1", "b2"),
("b2", "b3"),
]
groups = [{"a1", "a2", "a3"}, {"b1", "b2", "b3"}, {"c1"}]
known_vals = {"a1": 4, "a2": 4, "a3": 0, "b1": 6, "b2": 4, "b3": 0, "c1": 0}
G = nx.Graph()
G.add_edges_from(edges)
assert local_gefura(G, groups, normalized=False) == pytest.approx(known_vals)
def test_local_directed_wrong_direction_value():
with pytest.raises(ValueError):
local_gefura(nx.DiGraph(), [], direction="foobar")
def test_local_directed():
edges = [
("a1", "a2"),
("a1", "b1"),
("a2", "a1"),
("a2", "a3"),
("b1", "b2"),
("b1", "c1"),
("c1", "a1"),
("c2", "c1"),
]
groups = [{"a1", "a2", "a3"}, {"b1", "b2"}, {"c1", "c2"}]
known_out = {"a1": 0.375, "a2": 0, "a3": 0, "b1": 0, "b2": 0, "c1": 1, "c2": 0}
known_in = {"a1": 0.75, "a2": 0.375, "a3": 0, "b1": 0.8, "b2": 0, "c1": 0, "c2": 0}
known_all = {
"a1": 9 / 16,
"a2": 3 / 16,
"a3": 0,
"b1": 0.4,
"b2": 0,
"c1": 0.5,
"c2": 0,
}
G = nx.DiGraph()
G.add_edges_from(edges)
for d, vals in (("out", known_out), ("in", known_in), ("all", known_all)):
assert local_gefura(G, groups, direction=d) == pytest.approx(vals)