-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
398 lines (368 loc) · 12.4 KB
/
utils.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
from __future__ import division
from bayesnet import Graph_Node, network
import bayesnet
import numpy as np
import pandas as pd
import time
from collections import OrderedDict
# Setup the network
def setup_network(bif_alarm, dat_records):
# Parsing the network from .bif format
print "0: Reading Network . . . "
Alarm = bayesnet.read_network(bif_alarm)
# Finding markov blanket for every
print "1: Setting Markov Blankets . . . "
Alarm.set_mb()
# Get data from record.dat
print "2: Getting data from records . . . "
df = bayesnet.get_data(dat_records)
# Initialise parameters
print "3: Initialising parameters . . . "
init_params(df, Alarm)
# Get the index of nodes which have missing value in each row
print "4: Getting missing data indexes . . . "
mis_index = get_missing_index(df)
return Alarm, df, mis_index
# List of the indices of nodes which have missing values in each data point;
# equal to -1 if no value is missing
def get_missing_index(df):
mis_index = []
for index, row in df.iterrows():
if(row.isnull().any()):
mis_index.append(int(np.argwhere(np.isnan(np.asarray(row)))))
else:
mis_index.append(-1)
return mis_index
# Initialise parameters
def init_params(df, net):
N = df.shape[0]
curr_iter = 0
for node in net.Pres_Graph.values():
parents = net.get_parent_nodes(node)
n_parents = len(parents)
if n_parents==0:
v0 = [] # value of node variable
counts = []
for p0 in range(0,node.nvalues):
a = df[node.Node_Name]==p0
counts.append(pd.DataFrame(df[a]).shape[0] + 1)
# counts.append(0.01)
v0.append(p0)
cpt_df = pd.DataFrame({node.Node_Name:v0, "p": np.ones(len(counts))*(-1), "counts": counts})
node.set_cpt_data(cpt_df)
elif n_parents==1:
v0 = [] # value of parent1 variable
v1 = [] # value of node variable
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,node.nvalues):
b = df[node.Node_Name]==p1
counts.append(pd.DataFrame(df[a & b]).shape[0] + 1)
# counts.append(0.01)
v0.append(p0)
v1.append(p1)
cpt_df = pd.DataFrame({node.Node_Name:v1, parents[0].Node_Name:v0 ,
"p": np.ones(len(counts))*(-1), "counts": counts})
node.set_cpt_data(cpt_df)
elif n_parents==2:
v0 = [] # value of node variable
v1 = [] # value of parent1 variable
v2 = [] # value of parent2 variable
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,node.nvalues):
c = df[node.Node_Name]==p2
counts.append(pd.DataFrame(df[a & b & c]).shape[0] + 1)
# counts.append(0.01)
v0.append(p0)
v1.append(p1)
v2.append(p2)
cpt_df = pd.DataFrame({parents[0].Node_Name:v0 ,parents[1].Node_Name:v1, node.Node_Name:v2,
"p": np.ones(len(counts))*(-1), "counts": counts})
node.set_cpt_data(cpt_df)
elif n_parents==3:
v0 = [] # value of node variable
v1 = [] # value of parent1 variable
v2 = [] # value of parent2 variable
v3 = [] # value of parent3 variable
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,parents[2].nvalues):
c = df[parents[2].Node_Name]==p2
for p3 in range(0,node.nvalues):
d = df[node.Node_Name]==p3
counts.append(pd.DataFrame(df[a & b & c & d]).shape[0] + 1)
# counts.append(0.01)
v0.append(p0)
v1.append(p1)
v2.append(p2)
v3.append(p3)
cpt_df = pd.DataFrame({parents[0].Node_Name:v0 ,parents[1].Node_Name:v1, parents[2].Node_Name:v2,
node.Node_Name:v3, "p": np.ones(len(counts))*(-1), "counts": counts})
node.set_cpt_data(cpt_df)
elif n_parents==4:
v0 = [] # value of node variable
v1 = [] # value of parent1 variable
v2 = [] # value of parent2 variable
v3 = [] # value of parent3 variable
v4 = [] # value of parent4 variable
counts = [] # number of times the same data point is occuring
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,parents[2].nvalues):
c = df[parents[2].Node_Name]==p2
for p3 in range(0,parents[3].nvalues):
d = df[parents[3].Node_Name]==p3
for p4 in range(0,node.nvalues):
e = df[node.Node_Name]==p4
counts.append(pd.DataFrame(df[a & b & c & d & e]).shape[0] + 1)
# counts.append(0.01)
v0.append(p0)
v1.append(p1)
v2.append(p2)
v3.append(p3)
v4.append(p4)
cpt_df = pd.DataFrame({parents[0].Node_Name:v0 ,parents[1].Node_Name:v1, parents[2].Node_Name:v2 ,
parents[3].Node_Name:v3, node.Node_Name:v4, "p": np.ones(len(counts))*(-1), "counts": counts})
node.set_cpt_data(cpt_df)
curr_iter += 1
for X in net.Pres_Graph.keys():
net.normalise_cpt(X)
# Normalise a numpy array
def normalise_array(vals):
denom = np.sum(vals)
normalised_vals = []
for val in vals:
normalised_vals.append(val/float(denom))
return normalised_vals
# return the rows of the factor table with assignments as specified in E
def get_assignment_for(factor, E, nval):
curr_factor = factor
for key, value in E.items():
if key in list(factor.columns):
condition = curr_factor[key] == value
curr_factor = curr_factor[condition]
if curr_factor.shape[0] == nval:
return curr_factor
return curr_factor
# Inference by Markov Blanket Sampling
def markov_blanket_sampling(X, E, bn):
dist_X = []
children = bn.Pres_Graph[X].Children
parents = bn.Pres_Graph[X].Parents
x_cpt = bn.Pres_Graph[X].cpt_data
fac_x = get_assignment_for(x_cpt, E, bn.Pres_Graph[X].nvalues)
fac_c = np.log(np.asarray(fac_x['p']))
for c in children:
c_cpt = bn.Pres_Graph[bn.Pres_Graph.keys()[c]].cpt_data
temp = get_assignment_for(c_cpt, E, bn.Pres_Graph[X].nvalues)
# fac_c = fac_c*np.asarray(temp['p'])
fac_c = fac_c + np.log(np.asarray(temp['p']))
return normalise_array(np.exp(fac_c))
# Expectation Step
def Expectation(bn, df, mis_index):
"""
Input:
bn - Bayesian Network
df - Data table
mis_index - array of missing indices corresponding to the Data table 'df'
Output:
new_df - each missing value in a row replaced by the possible values variable can take
new_weights - array of weights assigned to each data point
"""
new_weights = []
mydict = df.to_dict(orient = 'records')
new_df = pd.DataFrame()
# new_df_list = []
for i in range(df.shape[0]):
row = pd.DataFrame(df.loc[i,]).T
if mis_index[i]!=-1:
X = bn.Pres_Graph.keys()[mis_index[i]]
mb_x = bn.MB[X]
# print "------------------------------------ " + str(i) + " ------------------------------------"
# print "------------------------------------ " + X + " ------------------------------------"
E = {key:value for key, value in mydict[i].items() if (key!=X and key in mb_x)}
dist_X = markov_blanket_sampling(X, E, bn)
for n in range(bn.Pres_Graph[X].nvalues):
row.iloc[0, bn.Pres_Graph.keys().index(X)] = n
new_weights.append(dist_X[n])
new_df = pd.concat([new_df, row])
# new_df_list.append(row)
# if there is no missing value
else:
new_weights.append(1.0)
new_df = pd.concat([new_df, row])
return new_weights, new_df
# Maximisation Step
def Maximisation(df, net, weights):
"""
Updates the CPTs of all the nodes based on data given weight of each data point
Input:
df - Data Table
net - Bayesian Net
Weights - weight corresponding to each data point in the table
Output:
None
"""
df['wts'] = weights
N = df.shape[0]
curr_iter = 0
for X, node in net.Pres_Graph.items():
# print "--------- " + X + " ----------"
parents = net.get_parent_nodes(node)
n_parents = len(parents)
if n_parents==0:
counts = []
for p0 in range(0,node.nvalues):
a = df[node.Node_Name]==p0
count = float(pd.DataFrame(df[a])['wts'].sum())
if count!=0:
counts.append(float(count))
else:
counts.append(0.000005)
node.cpt_data['counts'] = counts
net.normalise_cpt(X)
elif n_parents==1:
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,node.nvalues):
b = df[node.Node_Name]==p1
count = float(pd.DataFrame(df[a & b])['wts'].sum())
if count!=0:
counts.append(float(count))
else:
counts.append(0.000005)
node.cpt_data['counts'] = counts
net.normalise_cpt(X)
elif n_parents==2:
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,node.nvalues):
c = df[node.Node_Name]==p2
count = float(pd.DataFrame(df[a & b & c])['wts'].sum())
if count!=0:
counts.append(count)
else:
counts.append(0.000005)
node.cpt_data['counts'] = counts
net.normalise_cpt(X)
elif n_parents==3:
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,parents[2].nvalues):
c = df[parents[2].Node_Name]==p2
for p3 in range(0,node.nvalues):
d = df[node.Node_Name]==p3
count = float(pd.DataFrame(df[a & b & c & d])['wts'].sum())
if count!=0:
counts.append(float(count))
else:
counts.append(0.000005)
node.cpt_data['counts'] = counts
net.normalise_cpt(X)
elif n_parents==4:
counts = []
for p0 in range(0,parents[0].nvalues):
a = df[parents[0].Node_Name]==p0
for p1 in range(0,parents[1].nvalues):
b = df[parents[1].Node_Name]==p1
for p2 in range(0,parents[2].nvalues):
c = df[parents[2].Node_Name]==p2
for p3 in range(0,parents[3].nvalues):
d = df[parents[3].Node_Name]==p3
for p4 in range(0,node.nvalues):
e = df[node.Node_Name]==p4
count = float(pd.DataFrame(df[a & b & c & d & e])['wts'].sum())
if count!=0:
counts.append(float(count))
else:
counts.append(0.000005)
node.cpt_data['counts'] = counts
net.normalise_cpt(X)
curr_iter += 1
# Expectation-Maximisation
def Expectation_Maximisation(df, bn, mis_index):
"""
Input:
df - Data Table
bn - Bayesian Network
mis_index - array of missing indices corresponding to the Data table 'df'
Output:
bn - Bayesian Net with complete parameters learned from the given data by EM algorithm
"""
curr_iter = 1
time_i = time.time()
while True:
print "ITERATION #" + str(curr_iter)
step0 = time.time()
# print "STEP E: "+ str(curr_iter)
wts, new_df = Expectation(bn, df, mis_index)
prev_cpts = []
for X in bn.Pres_Graph.keys():
prev_cpts.append(np.array(list(bn.Pres_Graph[X].cpt_data['p'])))
step1 = time.time()
# print "STEP M: "+ str(curr_iter)
Maximisation(new_df, bn, wts)
step2 = time.time()
# print "E time: (%ss)" % (round((step1 - step0), 5))
# print "M time: (%ss)" % (round((step2 - step1), 5))
new_cpts = []
for X in bn.Pres_Graph.keys():
new_cpts.append(np.array(list(bn.Pres_Graph[X].cpt_data['p'])))
diffs = []
for i in range(len(prev_cpts)):
max_diff = max(abs(np.subtract(prev_cpts[i],new_cpts[i])))
diffs.append(max_diff)
delta = max(diffs)
time_f = time.time()
print "Delta: " + str(delta)
if ((time_f - time_i)>660):
# print "OVER TIME. . . . "
break
if delta <= 0.00005:
break
curr_iter +=1
print "Converged in (" + str(curr_iter) + ") iterations"
return bn
# Parse learned parameters to 'solved_alarm.bif'
def parse_output(Alarm, bif_alarm):
i = 0
with open('solved_alarm.bif', 'w') as output, open(bif_alarm, 'r') as input:
while True:
line0 = input.readline()
line = line0.strip()
if line == '':
break
tokens = line.split()
first_word = tokens[0]
if first_word == 'table':
X = Alarm.Pres_Graph.keys()[i]
l = [X] + Alarm.Pres_Graph[X].Parents
to_write = np.asarray(Alarm.Pres_Graph[X].cpt_data.sort_values(l, ascending = True)['p'])
to_write = ["{:10.4f}".format(item) for item in to_write]
to_write = str(to_write)[1:len(str(to_write))-1].replace("'", "")
to_write = to_write.replace(",", "")
to_write = to_write.replace(" ", " ")
to_write = to_write.replace(" ", "")
output.write('\ttable '+ to_write + " ;\n")
i+=1
else:
output.write(line0)
if __name__ == '__main__':
print "This file contains utility functions: Run main.py"