<s> import unittest <EOL> import networkx as nx <EOL> import pandas as pd <EOL> import numpy as np <EOL> import numpy . testing as np_test <EOL> from pgmpy . models import BayesianModel , MarkovModel <EOL> import pgmpy . tests . help_functions as hf <EOL> from pgmpy . factors import TabularCPD , JointProbabilityDistribution , Factor <EOL> from pgmpy . independencies import Independencies <EOL> class TestBaseModelCreation ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . G = BayesianModel ( ) <EOL> def test_class_init_without_data ( self ) : <EOL> self . assertIsInstance ( self . G , nx . DiGraph ) <EOL> def test_class_init_with_data_string ( self ) : <EOL> self . g = BayesianModel ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ] ) <EOL> self . assertListEqual ( sorted ( self . g . nodes ( ) ) , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertListEqual ( hf . recursive_sorted ( self . g . edges ( ) ) , <EOL> [ [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , [ '<STR_LIT:b>' , '<STR_LIT:c>' ] ] ) <EOL> def test_class_init_with_data_nonstring ( self ) : <EOL> BayesianModel ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_add_node_string ( self ) : <EOL> self . G . add_node ( '<STR_LIT:a>' ) <EOL> self . assertListEqual ( self . G . nodes ( ) , [ '<STR_LIT:a>' ] ) <EOL> def test_add_node_nonstring ( self ) : <EOL> self . G . add_node ( <NUM_LIT:1> ) <EOL> def test_add_nodes_from_string ( self ) : <EOL> self . G . add_nodes_from ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> self . assertListEqual ( sorted ( self . G . nodes ( ) ) , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' ] ) <EOL> def test_add_nodes_from_non_string ( self ) : <EOL> self . G . add_nodes_from ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> def test_add_edge_string ( self ) : <EOL> self . G . add_edge ( '<STR_LIT:d>' , '<STR_LIT:e>' ) <EOL> self . assertListEqual ( sorted ( self . G . nodes ( ) ) , [ '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> self . assertListEqual ( self . G . edges ( ) , [ ( '<STR_LIT:d>' , '<STR_LIT:e>' ) ] ) <EOL> self . G . add_nodes_from ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . G . add_edge ( '<STR_LIT:a>' , '<STR_LIT:b>' ) <EOL> self . assertListEqual ( hf . recursive_sorted ( self . G . edges ( ) ) , <EOL> [ [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , [ '<STR_LIT:d>' , '<STR_LIT:e>' ] ] ) <EOL> def test_add_edge_nonstring ( self ) : <EOL> self . G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> def test_add_edge_selfloop ( self ) : <EOL> self . assertRaises ( ValueError , self . G . add_edge , '<STR_LIT:a>' , '<STR_LIT:a>' ) <EOL> def test_add_edge_result_cycle ( self ) : <EOL> self . G . add_edges_from ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:a>' , '<STR_LIT:c>' ) ] ) <EOL> self . assertRaises ( ValueError , self . G . add_edge , '<STR_LIT:c>' , '<STR_LIT:a>' ) <EOL> def test_add_edges_from_string ( self ) : <EOL> self . G . add_edges_from ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ] ) <EOL> self . assertListEqual ( sorted ( self . G . nodes ( ) ) , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . assertListEqual ( hf . recursive_sorted ( self . G . edges ( ) ) , <EOL> [ [ '<STR_LIT:a>' , '<STR_LIT:b>' ] , [ '<STR_LIT:b>' , '<STR_LIT:c>' ] ] ) <EOL> self . G . add_nodes_from ( [ '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ] ) <EOL> self . G . add_edges_from ( [ ( '<STR_LIT:d>' , '<STR_LIT:e>' ) , ( '<STR_LIT:e>' , '<STR_LIT:f>' ) ] ) <EOL> self . assertListEqual ( sorted ( self . G . nodes ( ) ) , <EOL> [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' , '<STR_LIT:f>' ] ) <EOL> self . assertListEqual ( hf . recursive_sorted ( self . G . edges ( ) ) , <EOL> hf . recursive_sorted ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , <EOL> ( '<STR_LIT:d>' , '<STR_LIT:e>' ) , ( '<STR_LIT:e>' , '<STR_LIT:f>' ) ] ) ) <EOL> def test_add_edges_from_nonstring ( self ) : <EOL> self . G . add_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] ) <EOL> def test_add_edges_from_self_loop ( self ) : <EOL> self . assertRaises ( ValueError , self . G . add_edges_from , <EOL> [ ( '<STR_LIT:a>' , '<STR_LIT:a>' ) ] ) <EOL> def test_add_edges_from_result_cycle ( self ) : <EOL> self . assertRaises ( ValueError , self . G . add_edges_from , <EOL> [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , ( '<STR_LIT:c>' , '<STR_LIT:a>' ) ] ) <EOL> def test_update_node_parents_bm_constructor ( self ) : <EOL> self . g = BayesianModel ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ] ) <EOL> self . assertListEqual ( self . g . predecessors ( '<STR_LIT:a>' ) , [ ] ) <EOL> self . assertListEqual ( self . g . predecessors ( '<STR_LIT:b>' ) , [ '<STR_LIT:a>' ] ) <EOL> self . assertListEqual ( self . g . predecessors ( '<STR_LIT:c>' ) , [ '<STR_LIT:b>' ] ) <EOL> def test_update_node_parents ( self ) : <EOL> self . G . add_nodes_from ( [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ] ) <EOL> self . G . add_edges_from ( [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ] ) <EOL> self . assertListEqual ( self . G . predecessors ( '<STR_LIT:a>' ) , [ ] ) <EOL> self . assertListEqual ( self . G . predecessors ( '<STR_LIT:b>' ) , [ '<STR_LIT:a>' ] ) <EOL> self . assertListEqual ( self . G . predecessors ( '<STR_LIT:c>' ) , [ '<STR_LIT:b>' ] ) <EOL> def tearDown ( self ) : <EOL> del self . G <EOL> class TestBayesianModelMethods ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . G = BayesianModel ( [ ( '<STR_LIT:a>' , '<STR_LIT:d>' ) , ( '<STR_LIT:b>' , '<STR_LIT:d>' ) , <EOL> ( '<STR_LIT:d>' , '<STR_LIT:e>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) ] ) <EOL> self . G1 = BayesianModel ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> diff_cpd = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = [ [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> intel_cpd = TabularCPD ( '<STR_LIT>' , <NUM_LIT:3> , values = [ [ <NUM_LIT:0.5> ] , [ <NUM_LIT> ] , [ <NUM_LIT> ] ] ) <EOL> grade_cpd = TabularCPD ( '<STR_LIT>' , <NUM_LIT:3> , values = [ [ <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT:0.1> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . G1 . add_cpds ( diff_cpd , intel_cpd , grade_cpd ) <EOL> def test_moral_graph ( self ) : <EOL> moral_graph = self . G . moralize ( ) <EOL> self . assertListEqual ( sorted ( moral_graph . nodes ( ) ) , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> for edge in moral_graph . edges ( ) : <EOL> self . assertTrue ( edge in [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:a>' , '<STR_LIT:d>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , ( '<STR_LIT:d>' , '<STR_LIT:b>' ) , ( '<STR_LIT:e>' , '<STR_LIT:d>' ) ] or <EOL> ( edge [ <NUM_LIT:1> ] , edge [ <NUM_LIT:0> ] ) in [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:a>' , '<STR_LIT:d>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , ( '<STR_LIT:d>' , '<STR_LIT:b>' ) , ( '<STR_LIT:e>' , '<STR_LIT:d>' ) ] ) <EOL> def test_moral_graph_with_edge_present_over_parents ( self ) : <EOL> G = BayesianModel ( [ ( '<STR_LIT:a>' , '<STR_LIT:d>' ) , ( '<STR_LIT:d>' , '<STR_LIT:e>' ) , ( '<STR_LIT:b>' , '<STR_LIT:d>' ) , ( '<STR_LIT:b>' , '<STR_LIT:c>' ) , ( '<STR_LIT:a>' , '<STR_LIT:b>' ) ] ) <EOL> moral_graph = G . moralize ( ) <EOL> self . assertListEqual ( sorted ( moral_graph . nodes ( ) ) , [ '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] ) <EOL> for edge in moral_graph . edges ( ) : <EOL> self . assertTrue ( edge in [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:b>' ) , ( '<STR_LIT:d>' , '<STR_LIT:a>' ) , ( '<STR_LIT:d>' , '<STR_LIT:b>' ) , ( '<STR_LIT:d>' , '<STR_LIT:e>' ) ] or <EOL> ( edge [ <NUM_LIT:1> ] , edge [ <NUM_LIT:0> ] ) in [ ( '<STR_LIT:a>' , '<STR_LIT:b>' ) , ( '<STR_LIT:c>' , '<STR_LIT:b>' ) , ( '<STR_LIT:d>' , '<STR_LIT:a>' ) , ( '<STR_LIT:d>' , '<STR_LIT:b>' ) , ( '<STR_LIT:d>' , '<STR_LIT:e>' ) ] ) <EOL> def test_local_independencies ( self ) : <EOL> self . assertEqual ( self . G . local_independencies ( '<STR_LIT:a>' ) , Independencies ( [ '<STR_LIT:a>' , [ '<STR_LIT:b>' , '<STR_LIT:c>' ] ] ) ) <EOL> self . assertEqual ( self . G . local_independencies ( '<STR_LIT:c>' ) , Independencies ( [ '<STR_LIT:c>' , [ '<STR_LIT:a>' , '<STR_LIT:d>' , '<STR_LIT:e>' ] , '<STR_LIT:b>' ] ) ) <EOL> self . assertEqual ( self . G . local_independencies ( '<STR_LIT:d>' ) , Independencies ( [ '<STR_LIT:d>' , '<STR_LIT:c>' , [ '<STR_LIT:b>' , '<STR_LIT:a>' ] ] ) ) <EOL> self . assertEqual ( self . G . local_independencies ( '<STR_LIT:e>' ) , Independencies ( [ '<STR_LIT:e>' , [ '<STR_LIT:c>' , '<STR_LIT:b>' , '<STR_LIT:a>' ] , '<STR_LIT:d>' ] ) ) <EOL> self . assertEqual ( self . G . local_independencies ( '<STR_LIT:b>' ) , Independencies ( [ '<STR_LIT:b>' , '<STR_LIT:a>' ] ) ) <EOL> self . assertEqual ( self . G1 . local_independencies ( '<STR_LIT>' ) , Independencies ( ) ) <EOL> def test_get_independencies ( self ) : <EOL> chain = BayesianModel ( [ ( '<STR_LIT:X>' , '<STR_LIT:Y>' ) , ( '<STR_LIT:Y>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( chain . get_independencies ( ) , Independencies ( ( '<STR_LIT:X>' , '<STR_LIT>' , '<STR_LIT:Y>' ) , ( '<STR_LIT>' , '<STR_LIT:X>' , '<STR_LIT:Y>' ) ) ) <EOL> fork = BayesianModel ( [ ( '<STR_LIT:Y>' , '<STR_LIT:X>' ) , ( '<STR_LIT:Y>' , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( fork . get_independencies ( ) , Independencies ( ( '<STR_LIT:X>' , '<STR_LIT>' , '<STR_LIT:Y>' ) , ( '<STR_LIT>' , '<STR_LIT:X>' , '<STR_LIT:Y>' ) ) ) <EOL> collider = BayesianModel ( [ ( '<STR_LIT:X>' , '<STR_LIT:Y>' ) , ( '<STR_LIT>' , '<STR_LIT:Y>' ) ] ) <EOL> self . assertEqual ( collider . get_independencies ( ) , Independencies ( ( '<STR_LIT:X>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:X>' ) ) ) <EOL> def test_is_imap ( self ) : <EOL> val = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> JPD = JointProbabilityDistribution ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ] , val ) <EOL> fac = Factor ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:3> ] , val ) <EOL> self . assertTrue ( self . G1 . is_imap ( JPD ) ) <EOL> self . assertRaises ( TypeError , self . G1 . is_imap , fac ) <EOL> def test_get_immoralities ( self ) : <EOL> G = BayesianModel ( [ ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , ( '<STR_LIT:z>' , '<STR_LIT:y>' ) , ( '<STR_LIT:x>' , '<STR_LIT:z>' ) , ( '<STR_LIT:w>' , '<STR_LIT:y>' ) ] ) <EOL> self . assertEqual ( G . get_immoralities ( ) , { ( '<STR_LIT:w>' , '<STR_LIT:x>' ) , ( '<STR_LIT:w>' , '<STR_LIT:z>' ) } ) <EOL> G1 = BayesianModel ( [ ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , ( '<STR_LIT:z>' , '<STR_LIT:y>' ) , ( '<STR_LIT:z>' , '<STR_LIT:x>' ) , ( '<STR_LIT:w>' , '<STR_LIT:y>' ) ] ) <EOL> self . assertEqual ( G1 . get_immoralities ( ) , { ( '<STR_LIT:w>' , '<STR_LIT:x>' ) , ( '<STR_LIT:w>' , '<STR_LIT:z>' ) } ) <EOL> G2 = BayesianModel ( [ ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , ( '<STR_LIT:z>' , '<STR_LIT:y>' ) , ( '<STR_LIT:x>' , '<STR_LIT:z>' ) , ( '<STR_LIT:w>' , '<STR_LIT:y>' ) , ( '<STR_LIT:w>' , '<STR_LIT:x>' ) ] ) <EOL> self . assertEqual ( G2 . get_immoralities ( ) , { ( '<STR_LIT:w>' , '<STR_LIT:z>' ) } ) <EOL> def test_is_iequivalent ( self ) : <EOL> G = BayesianModel ( [ ( '<STR_LIT:x>' , '<STR_LIT:y>' ) , ( '<STR_LIT:z>' , '<STR_LIT:y>' ) , ( '<STR_LIT:x>' , '<STR_LIT:z>' ) , ( '<STR_LIT:w>' , '<STR_LIT:y>' ) ] ) <EOL> self . assertRaises ( TypeError , G . is_iequivalent , MarkovModel ( ) ) <EOL> G1 = BayesianModel ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:X>' ) , ( '<STR_LIT:X>' , '<STR_LIT:Y>' ) , ( '<STR_LIT>' , '<STR_LIT:Y>' ) ] ) <EOL> G2 = BayesianModel ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT:X>' , '<STR_LIT>' ) , ( '<STR_LIT:X>' , '<STR_LIT:Y>' ) , ( '<STR_LIT>' , '<STR_LIT:Y>' ) ] ) <EOL> self . assertTrue ( G1 . is_iequivalent ( G2 ) ) <EOL> G3 = BayesianModel ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT:X>' ) , ( '<STR_LIT:Y>' , '<STR_LIT:X>' ) , ( '<STR_LIT>' , '<STR_LIT:Y>' ) ] ) <EOL> self . assertFalse ( G3 . is_iequivalent ( G2 ) ) <EOL> def tearDown ( self ) : <EOL> del self . G <EOL> del self . G1 <EOL> class TestBayesianModelCPD ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . G = BayesianModel ( [ ( '<STR_LIT:d>' , '<STR_LIT:g>' ) , ( '<STR_LIT:i>' , '<STR_LIT:g>' ) , ( '<STR_LIT:g>' , '<STR_LIT:l>' ) , <EOL> ( '<STR_LIT:i>' , '<STR_LIT:s>' ) ] ) <EOL> def test_active_trail_nodes ( self ) : <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:d>' ) ) , [ '<STR_LIT:d>' , '<STR_LIT:g>' , '<STR_LIT:l>' ] ) <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:i>' ) ) , [ '<STR_LIT:g>' , '<STR_LIT:i>' , '<STR_LIT:l>' , '<STR_LIT:s>' ] ) <EOL> def test_active_trail_nodes_args ( self ) : <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:d>' , observed = '<STR_LIT:g>' ) ) , [ '<STR_LIT:d>' , '<STR_LIT:i>' , '<STR_LIT:s>' ] ) <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:l>' , observed = '<STR_LIT:g>' ) ) , [ '<STR_LIT:l>' ] ) <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:s>' , observed = [ '<STR_LIT:i>' , '<STR_LIT:l>' ] ) ) , [ '<STR_LIT:s>' ] ) <EOL> self . assertEqual ( sorted ( self . G . active_trail_nodes ( '<STR_LIT:s>' , observed = [ '<STR_LIT:d>' , '<STR_LIT:l>' ] ) ) , [ '<STR_LIT:g>' , '<STR_LIT:i>' , '<STR_LIT:s>' ] ) <EOL> def test_is_active_trail_triplets ( self ) : <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:l>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:g>' , '<STR_LIT:s>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:i>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:i>' , observed = '<STR_LIT:g>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:l>' , observed = '<STR_LIT:g>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:i>' , '<STR_LIT:l>' , observed = '<STR_LIT:g>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:i>' , observed = '<STR_LIT:l>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:g>' , '<STR_LIT:s>' , observed = '<STR_LIT:i>' ) ) <EOL> def test_is_active_trail ( self ) : <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:s>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:s>' , '<STR_LIT:l>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:s>' , observed = '<STR_LIT:g>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:s>' , '<STR_LIT:l>' , observed = '<STR_LIT:g>' ) ) <EOL> def test_is_active_trail_args ( self ) : <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:s>' , '<STR_LIT:l>' , '<STR_LIT:i>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:s>' , '<STR_LIT:l>' , '<STR_LIT:g>' ) ) <EOL> self . assertTrue ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:s>' , '<STR_LIT:l>' ) ) <EOL> self . assertFalse ( self . G . is_active_trail ( '<STR_LIT:d>' , '<STR_LIT:s>' , [ '<STR_LIT:i>' , '<STR_LIT:l>' ] ) ) <EOL> def test_get_cpds ( self ) : <EOL> cpd_d = TabularCPD ( '<STR_LIT:d>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd_i = TabularCPD ( '<STR_LIT:i>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> evidence = [ '<STR_LIT:g>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> cpd_s = TabularCPD ( '<STR_LIT:s>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> evidence = [ '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_d , cpd_i , cpd_g , cpd_l , cpd_s ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:d>' ) . variable , '<STR_LIT:d>' ) <EOL> def test_get_cpds1 ( self ) : <EOL> self . model = BayesianModel ( [ ( '<STR_LIT:A>' , '<STR_LIT>' ) ] ) <EOL> cpd_a = TabularCPD ( '<STR_LIT:A>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd_ab = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> evidence = [ '<STR_LIT:A>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . model . add_cpds ( cpd_a , cpd_ab ) <EOL> self . assertEqual ( self . model . get_cpds ( '<STR_LIT:A>' ) . variable , '<STR_LIT:A>' ) <EOL> self . assertEqual ( self . model . get_cpds ( '<STR_LIT>' ) . variable , '<STR_LIT>' ) <EOL> def test_add_single_cpd ( self ) : <EOL> cpd_s = TabularCPD ( '<STR_LIT:s>' , <NUM_LIT:2> , np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , [ '<STR_LIT:i>' ] , <NUM_LIT:2> ) <EOL> self . G . add_cpds ( cpd_s ) <EOL> self . assertListEqual ( self . G . get_cpds ( ) , [ cpd_s ] ) <EOL> def test_add_multiple_cpds ( self ) : <EOL> cpd_d = TabularCPD ( '<STR_LIT:d>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd_i = TabularCPD ( '<STR_LIT:i>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> evidence = [ '<STR_LIT:g>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> cpd_s = TabularCPD ( '<STR_LIT:s>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> evidence = [ '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_d , cpd_i , cpd_g , cpd_l , cpd_s ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:d>' ) , cpd_d ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:i>' ) , cpd_i ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:g>' ) , cpd_g ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:l>' ) , cpd_l ) <EOL> self . assertEqual ( self . G . get_cpds ( '<STR_LIT:s>' ) , cpd_s ) <EOL> def test_check_model ( self ) : <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> cpd_s = TabularCPD ( '<STR_LIT:s>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:g>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_g , cpd_s , cpd_l ) <EOL> self . assertTrue ( self . G . check_model ( ) ) <EOL> def test_check_model1 ( self ) : <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_g ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_g ) <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:s>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_g ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_g ) <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:l>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_g ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_g ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:d>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_l ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_l ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_l ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_l ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:g>' , '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_l ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_l ) <EOL> def test_check_model2 ( self ) : <EOL> cpd_s = TabularCPD ( '<STR_LIT:s>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT:0.5> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:i>' ] , evidence_card = <NUM_LIT:2> ) <EOL> self . G . add_cpds ( cpd_s ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_s ) <EOL> cpd_g = TabularCPD ( '<STR_LIT:g>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:d>' , '<STR_LIT:i>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_g ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_g ) <EOL> cpd_l = TabularCPD ( '<STR_LIT:l>' , <NUM_LIT:2> , values = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.1> , <NUM_LIT> ] ] ) , <EOL> evidence = [ '<STR_LIT:g>' ] , evidence_card = [ <NUM_LIT:2> ] ) <EOL> self . G . add_cpds ( cpd_l ) <EOL> self . assertRaises ( ValueError , self . G . check_model ) <EOL> self . G . remove_cpds ( cpd_l ) <EOL> def tearDown ( self ) : <EOL> del self . G <EOL> class TestBayesianModelFitPredict ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . model_disconnected = BayesianModel ( ) <EOL> self . model_disconnected . add_nodes_from ( [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' ] ) <EOL> self . model_connected = BayesianModel ( [ ( '<STR_LIT:A>' , '<STR_LIT:B>' ) , ( '<STR_LIT:C>' , '<STR_LIT:B>' ) , ( '<STR_LIT:C>' , '<STR_LIT:D>' ) , ( '<STR_LIT:B>' , '<STR_LIT:E>' ) ] ) <EOL> def test_disconnected_fit ( self ) : <EOL> values = pd . DataFrame ( np . random . randint ( low = <NUM_LIT:0> , high = <NUM_LIT:2> , size = ( <NUM_LIT:1000> , <NUM_LIT:5> ) ) , <EOL> columns = [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' ] ) <EOL> self . model_disconnected . fit ( values ) <EOL> for node in [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' ] : <EOL> cpd = self . model_disconnected . get_cpds ( node ) <EOL> self . assertEqual ( cpd . variable , node ) <EOL> np_test . assert_array_equal ( cpd . cardinality , np . array ( [ <NUM_LIT:2> ] ) ) <EOL> value = ( values . ix [ : , node ] . value_counts ( ) / <EOL> values . ix [ : , node ] . value_counts ( ) . sum ( ) ) <EOL> value = value . reindex ( sorted ( value . index ) ) . values <EOL> np_test . assert_array_equal ( cpd . values , value ) <EOL> def test_connected_predict ( self ) : <EOL> np . random . seed ( <NUM_LIT> ) <EOL> values = pd . DataFrame ( np . random . randint ( low = <NUM_LIT:0> , high = <NUM_LIT:2> , size = ( <NUM_LIT:1000> , <NUM_LIT:5> ) ) , <EOL> columns = [ '<STR_LIT:A>' , '<STR_LIT:B>' , '<STR_LIT:C>' , '<STR_LIT:D>' , '<STR_LIT:E>' ] ) <EOL> fit_data = values [ : <NUM_LIT> ] <EOL> predict_data = values [ <NUM_LIT> : ] . copy ( ) <EOL> self . model_connected . fit ( fit_data ) <EOL> self . assertRaises ( ValueError , self . model_connected . predict , predict_data ) <EOL> predict_data . drop ( '<STR_LIT:E>' , axis = <NUM_LIT:1> , inplace = True ) <EOL> e_predict = self . model_connected . predict ( predict_data ) <EOL> np_test . assert_array_equal ( e_predict . values . ravel ( ) , <EOL> np . array ( [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <EOL> <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ) ) <EOL> def tearDown ( self ) : <EOL> del self . model_connected <EOL> del self . model_disconnected <EOL> class TestDirectedGraphCPDOperations ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . graph = BayesianModel ( ) <EOL> def test_add_single_cpd ( self ) : <EOL> cpd = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd ] ) <EOL> def test_add_multiple_cpds ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd1 , cpd2 , cpd3 ] ) <EOL> def test_remove_single_cpd ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . graph . remove_cpds ( cpd1 ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd2 , cpd3 ] ) <EOL> def test_remove_multiple_cpds ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . graph . remove_cpds ( cpd1 , cpd3 ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd2 ] ) <EOL> def test_remove_single_cpd_string ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . graph . remove_cpds ( '<STR_LIT>' ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd2 , cpd3 ] ) <EOL> def test_remove_multiple_cpds_string ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . graph . remove_cpds ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertListEqual ( self . graph . get_cpds ( ) , [ cpd2 ] ) <EOL> def test_get_cpd_for_node ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . assertEqual ( self . graph . get_cpds ( '<STR_LIT>' ) , cpd1 ) <EOL> self . assertEqual ( self . graph . get_cpds ( '<STR_LIT>' ) , cpd2 ) <EOL> self . assertEqual ( self . graph . get_cpds ( '<STR_LIT>' ) , cpd3 ) <EOL> def test_get_cpd_raises_error ( self ) : <EOL> cpd1 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd2 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:1> ) ) <EOL> cpd3 = TabularCPD ( '<STR_LIT>' , <NUM_LIT:2> , values = np . random . rand ( <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> evidence = [ '<STR_LIT>' , '<STR_LIT>' ] , evidence_card = [ <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> self . graph . add_edges_from ( [ ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] ) <EOL> self . graph . add_cpds ( cpd1 , cpd2 , cpd3 ) <EOL> self . assertRaises ( ValueError , self . graph . get_cpds , '<STR_LIT>' ) <EOL> def tearDown ( self ) : <EOL> del self . graph </s>
