def setUp(self): dbn_1 = DynamicBayesianNetwork() dbn_1.add_edges_from( [(('Z', 0), ('X', 0)), (('Z', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_1 = TabularCPD(('Z', 0), 2, [[0.8, 0.2]]) cpd_x_1 = TabularCPD( ('X', 0), 2, [[0.9, 0.6], [0.1, 0.4]], [('Z', 0)], [2]) cpd_y_1 = TabularCPD( ('Y', 0), 2, [[0.7, 0.2], [0.3, 0.8]], [('Z', 0)], [2]) cpd_trans_z_1 = TabularCPD( ('Z', 1), 2, [[0.9, 0.1], [0.1, 0.9]], [('Z', 0)], [2]) dbn_1.add_cpds(cpd_start_z_1, cpd_trans_z_1, cpd_x_1, cpd_y_1) dbn_1.initialize_initial_state() self.dbn_inference_1 = DBNInference(dbn_1) dbn_2 = DynamicBayesianNetwork() dbn_2.add_edges_from([(('Z', 0), ('X', 0)), (('X', 0), ('Y', 0)), (('Z', 0), ('Z', 1))]) cpd_start_z_2 = TabularCPD(('Z', 0), 2, [[0.5, 0.5]]) cpd_x_2 = TabularCPD( ('X', 0), 2, [[0.6, 0.9], [0.4, 0.1]], [('Z', 0)], [2]) cpd_y_2 = TabularCPD( ('Y', 0), 2, [[0.2, 0.3], [0.8, 0.7]], [('X', 0)], [2]) cpd_z_2 = TabularCPD( ('Z', 1), 2, [[0.4, 0.7], [0.6, 0.3]], [('Z', 0)], [2]) dbn_2.add_cpds(cpd_x_2, cpd_y_2, cpd_z_2, cpd_start_z_2) dbn_2.initialize_initial_state() self.dbn_inference_2 = DBNInference(dbn_2)
def buildDBN(): # Construct a DBN object dbn = DBN() #!!!!!!!!!!!!!!! VERY IMPORTANT !!!!!!!!!!!!!!! # MAKE SURE to NAME THE RANDOM VARIABLE "LOCATION AS "L" AND "SENSOR" OBSERVATION AS "O" ########-----YOUR CODE STARTS HERE-----######## dbn.add_edges_from([(('L', 0), ('O', 0)), (('L', 0), ('L', 1))]) l_cpd = TabularCPD(('L', 0), 4, [[0], [0], [1], [0]]) o_cpd = TabularCPD(('O', 0), 4, [ [0.7, 0.1, 0.1, 0.1], [0.1, 0.7, 0.1, 0.1], [0.1, 0.1, 0.7, 0.1], [0.1, 0.1, 0.1, 0.7], ], evidence=[('L', 0)], evidence_card=[4]) # l_i_cpd = TabularCPD(('L', 1), 4, [[.5, .5, 0, 0], # [0, .5, 0, .5], # [.5, 0, .5, 0], # [0, 0, .5, .5],], # evidence=[('L',0)], # evidence_card=[4]) l_i_cpd = TabularCPD(('L', 1), 4, [ [.5, 0, .5, 0], [.5, .5, 0, 0], [0, 0, .5, .5], [0, .5, 0, .5], ], evidence=[('L', 0)], evidence_card=[4]) # print(o_i_cpd) # print(l_cpd) # print(o_cpd) # print(l_i_cpd) dbn.add_cpds(l_cpd, o_cpd, l_i_cpd) ########-----YOUR CODE ENDS HERE-----######## # Do NOT forget to initialize before doing any inference! Otherwise, errors will be introduced. dbn.initialize_initial_state() # Create an inference object dbn_inf = DBNInference(dbn) ########-----YOUR MAY TEST YOUR CODE BELOW -----######## ########-----ADDITIONAL CODE STARTS HERE-----######## #print(dbn_inf.query([('L',3)])[('L',3)].values) # print(dbn_inf.query([('L',1)])[('L',1)].values) #print(dbn_inf.query([('L',1)], {('O', 1):2})[('L',1)].values) ########-----YOUR CODE ENDS HERE-----######## return dbn_inf
def buildDBN(): # Construct a DBN object dbn = DBN() dbn.add_edges_from([(('L', 0), ('O', 0)), (('L', 0), ('L', 1)), (('L', 1), ('O', 1))]) # setup conditional probability tables for nodes in network O0_cpd = TabularCPD(('O', 0), 4, [[0.7, 0.1, 0.1, 0.1], # A [0.1, 0.7, 0.1, 0.1], # B [0.1, 0.1, 0.7, 0.1], # C [0.1, 0.1, 0.1, 0.7]], # D evidence=[('L', 0)], evidence_card=[4]) l0_cpd = TabularCPD(('L', 0), 4, [[0], # A [0], # B [1], # C [0]]) # D l1_cpd = TabularCPD(('L', 1), 4, [[0.5, 0.0, 0.5, 0.0], # A [0.5, 0.5, 0.0, 0.0], # B [0.0, 0.0, 0.5, 0.5], # C [0.0, 0.5, 0.0, 0.5]], # D evidence=[('L', 0)], evidence_card=[4]) #add these conditional probability tables to our BayesianModel dbn.add_cpds(l0_cpd, l1_cpd, O0_cpd) #initialize our model for time series analysis dbn.initialize_initial_state() # Create an inference object to perform queries dbn_inf = DBNInference(dbn) #print(dbn_inf.query(variables=[('L',1)], evidence={('O',1): 2})[('L',1)]) return dbn_inf
class DbnCnnInterface(object): def __init__(self, model_file='../DBN/network.nx'): nx_model = nx.read_gpickle(model_file) self.dbn = DynamicBayesianNetwork(nx_model.edges()) self.dbn.add_cpds(*nx_model.cpds) self.dbn.initialize_initial_state() self.dbn_infer = DBNInference(self.dbn) def filter_q_values(self, q_values, evidence=0, method='binary'): inferred = np.ndarray(shape=(len(q_values), ), dtype=float) inferred.fill(0) variables = self.dbn.get_slice_nodes(1) ev = {node: 0 for node in self.dbn.get_slice_nodes(0)} if evidence != 0: self.set_evidence(ev, evidence) q = self.dbn_infer.query(variables=variables, evidence=ev) for variable in q.values(): action = self.get_action_id(variable.variables[0]) if method == 'binary': inferred[action] = 1 if variable.values[1] > 0 else 0 else: inferred[action] = variable.values[1] return q_values * inferred def get_action_id(self, action): if action[0] == 'Prompt': return 0 elif action[0] == 'Reward': return 1 elif action[0] == 'Abort': return 2 return 3 def set_evidence(self, evidence, id): if id == 1: evidence[("Prompt", 0)] = 1
class TestDynamicBayesianNetworkMethods(unittest.TestCase): def setUp(self): self.network = DynamicBayesianNetwork() self.grade_cpd = TabularCPD( ('G', 0), 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.2, 0.2]], [('D', 0), ('I', 0)], [2, 2]) self.d_i_cpd = TabularCPD(('D', 1), 2, [[0.6, 0.3], [0.4, 0.7]], [('D', 0)], 2) self.diff_cpd = TabularCPD(('D', 0), 2, [[0.6, 0.4]]) self.intel_cpd = TabularCPD(('I', 0), 2, [[0.7, 0.3]]) self.i_i_cpd = TabularCPD(('I', 1), 2, [[0.5, 0.4], [0.5, 0.6]], [('I', 0)], 2) self.grade_1_cpd = TabularCPD( ('G', 1), 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25, 0.08, 0.3], [0.3, 0.7, 0.2, 0.2]], [('D', 1), ('I', 1)], [2, 2]) def test_get_intra_and_inter_edges(self): self.network.add_edges_from([(('a', 0), ('b', 0)), (('a', 0), ('a', 1)), (('b', 0), ('b', 1))]) self.assertListEqual(sorted(self.network.get_intra_edges()), [(('a', 0), ('b', 0))]) self.assertListEqual(sorted(self.network.get_intra_edges(1)), [(('a', 1), ('b', 1))]) self.assertRaises(ValueError, self.network.get_intra_edges, -1) self.assertRaises(ValueError, self.network.get_intra_edges, '-') self.assertListEqual(sorted(self.network.get_inter_edges()), [(('a', 0), ('a', 1)), (('b', 0), ('b', 1))]) def test_get_interface_nodes(self): self.network.add_edges_from([ (('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1)) ]) self.assertListEqual(sorted(self.network.get_interface_nodes()), [('D', 0), ('I', 0)]) self.assertRaises(ValueError, self.network.get_interface_nodes, -1) self.assertRaises(ValueError, self.network.get_interface_nodes, '-') def test_get_slice_nodes(self): self.network.add_edges_from([ (('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1)) ]) self.assertListEqual(sorted(self.network.get_slice_nodes()), [('D', 0), ('G', 0), ('I', 0)]) self.assertListEqual(sorted(self.network.get_slice_nodes(1)), [('D', 1), ('G', 1), ('I', 1)]) self.assertRaises(ValueError, self.network.get_slice_nodes, -1) self.assertRaises(ValueError, self.network.get_slice_nodes, '-') def test_add_single_cpds(self): self.network.add_edges_from([(('D', 0), ('G', 0)), (('I', 0), ('G', 0))]) self.network.add_cpds(self.grade_cpd) self.assertListEqual(self.network.get_cpds(), [self.grade_cpd]) def test_get_cpds(self): self.network.add_edges_from([ (('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1)) ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual(set(self.network.get_cpds()), set([self.diff_cpd, self.intel_cpd, self.grade_cpd])) self.assertEqual( self.network.get_cpds(time_slice=1)[0].variable, ('G', 1)) def test_add_multiple_cpds(self): self.network.add_edges_from([ (('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1)) ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.assertEqual(self.network.get_cpds(('G', 0)).variable, ('G', 0)) self.assertEqual(self.network.get_cpds(('D', 1)).variable, ('D', 1)) self.assertEqual(self.network.get_cpds(('D', 0)).variable, ('D', 0)) self.assertEqual(self.network.get_cpds(('I', 0)).variable, ('I', 0)) self.assertEqual(self.network.get_cpds(('I', 1)).variable, ('I', 1)) def test_initialize_initial_state(self): self.network.add_nodes_from(['D', 'G', 'I', 'S', 'L']) self.network.add_edges_from([ (('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1)) ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual(len(self.network.cpds), 6) self.assertEqual(self.network.get_cpds(('G', 1)).variable, ('G', 1)) def test_moralize(self): self.network.add_edges_from(([(('D', 0), ('G', 0)), (('I', 0), ('G', 0))])) moral_graph = self.network.moralize() self.assertListEqual(hf.recursive_sorted(moral_graph.edges()), [[('D', 0), ('G', 0)], [('D', 0), ('I', 0)], [('D', 1), ('G', 1)], [('D', 1), ('I', 1)], [('G', 0), ('I', 0)], [('G', 1), ('I', 1)]]) def tearDown(self): del self.network
('I', 1), 3, [[ 0.7, 0.1, 0.9, 0.6, 0.0, 0.8, 0.1, 0.0, 0.3, 0.5, 0.1, 0.7, 0.3, 0.0, 0.6, 0.1, 0.0, 0.1 ], [ 0.2, 0.1, 0.1, 0.3, 0.2, 0.2, 0.3, 0.1, 0.5, 0.4, 0.1, 0.3, 0.4, 0.4, 0.4, 0.2, 0.1, 0.1 ], [ 0.1, 0.8, 0.0, 0.1, 0.8, 0.0, 0.6, 0.9, 0.2, 0.1, 0.8, 0.0, 0.3, 0.6, 0.0, 0.7, 0.9, 0.8 ]], [('A', 0), ('I', 0), ('R', 0)], [2, 3, 3]) dbn.add_cpds(cpd_I, cpd_A, cpd_I1, cpd_O, cpd_R) dbn.initialize_initial_state() print "Model created successfully: ", dbn.check_model() # CREATES SIMULATED DATA FROM DBN MODEL samples = list() for i in range(1, 10000): top_order = list(nx.topological_sort(dbn)) sample = dict() for node in top_order: curr_cpd = dbn.get_cpds(node) evidence = curr_cpd.get_evidence() ev_index = 0 if len(evidence) != 0:
class TestDynamicBayesianNetworkMethods(unittest.TestCase): def setUp(self): self.network = DynamicBayesianNetwork() self.grade_cpd = TabularCPD(('G', 0), 3, [[0.3, 0.05, 0.8, 0.5], [0.4, 0.25, 0.1, 0.3], [0.3, 0.7, 0.1, 0.2]], [('D', 0), ('I', 0)], [2, 2]) self.d_i_cpd = TabularCPD(('D', 1), 2, [[0.6, 0.3], [0.4, 0.7]], [('D', 0)], 2) self.diff_cpd = TabularCPD(('D', 0), 2, [[0.6, 0.4]]) self.intel_cpd = TabularCPD(('I', 0), 2, [[0.7, 0.3]]) self.i_i_cpd = TabularCPD(('I', 1), 2, [[0.5, 0.4], [0.5, 0.6]], [('I', 0)], 2) self.grade_1_cpd = TabularCPD(('G', 1), 3, [[0.3, 0.05, 0.8, 0.5], [0.4, 0.25, 0.1, 0.3], [0.3, 0.7, 0.1, 0.2]], [('D', 1), ('I', 1)], [2, 2]) def test_get_intra_and_inter_edges(self): self.network.add_edges_from([(('a', 0), ('b', 0)), (('a', 0), ('a', 1)), (('b', 0), ('b', 1))]) self.assertListEqual(sorted(self.network.get_intra_edges()), [(('a', 0), ('b', 0))]) self.assertListEqual(sorted(self.network.get_intra_edges(1)), [(('a', 1), ('b', 1))]) self.assertRaises(ValueError, self.network.get_intra_edges, -1) self.assertRaises(ValueError, self.network.get_intra_edges, '-') self.assertListEqual(sorted(self.network.get_inter_edges()), [(('a', 0), ('a', 1)), (('b', 0), ('b', 1))]) def test_get_interface_nodes(self): self.network.add_edges_from( [(('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1))]) self.assertListEqual(sorted(self.network.get_interface_nodes()), [('D', 0), ('I',0)]) self.assertRaises(ValueError, self.network.get_interface_nodes, -1) self.assertRaises(ValueError, self.network.get_interface_nodes, '-') def test_get_slice_nodes(self): self.network.add_edges_from( [(('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1))]) self.assertListEqual(sorted(self.network.get_slice_nodes()), [('D', 0), ('G', 0), ('I', 0)]) self.assertListEqual(sorted(self.network.get_slice_nodes(1)), [('D', 1), ('G', 1), ('I', 1)]) self.assertRaises(ValueError, self.network.get_slice_nodes, -1) self.assertRaises(ValueError, self.network.get_slice_nodes, '-') def test_add_single_cpds(self): self.network.add_edges_from([(('D', 0), ('G', 0)), (('I', 0), ('G', 0))]) self.network.add_cpds(self.grade_cpd) self.assertListEqual(self.network.get_cpds(), [self.grade_cpd]) def test_get_cpds(self): self.network.add_edges_from( [(('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1))]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual(set(self.network.get_cpds()), set([self.diff_cpd, self.intel_cpd, self.grade_cpd])) self.assertEqual(self.network.get_cpds(time_slice=1)[0].variable, ('G', 1)) def test_add_multiple_cpds(self): self.network.add_edges_from( [(('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1))]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.assertEqual(self.network.get_cpds(('G', 0)).variable, ('G', 0)) self.assertEqual(self.network.get_cpds(('D', 1)).variable, ('D', 1)) self.assertEqual(self.network.get_cpds(('D', 0)).variable, ('D', 0)) self.assertEqual(self.network.get_cpds(('I', 0)).variable, ('I', 0)) self.assertEqual(self.network.get_cpds(('I', 1)).variable, ('I', 1)) def test_initialize_initial_state(self): self.network.add_nodes_from(['D', 'G', 'I', 'S', 'L']) self.network.add_edges_from( [(('D', 0), ('G', 0)), (('I', 0), ('G', 0)), (('D', 0), ('D', 1)), (('I', 0), ('I', 1))]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual(len(self.network.cpds), 6) self.assertEqual(self.network.get_cpds(('G', 1)).variable, ('G', 1)) def test_moralize(self): self.network.add_edges_from(([(('D',0), ('G',0)), (('I',0), ('G',0))])) moral_graph = self.network.moralize() self.assertListEqual(hf.recursive_sorted(moral_graph.edges()), [[('D', 0), ('G', 0)], [('D', 0), ('I', 0)], [('D', 1), ('G', 1)], [('D', 1), ('I', 1)], [('G', 0), ('I', 0)], [('G', 1), ('I', 1)]]) def tearDown(self): del self.network
class TestDynamicBayesianNetworkMethods(unittest.TestCase): def setUp(self): self.network = DynamicBayesianNetwork() self.grade_cpd = TabularCPD( ("G", 0), 3, values=[[0.3, 0.05, 0.8, 0.5], [0.4, 0.25, 0.1, 0.3], [0.3, 0.7, 0.1, 0.2]], evidence=[("D", 0), ("I", 0)], evidence_card=[2, 2], ) self.d_i_cpd = TabularCPD( ("D", 1), 2, values=[[0.6, 0.3], [0.4, 0.7]], evidence=[("D", 0)], evidence_card=[2], ) self.diff_cpd = TabularCPD(("D", 0), 2, values=[[0.6, 0.4]]) self.intel_cpd = TabularCPD(("I", 0), 2, values=[[0.7, 0.3]]) self.i_i_cpd = TabularCPD( ("I", 1), 2, values=[[0.5, 0.4], [0.5, 0.6]], evidence=[("I", 0)], evidence_card=[2], ) self.grade_1_cpd = TabularCPD( ("G", 1), 3, values=[[0.3, 0.05, 0.8, 0.5], [0.4, 0.25, 0.1, 0.3], [0.3, 0.7, 0.1, 0.2]], evidence=[("D", 1), ("I", 1)], evidence_card=[2, 2], ) def test_get_intra_and_inter_edges(self): self.network.add_edges_from([(("a", 0), ("b", 0)), (("a", 0), ("a", 1)), (("b", 0), ("b", 1))]) self.assertListEqual(sorted(self.network.get_intra_edges()), [(("a", 0), ("b", 0))]) self.assertListEqual(sorted(self.network.get_intra_edges(1)), [(("a", 1), ("b", 1))]) self.assertRaises(ValueError, self.network.get_intra_edges, -1) self.assertRaises(ValueError, self.network.get_intra_edges, "-") self.assertListEqual( sorted(self.network.get_inter_edges()), [(("a", 0), ("a", 1)), (("b", 0), ("b", 1))], ) def test_get_interface_nodes(self): self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) self.assertListEqual(sorted(self.network.get_interface_nodes()), [("D", 0), ("I", 0)]) self.assertRaises(ValueError, self.network.get_interface_nodes, -1) self.assertRaises(ValueError, self.network.get_interface_nodes, "-") def test_get_slice_nodes(self): self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) self.assertListEqual(sorted(self.network.get_slice_nodes()), [("D", 0), ("G", 0), ("I", 0)]) self.assertListEqual(sorted(self.network.get_slice_nodes(1)), [("D", 1), ("G", 1), ("I", 1)]) self.assertRaises(ValueError, self.network.get_slice_nodes, -1) self.assertRaises(ValueError, self.network.get_slice_nodes, "-") def test_add_single_cpds(self): self.network.add_edges_from([(("D", 0), ("G", 0)), (("I", 0), ("G", 0))]) self.network.add_cpds(self.grade_cpd) self.assertListEqual(self.network.get_cpds(), [self.grade_cpd]) def test_get_cpds(self): self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual( set(self.network.get_cpds()), set([self.diff_cpd, self.intel_cpd, self.grade_cpd]), ) self.assertEqual( {cpd.variable for cpd in self.network.get_cpds(time_slice=1)}, {("D", 1), ("I", 1), ("G", 1)}, ) def test_add_multiple_cpds(self): self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.assertEqual(self.network.get_cpds(("G", 0)).variable, ("G", 0)) self.assertEqual(self.network.get_cpds(("D", 1)).variable, ("D", 1)) self.assertEqual(self.network.get_cpds(("D", 0)).variable, ("D", 0)) self.assertEqual(self.network.get_cpds(("I", 0)).variable, ("I", 0)) self.assertEqual(self.network.get_cpds(("I", 1)).variable, ("I", 1)) def test_initialize_initial_state(self): self.network.add_nodes_from(["D", "G", "I", "S", "L"]) self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) self.network.add_cpds(self.grade_cpd, self.d_i_cpd, self.diff_cpd, self.intel_cpd, self.i_i_cpd) self.network.initialize_initial_state() self.assertEqual(len(self.network.cpds), 6) self.assertEqual(self.network.get_cpds(("G", 1)).variable, ("G", 1)) def test_moralize(self): self.network.add_edges_from(([(("D", 0), ("G", 0)), (("I", 0), ("G", 0))])) moral_graph = self.network.moralize() self.assertListEqual( hf.recursive_sorted(moral_graph.edges()), [ [("D", 0), ("G", 0)], [("D", 0), ("I", 0)], [("D", 1), ("G", 1)], [("D", 1), ("I", 1)], [("G", 0), ("I", 0)], [("G", 1), ("I", 1)], ], ) def test_copy(self): self.network.add_edges_from([ (("D", 0), ("G", 0)), (("I", 0), ("G", 0)), (("D", 0), ("D", 1)), (("I", 0), ("I", 1)), ]) cpd = TabularCPD( ("G", 0), 3, values=[[0.3, 0.05, 0.8, 0.5], [0.4, 0.25, 0.1, 0.3], [0.3, 0.7, 0.1, 0.2]], evidence=[("D", 0), ("I", 0)], evidence_card=[2, 2], ) self.network.add_cpds(cpd) copy = self.network.copy() self.assertIsInstance(copy, DynamicBayesianNetwork) self.assertListEqual(sorted(self.network._nodes()), sorted(copy._nodes())) self.assertListEqual(sorted(self.network.edges()), sorted(copy.edges())) self.assertListEqual(self.network.get_cpds(), copy.get_cpds()) self.assertListEqual(sorted(self.network.get_intra_edges()), sorted(copy.get_intra_edges())) self.assertListEqual(sorted(self.network.get_inter_edges()), sorted(copy.get_inter_edges())) self.assertListEqual(sorted(self.network.get_slice_nodes()), sorted(copy.get_slice_nodes())) copy.cpds[0].values = np.array([[0.4, 0.05, 0.3, 0.5], [0.3, 0.25, 0.5, 0.3], [0.3, 0.7, 0.2, 0.2]]) self.assertNotEqual(self.network.get_cpds(), copy.get_cpds()) self.network.add_cpds(self.i_i_cpd, self.d_i_cpd) copy.add_cpds(self.diff_cpd, self.intel_cpd) self.network.add_node("A") copy.add_node("Z") self.network.add_edge(("A", 0), ("D", 0)) copy.add_edge(("Z", 0), ("D", 0)) self.assertNotEqual(sorted(self.network._nodes()), sorted(copy._nodes())) self.assertNotEqual(sorted(self.network.edges()), sorted(copy.edges())) self.assertNotEqual(self.network.get_cpds(), copy.get_cpds()) self.assertNotEqual(sorted(self.network.get_intra_edges()), sorted(copy.get_intra_edges())) self.assertListEqual(sorted(self.network.get_inter_edges()), sorted(copy.get_inter_edges())) self.assertNotEqual(sorted(self.network.get_slice_nodes()), sorted(copy.get_slice_nodes())) self.network.add_edge(("A", 0), ("D", 1)) copy.add_edge(("Z", 0), ("D", 1)) self.assertNotEqual(sorted(self.network.get_inter_edges()), sorted(copy.get_inter_edges())) def tearDown(self): del self.network