def test_fixed_topology_constraints(self): """ Ensure that the randomizer correctly instantiates the constraints """ c1 = IsConnected() rand = FixedTopology(s_pombe, constraints=[c1]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, [c1]) c2 = lambda _: True # noqa rand = FixedTopology(s_pombe, constraints=[c2]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(len(rand.constraints), 1) self.assertIsInstance(rand.constraints[0], GenericTopological) self.assertEqual(rand.constraints[0].test, c2) with self.assertRaises(ConstraintError): FixedTopology(s_pombe, constraints=[lambda _: False]) with self.assertRaises(TypeError): FixedTopology(s_pombe, constraints=[IsIrreducible()])
def test_network_graph_metadata(self): from neet.boolean.examples import s_pombe nx_net = s_pombe.network_graph(labels='indices') self.assertEqual(nx_net.graph['name'], 'Fission Yeast Cell Cycle') self.assertEqual(nx_net.graph['name'], s_pombe.metadata['name'])
def test_fixed_out_degree(self): """ Ensure that the topologies generated by OutDegree have the same number of nodes and edges, and the same out-degree distribution as the original topology """ def out_degree(g): return Counter(dict(g.out_degree).values()) g = nx.DiGraph([(0, 1), (1, 1), (2, 1)]) rand = OutDegree(g) for h in islice(rand, 100): self.assertTrue(len(h), len(g)) self.assertTrue(h.size, g.size) try: self.assertEqual(out_degree(h), out_degree(g)) except Exception as err: raise Exception(h.in_degree, g.in_degree) from err g = s_pombe.network_graph() rand = OutDegree(g) for h in islice(rand, 100): self.assertTrue(len(h), len(g)) self.assertTrue(h.size, g.size) try: self.assertEqual(out_degree(h), out_degree(g)) except Exception as err: raise Exception(h.in_degree, g.in_degree) from err
def test_randomizer_init_timeout(self): """ Ensure that the randomizer correctly sets the timeout """ rand = MockRandomizer(s_pombe, timeout=0) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 0) self.assertEqual(rand.constraints, [])
def test_network_randomizer_constraints(self): """ Ensure that the randomizer correctly instantiates the constraints """ c1, c2, c3 = IsIrreducible(), (lambda _: False), IsConnected() rand = MockNetworkRandomizer(s_pombe, constraints=[c1]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, [c1]) self.assertEqual(rand.trand.constraints, []) rand = MockNetworkRandomizer(s_pombe, constraints=[c2]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(len(rand.constraints), 1) self.assertIsInstance(rand.constraints[0], GenericDynamical) self.assertEqual(rand.constraints[0].test, c2) self.assertEqual(rand.trand.constraints, []) rand = MockNetworkRandomizer(s_pombe, constraints=[c3]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, []) self.assertEqual(rand.trand.constraints, [c3]) rand = MockNetworkRandomizer(s_pombe, constraints=[c1, c3, c2]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(len(rand.constraints), 2) self.assertEqual(rand.constraints[0], c1) self.assertIsInstance(rand.constraints[1], GenericDynamical) self.assertEqual(rand.constraints[1].test, c2) self.assertEqual(rand.trand.constraints, [c3]) with self.assertRaises(TypeError): MockNetworkRandomizer(s_pombe, constraints=[5])
def test_randomizer_init_constraints(self): """ Ensure that the randomizer correctly instantiates the constraints """ constraint = IsIrreducible() rand = MockRandomizer(s_pombe, constraints=[constraint]) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, [constraint]) with self.assertRaises(TypeError): MockRandomizer(s_pombe, constraints=[lambda n: False])
def test_fixed_topology(self): """ Ensure that the topologies generated are isomorphic to the original topology """ g = nx.DiGraph([(0, 1), (1, 1), (2, 1)]) rand = FixedTopology(g) self.assertTrue( all(map(lambda h: nx.is_isomorphic(g, h), islice(rand, 10)))) g = s_pombe.network_graph() rand = FixedTopology(g) self.assertTrue( all(map(lambda h: nx.is_isomorphic(g, h), islice(rand, 10))))
def test_default_bias(self): """ Ensure the generated networks have a bias of 0.5 by default """ rand = UniformBias(s_pombe) s_pombe_graph = s_pombe.network_graph() indegree = [ s_pombe_graph.in_degree(n) for n in sorted(s_pombe_graph.nodes) ] expect = [ 0.0 if s_pombe_graph.in_degree(n) == 0 else 0.5 for n in indegree ] got = list(map(self.bias, islice(rand, 10))) self.assertEqual(got, [expect] * len(got))
def test_other_bias(self): """ Ensure the generated networks have the specified """ p = 0.3 rand = UniformBias(s_pombe, p) s_pombe_graph = s_pombe.network_graph() indegree = [ s_pombe_graph.in_degree(n) for n in sorted(s_pombe_graph.nodes) ] high = [ 0.0 if k == 0 else math.ceil(p * 2**k) / 2**k for k in indegree ] low = [ 0.0 if k == 0 else math.floor(p * 2**k) / 2**k for k in indegree ] for net in islice(rand, 10): got = self.bias(net) for g, l, h in zip(got, low, high): self.assertTrue(g == l or g == h)
def test_randomizer_default_init(self): """ Ensure that randomizer correctly initializes with default arguments """ g = nx.DiGraph([(0, 1), (1, 1)]) rand = MockRandomizer(g) self.assertIsNone(rand.network) self.assertEqual(rand.graph, g) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, []) rand = MockRandomizer(s_pombe) self.assertEqual(rand.network, s_pombe) self.assertTrue(nx.is_isomorphic(rand.graph, s_pombe.network_graph())) self.assertEqual(rand.timeout, 1000) self.assertEqual(rand.constraints, []) with self.assertRaises(TypeError): MockRandomizer(None) with self.assertRaises(TypeError): MockRandomizer(nx.Graph())
def test_network_graph_names(self): from neet.boolean.examples import s_pombe nx_net = s_pombe.network_graph(labels='names') self.assertEqual(set(nx_net), set(s_pombe.names))