Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_AllHaveRun_2(self):
comp = Composition()
A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A')
B = TransferMechanism(function=Linear(intercept=4.0), name='B')
C = TransferMechanism(function=Linear(intercept=1.5), name='C')
for m in [A, B, C]:
comp.add_node(m)
comp.add_projection(MappingProjection(), A, B)
comp.add_projection(MappingProjection(), B, C)
sched = Scheduler(composition=comp)
sched.add_condition(A, EveryNPasses(1))
sched.add_condition(B, EveryNCalls(A, 2))
sched.add_condition(C, EveryNCalls(B, 2))
termination_conds = {}
termination_conds[TimeScale.RUN] = AfterNTrials(1)
termination_conds[TimeScale.TRIAL] = AllHaveRun()
def test_one_input_port_one_output_port(self):
comp = Composition()
A = TransferMechanism(name="A",
function=Linear(slope=2.0))
B = TransferMechanism(name="B",
function=Linear(slope=3.0))
comp.add_node(A)
comp.add_node(B)
comp.add_projection(MappingProjection(sender=A, receiver=B), A, B)
inputs_dict = {
A: [[5.]],
}
sched = Scheduler(composition=comp)
def test_AfterNTrials(self):
comp = Composition()
A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A')
comp.add_node(A)
sched = Scheduler(composition=comp)
sched.add_condition(A, AfterNPasses(1))
termination_conds = {}
termination_conds[TimeScale.RUN] = AfterNTrials(1)
termination_conds[TimeScale.TRIAL] = AtPass(5)
output = list(sched.run(termination_conds=termination_conds))
expected_output = [set(), A, A, A, A]
assert output == pytest.helpers.setify_expected_output(expected_output)
predator_value_idx = 5
prey_value_idx = 8
player_len = prey_len = predator_len = obs_len
# The original needs GaussianDistort
# player = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PLAYER OBS")
# prey = ProcessingMechanism(size=prey_len, function=GaussianDistort, name="PREY OBS")
player = TransferMechanism(size=prey_len, name="PLAYER OBS")
prey = TransferMechanism(size=prey_len, name="PREY OBS")
# Use ComparatorMechanism to compute direction of action as difference of coordinates between player and prey:
# note: unitization is done in main loop, to allow compilation of LinearCombination function) (TBI)
greedy_action_mech = ComparatorMechanism(name='MOTOR OUTPUT',sample=player,target=prey)
agent_comp = Composition(name='PREDATOR-PREY COMPOSITION')
agent_comp.add_node(player)
agent_comp.add_node(prey)
agent_comp.add_node(greedy_action_mech)
# Projections to greedy_action_mech were created by assignments of sample and target args in its constructor,
# so just add them to the Composition).
for projection in greedy_action_mech.projections:
agent_comp.add_projection(projection)
run_results = agent_comp.run(inputs={player:[[619,177]],
prey:[[419,69]]},
bin_execute=mode)
assert np.allclose(run_results, [[-200, -108]])
benchmark(agent_comp.run, **{'inputs':{
player:[[619,177]],
prey:[[419,69]],
def test_partial_override_scheduler(self):
comp = Composition()
A = TransferMechanism(name='scheduler-pytests-A')
B = TransferMechanism(name='scheduler-pytests-B')
for m in [A, B]:
comp.add_node(m)
comp.add_projection(MappingProjection(), A, B)
sched = Scheduler(composition=comp)
sched.add_condition(B, EveryNCalls(A, 2))
termination_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)}
output = list(sched.run(termination_conds=termination_conds))
expected_output = [A, A, B, A, A, B]
assert output == pytest.helpers.setify_expected_output(expected_output)
def test_process(self):
a = TransferMechanism(name="a", default_variable=[0, 0, 0])
b = TransferMechanism(name="b")
comp = Composition()
comp.add_linear_processing_pathway([a, b])
a_label = comp._show_graph._get_graph_node_label(comp, a, show_dimensions=ALL)
b_label = comp._show_graph._get_graph_node_label(comp, b, show_dimensions=ALL)
assert "out (3)" in a_label and "in (3)" in a_label
assert "out (1)" in b_label and "in (1)" in b_label
def test_10c(self):
comp = Composition()
A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A')
A.is_finished_flag = True
B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B')
for m in [A, B]:
comp.add_node(m)
comp.add_projection(MappingProjection(), A, B)
sched = Scheduler(composition=comp)
sched.add_condition(A, EveryNPasses(1))
sched.add_condition(B, All(WhenFinished(A), AfterNCalls(A, 3)))
termination_conds = {}
termination_conds[TimeScale.RUN] = AfterNTrials(1)
termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 4)
def test_converging_pathways(self):
a = TransferMechanism(name="a", default_variable=[0, 0, 0])
b = TransferMechanism(name="b")
c = TransferMechanism(name="c", default_variable=[0, 0, 0, 0, 0])
LC = LCControlMechanism(
modulated_mechanisms=[a, b],
objective_mechanism=ObjectiveMechanism(
function=Linear, monitor=[b], name="lc_om"
),
name="lc",
)
comp = Composition()
comp.add_linear_processing_pathway([a, c])
comp.add_linear_processing_pathway([b, c])
a_label = comp._show_graph._get_graph_node_label(comp, a, show_dimensions=ALL)
b_label = comp._show_graph._get_graph_node_label(comp, b, show_dimensions=ALL)
c_label = comp._show_graph._get_graph_node_label(comp, c, show_dimensions=ALL)
assert "out (3)" in a_label and "in (3)" in a_label
assert "out (1)" in b_label and "in (1)" in b_label
assert "out (5)" in c_label and "in (5)" in c_label
def test_checkmark_2(self):
comp = Composition()
A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A')
B = TransferMechanism(function=Linear(intercept=4.0), name='scheduler-pytests-B')
C = TransferMechanism(function=Linear(intercept=1.5), name='scheduler-pytests-C')
D = TransferMechanism(function=Linear(intercept=.5), name='scheduler-pytests-D')
for m in [A, B, C, D]:
comp.add_node(m)
comp.add_projection(MappingProjection(), A, B)
comp.add_projection(MappingProjection(), B, D)
comp.add_projection(MappingProjection(), C, D)
sched = Scheduler(composition=comp)
sched.add_condition(A, EveryNPasses(1))
sched.add_condition(B, EveryNCalls(A, 2))
sched.add_condition(C, EveryNCalls(A, 2))
sched.add_condition(D, All(EveryNCalls(B, 2), EveryNCalls(C, 2)))
__all__ = [
'SystemComposition', 'SystemCompositionError'
]
class SystemCompositionError(Exception):
def __init__(self, error_value):
self.error_value = error_value
def __str__(self):
return repr(self.error_value)
class SystemComposition(Composition):
"""
Arguments
----------
Attributes
----------
Returns
----------
"""
def __init__(self):
super(SystemComposition, self).__init__()