forked from stellargraph/stellargraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_biased_random_walker.py
679 lines (569 loc) · 19.7 KB
/
test_biased_random_walker.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# -*- coding: utf-8 -*-
#
# Copyright 2017-2018 Data61, CSIRO
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pytest
import networkx as nx
from stellargraph.data.explorer import BiasedRandomWalk
from stellargraph.core.graph import StellarGraph
def create_test_graph():
"""
Creates a simple graph for testing the BreadthFirstWalk class. The node ids are string or integers.
:return: A simple graph with 13 nodes and 24 edges (including self loops for all but two of the nodes) in
networkx format.
"""
g = nx.Graph()
edges = [
("0", 1),
("0", 2),
(1, 3),
(1, 4),
(3, 6),
(4, 7),
(4, 8),
(2, 5),
(5, 9),
(5, 10),
("0", "0"),
(1, 1),
(3, 3),
(6, 6),
(4, 4),
(7, 7),
(8, 8),
(2, 2),
(5, 5),
(9, 9),
("self lonely", "self lonely"), # an isolated node with a self link
]
g.add_edges_from(edges)
g.add_node("lonely") # an isolated node without self link
g = StellarGraph(g)
return g
def create_test_simple_weighted_graph():
"""
Creates a simple graph for testing the weighted biased random walk class. The node ids are string or integers.
:return: .
"""
g = nx.Graph()
edges = [
("0", 1, 3),
("0", 2, 4),
(1, 3, 1),
(1, 4, 7),
(3, 6, 9),
(4, 7, 2),
(4, 8, 5),
(2, 5, 7),
(5, 9, 5),
(5, 10, 6),
("0", "0", 7),
(1, 1, 8),
(3, 3, 8),
(6, 6, 9),
(4, 4, 1),
(7, 7, 2),
(8, 8, 3),
(2, 2, 4),
(5, 5, 5),
(9, 9, 6),
("self lonely", "self lonely", 0), # an isolated node with a self link
]
g.add_weighted_edges_from(edges)
g.add_node("lonely") # an isolated node without self link
g = StellarGraph(g)
return g
def create_test_weighted_multigraph():
"""
Creates a weighted multigraph for testing the weighted random biased walk method. The node ids are string or integers.
:return: .
"""
g = nx.MultiGraph()
edges = [
("0", 1, 3),
("0", 1, 3),
(1, 3, 1),
(1, 4, 5),
(2, 5, 7),
(2, 5, 7),
(3, 6, 9),
(3, 6, 9),
(4, 7, 2),
(4, 8, 5),
(5, 9, 5),
(5, 10, 6),
("0", "0", 7),
(1, 1, 8),
(2, 2, 4),
(3, 3, 8),
(6, 6, 9),
(4, 4, 1),
(7, 7, 2),
(8, 8, 3),
(5, 5, 5),
(9, 9, 6),
("self lonely", "self lonely", 0), # an isolated node with a self link
]
g.add_weighted_edges_from(edges)
g.add_node("lonely") # an isolated node without self link
g = StellarGraph(g)
return g
class TestBiasedWeightedRandomWalk(object):
def test_parameter_checking(self):
g = create_test_simple_weighted_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0"]
n = 1
length = 2
p = 1.0
q = 1.0
seed = None
with pytest.raises(ValueError):
# weighted is boolean which is by default False. It is True if walk has to be weighted.
biasedrw.run(
nodes=nodes,
n=n,
p=p,
q=q,
length=length,
seed=seed,
weighted="unknown",
edge_weight_label="weight",
)
with pytest.raises(ValueError):
# edge weight labels are by default called weight as is in networkx but they can be any string value if user specified
biasedrw.run(
nodes=nodes,
n=n,
p=p,
q=q,
length=length,
seed=seed,
weighted="unknown",
edge_weight_label=None,
)
def test_identity_unweighted_weighted_1_walks(self):
# graph with all edge weights = 1
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, 1), (3, 4, 1), (4, 1, 1)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
nodes = g.nodes()
n = 4
length = 4
seed = 42
p = 1.0
q = 1.0
biasedrw = BiasedRandomWalk(g)
assert biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
) == biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=False
)
def test_weighted_walks(self):
# all positive walks
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, 2), (3, 4, 3), (4, 1, 4)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
nodes = list(g.nodes())
n = 1
length = 1
seed = None
p = 1.0
q = 1.0
biasedrw = BiasedRandomWalk(g)
assert (
len(
biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
)
)
== 4
)
# negative edge
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, -2), (3, 4, 3), (4, 1, 4)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
biasedrw = BiasedRandomWalk(g)
with pytest.raises(ValueError):
biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
)
# edge with weight infinity
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, np.inf), (3, 4, 3), (4, 1, 4)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
biasedrw = BiasedRandomWalk(g)
with pytest.raises(ValueError):
biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
)
# missing edges
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, None), (3, 4, 3), (4, 1, 4)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
biasedrw = BiasedRandomWalk(g)
with pytest.raises(ValueError):
biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
)
# edges with NaN
g = nx.Graph()
edges = [(1, 2, 1), (2, 3, np.NaN), (3, 4, 3), (4, 1, 4)]
g.add_weighted_edges_from(edges)
g = StellarGraph(g)
biasedrw = BiasedRandomWalk(g)
with pytest.raises(ValueError):
biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, seed=seed, weighted=True
)
def test_weighted_graph_label(self):
g = nx.Graph()
edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
g.add_edges_from(edges)
g[1][2]["w"] = 1
g[2][3]["w"] = 2
g[3][4]["w"] = 3
g[4][1]["w"] = 4
g = StellarGraph(g)
nodes = list(g.nodes())
n = 1
length = 1
seed = None
p = 1.0
q = 1.0
biasedrw = BiasedRandomWalk(g)
assert (
len(
biasedrw.run(
nodes=nodes,
n=n,
p=p,
q=q,
length=length,
seed=seed,
weighted=True,
edge_weight_label="w",
)
)
== 4
)
g = nx.Graph()
edges = [(1, 2), (2, 3), (3, 4), (4, 1)]
g.add_edges_from(edges)
g[1][2]["wt"] = 1
g[2][3]["wt"] = 2
g[3][4]["wt"] = 3
g[4][1]["wt"] = 4
g = StellarGraph(g)
nodes = list(g.nodes())
n = 1
length = 1
seed = None
p = 1.0
q = 1.0
biasedrw = BiasedRandomWalk(g)
with pytest.raises(ValueError):
biasedrw.run(
nodes=nodes,
n=n,
p=p,
q=q,
length=length,
seed=seed,
weighted=True,
edge_weight_label="w",
)
def test_benchmark_biasedweightedrandomwalk(self, benchmark):
g = create_test_simple_weighted_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0"]
n = 5
p = 2
q = 3
length = 5
benchmark(
lambda: biasedrw.run(
nodes=nodes, n=n, p=p, q=q, length=length, weighted=True
)
)
class TestBiasedRandomWalk(object):
def test_parameter_checking(self):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0"]
n = 1
length = 2
p = 1.0
q = 1.0
seed = None
with pytest.raises(ValueError):
# nodes should be a list of node ids even for a single node
biasedrw.run(nodes=None, n=n, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(
nodes="0", n=n, p=p, q=q, length=length, seed=seed
) # can't just pass a node id, need list, e.g., ["0"]
# n has to be positive integer
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=0, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=-121, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=21.4, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=-0.5, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=0.0001, p=p, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n="2", p=p, q=q, length=length, seed=seed)
# p has to be > 0.
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=0.0, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=-0.25, q=q, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=-1, q=q, length=length, seed=seed)
# q has to be > 0.
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=0.0, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=-0.9, length=length, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=-75, length=length, seed=seed)
# length has to be positive integer
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=0, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=-5, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=11.9, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=-9.9, seed=seed)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length="10", seed=seed)
# seed has to be None, 0, or positive integer
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=-1)
with pytest.raises(ValueError):
biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=1010.8)
# If no root nodes are given, an empty list is returned which is not an error but I thought this method
# is the best for checking this behaviour.
nodes = []
subgraph = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=None)
assert len(subgraph) == 0
def test_walk_generation_single_root_node(self):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0"]
n = 1
length = 1
seed = 42
p = 0.25
q = 0.5
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs[0]) == length
length = 2
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
for subgraph in subgraphs:
assert len(subgraph) == length
length = 2
n = 2
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert len(subgraph) == length
n = 3
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert len(subgraph) == length
def test_walk_generation_many_root_nodes(self):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0", 2]
n = 1
length = 1
seed = None
p = 1.0
q = 0.3
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n * len(nodes)
for i, subgraph in enumerate(subgraphs):
assert len(subgraph) == length # should be 1
assert subgraph[0] == nodes[i] # should equal the root node
length = 2
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n * len(nodes)
for subgraph in subgraphs:
assert len(subgraph) <= length
n = 2
length = 2
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n * len(nodes)
for subgraph in subgraphs:
assert len(subgraph) <= length
length = 3
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n * len(nodes)
for subgraph in subgraphs:
assert len(subgraph) <= length
n = 5
length = 10
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n * len(nodes)
for subgraph in subgraphs:
assert len(subgraph) <= length
def test_walk_generation_lonely_root_node(self):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["lonely"] # this node has no edges including itself
n = 1
length = 1
seed = None
p = 0.5
q = 1.0
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == 1
assert (
len(subgraphs[0]) == 1
) # always 1 since only the root node can every be added to the walk
n = 10
length = 1
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert (
len(subgraph) == 1
) # always 1 since only the root node can ever be added to the walk
n = 10
length = 10
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert (
len(subgraph) == 1
) # always 1 since only the root node can ever be added to the walk
def test_walk_generation_self_lonely_root_node(self):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["self lonely"] # this node has link to self but no other edges
n = 1
length = 1
seed = None
p = 1.0
q = 1.0
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == 1
assert len(subgraphs[0]) == 1
n = 10
length = 1
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert len(subgraph) == length
for node in subgraph:
assert node == "self lonely" # all nodes should be the same node
n = 1
length = 99
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert len(subgraph) == length
for node in subgraph:
assert node == "self lonely" # all nodes should be the same node
n = 10
length = 10
subgraphs = biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
assert len(subgraphs) == n
for subgraph in subgraphs:
assert len(subgraph) == length
for node in subgraph:
assert node == "self lonely" # all nodes should be the same node
def test_walk_biases(self):
graph = nx.Graph()
# a square with a triangle:
# 0-3
# /| |
# 1-2-4
graph.add_edges_from([(0, 1), (0, 2), (0, 3), (1, 2), (2, 4), (3, 4)])
graph = StellarGraph(graph)
biasedrw = BiasedRandomWalk(graph)
# there's 18 total walks of length 4 starting at 0 in `graph`,
# and the non-tiny transition probabilities are always equal
# so with a large enough sample, all the possible paths for a
# given p, q should come up.
nodes = [0]
n = 1000
seed = None
length = 4
always = 1e-100
never = 1e100
# always return to the last visited node
p = always
q = never
walks = {
tuple(w)
for w in biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
}
assert walks == {(0, 1, 0, 1), (0, 2, 0, 2), (0, 3, 0, 3)}
# always explore (when possible)
p = never
q = always
walks = {
tuple(w)
for w in biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
}
assert walks == {
# follow the square
(0, 2, 4, 3),
(0, 3, 4, 2),
# go around the triangle (2 is a neighbour of 0 and so
# isn't exploring, but q = never < 1)
(0, 1, 2, 4),
}
# always go to a neighbour, if possible, otherwise equal
# chance of returning or exploring
p = never
q = never
walks = {
tuple(w)
for w in biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length, seed=seed)
}
assert walks == {
# follow the triangle
(0, 1, 2, 0),
(0, 2, 1, 0),
# all explorations around the square should appear (none
# are neighbours)
(0, 3, 0, 1),
(0, 3, 0, 2),
(0, 3, 0, 3),
(0, 3, 4, 3),
(0, 3, 4, 2),
}
def test_benchmark_biasedrandomwalk(self, benchmark):
g = create_test_graph()
biasedrw = BiasedRandomWalk(g)
nodes = ["0"]
n = 5
p = 2
q = 3
length = 5
benchmark(lambda: biasedrw.run(nodes=nodes, n=n, p=p, q=q, length=length))