-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBifTool.py
355 lines (295 loc) · 10.1 KB
/
BifTool.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
import numpy as np
import itertools as it
import networkx as nx
class BifTool:
"""
A .bif file is a popular and simple text file format for saving Bayesian
Networks. There are several very helpful Bayesian Networks Repositories
on the internet that collect Bnets in .bif and other formats. This is a
simple stand-alone Python class from Quantum Fog that reads/writes a
.bif file and loads it into convenient attributes. Different Python
Bayesian network programs can access the attributes of this class to
fill their own native attributes.
This class can handle both real and complex valued CPT = Conditional
Probability Table. real positive CPT for CBnets (is_quantum==False) and
complex CPT for QBnets (is_quantum==True)
As an example, here is the famous Asia network in bif format:
network unknown {
variable asia {
type discrete [ 2 ] { yes, no };
}
variable tub {
type discrete [ 2 ] { yes, no };
}
variable smoke {
type discrete [ 2 ] { yes, no };
}
variable lung {
type discrete [ 2 ] { yes, no };
}
variable bronc {
type discrete [ 2 ] { yes, no };
}
variable either {
type discrete [ 2 ] { yes, no };
}
variable xray {
type discrete [ 2 ] { yes, no };
}
variable dysp {
type discrete [ 2 ] { yes, no };
}
probability ( asia ) {
table 0.01, 0.99;
}
probability ( tub | asia ) {
(yes) 0.05, 0.95;
(no) 0.01, 0.99;
}
probability ( smoke ) {
table 0.5, 0.5;
}
probability ( lung | smoke ) {
(yes) 0.1, 0.9;
(no) 0.01, 0.99;
}
probability ( bronc | smoke ) {
(yes) 0.6, 0.4;
(no) 0.3, 0.7;
}
probability ( either | lung, tub ) {
(yes, yes) 1.0, 0.0;
(no, yes) 1.0, 0.0;
(yes, no) 1.0, 0.0;
(no, no) 0.0, 1.0;
}
probability ( xray | either ) {
(yes) 0.98, 0.02;
(no) 0.05, 0.95;
}
probability ( dysp | bronc, either ) {
(yes, yes) 0.9, 0.1;
(no, yes) 0.7, 0.3;
(yes, no) 0.8, 0.2;
(no, no) 0.1, 0.9;
}
}
Attributes
----------
is_quantum : bool
nd_sizes : dict[str, int]
parents : dict[str, list[str]]
pot_arrays : dict[str, numpy.ndarray]
states : dict[str, list[str]]
"""
def __init__(self, is_quantum=False):
"""
Constructor
Parameters
----------
is_quantum : bool
Returns
-------
"""
self.is_quantum = is_quantum
self.nd_sizes = {}
self.states = {}
self.parents = {}
self.pot_arrays = {}
def describe_yourself(self):
"""
For debugging purposes
Returns
-------
"""
print("\nBifTool attributes:")
print("is_quantum= ", self.is_quantum, "\n")
print(self.nd_sizes, "\n")
print(self.states, "\n")
print(self.parents, "\n")
print(self.pot_arrays)
def read_bif(self, path):
"""
Reads a .bif file (really just a .txt file)
Parameters
----------
path : str
Returns
-------
"""
def fix(in_str, bad_chs, sub):
"""
This replaces in 'in_str' each character of 'bad_chs' by a 'sub'
Parameters
----------
in_str : str
bad_chs : str
sub : str
Returns
-------
str
"""
for c in bad_chs:
in_str = in_str.replace(c, sub)
return in_str
with open(path, 'r') as f:
while True:
line = f.readline()
if 'variable' in line:
fix(line, "{", "")
split = line.split()
node = split[1]
new_split = fix(f.readline(), '[]{,};', ' ').split()
self.nd_sizes[node] = int(new_split[2])
self.states[node] = new_split[3:]
elif 'probability' in line:
split = fix(line, "(|,){", ' ').split()
node = split[1]
if len(split) == 2:
parents = []
else:
parents = split[2:]
self.parents[node] = parents
num_parents = len(parents)
nd_size = self.nd_sizes[node]
parent_sizes = [self.nd_sizes[pa] for pa in parents]
if not self.is_quantum:
ty = np.float64
else:
ty = np.complex128
self.pot_arrays[node] = \
np.zeros(parent_sizes + [nd_size], dtype=ty)
if num_parents != 0:
x = (range(parent_sizes[k])
for k in range(num_parents))
generator = it.product(*x)
else:
generator = [0]
for index in generator:
new_line = fix(f.readline(), ')', ',')
new_line = fix(new_line, '(;', '')
# remove whitespace from beginning and end of new_line
new_line = new_line.strip()
if num_parents == 0:
# root nodes don't have parentheses enclosing
# state so replace first blank space by comma
new_line = new_line.replace(' ', ',', 1)
# now new_line is in proper comma separated form
new_split = new_line.split(',')[-nd_size:]
if not self.is_quantum:
pot_vals = list(map(float, new_split))
else:
pot_vals = list(map(complex, new_split))
if num_parents != 0:
padded_index = \
tuple(list(index) + [slice(None)])
else:
padded_index = slice(None)
self.pot_arrays[node][padded_index] = pot_vals
if line == '':
# self.describe_yourself()
break
def write_bif(self, path):
"""
Writes a .bif file.
Parameters
----------
path : str
Returns
-------
"""
# self.describe_yourself()
with open(path, 'w') as f:
f.write('network unknown {\n')
f.write('\n')
for node, nd_size in self.nd_sizes.items():
f.write('variable ' + node + ' {\n')
line = 'type discrete [ ' + str(nd_size) + ' ] { '
for st in self.states[node]:
line += st + ", "
line = line[:-2] + " };\n"
f.write(line)
f.write("}\n")
for node in self.nd_sizes:
line = 'probability ( ' + node + ' | '
parents = self.parents[node]
num_parents = len(parents)
parent_sizes = [self.nd_sizes[pa] for pa in parents]
pot_arr = self.pot_arrays[node]
for pa in parents:
line += pa + ", "
line = line[:-2] + ' ) {\n'
f.write(line)
if num_parents != 0:
x = (range(parent_sizes[k])
for k in range(num_parents))
generator = it.product(*x)
else:
generator = [0]
for index in generator:
line = "\t"
if num_parents != 0:
line += "("
for pa, st in dict(zip(parents, index)).items():
line += self.states[pa][st] + ", "
else:
line += "table, "
line = line[:-2]
if num_parents != 0:
line += ") "
else:
line += " "
if num_parents != 0:
padded_index = \
tuple(list(index) + [slice(None)])
else:
padded_index = slice(None)
# print("\n", node)
# print(parents)
# print(padded_index)
arr_str = np.array2string(pot_arr[padded_index],
precision=7, separator=',')
line += arr_str[1:-1]
line += ";\n"
f.write(line)
f.write("}\n")
f.write("}\n")
def bif2dot(self, in_path, out_path):
"""
This function reads a bif file and writes a dot (graphviz) file.
Parameters
----------
in_path : str
path to input bif file
out_path : str
path to output dot file
Returns
-------
None
"""
self.read_bif(in_path)
nx_graph = nx.DiGraph()
vtx_list = self.parents.keys()
for vtx in vtx_list:
nx_graph.add_node(vtx)
for pa_vtx in self.parents[vtx]:
nx_graph.add_edge(pa_vtx, vtx)
nx.nx_pydot.write_dot(nx_graph, out_path)
if __name__ == "__main__":
from graphs.BayesNet import *
def main():
in_path = "examples_cbnets/asia.bif"
out_path = "examples_cbnets/asia_copy.bif"
tool = BifTool()
tool.read_bif(in_path)
tool.write_bif(out_path)
in_path = "examples_cbnets/WetGrass.bif"
out_path = "examples_cbnets/WetGrass_test1.dot"
bnet = BayesNet.read_bif(in_path, False)
bnet.write_dot(out_path)
# the function bif2dot() avoids calls to any QFog files except this one
tool = BifTool()
in_path = "examples_cbnets/WetGrass.bif"
out_path = "examples_cbnets/WetGrass_test2.dot"
tool.bif2dot(in_path, out_path)
main()