Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
net = models.Network.query.one()
# create a participant
participant = models.Participant(
recruiter_id="hotair",
worker_id=str(1),
hit_id=str(1),
assignment_id=str(1),
mode="test",
)
db_session.add(participant)
db_session.commit()
# create some nodes
node = models.Node(network=net)
agent = Agent(network=net, participant=participant)
source = Source(network=net)
# create vectors
source.connect(direction="to", whom=agent)
agent.connect(direction="both", whom=node)
# create some infos
info = models.Info(origin=agent, contents="ethwth")
gene = Gene(origin=source, contents="hkhkhkh")
# conditionally transmit and transform
source.transmit(what=models.Info)
agent.receive()
agent.transmit(what=Gene)
models.Transformation(info_in=gene, info_out=info)
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) == (
""
def test_network_downstream_nodes(self, db_session):
net = networks.Network()
db_session.add(net)
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])
assert pytest.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()
def test_network_add_source_global(self, db_session):
net = networks.Network()
db_session.add(net)
agent1 = nodes.Agent(network=net)
nodes.Agent(network=net)
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 moran_sexual(network):
"""The generalized sexual Moran process.
Ateach time step, and individual is chosen for replication and another
individual is chosen to die. The replication replaces the one who dies.
For this process to work you need to add a new agent before calling step.
"""
if not network.transmissions():
replacer = random.choice(network.nodes(type=Source))
replacer.transmit()
else:
from operator import attrgetter
agents = network.nodes(type=Agent)
baby = max(agents, key=attrgetter("creation_time"))
agents = [a for a in agents if a.id != baby.id]
replacer = random.choice(agents)
replaced = random.choice(replacer.neighbors(direction="to", type=Agent))
# Give the baby the same outgoing connections as the replaced.
for node in replaced.neighbors(direction="to"):
baby.connect(direction="to", whom=node)
# Give the baby the same incoming connections as the replaced.
for node in replaced.neighbors(direction="from"):
node.connect(direction="to", whom=baby)
# Kill the replaced agent.
replaced.fail()
def create_node(self, participant, network):
"""Create a node for a participant."""
return dlgr.nodes.Agent(network=network, participant=participant)
import random
import json
from sqlalchemy import Boolean
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql.expression import cast
from dallinger.models import Info
from dallinger.models import Transformation
from dallinger.nodes import Agent
from dallinger.nodes import Source
class MCMCPAgent(Agent):
__mapper_args__ = {"polymorphic_identity": "MCMCP_agent"}
def update(self, infos):
info = infos[0]
self.replicate(info)
new_info = AnimalInfo(origin=self, contents=info.perturbed_contents())
Perturbation(info_in=info, info_out=new_info)
def _what(self):
infos = self.infos()
return [i for i in infos if i.chosen][0]
class AnimalSource(Source):
"""A source that transmits animal shapes."""
def create_node(self, participant, network):
"""Create a node for a participant."""
return dlgr.nodes.Agent(network=network, participant=participant)
return float(self.property1)
except TypeError:
return None
@fitness.setter
def fitness(self, fitness):
"""Assign fitness to property1."""
self.property1 = repr(fitness)
@fitness.expression
def fitness(self):
"""Retrieve fitness via property1."""
return cast(self.property1, Float)
class ReplicatorAgent(Agent):
"""An agent that copies incoming transmissions."""
__mapper_args__ = {"polymorphic_identity": "replicator_agent"}
def update(self, infos):
"""Replicate the incoming information."""
for info_in in infos:
self.replicate(info_in=info_in)
class Source(Node):
"""An AI Node that only sends transmissions.
By default, when asked to transmit, a Source creates and sends
a new Info. Sources cannot receive transmissions.
"""
def create_node(self, participant, network):
"""Create a node for a participant."""
return dlgr.nodes.Agent(network=network, participant=participant)