forked from sa-and/KR21_project2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
234 lines (164 loc) · 6.71 KB
/
test.py
File metadata and controls
234 lines (164 loc) · 6.71 KB
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
import pytest
import pandas as pd
from pandas.testing import assert_frame_equal
from BayesNet import BayesNet
from BNReasoner import BNReasoner
# ---------------------------- Helper Functions ----------------------------- #
def frames_are_equal(df1, df2):
try:
assert_frame_equal(df1, df2)
return True
except:
return False
# --------------------------------- Test BNs -------------------------------- #
# Dog problem
@pytest.fixture
def bn1():
return BNReasoner("./testing/dog_problem.BIFXML")
# Lecture example 1 (Winter?, Sprinkler? ... Slippery Road?)
@pytest.fixture
def bn2():
bn = BayesNet()
bn.load_from_bifxml("./testing/lecture_example.BIFXML")
return BNReasoner(bn)
# Lecture example 2 (I, J, X, Y, O)
@pytest.fixture
def bn3():
return BNReasoner("./testing/lecture_example2.BIFXML")
# Example for d-sep from lecture 2
@pytest.fixture
def bn4():
variables = ['A', 'S', 'T', 'C', 'P', 'B', 'X', 'D']
edges = [('A', 'T'), ('S', 'C'), ('S', 'B'), ('T', 'P'), ('C', 'P'), ('P', 'X'), ('P', 'D'), ('B', 'D')]
cpts = {}
for v in variables:
cpts[v] = None
bn = BayesNet()
bn.create_bn(variables, edges, cpts)
return BNReasoner(bn)
# ----------------------------------- Tests -----------------------------------
class TestPruning:
def test_case1(self, bn2):
Q = ['Wet Grass?']
e = pd.Series({'Winter?': True, 'Rain?': False})
bn2.prune(Q, e)
expected_nodes = ['Winter?', 'Sprinkler?', 'Rain?', 'Wet Grass?']
expected_edges = [('Sprinkler?', 'Wet Grass?')]
cpt_winter = pd.DataFrame({'Winter?': [False, True], 'p': [0.4, 0.6]})
cpt_sprinkler = pd.DataFrame({'Sprinkler?': [False, True], 'p': [0.8, 0.2]})
cpt_rain = pd.DataFrame({'Rain?': [False, True], 'p': [0.2, 0.8]})
cpt_wetgrass = pd.DataFrame({'Sprinkler?': [False, False, True, True], 'Wet Grass?': [False, True, False, True], 'p': [1, 0, 0.1, 0.9]})
assert expected_nodes == bn2.bn.get_all_nodes()
assert expected_edges == bn2.bn.get_all_edges()
assert frames_are_equal(cpt_winter, bn2.bn.get_cpt('Winter?'))
assert frames_are_equal(cpt_sprinkler, bn2.bn.get_cpt('Sprinkler?'))
assert frames_are_equal(cpt_rain, bn2.bn.get_cpt('Rain?'))
assert frames_are_equal(cpt_wetgrass, bn2.bn.get_cpt('Wet Grass?'))
class TestDSeparation:
def test_case1(self, bn4):
X = ['B']
Y = ['C']
Z = ['S']
assert bn4.is_dsep(X, Y, Z)
def test_case2(self, bn4):
X = ['X']
Y = ['S']
Z = ['C', 'D']
assert not bn4.is_dsep(X, Y, Z)
def test_case3(self, bn2):
X = ["Winter?"]
Y = ["Slippery Road?"]
Z = ["Rain?"]
assert bn2.is_dsep(X, Y, Z)
class TestMarginalisation:
def test_case1(self, bn2):
bayes = BayesNet()
bayes.load_from_bifxml("./testing/lecture_example.BIFXML")
all_ctp = bayes.get_all_cpts()
test_cpt = all_ctp["Sprinkler?"]
X = 'Winter?'
outcome = bn2.marginalization(X, test_cpt)
expected_outcome = pd.DataFrame({"Sprinkler?": [False, True], "p": [1.05, 0.95]})
assert outcome.equals(expected_outcome)
class TestMaxingOut:
def testcase1(self, bn2):
bayes = BayesNet()
bayes.load_from_bifxml("./testing/lecture_example.BIFXML")
all_cpt = bayes.get_all_cpts()
test_cpt = all_cpt["Wet Grass?"]
X = 'Rain?'
cpt = bn2.maxing_out(X, test_cpt)
expected_cpt = pd.DataFrame({"Sprinkler?": [False, False, True, True], "Wet Grass?": [False, True, False, True], "p": [1.00, 0.80, 0.10, 0.95],
f'extended factor {X}': [False, True, False, True]})
assert cpt.equals(expected_cpt)
class TestFactorMultiplication:
def testcase1(self, bn2):
bayes = BayesNet()
bayes.load_from_bifxml("./testing/lecture_example.BIFXML")
all_cpt = bayes.get_all_cpts()
test_cpt1 = all_cpt["Winter?"]
test_cpt2 = all_cpt["Rain?"]
multiplication = bn2.factor_multiplication(test_cpt1, test_cpt2)
expected = pd.DataFrame({'Winter?': [False, False, True, True], 'Rain?': [False, True, False, True], 'p': [0.36, 0.04, 0.12, 0.48]})
assert frames_are_equal(expected, multiplication)
class TestVarElimination:
def testcase1(self, bn4):
cpt = pd.DataFrame({
'I': [False, False, True, True],
'J': [False, True, False, True],
'p': [0.25, 0.25, 0.25, 0.25]
})
result = bn4.variable_elimination(cpt, set(['I']))
bayes = BNReasoner('testing/lecture_example2.BIFXML')
expected = bayes.bn.get_all_cpts()['J']
assert frames_are_equal(result, expected)
class TestOrdering:
def testcase_mindeg(self):
bayes = BNReasoner('testing/lecture_example2.BIFXML')
order = bayes.min_degree_ordering(bayes.bn.get_all_variables())
assert order == ['I', 'J', 'O', 'X', 'Y']
def testcase_minfill(self):
bayes = BNReasoner('testing/lecture_example2.BIFXML')
order = bayes.min_degree_ordering(bayes.bn.get_all_variables())
print(order)
assert order == ['I', 'J', 'O', 'X', 'Y']
class TestMarginalDistribution:
def testcase1(self):
bayes = BNReasoner('testing/lecture_example2.BIFXML')
Q = set(['I', 'J'])
e = pd.Series({'O': True})
result = bayes.marginal_distribution(Q, e).round(6)
expected = pd.DataFrame({
'I': [False, False, True, True],
'J': [False, True, False, True],
'p': [0.32896, 0.02613, 0.32896, 0.315949]
})
print(result)
print(expected)
assert frames_are_equal(result, expected)
def testcase2(self):
bayes = BNReasoner('testing/lecture_example2.BIFXML')
Q = set(['I', 'J'])
e = pd.Series({})
result = bayes.marginal_distribution(Q, e)
expected = pd.DataFrame({
'I': [False, False, True, True],
'J': [False, True, False, True],
'p': [0.25, 0.25, 0.25, 0.25]
})
print(result)
print(expected)
assert frames_are_equal(result, expected)
class TestMAP:
def testcase1(self, bn2):
Q = {"Slippery Road?"}
e = pd.Series({"Winter?": True})
map = bn2.MAP(Q, e)
map_real = pd.DataFrame({"Winter?": [True], "p": [0.336], "extended factor Slippery Road?": [True]})
assert map.equals(map_real)
class TestMPE:
def testcase1(self, bn2):
Q = {"Slippery Road?", "Sprinkler?", "Wet Grass?", "Rain?"}
e = pd.Series({"Winter?": True})
mpe = bn2.MPE(Q, e).round(5)
assert mpe["p"].squeeze() == 0.21504