<s> import os <EOL> import numpy as N <EOL> from pebl import network , data , config <EOL> class TestEdgeSet : <EOL> def setUp ( self ) : <EOL> self . edges = network . EdgeSet ( num_nodes = <NUM_LIT:6> ) <EOL> self . tuplelist = [ ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> for edge in self . tuplelist : <EOL> self . edges . add ( edge ) <EOL> def test_add ( self ) : <EOL> self . edges . add ( ( <NUM_LIT:5> , <NUM_LIT:1> ) ) <EOL> assert set ( self . edges ) == set ( self . tuplelist + [ ( <NUM_LIT:5> , <NUM_LIT:1> ) ] ) <EOL> def test_add_many ( self ) : <EOL> self . edges . add_many ( [ ( <NUM_LIT:5> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , <NUM_LIT:2> ) ] ) <EOL> assert set ( self . edges ) == set ( self . tuplelist + [ ( <NUM_LIT:5> , <NUM_LIT:1> ) , ( <NUM_LIT:5> , <NUM_LIT:2> ) ] ) <EOL> def test_remove ( self ) : <EOL> self . edges . remove ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> assert set ( self . edges ) == set ( [ ( <NUM_LIT:0> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> def test_remove_many ( self ) : <EOL> self . edges . remove_many ( [ ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:5> ) ] ) <EOL> assert set ( self . edges ) == set ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> def test_edgeiter ( self ) : <EOL> assert set ( self . edges ) == set ( self . tuplelist ) , "<STR_LIT>" <EOL> def test_len ( self ) : <EOL> assert len ( self . edges ) == len ( self . tuplelist ) , "<STR_LIT>" <EOL> def test_addedges1 ( self ) : <EOL> self . edges . add ( ( <NUM_LIT:0> , <NUM_LIT:3> ) ) <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:3> ) in self . edges , "<STR_LIT>" <EOL> def test_incoming ( self ) : <EOL> assert set ( self . edges . incoming ( <NUM_LIT:0> ) ) == set ( [ ] ) , "<STR_LIT>" <EOL> assert set ( self . edges . incoming ( <NUM_LIT:2> ) ) == set ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , "<STR_LIT>" <EOL> def test_outgoing ( self ) : <EOL> assert set ( self . edges . outgoing ( <NUM_LIT:2> ) ) == set ( [ ] ) , "<STR_LIT>" <EOL> assert set ( self . edges . outgoing ( <NUM_LIT:0> ) ) == set ( [ <NUM_LIT:2> , <NUM_LIT:5> ] ) , "<STR_LIT>" <EOL> def test_parents ( self ) : <EOL> assert set ( self . edges . parents ( <NUM_LIT:0> ) ) == set ( [ ] ) , "<STR_LIT>" <EOL> assert set ( self . edges . parents ( <NUM_LIT:2> ) ) == set ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , "<STR_LIT>" <EOL> def test_children ( self ) : <EOL> assert set ( self . edges . children ( <NUM_LIT:2> ) ) == set ( [ ] ) , "<STR_LIT>" <EOL> assert set ( self . edges . children ( <NUM_LIT:0> ) ) == set ( [ <NUM_LIT:2> , <NUM_LIT:5> ] ) , "<STR_LIT>" <EOL> def test_contains1 ( self ) : <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:2> ) in self . edges , "<STR_LIT>" <EOL> def test_contains2 ( self ) : <EOL> assert ( <NUM_LIT> , <NUM_LIT> ) not in self . edges , "<STR_LIT>" <EOL> def test_remove ( self ) : <EOL> self . edges . remove ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> assert set ( self . edges ) == set ( [ ( <NUM_LIT:0> , <NUM_LIT:5> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) , "<STR_LIT>" <EOL> def test_clear ( self ) : <EOL> self . edges . clear ( ) <EOL> assert list ( self . edges ) == [ ] , "<STR_LIT>" <EOL> def test_adjacency_matrix ( self ) : <EOL> expected = [ <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> ] <EOL> assert ( self . edges . adjacency_matrix == N . array ( expected , dtype = bool ) ) . all ( ) , "<STR_LIT>" <EOL> class TestIsAcyclic : <EOL> def setUp ( self ) : <EOL> self . net = network . Network ( [ data . DiscreteVariable ( i , <NUM_LIT:3> ) for i in xrange ( <NUM_LIT:6> ) ] ) <EOL> for edge in [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] : <EOL> self . net . edges . add ( edge ) <EOL> def test_loopchecking ( self ) : <EOL> assert self . net . is_acyclic ( ) , "<STR_LIT>" <EOL> def test_loopchecking2 ( self ) : <EOL> self . net . edges . add ( ( <NUM_LIT:2> , <NUM_LIT:0> ) ) <EOL> assert not self . net . is_acyclic ( ) , "<STR_LIT>" <EOL> class TestNetwork : <EOL> expected_dotstring = """<STR_LIT>""" <EOL> expected_string = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . net = network . Network ( [ data . DiscreteVariable ( i , <NUM_LIT:3> ) for i in xrange ( <NUM_LIT:6> ) ] ) <EOL> for edge in [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] : <EOL> self . net . edges . add ( edge ) <EOL> def test_as_pydot ( self ) : <EOL> assert len ( self . net . as_pydot ( ) . get_edges ( ) ) == <NUM_LIT:3> , "<STR_LIT>" <EOL> def test_as_image ( self ) : <EOL> filename = "<STR_LIT>" <EOL> self . net . as_image ( filename = filename ) <EOL> file_exists = filename in os . listdir ( "<STR_LIT:.>" ) <EOL> if file_exists : <EOL> os . remove ( "<STR_LIT>" + filename ) <EOL> assert file_exists , "<STR_LIT>" <EOL> def test_as_dotstring ( self ) : <EOL> assert self . net . as_dotstring ( ) == self . expected_dotstring , "<STR_LIT>" <EOL> def test_as_dotfile ( self ) : <EOL> self . net . as_dotfile ( '<STR_LIT>' ) <EOL> assert open ( '<STR_LIT>' ) . read ( ) == self . expected_dotstring , "<STR_LIT>" <EOL> def test_as_string ( self ) : <EOL> assert self . net . as_string ( ) == self . expected_string , "<STR_LIT>" <EOL> def test_layout ( self ) : <EOL> self . net . layout ( ) <EOL> assert hasattr ( self . net , '<STR_LIT>' ) , "<STR_LIT>" <EOL> assert len ( self . net . node_positions [ <NUM_LIT:0> ] ) == <NUM_LIT:2> , "<STR_LIT>" <EOL> assert isinstance ( self . net . node_positions [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , ( int , float ) ) , "<STR_LIT>" <EOL> class TestNetworkFromListOfEdges : <EOL> def setUp ( self ) : <EOL> self . net = network . Network ( <EOL> [ data . DiscreteVariable ( str ( i ) , <NUM_LIT:3> ) for i in xrange ( <NUM_LIT:6> ) ] , <EOL> [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:4> , <NUM_LIT:5> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) ] <EOL> ) <EOL> def test_number_of_edges ( self ) : <EOL> assert len ( list ( self . net . edges ) ) == <NUM_LIT:3> <EOL> def test_edges_exist ( self ) : <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:1> ) in self . net . edges and ( <NUM_LIT:4> , <NUM_LIT:5> ) in self . net . edges and ( <NUM_LIT:2> , <NUM_LIT:3> ) in self . net . edges <EOL> class TestNetworkFromString ( TestNetworkFromListOfEdges ) : <EOL> def setUp ( self ) : <EOL> self . net = network . Network ( <EOL> [ data . DiscreteVariable ( str ( i ) , <NUM_LIT:3> ) for i in xrange ( <NUM_LIT:6> ) ] , <EOL> "<STR_LIT>" <EOL> ) <EOL> class TestRandomNetwork : <EOL> def setUp ( self ) : <EOL> self . nodes = [ data . DiscreteVariable ( str ( i ) , <NUM_LIT:3> ) for i in xrange ( <NUM_LIT:6> ) ] <EOL> def test_acyclic ( self ) : <EOL> net = network . random_network ( self . nodes ) <EOL> assert net . is_acyclic ( ) == True , "<STR_LIT>" <EOL> def test_required_edges ( self ) : <EOL> net = network . random_network ( self . nodes , required_edges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:0> ) ] ) <EOL> assert net . is_acyclic ( ) == True and ( <NUM_LIT:0> , <NUM_LIT:1> ) in net . edges and ( <NUM_LIT:3> , <NUM_LIT:0> ) in net . edges <EOL> def test_prohibited_edges ( self ) : <EOL> net = network . random_network ( self . nodes , prohibited_edges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:0> ) ] ) <EOL> assert net . is_acyclic ( ) == True and ( <NUM_LIT:0> , <NUM_LIT:1> ) not in net . edges and ( <NUM_LIT:3> , <NUM_LIT:0> ) not in net . edges <EOL> def test_required_and_prohibited_edges ( self ) : <EOL> net = network . random_network ( self . nodes , required_edges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:3> , <NUM_LIT:0> ) ] , <EOL> prohibited_edges = [ ( <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:4> ) ] ) <EOL> assert net . is_acyclic ( ) == True and ( <NUM_LIT:0> , <NUM_LIT:1> ) in net . edges and ( <NUM_LIT:3> , <NUM_LIT:0> ) in net . edges and ( <NUM_LIT:2> , <NUM_LIT:3> ) not in net . edges and ( <NUM_LIT:1> , <NUM_LIT:4> ) not in net . edges </s>
