-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_core.py
117 lines (91 loc) · 3.88 KB
/
test_core.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
import numpy as np
import pytest
from graph_tool import Graph
from core import (TreeNotFound,
build_closure_with_order,
find_tree_by_closure)
from gt_utils import extract_edges
from cascade import gen_nontrivial_cascade
from utils import tree_sizes_by_roots, get_rank_index
from fixtures import grid_and_cascade, tree_and_cascade
@pytest.fixture
def g():
g = Graph(directed=False)
g.add_vertex(4)
g.add_edge_list([(0, 1), (1, 2), (0, 3)])
return g
@pytest.fixture
def cand_source():
return 0
@pytest.fixture
def terminals():
return {0, 1, 2}
@pytest.fixture
def infection_times():
return {0: 0, 1: 1, 2: 2, 3: -1}
@pytest.fixture
def infection_times_invalid():
return {0: 0, 1: 2, 2: 1, 3: -1}
def test_build_closure_with_order(g, cand_source, terminals,
infection_times, infection_times_invalid):
# just a simple test: 4 node tree
cg, eweight = build_closure_with_order(g, cand_source, terminals, infection_times,
return_r2pred=False,
debug=True)
assert set(map(int, cg.vertices())) == {0, 1, 2}
assert set(extract_edges(cg)) == {(0, 1), (1, 2)}
edges_and_weights = [(0, 1, 1), (1, 2, 1)]
for u, v, weight in edges_and_weights:
assert eweight[cg.edge(u, v)] == weight
# no feasible solution
cg, eweight = build_closure_with_order(g, cand_source, terminals,
infection_times_invalid,
return_r2pred=False,
debug=True)
assert set(map(int, cg.vertices())) == {0, 1, 2}
assert set(extract_edges(cg)) == {(0, 1), (2, 1)}
edges_and_weights = [(0, 1, 1),
(2, 1, 1)] # notice that it's not (1, 2)
for u, v, weight in edges_and_weights:
assert eweight[cg.edge(u, v)] == weight
def test_find_tree_by_closure(g, cand_source, terminals,
infection_times, infection_times_invalid):
t = find_tree_by_closure(
g, cand_source, infection_times, terminals,
closure_builder=build_closure_with_order,
strictly_smaller=True,
return_closure=False,
k=-1,
debug=False,
verbose=False)
assert set(map(int, t.vertices())) == {0, 1, 2}
assert set(extract_edges(t)) == {(0, 1), (1, 2)}
with pytest.raises(TreeNotFound):
t = find_tree_by_closure(
g, cand_source, infection_times_invalid, terminals,
closure_builder=build_closure_with_order,
strictly_smaller=True,
return_closure=False,
k=-1,
debug=True,
verbose=False)
def test_best_tree_sizes_grid_closure(grid_and_cascade):
g, infection_times, source, obs_nodes = grid_and_cascade
scores = tree_sizes_by_roots(g, obs_nodes, infection_times, source,
method='closure')
assert get_rank_index(scores, source) <= 10 # make sure it runs, how can we assume the source's rank?
def test_full_observation_tree_closure(tree_and_cascade):
g = tree_and_cascade[0]
for p in np.arange(0.2, 1.0, 0.1):
infection_times, source, obs_nodes = gen_nontrivial_cascade(g, p, 1.0)
scores = tree_sizes_by_roots(g, obs_nodes, infection_times, source,
method='closure')
assert get_rank_index(scores, source) == 0
def test_full_observation_grid_closure(grid_and_cascade):
g = grid_and_cascade[0]
for p in np.arange(0.5, 1.0, 0.1):
print('p={}'.format(p))
infection_times, source, obs_nodes = gen_nontrivial_cascade(g, p, 1.0)
scores = tree_sizes_by_roots(g, obs_nodes, infection_times, source,
method='closure')
assert get_rank_index(scores, source) == 0