How to use the dallinger.models.Network function in dallinger

To help you get started, we’ve selected a few dallinger examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Dallinger / Dallinger / tests / test_sources.py View on Github external
def test_broadcast_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        agent1 = nodes.ReplicatorAgent(network=net)
        agent2 = nodes.ReplicatorAgent(network=net)
        db_session.add(agent1)
        db_session.add(agent2)
        db_session.commit()
        source.connect(whom=agent1)
        source.connect(whom=agent2)
        self.add(db_session, source, agent1, agent2)

        source.transmit(what=source.create_information())
        db_session.commit()

        agent1.receive()
        agent2.receive()
github Dallinger / Dallinger / tests / test_information.py View on Github external
def test_create_genome(self, db_session):
        net = models.Network()
        db_session.add(net)
        node = models.Node(network=net)
        info = information.Gene(origin=node)
        db_session.commit()

        assert info.type == "gene"
        assert info.contents is None
github Dallinger / Dallinger / tests / test_models.py View on Github external
def test_node_repr(self, db_session):
        """Test the repr of a node"""
        net = models.Network()
        db_session.add(net)
        node = models.Node(network=net)
        self.add(db_session, node)

        assert repr(node).split("-") == ["Node", str(node.id), "node"]
github Dallinger / Dallinger / tests / test_sources.py View on Github external
def test_transmit_random_binary_string_source(self, db_session):
        net = models.Network()
        self.add(db_session, net)
        source = nodes.RandomBinaryStringSource(network=net)
        agent = nodes.ReplicatorAgent(network=net)
        db_session.add(source)
        db_session.add(agent)
        db_session.commit()

        source.connect(whom=agent)
        self.add(db_session, source, agent)

        source.transmit(to_whom=agent)
        db_session.commit()

        agent.receive()
        db_session.commit()
github Dallinger / Dallinger / dallinger / networks.py View on Github external
__mapper_args__ = {"polymorphic_identity": "chain"}

    def add_node(self, node):
        """Add an agent, connecting it to the previous node."""
        other_nodes = [n for n in self.nodes() if n.id != node.id]

        if isinstance(node, Source) and other_nodes:
            raise Exception("Chain network already has a nodes, " "can't add a source.")

        if other_nodes:
            parent = max(other_nodes, key=attrgetter("creation_time"))
            parent.connect(whom=node)


class FullyConnected(Network):
    """A fully-connected network (complete graph) with all possible vectors."""

    __mapper_args__ = {"polymorphic_identity": "fully-connected"}

    def add_node(self, node):
        """Add a node, connecting it to everyone and back."""
        other_nodes = [n for n in self.nodes() if n.id != node.id]

        for n in other_nodes:
            if isinstance(n, Source):
                node.connect(direction="from", whom=n)
            else:
                node.connect(direction="both", whom=n)


class Empty(Network):
github Dallinger / Dallinger / dallinger / experiment.py View on Github external
"""All the networks in the experiment."""
        if full not in ["all", True, False]:
            raise ValueError("full must be boolean or all, it cannot be {}"
                             .format(full))

        if full == "all":
            if role == "all":
                return Network.query.all()
            else:
                return Network\
                    .query\
                    .filter_by(role=role)\
                    .all()
        else:
            if role == "all":
                return Network.query.filter_by(full=full)\
                    .all()
            else:
                return Network\
                    .query\
                    .filter(and_(Network.role == role, Network.full == full))\
                    .all()
github Dallinger / Dallinger / demos / mafia / models.py View on Github external
"""Make death time settable."""
        self.property3 = death_time

    @deathtime.expression
    def deathtime(self):
        """Make death time queryable."""
        return self.property3


class Mafioso(Bystander):
    """Member of the mafia."""

    __mapper_args__ = {"polymorphic_identity": "mafioso"}


class MafiaNetwork(Network):
    """A mafia network that switches between FullyConnected
    for all and only mafia nodes."""

    __mapper_args__ = {"polymorphic_identity": "mafia-network"}

    def add_node(self, node):
        """Add a node, connecting it to other mafia if mafioso."""
        if node.type == "mafioso":
            other_mafiosi = [n for n in Node.query.filter_by(type="mafioso")
                             if n.id != node.id]
            for n in other_mafiosi:
                node.connect(direction="both", whom=n)

    def add_source(self, source):
        """Connect the source to all existing other nodes."""
        nodes = [n for n in self.nodes() if not isinstance(n, Source)]
github Dallinger / Dallinger / dallinger / experiment.py View on Github external
.format(full))

        if full == "all":
            if role == "all":
                return Network.query.all()
            else:
                return Network\
                    .query\
                    .filter_by(role=role)\
                    .all()
        else:
            if role == "all":
                return Network.query.filter_by(full=full)\
                    .all()
            else:
                return Network\
                    .query\
                    .filter(and_(Network.role == role, Network.full == full))\
                    .all()
github Dallinger / Dallinger / dallinger / models.py View on Github external
Node, foreign_keys=[origin_id], backref="all_outgoing_vectors"
    )

    #: the id of the Node at which the vector terminates.
    destination_id = Column(Integer, ForeignKey("node.id"), index=True)

    #: the Node at which the vector terminates.
    destination = relationship(
        Node, foreign_keys=[destination_id], backref="all_incoming_vectors"
    )

    #: the id of the network the vector is in.
    network_id = Column(Integer, ForeignKey("network.id"), index=True)

    #: the network the vector is in.
    network = relationship(Network, backref="all_vectors")

    def __init__(self, origin, destination):
        """Create a vector."""
        # check origin and destination are in the same network
        if origin.network_id != destination.network_id:
            raise ValueError(
                "{}, in network {}, cannot connect with {} "
                "as it is in network {}".format(
                    origin, origin.network_id, destination, destination.network_id
                )
            )

        # check neither the origin or destination have failed
        if origin.failed:
            raise ValueError(
                "{} cannot connect to {} as {} has failed".format(