def test_node_connect(self): """Test connecting one node to another""" net = models.Network() self.db.add(net) self.db.commit() node1 = models.Node(network=net) node2 = models.Node(network=net) node3 = models.Node(network=net) node4 = models.Node(network=net) # self.add(node1, node2, node3, node4) # self.db.commit() node1.connect(whom=node2) assert node1.neighbors(direction="to") == [node2] assert node2.neighbors(direction="from") == [node1] node2.connect(whom=[node3, node4]) for n in node2.neighbors(direction="to"): assert n in [node3, node4] assert node3.neighbors(direction="from") == [node2] assert_raises(ValueError, node1.connect, whom=node1) net = models.Network() self.add(net) assert_raises(TypeError, node1.connect, whom=net)
def test_agent_transmit_everything_to_everyone(self): net = models.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="to", whom=agent2) agent1.connect(direction="to", whom=agent3) info = models.Info(origin=agent1, contents="foo") agent1.transmit(what=models.Info, to_whom=nodes.Agent) agent2.receive() agent3.receive() assert agent1.infos()[0].contents == agent2.infos()[0].contents assert agent1.infos()[0].contents == agent3.infos()[0].contents assert agent1.infos()[0].id != agent2.infos()[0].id != agent3.infos( )[0].id transmissions = info.transmissions() assert len(transmissions) == 2
def test_transmit_selector_all_of_type(self): net = models.Network() self.db.add(net) # Create a network of two biological nodes. agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="to", whom=agent2) information.Meme(origin=agent1, contents="foo1") information.Meme(origin=agent1, contents="foo2") information.Meme(origin=agent1, contents="foo3") information.Gene(origin=agent1, contents="bar") assert len(agent1.infos(type=Meme)) == 3 assert len(agent2.infos(type=Meme)) == 0 assert len(agent1.infos(type=Gene)) == 1 assert len(agent2.infos(type=Gene)) == 0 # Transmit memes from agent 1 to 2. agent1.transmit(what=information.Meme, to_whom=agent2) # Receive the transmission. agent2.receive() # Make sure that Agent 2 has a blank memome and the right gene. assert not agent2.infos(type=Gene) assert len(agent2.infos(type=Meme)) == 3
def test_create_bidirectional_vectors(self): """Test creating a bidirectional connection between nodes""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector1 = models.Vector(origin=node1, destination=node2) vector2 = models.Vector(origin=node2, destination=node1) self.add(node1, node2, vector1, vector2) assert vector1.origin_id == node1.id assert vector1.destination_id == node2.id assert vector2.origin_id == node2.id assert vector2.destination_id == node1.id assert node1.vectors(direction="incoming") == [vector2] assert node1.vectors(direction="outgoing") == [vector1] assert node2.vectors(direction="incoming") == [vector1] assert node2.vectors(direction="outgoing") == [vector2] assert node1.is_connected(direction="to", whom=node2) assert node1.is_connected(direction="from", whom=node2) assert node2.is_connected(direction="to", whom=node1) assert node2.is_connected(direction="from", whom=node1) assert len(node1.vectors(direction="incoming")) == 1 assert len(node2.vectors(direction="incoming")) == 1 assert len(node1.vectors(direction="outgoing")) == 1 assert len(node2.vectors(direction="outgoing")) == 1 assert len(vector1.transmissions()) == 0 assert len(vector2.transmissions()) == 0
def test_random_walk_from_source(self): net = models.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(whom=agent2) agent2.connect(whom=agent3) source = nodes.RandomBinaryStringSource(network=net) from operator import attrgetter source.connect( whom=min(net.nodes(type=Agent), key=attrgetter('creation_time'))) source.create_information() processes.random_walk(net) agent1.receive() msg = agent1.infos()[0].contents processes.random_walk(net) agent2.receive() agent2.infos()[0].contents processes.random_walk(net) agent3.receive() agent3.infos()[0].contents assert msg == agent3.infos()[0].contents
def test_create_transmission(self): """Try creating a transmission""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) self.db.commit() node1.connect(whom=node2) info = models.Info(origin=node1) node1.transmit(what=node1.infos()[0], to_whom=node2) #transmission = models.Transmission(info=info, destination=node2) #self.add(node1, node2, vector, info, transmission) transmission = node1.transmissions()[0] vector = node1.vectors()[0] assert isinstance(transmission.id, int) assert transmission.info_id == info.id assert transmission.origin_id == vector.origin_id assert transmission.destination_id == vector.destination_id assert transmission.creation_time assert transmission.vector == vector assert vector.transmissions() == [transmission]
def test_identity_transformation(self): net = models.Network() self.add(net) node = models.Node(network=net) self.db.add(node) self.db.commit() info_in = models.Info(origin=node, contents="foo") self.db.add(info_in) self.db.commit() node.replicate(info_in) # # Create a new info based on the old one. # info_out = models.Info(origin=node, contents=info_in.contents) # self.db.add(info_in) # self.db.commit() # # Register the transformation. # transformation = transformations.Replication( # info_out=info_out, # info_in=info_in) # self.db.add(transformation) # self.db.commit() assert node.infos()[-1].contents == "foo" assert len(node.infos()) == 2
def test_agent_transmit_no_connection(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) info = models.Info(origin=agent1, contents="foo") agent1.transmit(what=info, to_whom=agent2)
def test_create_random_binary_string_source(self): net = models.Network() self.add(net) source = nodes.RandomBinaryStringSource(network=net) self.add(source) assert source
def test_node_repr(self): """Test the repr of a node""" net = models.Network() self.db.add(net) node = models.Node(network=net) self.add(node) assert repr(node).split("-") == ["Node", str(node.id), "node"]
def test_network_base_add_node_not_implemented(self): net = models.Network() self.db.add(net) self.db.commit() node = models.Node(network=net) self.db.add(net) self.db.commit() net.add_node(node)
def test_property_node(self): net = models.Network() self.db.add(net) node = models.Node(network=net) node.property1 = "foo" self.add(node) assert node.property1 == "foo"
def test_different_node_ids(self): """Test that two nodes have different ids""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) assert node1.id != node2.id
def test_info_repr(self): """Check the info repr""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node) self.add(info) assert repr(info).split("-") == ["Info", str(info.id), "info"]
def test_create_memome(self): net = models.Network() self.db.add(net) node = models.Node(network=net) info = information.Meme(origin=node) self.db.commit() assert info.type == "meme" assert info.contents is None
def test_agent_transmit_invalid_info(self): net = models.Network() self.db.add(net) agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent1.connect(direction="to", whom=agent2) info = models.Info(origin=agent2, contents="foo") agent1.transmit(what=info, to_whom=agent2)
def test_create_replicator_agent(self): net = models.Network() self.db.add(net) agent = nodes.ReplicatorAgent(network=net) assert len(agent.infos()) is 0 info = information.Info(origin=agent, contents="foo") assert agent.infos()[0] == info
def test_create_agent_generic_transmit_to_all(self): net = models.Network() self.db.add(net) agent1 = nodes.Agent(network=net) agent2 = nodes.Agent(network=net) agent3 = nodes.Agent(network=net) agent1.connect(direction="to", whom=agent2) agent1.connect(direction="to", whom=agent3) agent1.transmit(to_whom=models.Node)
def test_info_write_twice(self): """Overwrite an info's contents.""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node, contents="foo") self.add(node, info) assert info.contents == "foo" info.contents = "ofo"
def test_fail_agent(self): net = models.Network() self.db.add(net) agent = nodes.Agent(network=net) self.db.commit() assert agent.failed is False assert agent.time_of_death is None agent.fail() assert agent.failed is True assert agent.time_of_death is not None
def test_create_environment(self): """Create an environment""" net = models.Network() self.db.add(net) environment = nodes.Environment(network=net) information.State(origin=environment, contents="foo") self.db.commit() assert isinstance(environment.id, int) assert environment.type == "environment" assert environment.creation_time assert environment.state().contents == "foo"
def test_kill_vector(self): net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector = models.Vector(origin=node1, destination=node2) self.add(node1, node2, vector) assert vector.failed is False vector.fail() assert vector.failed is True
def test_create_environment_get_observed(self): net = models.Network() self.db.add(net) environment = nodes.Environment(network=net) information.State(origin=environment, contents="foo") agent = nodes.ReplicatorAgent(network=net) environment.connect(direction="to", whom=agent) environment.transmit(to_whom=agent) agent.receive() assert agent.infos()[0].contents == "foo"
def test_node_has_connection_from(self): net = models.Network() self.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) self.db.commit() node1.connect(whom=node2) self.add(node1, node2) assert not node1.is_connected(direction="from", whom=node2) assert node2.is_connected(direction="from", whom=node1)
def test_create_vector(self): """Test creating a vector between two nodes""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) #vector = models.Vector(origin=node1, destination=node2) #self.add(node1, node2, vector) self.add(node1, node2) self.db.commit() node1.connect(whom=node2) self._check_single_connection(node1, node2)
def test_create_node(self): """Create a basic node""" net = models.Network() self.db.add(net) node = models.Node(network=net) self.add(node) assert isinstance(node.id, int) assert node.type == "node" assert node.creation_time assert len(node.infos()) == 0 assert len(node.vectors(direction="outgoing")) == 0 assert len(node.vectors(direction="incoming")) == 0 assert len(node.vectors(direction="outgoing")) == 0 assert len(node.vectors(direction="incoming")) == 0
def test_vector_repr(self): """Test the repr of a vector""" net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) vector1 = models.Vector(origin=node1, destination=node2) vector2 = models.Vector(origin=node2, destination=node1) self.add(node1, node2, vector1, vector2) assert (repr(vector1).split("-") == [ "Vector", str(node1.id), str(node2.id) ]) assert (repr(vector2).split("-") == [ "Vector", str(node2.id), str(node1.id) ])
def test_create_info(self): """Try creating an info""" net = models.Network() self.db.add(net) node = models.Node(network=net) info = models.Info(origin=node, contents="foo") self.add(node, info) assert isinstance(info.id, int) assert info.type == "info" assert info.origin_id == node.id assert info.creation_time assert info.contents == "foo" assert len(info.transmissions()) == 0 assert node.infos() == [info]
def test_moran_process_cultural(self): # Create a fully-connected network. net = models.Network() self.db.add(net) self.db.commit() agent1 = nodes.ReplicatorAgent(network=net) agent2 = nodes.ReplicatorAgent(network=net) agent3 = nodes.ReplicatorAgent(network=net) self.db.commit() agent1.connect(whom=agent2) agent1.connect(whom=agent3) agent2.connect(whom=agent1) agent2.connect(whom=agent3) agent3.connect(whom=agent1) agent3.connect(whom=agent2) # Add a global source and broadcast to all the nodes. source = nodes.RandomBinaryStringSource(network=net) for agent in net.nodes(type=Agent): source.connect(whom=agent) source.transmit(to_whom=agent) agent.receive() # Run a Moran process for 100 steps. for _ in xrange(100): processes.moran_cultural(net) for agent in net.nodes(type=Agent): agent.receive() # Ensure that the process had reached fixation. from operator import attrgetter assert max(agent1.infos(), key=attrgetter('creation_time')).contents == max( agent2.infos(), key=attrgetter('creation_time')).contents assert max(agent2.infos(), key=attrgetter('creation_time')).contents == max( agent3.infos(), key=attrgetter('creation_time')).contents assert max(agent3.infos(), key=attrgetter('creation_time')).contents == max( agent1.infos(), key=attrgetter('creation_time')).contents
def test_transmission_repr(self): net = models.Network() self.db.add(net) node1 = models.Node(network=net) node2 = models.Node(network=net) self.add(node1, node2) node1.connect(whom=node2) models.Info(origin=node1) node1.transmit(what=node1.infos()[0], to_whom=node2) transmission = node1.transmissions()[0] node1.vectors()[0] assert (repr(transmission).split("-") == [ "Transmission", str(transmission.id) ])