-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_parameters_bn.py
912 lines (721 loc) · 33.9 KB
/
data_parameters_bn.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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
# This script is based on pgmpy
# pgmpy is a python library for working with Probabilistic Graphical Models
# For more information: https://github.com/pgmpy/pgmpy
from pgmpy.utils import get_example_model
from pgmpy.sampling import BayesianModelSampling
from pgmpy.readwrite import BIFReader
from pgmpy.estimators import MaximumLikelihoodEstimator
from pgmpy.estimators import ParameterEstimator
from pgmpy.inference import ApproxInference
from pgmpy.metrics import log_likelihood_score
from pgmpy.sampling import BayesianModelInference
from itertools import chain
from pgmpy.models import JunctionTree
import math
import networkx as nx
import numpy as np
import pandas as pd
import re
class BayesianModelProbability(BayesianModelInference):
"""
Class to calculate probability (pmf) values specific to Bayesian Models
"""
def __init__(self, model):
"""
Class to calculate probability (pmf) values specific to Bayesian Models
Parameters
----------
model: Bayesian Model
model on which inference queries will be computed
"""
# super().__init__(model)
super().__init__(model)
from pgmpy.models import BayesianNetwork
if not isinstance(model, BayesianNetwork):
raise TypeError(
f"Model expected type: BayesianNetwork, got type: {type(model)}"
)
self.model = model
if isinstance(self.model, JunctionTree):
self.variables = set(chain(*self.model.nodes()))
else:
self.variables = self.model.nodes()
self._initialize_structures()
self.topological_order = list(nx.topological_sort(model))
# super(BayesianModelProbability, self).__init__(model)
def _log_probability_node(self, data, ordering, node):
"""
Evaluate the log probability of each datapoint for a specific node.
Internal function used by log_probability().
Parameters
----------
data: array_like, shape (n_samples, n_features)
List of n_features-dimensional data points. Each row
corresponds to a single data point.
ordering: list
ordering of columns in data, used by the Bayesian model.
default is topological ordering used by model.
node: Bayesian Model Node
node from the Bayesian network.
Returns
-------
Log probability of node: np.array (n_samples,)
The array of log(density) evaluations. These are normalized to be
probability densities, so values will be low for high-dimensional
data.
"""
def vec_translate(a, my_dict):
return np.vectorize(my_dict.__getitem__)(a)
cpd = self.model.get_cpds(node)
# variable to probe: data[n], where n is the node number
current = cpd.variables[0]
current_idx = ordering.index(current)
current_val = data[:, current_idx]
current_no = vec_translate(current_val, cpd.name_to_no[current])
# conditional dependencies E of the probed variable
evidence = cpd.variables[:0:-1]
evidence_idx = [ordering.index(ev) for ev in evidence]
evidence_val = data[:, evidence_idx]
evidence_no = np.empty_like(evidence_val, dtype=int)
for i, ev in enumerate(evidence):
evidence_no[:, i] = vec_translate(evidence_val[:, i], cpd.name_to_no[ev])
if evidence:
# there are conditional dependencies E for data[n] for this node
# Here we retrieve the array: p(x[n]|E). We do this for each x in data.
# We pick the specific node value from the arrays below.
state_to_index, index_to_weight = self.pre_compute_reduce_maps(
variable=node
)
unique, inverse = np.unique(evidence_no, axis=0, return_inverse=True)
weights = np.array(
[index_to_weight[state_to_index[tuple(u)]] for u in unique]
)[inverse]
else:
# there are NO conditional dependencies for this node
# retrieve array: p(x[n]). We do this for each x in data.
# We pick the specific node value from the arrays below.
weights = np.array([cpd.values] * len(data))
# pick the specific node value x[n] from the array p(x[n]|E) or p(x[n])
# We do this for each x in data.
probability_node = np.array([weights[i][cn] for i, cn in enumerate(current_no)])
return np.log(probability_node)
def log_probability(self, data, ordering=None):
"""
Evaluate the logarithmic probability of each point in a data set.
Parameters
----------
data: pandas dataframe OR array_like, shape (n_samples, n_features)
List of n_features-dimensional data points. Each row
corresponds to a single data point.
ordering: list
ordering of columns in data, used by the Bayesian model.
default is topological ordering used by model.
Returns
-------
Log probability of each datapoint: np.array (n_samples,)
The array of log(density) evaluations. These are normalized to be
probability densities, so values will be low for high-dimensional
data.
"""
if isinstance(data, pd.DataFrame):
# use numpy array from now on.
ordering = data.columns.to_list()
data = data.values
if ordering is None:
ordering = self.topological_order
data = data.loc[:, ordering].values
logp = np.array(
[self._log_probability_node(data, ordering, node) for node in ordering]
)
return np.sum(logp, axis=0)
def score(self, data, ordering=None):
"""
Compute the total log probability density under the model.
Parameters
----------
data: pandas dataframe OR array_like, shape (n_samples, n_features)
List of n_features-dimensional data points. Each row
corresponds to a single data point.
ordering: list
ordering of columns in data, used by the Bayesian model.
default is topological ordering used by model.
Returns
-------
Log-likelihood of data: float
This is normalized to be a probability density, so the value
will be low for high-dimensional data.
"""
log_prob = self.log_probability(data, ordering)
return log_prob
# return np.sum(self.log_probability(data, ordering))
def kl_divergence(p, q):
"""
Compute the KL divergence between discrete probability distributions p and q.
It is the amount of information lost when q is used to approximate p.
p typically represents the "true" distribution of data,while q typically represents approximation of p
:param p: discrete probability distribution
:param q: discrete probability distribution
:return: KL divergence "score"
"""
return np.sum(np.where(p != 0, p * np.log2(p / q), 0))
def generate_data(ans, filename, output_file, num_tuples):
"""
This functions generate data based on BNs,using forward_sampling method
:param ans: internal parameter
:param filename: the input file or name of BN
:param output_file: the name of file where the data will be written
:param num_tuples: the size of dataset
:return: 0 on success , -1 on failure
"""
if ans == 1:
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Check model
if model.check_model():
print("CPDs of model are valid")
else:
return -1
# Print the nodes and edges from BN
print("Nodes in the model:", model.nodes())
print("Edges in the model:", model.edges())
# Get the CPDs of model
cpds = model.get_cpds()
# Print the CPDs of model
for cpd in cpds:
print(cpd)
# Get the cardinalities of nodes
cards_nodes = model.get_cardinality()
# Print the cardinalities of nodes
print("Cardinalities of nodes")
for key, value in cards_nodes.items():
print(key, ":", value)
# Generate data based on BN using forward sampling
# or model.simulate(n_samples=num_tuples)
samples = BayesianModelSampling(model).forward_sample(size=num_tuples)
# Write samples to output_file
# mode = 'a' for cardinality
samples.to_csv(output_file, header=False, index=False)
return 0
def generate_data_np(ans, filename, output_file, num_tuples):
"""
This functions generate data based on BNs,using forward_sampling method
:param ans: internal parameter
:param filename: the input file or name of BN
:param output_file: the name of file where the data will be written
:param num_tuples: the size of dataset
:return: 0 on success , -1 on failure
"""
if ans == 1:
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Check model
if model.check_model():
print("CPDs of model are valid")
else:
return -1
# Generate data based on BN using forward sampling
# or model.simulate(n_samples=num_tuples)
samples = BayesianModelSampling(model).forward_sample(size=num_tuples)
# Write samples to output_file
# mode = 'a' for cardinality
samples.to_csv(output_file, header=True, index=False)
print("\n")
return 0
def learning_parameter(choice, filename, num_tuples):
"""
Learning the model parameters using Maximum Likelihood Estimate(MLE)
:param choice: internal parameter
:param filename: the input file or name of BN
:param num_tuples: the size of dataset
:return: 0 on success , -1 on failure
"""
np.set_printoptions(linewidth=np.inf)
if choice == "I":
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Check model
if model.check_model():
print("CPDs of model are valid")
else:
return -1
# Print the nodes and edges from BN
print("Nodes in the model:", model.nodes())
print("Edges in the model:", model.edges())
# Generate data using forward sampling
data = BayesianModelSampling(model).forward_sample(size=num_tuples)
# Parameter estimator
par_est = ParameterEstimator(model, data)
# Print the state counts for each variable of model
print("==================State counts==================")
for node in model.nodes():
print("\n", par_est.state_counts(node))
print("================================================\n\n")
# Fitting the model using Maximum Likelihood Estimator
# or using the method fit => model.fit(data,estimator=MaximumLikelihoodEstimator)
mle = MaximumLikelihoodEstimator(model, data)
# Estimate the CPD of each node
print("Estimated CPDs")
for node in model.nodes():
print(mle.estimate_cpd(node))
# Get all the parameters
cpds = mle.get_parameters()
# Print the CPDs(same as estimated CPDs)
print("\n\nParameters of model")
for cpd in cpds:
print(cpd)
# Correctness check of parameters
# Print the differences between true parameters and learned parameters
print("\n")
for cpd in cpds:
# Print the name of node
print("Node", cpd.variable)
# Print flattened array(row-major)
print("True parameters : ", np.flipud(model.get_cpds(cpd.variable).values.flatten())) # flip the array vertically so the values of arrays are matched
print("Learned parameters : ", cpd.values.flatten())
# Print the KL-divergence between true CPD and learned CPD
print("KL(T||L) = %1.3f" % kl_divergence(np.flipud(model.get_cpds(cpd.variable).values.flatten()), cpd.values.flatten()))
# Print the differences(true-learned) of arrays
print("Differences: ", np.subtract(np.flipud(model.get_cpds(cpd.variable).values.flatten()), cpd.values.flatten()), "\n")
return 0
def estimate_gt_mle_prob(choice, filename, num_tuples, query_size):
"""
Estimate the joint probability using the ground truth parameters and parameters which learnt from MLE
:param choice: internal parameter
:param filename: the input file or name of BN
:param num_tuples: the size of dataset
:param query_size: the size of queries
:return: 0 on success , -1 on failure
"""
np.set_printoptions(linewidth=np.inf)
if choice == "I":
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Check model
if model.check_model():
print("CPDs of model are valid")
else:
return -1
# Print the nodes and edges from BN
print("Nodes in the model:", model.nodes())
print("Edges in the model:", model.edges())
# Generate data using forward sampling
data = BayesianModelSampling(model).forward_sample(size=num_tuples)
# Parameter estimator
# par_est = ParameterEstimator(model, data)
# Print the state counts for each variable of model
# print("==================State counts==================")
# for node in model.nodes():
# print("\n", par_est.state_counts(node))
# print("================================================\n\n")
# Fitting the model using Maximum Likelihood Estimator
# or using the method fit => model.fit(data,estimator=MaximumLikelihoodEstimator)
mle = MaximumLikelihoodEstimator(model, data)
# Get all the parameters
cpds = mle.get_parameters()
# Correctness check of parameters
# Print the differences between true parameters and learned parameters
print("\n")
for cpd in cpds:
# Print the name of node
print("Node", cpd.variable)
# Print flattened array(row-major)
print("True parameters : ", np.flipud(model.get_cpds(cpd.variable).values.flatten())) # flip the array vertically so the values of arrays are matched
print("Learned parameters : ", cpd.values.flatten())
# Print the KL-divergence between true CPD and learned CPD
# print("KL(T||L) = %1.3f" % kl_divergence(np.flipud(model.get_cpds(cpd.variable).values.flatten()), cpd.values.flatten()))
# Print the differences(true-learned) of arrays
print("Differences: ", np.subtract(np.flipud(model.get_cpds(cpd.variable).values.flatten()), cpd.values.flatten()), "\n")
# Log likelihood of a given dataset/queries
queries = BayesianModelSampling(model).forward_sample(size=query_size)
queries.to_csv("querySource_"+filename, header=False, index=False)
# queries = queries.drop(['Burglary', 'Alarm', 'JohnCalls', 'MaryCalls'], axis=1)
# Ground truth log likelihood
gt_log_likelihood = BayesianModelProbability(model).score(queries)
print("Ground truth Log likelihood :", gt_log_likelihood)
print("Average Ground truth Log likelihood :", np.sum(gt_log_likelihood)/gt_log_likelihood.shape[0])
print("Count of log(prob) > log(0.01) : ", np.count_nonzero(gt_log_likelihood > math.log(0.01)))
# print("Ground truth Log likelihood score :", log_likelihood_score(model, queries))
# Estimated log likelihood
# Adjust all cpds from model with the estimated cpds
# Remove all cpds from the model
model_cpds = model.get_cpds().copy()
for cpd in model_cpds:
model.remove_cpds(cpd)
# Add all cpds from MLE
for cpd in cpds:
model.add_cpds(cpd)
est_log_likelihood = BayesianModelProbability(model).score(queries)
print("Estimated Log likelihood :", est_log_likelihood)
# print("Estimated Log likelihood score :", log_likelihood_score(model, queries))
# Print the differences between them and get the average
diff = np.abs(est_log_likelihood-gt_log_likelihood)
print("Absolute error :", diff)
diff[~np.isfinite(diff)] = -1
diff = diff[diff > 0]
print("Queries size included on average :", diff.shape[0])
print("Average Absolute error :", np.sum(diff)/diff.shape[0])
# Write statistics about querySource to file
f = open("querySource_"+filename+"_stats", "w")
f.write("Training dataset : " + str(num_tuples)+"\n")
f.write("Query size : " + str(query_size)+"\n")
f.write("\n\nGround truth Log likelihood : " + str(gt_log_likelihood))
f.write("\nAverage Ground truth Log likelihood : " + str(np.sum(gt_log_likelihood)/gt_log_likelihood.shape[0]))
f.write("\nCount of log(prob) > log(0.01) : " + str(np.count_nonzero(gt_log_likelihood > math.log(0.01))))
f.write("\n\nEstimated Log likelihood : " + str(est_log_likelihood))
f.write("\n\nAbsolute error : " + str(diff))
f.write("\n\nQueries size included on average : " + str(diff.shape[0]))
f.write("\nAverage Absolute error : " + str(np.sum(diff) / diff.shape[0]))
f.close()
# Estimate query using learning parameters
# query = "False,False,False,True,True"
# q = infer.query(variables=["Burglary","Earthquake","Alarm"],n_samples=1000)
# print(q)
infer = ApproxInference(model)
# Probability distribution of Earthquake
# dist = infer.get_distribution(data, variables=['Earthquake'])
# print(dist)
# Probability distribution of Bulglary
# dist = infer.get_distribution(data, variables=['Burglary'])
# print(dist)
# Probability distribution of Bulglary,Earthquake,Alarm
# dist = infer.get_distribution(data, variables=['Burglary','Earthquake','Alarm'],joint=True)
# print(dist)
# Probability distribution of all nodes of BN
dist = infer.get_distribution(data, variables=mle.variables, joint=True)
for x in dist:
print(dist.get(x))
return 0
def convert_bn_to_string(choice, filename):
"""
Convert a Bayesian Network to String with the follow format:\n
(Node_Name:Cardinality_of_Node[Values] | Parents) for every node in BN \n
Node_Name : the name of node \n
Cardinality_of_Node : the cardinality of node \n
Values : Correspond to comma-separated list of node values \n
Parents : Correspond to comma-separated list of node parents where every node depicted as Node_Name:Cardinality_of_Node[Values] \n
:param choice: internal parameter
:param filename: the input file or name of BN
:return: The transformed string
"""
if choice == "I":
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Create the string
strBN = ""
for cpd in model.get_cpds():
strBN += "( " + cpd.variable + ":" + str(model.get_cardinality(cpd.variable))
# Get states of variable
states = "[ "
for state in cpd.state_names.get(cpd.variable):
states += state + ","
# Appended to str
states += " ]"
states = states.replace(", ]", " ]")
strBN += states
# Check for parents
if len(cpd.get_evidence()) > 0:
strBN += " | "
for par in cpd.get_evidence():
strBN += par + ":" + str(model.get_cardinality(par))
# Get states of variable
states = "[ "
for state in cpd.state_names.get(par):
states += state + ","
# Appended to str
states += " ]"
states = states.replace(", ]", " ]")
strBN += states + ","
strBN += " )\n"
# Return string
strBN = strBN.replace(", )", " )")
print(strBN)
return strBN
def convert_bn_to_json(choice, filename):
"""
Convert a Bayesian Network to JSON string with the follow format:\n
{"name":node_name,"trueParameters":trueParameters,"cardinality":cardinality_of_Node,"values":[values],parents":[{node}]} for every node in BN \n
node_Name : the name of node \n
trueParameters : the true parameters of node \n
cardinality_of_Node : the cardinality of node \n
values : Correspond to comma-separated list of node values \n
parents : Correspond to comma-separated list of node parents where every node depicted as {"name":node_name,"cardinality":cardinality_of_Node,"values":[values],"parents":[]} \n
:param choice: internal parameter
:param filename: the input file or name of BN
:return: The transformed string
"""
if choice == "I":
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Create the json string
strBN = "\"["
for cpd in model.get_cpds():
# Get the parameters
trueParameters = ",\\\"trueParameters\\\":["
if len(cpd.get_evidence()) > 0:
for value in cpd.values:
tmp_var = str(value)
tmp_var = "\""+tmp_var.replace("\n", "").replace("[", "").replace("]", "").strip().replace(" ", ",")
tmp_var = re.sub(",+", ",", tmp_var).strip()+"\","
trueParameters += tmp_var
# trueParameters += "\\\""+str(np.hstack(value).tolist()).replace(" ", "").replace("[", "").replace("]", "")+"\\\","
else:
# No parents exists
for i in range(len(cpd.values)):
trueParameters += "\\\""+str(cpd.values[i])+"\\\","
trueParameters += "]"
trueParameters = trueParameters.replace(",]", "]")
strBN += "{\\\"name\\\":" + str("\\\""+cpd.variable+"\\\"") + trueParameters + "," + "\\\"cardinality\\\":" + str(model.get_cardinality(cpd.variable))
# Get states of variable
states = ",\\\"values\\\":["
for state in cpd.state_names.get(cpd.variable):
states += str("\\\""+state+"\\\"") + ","
# Appended to str
states += "]"
states = states.replace(",]", "]")
strBN += states
# Check for parents
strBN += ",\\\"parents\\\":["
if len(cpd.get_evidence()) > 0:
for par in cpd.get_evidence()[::-1]:
strBN += "{\\\"name\\\":"+str("\\\""+par+"\\\"") + "," + "\\\"cardinality\\\":" + str(model.get_cardinality(par))
# Get states of variable
states = ",\\\"values\\\":["
for state in cpd.state_names.get(par):
states += str("\\\""+state+"\\\"") + ","
# Appended to str
states += "]"
states = states.replace(",]", "]")
states += ",\\\"parents\\\":[]"
strBN += states + "},"
strBN += " "
strBN = strBN.replace("}, ", "}")
strBN += "] },"
# Return string
strBN += " "
strBN = strBN.replace("}, ", "}")
strBN += "]\""
print(strBN)
return strBN
def convert_bn_to_json_obj(choice, filename):
"""
Convert a Bayesian Network to JSON string with the follow format:\n
{"name":node_name,"trueParameters":trueParameters,"cardinality":cardinality_of_Node,"values":[values],parents":[{node}]} for every node in BN \n
node_Name : the name of node \n
trueParameters : the true parameters of node \n
cardinality_of_Node : the cardinality of node \n
values : Correspond to comma-separated list of node values \n
parents : Correspond to comma-separated list of node parents where every node depicted as {"name":node_name,"cardinality":cardinality_of_Node,"values":[values],"parents":[]} \n
:param choice: internal parameter
:param filename: the input file or name of BN
:return: The transformed string
"""
if choice == "I":
# Read the file
reader = BIFReader(filename)
# Get the model from file
model = reader.get_model()
else:
# Load the model
model = get_example_model(filename)
# Create the json string
strBN = "["
for cpd in model.get_cpds():
# Get the parameters
trueParameters = ",\"trueParameters\":["
if len(cpd.get_evidence()) > 0:
for value in cpd.values:
tmp_var = str(value)
tmp_var = "\""+tmp_var.replace("\n", "").replace("[", "").replace("]", "").strip().replace(" ", ",")
tmp_var = re.sub(",+", ",", tmp_var).strip()+"\","
trueParameters += tmp_var
# trueParameters += "\""+str(np.hstack(value).tolist()).replace(" ", "").replace("[", "").replace("]", "")+"\","
else:
# No parents exists
for i in range(len(cpd.values)):
trueParameters += "\""+str(cpd.values[i])+"\","
trueParameters += "]"
trueParameters = trueParameters.replace(",]", "]")
# Append name,trueParameters and cardinality to string
strBN += "{\"name\":" + str("\""+cpd.variable+"\"") + trueParameters + "," + "\"cardinality\":" + str(model.get_cardinality(cpd.variable))
# Get states of variable
states = ",\"values\":["
for state in cpd.state_names.get(cpd.variable):
states += str("\""+state+"\"") + ","
# Appended to str
states += "]"
states = states.replace(",]", "]")
strBN += states
# Check for parents
strBN += ",\"parents\":["
if len(cpd.get_evidence()) > 0:
for par in cpd.get_evidence()[::-1]:
strBN += "{\"name\":"+str("\""+par+"\"") + "," + "\"cardinality\":" + str(model.get_cardinality(par))
# Get states of variable
states = ",\"values\":["
for state in cpd.state_names.get(par):
states += str("\""+state+"\"") + ","
# Appended to str
states += "]"
states = states.replace(",]", "]")
states += ",\"parents\":[]"
strBN += states + "},"
strBN += " "
strBN = strBN.replace("}, ", "}")
strBN += "] },\n"
# Return string
strBN += " "
strBN = strBN.replace("},\n ", "}")
strBN += "]"
print(strBN)
return strBN
def print_menu():
while 1:
try:
print("==========================================\n"
+ "1:Generate data for BNs\n"
+ "2:Generate data for built-in BNs\n"
+ "3:Learning the model parameters using MLE\n"
+ "4:Convert BN to string\n"
+ "5:Convert BN to JSON object as string\n"
+ "6:Convert BN to JSON object\n"
+ "7:Estimate the log of probability of joint distribution(GT,MLE)\n"
+ "8:Exit the program\n==========================================\n")
ans = int(input("Give your preference: "))
if 1 <= ans <= 8:
if ans == 1:
print("The input file must have .BIF extension")
file = str(input("Give your input file path: "))
output_file = str(input("Give your output file path: "))
num_tuples = int(input("Give the size of dataset: "))
if generate_data(ans, file, output_file, num_tuples) == -1:
print("Invalid Bayesian Network")
print("\n\n")
elif ans == 2:
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
name_model = str(input("Give the name of model:")).strip()
output_file = str(input("Give your output file path: "))
num_tuples = int(input("Give the size of dataset: "))
for i in range(1):
if generate_data_np(ans, name_model.lower(), "data_"+output_file+str(i)+"_"+str(num_tuples), num_tuples) == -1:
print("Invalid Bayesian Network")
print("\n\n")
elif ans == 3:
print("Import BN(I) or Example BN(E)")
choice = str(input("Give your preference(I or E): "))
if choice == "I":
print("The input file must have .BIF extension")
filename = str(input("Give your input file path: "))
elif choice == "E":
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
filename = str(input("Give the name of model:")).strip().lower()
else:
print("Invalid option\n\n")
return
num_tuples = int(input("Give the size of dataset for training: "))
if learning_parameter(choice, filename, num_tuples) == -1:
print("Invalid Bayesian Network")
print("\n\n")
elif ans == 4:
print("Import BN(I) or Example BN(E)")
choice = str(input("Give your preference(I or E): "))
if choice == "I":
print("The input file must have .BIF extension")
filename = str(input("Give your input file path: "))
elif choice == "E":
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
filename = str(input("Give the name of model:")).strip().lower()
else:
print("Invalid option\n\n")
return
strBN = convert_bn_to_string(choice, filename)
# Write to file
f = open("bn" + ("_"+filename if choice == "E" else "") + ".txt", "w")
f.write(strBN)
f.close()
print("\n\n")
elif ans == 5:
print("Import BN(I) or Example BN(E)")
choice = str(input("Give your preference(I or E): "))
if choice == "I":
print("The input file must have .BIF extension")
filename = str(input("Give your input file path: "))
elif choice == "E":
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
filename = str(input("Give the name of model:")).strip().lower()
else:
print("Invalid option\n\n")
return
strBN = convert_bn_to_json(choice, filename)
# Write to file
f = open("bn_JSON" + ("_"+filename if choice == "E" else "") + ".txt", "w")
f.write(strBN)
f.close()
elif ans == 6:
print("Import BN(I) or Example BN(E)")
choice = str(input("Give your preference(I or E): "))
if choice == "I":
print("The input file must have .BIF extension")
filename = str(input("Give your input file path: "))
elif choice == "E":
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
filename = str(input("Give the name of model:")).strip().lower()
else:
print("Invalid option\n\n")
return
strBN = convert_bn_to_json_obj(choice, filename)
# Write to file
f = open("bn_JSON" + ("_"+filename if choice == "E" else "") + ".json", "w")
f.write(strBN)
f.close()
elif ans == 7:
print("Import BN(I) or Example BN(E)")
choice = str(input("Give your preference(I or E): "))
if choice == "I":
print("The input file must have .BIF extension")
filename = str(input("Give your input file path: "))
elif choice == "E":
print("Contain any model from bnlearn repository (http://www.bnlearn.com/bnrepository)")
filename = str(input("Give the name of model:")).strip().lower()
else:
print("Invalid option\n\n")
return
num_tuples = int(input("Give the size of dataset for training: "))
query_size = int(input("Give the size of queries for estimation: "))
if estimate_gt_mle_prob(choice, filename, num_tuples,query_size) == -1:
print("Invalid Bayesian Network")
print("\n\n")
else:
return
except (ValueError, IOError):
pass
print_menu()