Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
# Test attributes
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
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."""
__mapper_args__ = {"polymorphic_identity": "animal_source"}
def create_information(self):
"""Create a new Info.
transmit() -> _what() -> create_information().
"""
return AnimalInfo(origin=self, contents=None)
class AnimalInfo(Info):
"""An Info that can be chosen."""
__mapper_args__ = {"polymorphic_identity": "vector_info"}
ObjectPhotographSource(network=net)
def add_node_to_network(self, node, network):
"""Add node to the network and receive transmissions."""
network.add_node(node)
source = dlgr.nodes.Source.query.one()
source.connect(direction="to", whom=node)
source.transmit()
node.receive()
def recruit(self):
"""Don't recruit new participants."""
pass
class ObjectPhotographSource(dlgr.nodes.Source):
"""Transmits a photograph of an object."""
__mapper_args__ = {
"polymorphic_identity": "object_photograph_source"
}
def _contents(self):
"""Return an image."""
stimuli_dir = os.path.join("static", "stimuli")
for i in os.listdir(stimuli_dir):
if i.endswith(".jpg"):
with open(os.path.join(stimuli_dir, i), "rb") as f:
return base64.b64encode(f.read())
def add_node_to_network(self, node, network):
"""Add node to the network and receive transmissions."""
network.add_node(node)
source = dlgr.nodes.Source.query.one()
source.connect(direction="to", whom=node)
source.transmit()
node.receive()
def add_node_to_network(self, node, network):
"""Add node to the chain and receive transmissions."""
network.add_node(node)
source = network.nodes(type=Source)[0] # find the source in the network
source.connect(direction="to", whom=node) # link up the source to the new node
source.transmit(to_whom=node) # in networks.py code, transmit info to the new node
node.receive() # new node receives everything
all_edges = []
all_edges = [(0,1), (0,2), (1,2)] # TEMPORARY
# here are all the edges that need to be connected
# BABY_NETWORK:
#all_edges = [(0, 1), (0, 2), (0, 3), (2, 3)]
#all_edges = [(0,1), (0,2)]
# FULLY CONNECTED:
# 16 subjects
#all_edges = [(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12),
"""Run when a request to create an info is complete."""
for agent in node.neighbors():
node.transmit(what=info, to_whom=agent)
#def info_post_request(self, node, info):
# """Run when a request to create an info is complete."""
# """Transfer info to only one neighbor."""
# agent = random.choice(node.neighbors())
# node.transmit(what=info, to_whom=agent)
def create_node(self, participant, network):
"""Create a node for a participant."""
return dlgr.nodes.Agent(network=network, participant=participant)
class FreeRecallListSource(Source):
"""A Source that reads in a random list from a file and transmits it."""
__mapper_args__ = {
"polymorphic_identity": "free_recall_list_source"
}
def _contents(self):
"""Define the contents of new Infos.
transmit() -> _what() -> create_information() -> _contents().
"""
# randomly shuffles categories, randomly shuffles words in categories,
# but presents lists by categories
# load in the wordlists
#: dictionary, the classes Dallinger can make in response
#: to front-end requests. Experiments can add new classes to this
#: dictionary.
self.known_classes = {
"Agent": Agent,
"Compression": Compression,
"Environment": Environment,
"Gene": Gene,
"Info": Info,
"Meme": Meme,
"Mutation": Mutation,
"Node": Node,
"Replication": Replication,
"Response": Response,
"Source": Source,
"State": State,
"Transformation": Transformation,
}
def _select_oldest_source(self):
return min(self.nodes(type=Source), key=attrgetter("creation_time"))
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)]
source.connect(whom=nodes)