def test_network_downstream_nodes(self): net = networks.Network() self.db.add(net) self.db.commit() node1 = models.Node(network=net) node2 = models.Node(network=net) agent1 = nodes.Agent(network=net) agent2 = nodes.ReplicatorAgent(network=net) source1 = nodes.Source(network=net) nodes.Source(network=net) node1.connect(whom=[node2, agent1, agent2]) raises(TypeError, node1.connect, whom=source1) assert set(node1.neighbors(direction="to")) == set( [node2, agent1, agent2]) assert len(node1.vectors(direction="outgoing")) == 3 assert set(node1.neighbors(direction="to", type=nodes.Agent)) == set([agent1, agent2]) agent1.fail() agent2.fail() raises(ValueError, node1.neighbors, direction="ghbhfgjd")
def test_network_sources(self, db_session): net = networks.Network() db_session.add(net) assert len(net.nodes(type=nodes.Source)) == 0 source = nodes.Source(network=net) assert net.nodes(type=nodes.Source) == [source]
def test_network_agents(self, db_session): net = networks.Network() db_session.add(net) assert len(net.nodes(type=nodes.Agent)) == 0 agent = nodes.Agent(network=net) assert net.nodes(type=nodes.Agent) == [agent] assert isinstance(net, models.Network)
def test_network_sources(self): net = networks.Network() self.db.add(net) self.db.commit() assert len(net.nodes(type=nodes.Source)) == 0 source = nodes.Source(network=net) assert net.nodes(type=nodes.Source) == [source]
def test_network_add_node(self, db_session): net = networks.Network() db_session.add(net) nodes.Agent(network=net) nodes.Agent(network=net) nodes.Agent(network=net) assert len(net.nodes(type=nodes.Agent)) == 3 assert len(net.vectors()) == 0 assert len(net.nodes(type=nodes.Source)) == 0
def test_network_agents(self): net = networks.Network() self.db.add(net) self.db.commit() assert len(net.nodes(type=nodes.Agent)) == 0 agent = nodes.Agent(network=net) assert net.nodes(type=nodes.Agent) == [agent] assert isinstance(net, models.Network)
def test_network_add_node(self): net = networks.Network() self.db.add(net) self.db.commit() nodes.Agent(network=net) nodes.Agent(network=net) nodes.Agent(network=net) assert len(net.nodes(type=nodes.Agent)) == 3 assert len(net.vectors()) == 0 assert len(net.nodes(type=nodes.Source)) == 0
def test_network_degrees(self, db_session): net = networks.Network() db_session.add(net) agent1 = nodes.Agent(network=net) agent2 = nodes.Agent(network=net) assert [len(n.vectors(direction="outgoing")) for n in net.nodes()] == [0, 0] agent1.connect(whom=agent2) assert 1 in [len(n.vectors(direction="outgoing")) for n in net.nodes()] assert 0 in [len(n.vectors(direction="outgoing")) for n in net.nodes()]
def test_network_add_source_local(self, db_session): net = networks.Network() db_session.add(net) nodes.Agent(network=net) nodes.Agent(network=net) source = nodes.RandomBinaryStringSource(network=net) source.connect(whom=net.nodes(type=nodes.Agent)[0]) assert len(net.vectors()) == 1 assert [len(n.vectors(direction="outgoing")) for n in net.nodes(type=nodes.Agent)] == [0, 0] assert len(net.nodes(type=nodes.Source)[0].vectors(direction="outgoing")) == 1
def test_network_repr(self, db_session): net = networks.Network() db_session.add(net) nodes.Agent(network=net) nodes.Agent(network=net) source = nodes.RandomBinaryStringSource(network=net) source.connect(whom=net.nodes(type=nodes.Agent)) assert repr(net) == ("<Network-" + str(net.id) + "-network with 3 nodes, 2 vectors, " "0 infos, 0 transmissions and 0 transformations>")
def test_network_vectors(self, db_session): net = networks.Network() db_session.add(net) assert len(net.vectors()) == 0 agent1 = nodes.Agent(network=net) agent2 = nodes.Agent(network=net) agent1.connect(whom=agent2) assert len(net.vectors()) == 1 assert net.vectors()[0].origin == agent1 assert net.vectors()[0].destination == agent2
def test_network_vectors(self): net = networks.Network() self.db.add(net) self.db.commit() assert len(net.vectors()) == 0 agent1 = nodes.Agent(network=net) agent2 = nodes.Agent(network=net) agent1.connect(whom=agent2) assert len(net.vectors()) == 1 assert net.vectors()[0].origin == agent1 assert net.vectors()[0].destination == agent2
def test_node_failure(self, db_session): net = networks.Network() db_session.add(net) for _ in range(5): nodes.Agent(network=net) nodes.Source(network=net) assert len(net.nodes(type=nodes.Agent)) == 5 random.choice(net.nodes(type=nodes.Agent)).fail() assert len(net.nodes(type=nodes.Agent)) == 4 assert len(net.nodes(type=nodes.Agent, failed="all")) == 5 assert len(net.nodes()) == 5 assert len(net.nodes(failed="all")) == 6 assert len(net.nodes(failed=True)) == 1
def test_network_add_source_global(self): net = networks.Network() self.db.add(net) self.db.commit() agent1 = nodes.Agent(network=net) nodes.Agent(network=net) # self.db.commit() source = nodes.RandomBinaryStringSource(network=net) source.connect(whom=net.nodes(type=nodes.Agent)) assert len(net.vectors()) == 2 assert source.network == net assert agent1.network == net assert [ len(n.vectors(direction="outgoing")) for n in net.nodes(type=nodes.Agent) ] == [0, 0] assert len( net.nodes(type=nodes.Source)[0].vectors(direction="outgoing")) == 2
def test_moran_process_sexual(self): # Create a fully-connected network. net = networks.Network() self.db.add(net) self.db.commit() agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="both", whom=[agent2, agent3]) agent2.connect(direction="both", whom=agent3) # Add a global source and broadcast to all the nodes. source = nodes.RandomBinaryStringSource(network=net) source.connect(direction="to", whom=net.nodes(type=Agent)) source.create_information() for agent in net.nodes(type=Agent): source.transmit(to_whom=agent) agent.receive() # Run a Moran process for 100 steps. for i in range(100): nodes.ReplicatorAgent(network=net) processes.moran_sexual(net) for agent in net.nodes(type=Agent): agent.receive() # Ensure that the process had reached fixation. assert agent1.failed is True assert agent2.failed is True assert agent3.failed is True for a in net.nodes(type=Agent): for a2 in net.nodes(type=Agent): assert a.infos()[0].contents == a2.infos()[0].contents