def test_6(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') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, A, A, A, A, B, C, B, C, B, C] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_checkmark2_1(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') D = TransferMechanism(function=Linear(intercept=.5), name='D') for m in [A, B, C, D]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(A, MappingProjection(), D) comp.add_projection(B, MappingProjection(), D) comp.add_projection(C, MappingProjection(), 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))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( D, 1, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, set([A, C]), B, A, set([A, C]), B, D] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_linear_ABBCC(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, Any(AtPass(0), EveryNCalls(C, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) sched.add_condition(C, Any(EveryNCalls(B, 2), JustRan(C))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [A, B, B, C, C, A, B, B, C, C] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_LCA_length_2(self): T = TransferMechanism(function=Linear(slope=1.0), size=2) L = LCA(function=Linear(slope=2.0), size=2, self_excitation=3.0, leak=0.5, competition=1.0, time_step_size=0.1) P = Process(pathway=[T, L]) S = System(processes=[P]) # - - - - - - - Equations to be executed - - - - - - - # new_transfer_input = # previous_transfer_input # + (leak * previous_transfer_input_1 + self_excitation * result1 + competition * result2 + outside_input1) * dt # + noise # result = new_transfer_input*2.0 # recurrent_matrix = [[3.0]] # - - - - - - - - - - - - - - - - - - - - - - - - - - results = [] def record_execution(): results.append(L.value[0]) S.run(inputs={T: [1.0, 2.0]}, num_trials=3, call_after_trial=record_execution) # - - - - - - - TRIAL 1 - - - - - - - # new_transfer_input_1 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 1.0)*0.1 + 0.0 = 0.1 # f(new_transfer_input_1) = 0.1 * 2.0 = 0.2 # new_transfer_input_2 = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 - 1.0*0.0 + 2.0)*0.1 + 0.0 = 0.2 # f(new_transfer_input_2) = 0.2 * 2.0 = 0.4 # - - - - - - - TRIAL 2 - - - - - - - # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 - 1.0*0.4 + 1.0)*0.1 + 0.0 = 0.225 # f(new_transfer_input) = 0.265 * 2.0 = 0.45 # new_transfer_input_2 = 0.2 + ( 0.5 * 0.2 + 3.0 * 0.4 - 1.0*0.2 + 2.0)*0.1 + 0.0 = 0.51 # f(new_transfer_input_2) = 0.1 * 2.0 = 1.02 # - - - - - - - TRIAL 3 - - - - - - - # new_transfer_input = 0.225 + ( 0.5 * 0.225 + 3.0 * 0.45 - 1.0*1.02 + 1.0)*0.1 + 0.0 = 0.36925 # f(new_transfer_input) = 0.36925 * 2.0 = 0.7385 # new_transfer_input_2 = 0.51 + ( 0.5 * 0.51 + 3.0 * 1.02 - 1.0*0.45 + 2.0)*0.1 + 0.0 = 0.9965 # f(new_transfer_input_2) = 0.9965 * 2.0 = 1.463 assert np.allclose(results, [[0.2, 0.4], [0.45, 1.02], [0.7385, 1.993]])
def test_triangle_4b(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(A, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, All(WhenFinished(A), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 1) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 10: A.is_finished = True output.append(step) i += 1 expected_output = [A, A, B, A, A, B, A, A, B, A, A, set([B, C])] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_five_ABABCDE(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) C = IntegratorMechanism(name='C', default_variable=[0], function=SimpleIntegrator(rate=.5)) D = TransferMechanism( name='D', default_variable=[0], function=Linear(slope=1.0), ) E = TransferMechanism( name='E', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, C, D], name='p') q = Process(default_variable=[0], pathway=[B, C, E], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(E, 1)} stim_list = {A: [[1]], B: [[2]]} sched = Scheduler(system=s) sched.add_condition(C, Any(EveryNCalls(A, 1), EveryNCalls(B, 1))) sched.add_condition(D, EveryNCalls(C, 1)) sched.add_condition(E, EveryNCalls(C, 1)) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [D, E] expected_output = [ [ numpy.array([3.]), ], [ numpy.array([6.]), ], ] for m in range(len(terminal_mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose( expected_output[m][i], terminal_mechs[m].output_values[i])
def test_invtriangle_1(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_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, Any(AfterNCalls(A, 3), AfterNCalls(B, 3))) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls( C, 4, time_scale=TimeScale.TRIAL) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ A, set([A, B]), A, C, set([A, B]), C, A, C, set([A, B]), C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_termination_conditions_reset(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') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(B, EveryNCalls(A, 2)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) 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) # reset the RUN because schedulers run TRIALs sched.clock._increment_time(TimeScale.RUN) sched._reset_counts_total(TimeScale.RUN) output = list(sched.run()) expected_output = [A, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_no_termination_conds(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNCalls(A, 2)) sched.add_condition(C, EveryNCalls(B, 3)) output = list(sched.run()) expected_output = [ A, A, B, A, A, B, A, A, B, C, ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_6_two_trials(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_mechanism(m) comp.add_projection(A, MappingProjection(), B) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, BeforePass(5)) sched.add_condition(B, AfterNCalls(A, 5)) sched.add_condition(C, AfterNCalls(B, 1)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(2) termination_conds[TimeScale.TRIAL] = AfterNCalls(C, 3) comp.run(inputs={A: range(6)}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list expected_output = [ A, A, A, A, A, B, C, B, C, B, C, A, A, A, A, A, B, C, B, C, B, C ] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_9(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(intercept=4.0), name='B') for m in [A, B]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), B) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, WhenFinished(A)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(B, 2) output = [] i = 0 for step in sched.run(termination_conds=termination_conds): if i == 3: A.is_finished = True output.append(step) i += 1 expected_output = [A, A, A, A, B, A, B] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_WhenFinishedAll_2(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') A.is_finished = False B = TransferMechanism(function=Linear(intercept=4.0), name='B') B.is_finished = True C = TransferMechanism(function=Linear(intercept=1.5), name='C') for m in [A, B, C]: comp.add_mechanism(m) comp.add_projection(A, MappingProjection(), C) comp.add_projection(B, MappingProjection(), C) sched = Scheduler(composition=comp) sched.add_condition(A, EveryNPasses(1)) sched.add_condition(B, EveryNPasses(1)) sched.add_condition(C, WhenFinishedAll(A, B)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = AfterNCalls(A, 5) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A, B]), set([A, B]), set([A, B]), set([A, B]), set([A, B]), ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_change_scheduler(self): comp = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A') B = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='B') for m in [A, B]: comp.add_mechanism(m) s1 = Scheduler(composition=comp) s2 = Scheduler(composition=comp) cs = ConditionSet(s1) cs.add_condition(A, Always()) cs.add_condition(B, Always()) assert cs.scheduler is s1 for owner, cond in cs.conditions.items(): assert cond.scheduler is s1 cs.scheduler = s2 assert cs.scheduler is s2 for owner, cond in cs.conditions.items(): assert cond.scheduler is s2
def test_LCA_length_1(self): T = TransferMechanism(function=Linear(slope=1.0)) L = LCA( function=Linear(slope=2.0), self_excitation=3.0, leak=0.5, competition= 1.0, # competition does not matter because we only have one unit time_step_size=0.1) P = Process(pathway=[T, L]) S = System(processes=[P]) L.reinitialize_when = Never() # - - - - - - - Equations to be executed - - - - - - - # new_transfer_input = # previous_transfer_input # + (leak * previous_transfer_input_1 + self_excitation * result1 + competition * result2 + outside_input1) * dt # + noise # result = new_transfer_input*2.0 # recurrent_matrix = [[3.0]] # - - - - - - - - - - - - - - - - - - - - - - - - - - results = [] def record_execution(): results.append(L.value[0][0]) S.run(inputs={T: [1.0]}, num_trials=3, call_after_trial=record_execution) # - - - - - - - TRIAL 1 - - - - - - - # new_transfer_input = 0.0 + ( 0.5 * 0.0 + 3.0 * 0.0 + 0.0 + 1.0)*0.1 + 0.0 = 0.1 # f(new_transfer_input) = 0.1 * 2.0 = 0.2 # - - - - - - - TRIAL 2 - - - - - - - # new_transfer_input = 0.1 + ( 0.5 * 0.1 + 3.0 * 0.2 + 0.0 + 1.0)*0.1 + 0.0 = 0.265 # f(new_transfer_input) = 0.265 * 2.0 = 0.53 # - - - - - - - TRIAL 3 - - - - - - - # new_transfer_input = 0.265 + ( 0.5 * 0.265 + 3.0 * 0.53 + 0.0 + 1.0)*0.1 + 0.0 = 0.53725 # f(new_transfer_input) = 0.53725 * 2.0 = 1.0745 assert np.allclose(results, [0.2, 0.53, 1.0745])
def __init__( self, sender=None, receiver=None, function=Linear( params={FUNCTION_OUTPUT_TYPE: FunctionOutputType.RAW_NUMBER}), weight=None, exponent=None, gating_signal_params: tc.optional(dict) = None, params=None, name=None, prefs: is_pref_set = None): # Assign args to params and functionParams dicts (kwConstants must == arg names) params = self._assign_args_to_param_dicts( function=function, gating_signal_params=gating_signal_params, params=params) # If receiver has not been assigned, defer init to State.instantiate_projection_to_state() if sender is None or receiver is None: # Flag for deferred initialization self.context.initialization_status = ContextFlags.DEFERRED_INIT # Validate sender (as variable) and params, and assign to variable and paramInstanceDefaults # Note: pass name of mechanism (to override assignment of componentName in super.__init__) super().__init__(sender=sender, receiver=receiver, weight=weight, exponent=exponent, function=function, params=params, name=name, prefs=prefs, context=ContextFlags.CONSTRUCTOR)
def test_two_compositions_one_scheduler(self): comp1 = Composition() comp2 = Composition() A = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='scheduler-pytests-A') comp1.add_mechanism(A) comp2.add_mechanism(A) sched = Scheduler(composition=comp1) sched.add_condition(A, BeforeNCalls(A, 5, time_scale=TimeScale.LIFE)) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(6) termination_conds[TimeScale.TRIAL] = AfterNPasses(1) comp1.run(inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list[comp1._execution_id] expected_output = [A, A, A, A, A, set()] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output) comp2.run(inputs={A: [[0], [1], [2], [3], [4], [5]]}, scheduler_processing=sched, termination_processing=termination_conds) output = sched.execution_list[comp2._execution_id] expected_output = [A, A, A, A, A, set()] # pprint.pprint(output) assert output == pytest.helpers.setify_expected_output(expected_output)
def test_previous_value_stored(self): G = LCA(integrator_mode=True, leak=-1.0, noise=0.0, time_step_size=0.02, function=Linear(slope=2.0), self_excitation=1.0, competition=-1.0, initial_value=np.array([[1.0]])) P = Process(pathway=[G]) S = System(processes=[P]) G.output_state.value = [0.0] # - - - - - LCA integrator functions - - - - - # X = previous_value + (rate * previous_value + variable) * self.time_step_size + noise # f(X) = 2.0*X + 0 # - - - - - starting values - - - - - # variable = G.output_state.value + stimulus = 0.0 + 1.0 = 1.0 # previous_value = initial_value = 1.0 # single_run = S.execute([[1.0]]) # np.testing.assert_allclose(single_run, np.array([[2.0]])) np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.0]])) # X = 1.0 + (-1.0 + 1.0)*0.02 + 0.0 # X = 1.0 + 0.0 + 0.0 = 1.0 <--- previous value 1.0 # f(X) = 2.0*1.0 <--- return 2.0, recurrent projection 2.0 np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.08]])) # X = 1.0 + (-1.0 + 3.0)*0.02 + 0.0 # X = 1.0 + 0.04 = 1.04 <--- previous value 1.04 # f(X) = 2.0*1.04 <--- return 2.08 np.testing.assert_allclose(S.execute([[1.0]]), np.array([[2.1616]]))
def __init__(self, default_gating_policy=None, size=None, function=Linear(slope=1, intercept=0), gating_signals: tc.optional(list) = None, modulation: tc.optional( _is_modulation_param) = ModulationParam.MULTIPLICATIVE, params=None, name=None, prefs: is_pref_set = None, context=None): # self.system = None # Assign args to params and functionParams dicts (kwConstants must == arg names) params = self._assign_args_to_param_dicts( gating_signals=gating_signals, function=function, params=params) super().__init__(variable=default_gating_policy, size=size, modulation=modulation, params=params, name=name, prefs=prefs, context=self)
def __init__( self, system: tc.optional(System_Base) = None, monitored_output_states=None, function=Linear(slope=1, intercept=0), # control_signals:tc.optional(list) = None, control_signals=None, modulation: tc.optional( _is_modulation_param) = ModulationParam.MULTIPLICATIVE, params=None, name=None, prefs: is_pref_set = None): # Assign args to params and functionParams dicts (kwConstants must == arg names) params = self._assign_args_to_param_dicts( function=function, control_signals=control_signals, params=params) super().__init__(system=system, objective_mechanism=ObjectiveMechanism( monitored_output_states=monitored_output_states, function=AGTUtilityIntegrator), control_signals=control_signals, modulation=modulation, params=params, name=name, prefs=prefs, context=ContextFlags.CONSTRUCTOR) self.objective_mechanism.name = self.name + '_ObjectiveMechanism' self.objective_mechanism._role = CONTROL
def test_two_ABB(self): A = TransferMechanism( name='A', default_variable=[0], function=Linear(slope=2.0), ) B = IntegratorMechanism(name='B', default_variable=[0], function=SimpleIntegrator(rate=.5)) p = Process(default_variable=[0], pathway=[A, B], name='p') s = System(processes=[p], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(B, 2)} stim_list = {A: [[1]]} sched = Scheduler(system=s) sched.add_condition(A, Any(AtPass(0), AfterNCalls(B, 2))) sched.add_condition(B, Any(JustRan(A), JustRan(B))) s.scheduler_processing = sched s.run(inputs=stim_list, termination_processing=term_conds) terminal_mech = B expected_output = [ numpy.array([2.]), ] for i in range(len(expected_output)): numpy.testing.assert_allclose(expected_output[i], terminal_mech.output_values[i])
def test_transfer_mech_2d_variable(self): from psyneulink.globals.keywords import MEAN T = TransferMechanism(name='T', function=Linear(slope=2.0, intercept=1.0), default_variable=[[0.0, 0.0], [0.0, 0.0]], output_states=[MEAN]) val = T.execute([[1.0, 2.0], [3.0, 4.0]])
def test_transfer_mech_2d_variable_noise(self): T = TransferMechanism( name='T', function=Linear(slope=2.0, intercept=1.0), noise=NormalDist(), default_variable=[[0.0, 0.0], [0.0, 0.0]] ) val = T.execute([[1.0, 2.0], [3.0, 4.0]])
def test_transfer_mech_time_constant_0_0(self): T = TransferMechanism(name='T', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0.0, integrator_mode=True) val = T.execute([1, 1, 1, 1]) assert np.allclose(val, [[0.0, 0.0, 0.0, 0.0]])
def test_three_ABAC_convenience(self): A = IntegratorMechanism(name='A', default_variable=[0], function=SimpleIntegrator(rate=.5)) B = TransferMechanism( name='B', default_variable=[0], function=Linear(slope=2.0), ) C = TransferMechanism( name='C', default_variable=[0], function=Linear(slope=2.0), ) p = Process(default_variable=[0], pathway=[A, B], name='p') q = Process(default_variable=[0], pathway=[A, C], name='q') s = System(processes=[p, q], name='s') term_conds = {TimeScale.TRIAL: AfterNCalls(C, 1)} stim_list = {A: [[1]]} s.scheduler_processing.add_condition( B, Any(AtNCalls(A, 1), EveryNCalls(A, 2))) s.scheduler_processing.add_condition(C, EveryNCalls(A, 2)) s.run(inputs=stim_list, termination_processing=term_conds) terminal_mechs = [B, C] expected_output = [ [ numpy.array([1.]), ], [ numpy.array([2.]), ], ] for m in range(len(terminal_mechs)): for i in range(len(expected_output[m])): numpy.testing.assert_allclose( expected_output[m][i], terminal_mechs[m].output_values[i])
def test_processing_mechanism_linear_function(self): PM1 = ProcessingMechanism() PM1.execute(1.0) assert np.allclose(PM1.value, 1.0) PM2 = ProcessingMechanism(function=Linear(slope=2.0, intercept=1.0)) PM2.execute(1.0) assert np.allclose(PM2.value, 3.0)
def test_transfer_mech_time_constant_0(self): with pytest.raises(TransferError) as error_text: T = TransferMechanism(name='T', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0, integrator_mode=True) T.execute([1, 1, 1, 1]) assert ("time_constant parameter" in str(error_text.value) and "must be a float between 0 and 1" in str(error_text.value))
def test_multisource_2(self): comp = Composition() A1 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A1') A2 = TransferMechanism(function=Linear(slope=5.0, intercept=2.0), name='A2') B1 = TransferMechanism(function=Linear(intercept=4.0), name='B1') B2 = TransferMechanism(function=Linear(intercept=4.0), name='B2') B3 = TransferMechanism(function=Linear(intercept=4.0), name='B3') C1 = TransferMechanism(function=Linear(intercept=1.5), name='C1') C2 = TransferMechanism(function=Linear(intercept=.5), name='C2') for m in [A1, A2, B1, B2, B3, C1, C2]: comp.add_mechanism(m) comp.add_projection(A1, MappingProjection(), B1) comp.add_projection(A1, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B1) comp.add_projection(A2, MappingProjection(), B2) comp.add_projection(A2, MappingProjection(), B3) comp.add_projection(B1, MappingProjection(), C1) comp.add_projection(B2, MappingProjection(), C1) comp.add_projection(B1, MappingProjection(), C2) comp.add_projection(B3, MappingProjection(), C2) sched = Scheduler(composition=comp) sched.add_condition_set({ A1: Always(), A2: Always(), B1: EveryNCalls(A1, 2), B3: EveryNCalls(A2, 2), B2: All(EveryNCalls(A1, 4), EveryNCalls(A2, 4)), C1: Any(AfterNCalls(B1, 2), AfterNCalls(B2, 2)), C2: Any(AfterNCalls(B2, 2), AfterNCalls(B3, 2)), }) termination_conds = {} termination_conds[TimeScale.RUN] = AfterNTrials(1) termination_conds[TimeScale.TRIAL] = All(AfterNCalls(C1, 1), AfterNCalls(C2, 1)) output = list(sched.run(termination_conds=termination_conds)) expected_output = [ set([A1, A2]), set([A1, A2]), set([B1, B3]), set([A1, A2]), set([A1, A2]), set([B1, B2, B3]), set([C1, C2]) ] assert output == pytest.helpers.setify_expected_output(expected_output)
def test_kwta_function_various_spec(self): specs = [Logistic, Linear, Linear(slope=3), Logistic(gain=2, offset=-4.2)] for s in specs: K = KWTA( name='K', size=5, function=s, k_value=4 ) K.execute([1, 2, 5, -2, .3])
def test_transfer_mech_array_var_float_noise(self): T = TransferMechanism(name='T', default_variable=[0, 0, 0, 0], function=Linear(), noise=5.0, time_constant=1.0, integrator_mode=True) val = T.execute([0, 0, 0, 0]) assert np.allclose(val, [[5.0, 5.0, 5.0, 5.0]])
def test_kwta_linear_slope(self): K = KWTA( name='K', threshold=.5, size=5, k_value=2, function=Linear(slope=2) ) val = K.execute(input=[1, 3, 4, 2, 1]) assert np.allclose(val, [[-2, 2, 4, 0, -2]])