コード例 #1
0
 def test_mech_spec_list(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     T = TransferMechanism(default_variable=[[0]], input_ports=[R1])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0]]))
     assert len(T.input_ports) == 1
     assert T.input_port.path_afferents[0].sender == R1.output_port
     T.execute()
コード例 #2
0
    def test_runtime_params_reset_isolated(self):

        T = TransferMechanism()

        # Intercept attr updated
        T.function.intercept = 2.0
        assert T.function.intercept == 2.0

        # Runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        assert T.function.slope == 10.0
        assert T.parameter_states['slope'].value == 10.0

        # Intercept attr NOT affected by runtime params
        assert T.function.intercept == 2.0
        assert T.value == 22.0

        # Runtime param NOT used for slope
        T.execute(input=2.0)
        assert T.function.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0

        # Intercept attr NOT affected by runtime params reset
        assert T.function.intercept == 2.0
        assert T.value == 4.0
コード例 #3
0
 def test_mech_spec_standalone(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     # Mechanism outside of list specification
     T = TransferMechanism(default_variable=[[0]], input_ports=R1)
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0]]))
     assert len(T.input_ports) == 1
     assert T.input_port.path_afferents[0].sender == R1.output_port
     T.execute()
コード例 #4
0
 def test_2_item_tuple_value_for_first_item(self):
     R2 = TransferMechanism(size=3)
     T = TransferMechanism(input_ports=[([0,0], R2)])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
     assert len(T.input_ports) == 1
     assert T.input_port.path_afferents[0].sender.socket_width == 3
     assert T.input_port.socket_width == 2
     T.execute()
コード例 #5
0
 def test_projection_tuple_with_matrix_spec(self):
     R2 = TransferMechanism(size=3)
     T = TransferMechanism(size=2, input_ports=[(R2, None, None, np.zeros((3, 2)))])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
     assert len(T.input_ports) == 1
     assert T.input_port.path_afferents[0].sender.defaults.variable.shape[-1] == 3
     assert T.input_port.socket_width == 2
     T.execute()
コード例 #6
0
 def test_output_port_spec_standalone(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     T = TransferMechanism(default_variable=[0],
                           input_ports=R1.output_ports['FIRST'])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0]]))
     assert len(T.input_ports) == 1
     assert T.input_ports.names[0] == 'InputPort-0'
     T.input_port.path_afferents[0].sender == R1.output_port
     T.execute()
コード例 #7
0
 def test_2_item_tuple_spec(self):
     R2 = TransferMechanism(size=3)
     T = TransferMechanism(size=2, input_states=[(R2, np.zeros((3, 2)))])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(
         T.input_state.path_afferents[0].sender.defaults.variable) == 3
     assert T.input_state.socket_width == 2
     T.execute()
コード例 #8
0
 def test_projection_list(self):
     R2 = TransferMechanism(size=3)
     P = MappingProjection(sender=R2)
     T = TransferMechanism(size=2, input_states=[P])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(
         T.input_state.path_afferents[0].sender.defaults.variable) == 3
     assert len(T.input_state.defaults.variable) == 2
     T.execute()
コード例 #9
0
 def test_projection_in_tuple(self):
     R2 = TransferMechanism(size=3)
     P = MappingProjection(sender=R2)
     T = TransferMechanism(size=2, input_ports=[(R2, None, None, P)])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
     assert len(T.input_ports) == 1
     assert len(
         T.input_port.path_afferents[0].sender.defaults.variable) == 3
     assert len(T.input_port.defaults.variable) == 2
     T.execute()
コード例 #10
0
    def test_inspect_function_params_slope_noise(self):
        A = TransferMechanism()
        B = TransferMechanism()
        assert A.function.slope.base == 1.0
        assert B.function.slope.base == 1.0
        assert A.function.slope.modulated == [1.0]
        assert B.function.slope.modulated == [1.0]

        assert A.noise.base == 0.0
        assert B.noise.base == 0.0
        assert A.noise.modulated == 0.0
        assert B.noise.modulated == 0.0

        A.function.slope.base = 0.2

        assert A.function.slope.base == 0.2
        assert B.function.slope.base == 1.0
        assert A.function.slope.modulated == [1.0]
        assert B.function.slope.modulated == [1.0]

        A.noise.base = 0.5

        assert A.noise.base == 0.5
        assert B.noise.base == 0.0
        assert A.noise.modulated == 0.0
        assert B.noise.modulated == 0.0

        B.function.slope.base = 0.7

        assert A.function.slope.base == 0.2
        assert B.function.slope.base == 0.7
        assert A.function.slope.modulated == [1.0]
        assert B.function.slope.modulated == [1.0]

        B.noise.base = 0.6

        assert A.noise.base == 0.5
        assert B.noise.base == 0.6
        assert A.noise.modulated == 0.0
        assert B.noise.modulated == 0.0

        A.execute(1.0)
        assert A.function.slope.modulated == [0.2]

        B.execute(1.0)

        assert A.function.slope.base == 0.2
        assert B.function.slope.base == 0.7
        assert A.function.slope.modulated == [0.2]
        assert B.function.slope.modulated == [0.7]

        assert A.noise.base == 0.5
        assert B.noise.base == 0.6
        assert A.noise.modulated == 0.5
        assert B.noise.modulated == 0.6
コード例 #11
0
    def test_default_variable_override_mech_list(self):

        R2 = TransferMechanism(size=3)

        # default_variable override of OutputPort.value
        T = TransferMechanism(default_variable=[[0, 0]], input_ports=[R2])
        np.testing.assert_array_equal(T.defaults.variable, np.array([[0, 0]]))
        assert len(T.input_ports) == 1
        assert len(
            T.input_port.path_afferents[0].sender.defaults.variable) == 3
        assert len(T.input_port.defaults.variable[0]) == 2
        T.execute()
コード例 #12
0
 def test_output_port_spec_list_two_items(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     T = TransferMechanism(
         default_variable=[[0], [0]],
         input_ports=[R1.output_ports['FIRST'], R1.output_ports['SECOND']])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0],
                                                                  [0]]))
     assert len(T.input_ports) == 2
     assert T.input_ports.names[0] == 'InputPort-0'
     assert T.input_ports.names[1] == 'InputPort-1'
     for input_port in T.input_ports:
         for projection in input_port.path_afferents:
             assert projection.sender.owner is R1
     T.execute()
コード例 #13
0
 def test_projection_in_specification_dict(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     T = TransferMechanism(input_ports=[{
         NAME:
         'My InputPort with Two Projections',
         PROJECTIONS: [R1.output_ports['FIRST'], R1.output_ports['SECOND']]
     }])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0]]))
     assert len(T.input_ports) == 1
     assert T.input_port.name == 'My InputPort with Two Projections'
     for input_port in T.input_ports:
         for projection in input_port.path_afferents:
             assert projection.sender.owner is R1
     T.execute()
コード例 #14
0
def test_mechanism(benchmark, executions, mech_mode):
    benchmark.group = "TransferMechanism multirun {}".format(executions)
    variable = [0 for _ in range(SIZE)]
    T = TransferMechanism(
        name='T',
        default_variable=variable,
        integration_rate=1.0,
        noise=-2.0,
        integrator_mode=True
    )
    var = [[10.0 for _ in range(SIZE)] for _ in range(executions)]
    expected = [[8.0 for i in range(SIZE)]]
    if mech_mode == 'Python':
        f = lambda x : [T.execute(x[i]) for i in range(executions)]
        res = benchmark(f if executions > 1 else T.execute, var)
    elif mech_mode == 'LLVM':
        e = pnlvm.execution.MechExecution(T, [None for _ in range(executions)])
        res = benchmark(e.execute, var)
    elif mech_mode == 'PTX':
        e = pnlvm.execution.MechExecution(T, [None for _ in range(executions)])
        res = benchmark(e.cuda_execute, var)
    if executions > 1:
        expected = [expected for _ in range(executions)]

    assert np.allclose(res, expected)
    assert len(res) == executions
コード例 #15
0
    def test_function_runtime_param(self):

        T = TransferMechanism()
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0

        # runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        assert T.value == 20.0

        # defalut values are restored
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0
        T.execute(input=2.0)
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0
        assert T.value == 2.0
コード例 #16
0
    def test_mechanism_runtime_param(self):

        T = TransferMechanism()
        assert T.noise.base == 0.0
        assert T.parameter_ports['noise'].value == 0.0

        # runtime param used for noise
        T.execute(runtime_params={"noise": 10.0}, input=2.0)
        assert T.value == 12.0

        # defalut values are restored
        assert T.noise.base == 0.0
        assert T.parameter_ports['noise'].value == 0.0
        T.execute(input=2.0)
        assert T.noise.base == 0.0
        assert T.parameter_ports['noise'].value == 0.0
        assert T.value == 2.0
コード例 #17
0
    def test_mechanism_execute_function_param(self):

        # Construction
        T = TransferMechanism()
        assert T.function.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0

        # Runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        assert T.function.slope == 10.0
        assert T.parameter_states['slope'].value == 10.0
        assert T.value == 20.0

        # Runtime param NOT used for slope
        T.execute(input=2.0)
        assert T.function.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0
        assert T.value == 2.0
コード例 #18
0
    def test_mechanism_execute_mechanism_param(self):

        # Construction
        T = TransferMechanism()
        assert T.noise == 0.0
        assert T.parameter_states['noise'].value == 0.0

        # Runtime param used for noise
        T.execute(runtime_params={"noise": 10.0}, input=2.0)
        assert T.noise == 10.0
        assert T.parameter_states['noise'].value == 10.0
        assert T.value == 12.0

        # Runtime param NOT used for noise
        T.execute(input=2.0)
        assert T.noise == 0.0
        assert T.parameter_states['noise'].value == 0.0
        assert T.value == 2.0
コード例 #19
0
    def test_transfer_mech_process_matrix_change(self):
        from psyneulink.core.components.projections.pathway.mappingprojection import MappingProjection
        T1 = TransferMechanism(
            size=4,
            function=Linear)
        proj = MappingProjection(matrix=[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]])
        T2 = TransferMechanism(
            size=4,
            function=Linear)

        p = Process(size=4, pathway=[T1, proj, T2])

        p.run(inputs={T1: [[1, 2, 3, 4]]})
        proj.matrix = [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]
        assert np.allclose(proj.matrix, [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]])
        # p.run(inputs={T1: [[1, 2, 3, 4]]})
        T1.execute([[1, 2, 3, 4]])
        proj.execute(context="EXECUTING testing projection")
        assert np.allclose(proj.matrix, np.array([[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]))
コード例 #20
0
    def test_reset_to_previously_assigned_val(self):

        T = TransferMechanism()
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0

        # set slope directly
        T.function.slope.base = 2.0
        assert T.function.slope.base == 2.0

        # runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        assert T.value == 20.0

        # slope restored to previously assigned value
        assert T.function.slope.base == 2.0
        assert T.parameter_ports['slope'].value == 2.0
        T.execute(input=2.0)
        assert T.value == 4.0
        assert T.function.slope.base == 2.0
コード例 #21
0
    def test_reset_state_transfer_mechanism(self):
        A = TransferMechanism(name='A', integrator_mode=True)

        # Execute A twice
        original_output = [A.execute(1.0), A.execute(1.0)]

        # SAVING STATE  - - - - - - - - - - - - - - - - - - - - - - - - -
        reinitialize_values = []

        for attr in A.integrator_function.stateful_attributes:
            reinitialize_values.append(getattr(A.integrator_function, attr))

        # Execute A twice AFTER saving the state so that it continues accumulating.
        # We expect the next two outputs to repeat once we reset the state b/c we will return it to the current state
        output_after_saving_state = [A.execute(1.0), A.execute(1.0)]

        # RESETTING STATE - - - - - - - - - - - - - - - - - - - - - - - -
        A.reinitialize(*reinitialize_values)

        # We expect these results to match the results from immediately after saving the state
        output_after_reinitialization = [A.execute(1.0), A.execute(1.0)]

        assert np.allclose(output_after_saving_state,
                           output_after_reinitialization)
        assert np.allclose(
            original_output,
            [np.array([[0.5]]), np.array([[0.75]])])
        assert np.allclose(
            output_after_reinitialization,
            [np.array([[0.875]]), np.array([[0.9375]])])
コード例 #22
0
 def test_specification_dict(self):
     R1 = TransferMechanism(output_ports=['FIRST', 'SECOND'])
     T = TransferMechanism(
         default_variable=[[0], [0]],
         input_ports=[
             {
                 NAME: 'FROM DECISION',
                 PROJECTIONS: [R1.output_ports['FIRST']]
             },
             {
                 NAME: 'FROM RESPONSE_TIME',
                 PROJECTIONS: R1.output_ports['SECOND']
             }
         ])
     np.testing.assert_array_equal(T.defaults.variable, np.array([[0], [0]]))
     assert len(T.input_ports) == 2
     assert T.input_ports.names[0] == 'FROM DECISION'
     assert T.input_ports.names[1] == 'FROM RESPONSE_TIME'
     for input_port in T.input_ports:
         for projection in input_port.path_afferents:
             assert projection.sender.owner is R1
     T.execute()
コード例 #23
0
    def test_runtime_params_reset_to_most_recent_val(self):
        # NOT instance defaults

        # Construction
        T = TransferMechanism()
        assert T.function.slope == 1.0
        assert T.parameter_states['slope'].value == 1.0

        # Set slope attribute value directly
        T.function.slope = 2.0
        assert T.function.slope == 2.0

        # Runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        assert T.function.slope == 10.0
        assert T.parameter_states['slope'].value == 10.0
        assert T.value == 20.0

        # Runtime param NOT used for slope - reset to most recent slope value (2.0)
        T.execute(input=2.0)
        assert T.function.slope == 2.0
        assert T.value == 4.0
コード例 #24
0
    def test_use_and_reset_not_affect_other_assigned_vals(self):

        T = TransferMechanism()

        # Intercept attr assigned
        T.function.intercept.base = 2.0
        assert T.function.intercept.base == 2.0

        # runtime param used for slope
        T.execute(runtime_params={"slope": 10.0}, input=2.0)
        # Assigned intercept and runtime_param for slope are used:
        assert T.value == 22.0

        # slope restored to default, but intercept retains assigned value
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0
        assert T.function.intercept.base == 2.0

        # previous runtime_param for slope not used again
        T.execute(input=2.0)
        assert T.value == 4.0
        assert T.function.slope.base == 1.0
        assert T.parameter_ports['slope'].value == 1.0
コード例 #25
0
    def test_configurable_params(self):
        old_value = 0.2
        new_value = 0.7
        T = TransferMechanism(function=Linear(slope=old_value,
                                              intercept=old_value),
                              noise=old_value,
                              integration_rate=old_value)

        # SLOPE - - - - - - - -

        assert np.allclose(T.function.slope.base, old_value)
        assert np.allclose(T.function.slope.modulated, old_value)

        T.function.slope.base = new_value

        assert np.allclose(T.function.slope.base, new_value)
        assert np.allclose(T.function.slope.modulated, old_value)

        # INTERCEPT - - - - - - - -

        assert np.allclose(T.function.intercept.base, old_value)
        assert np.allclose(T.function.intercept.modulated, old_value)

        T.function.intercept.base = new_value

        assert np.allclose(T.function.intercept.base, new_value)
        assert np.allclose(T.function.intercept.modulated, old_value)

        # SMOOTHING FACTOR - - - - - - - -

        assert np.allclose(T.integration_rate.base, old_value)
        assert np.allclose(T.integration_rate.modulated, old_value)

        T.integration_rate.base = new_value

        # KAM changed 3/2/18 --
        # function_params looks at ParameterPort value, so this will not update until next execution
        assert np.allclose(T.integration_rate.base, new_value)
        assert np.allclose(T.integration_rate.modulated, old_value)

        # NOISE - - - - - - - -

        assert np.allclose(T.noise.base, old_value)
        assert np.allclose(T.noise.modulated, old_value)

        T.noise.base = new_value

        # KAM changed 3/2/18 --
        # function_params looks at ParameterPort value, so this will not update until next execution
        assert np.allclose(T.noise.base, new_value)
        assert np.allclose(T.noise.modulated, old_value)

        T.execute(1.0)

        assert np.allclose(T.function.slope.base, new_value)
        assert np.allclose(T.function.slope.modulated, new_value)

        assert np.allclose(T.function.intercept.base, new_value)
        assert np.allclose(T.function.intercept.modulated, new_value)

        assert np.allclose(T.integration_rate.base, new_value)
        assert np.allclose(T.integration_rate.modulated, new_value)

        assert np.allclose(T.noise.base, new_value)
        assert np.allclose(T.noise.modulated, new_value)
コード例 #26
0
 def test_runtime_param_error(self):
     T = TransferMechanism()
     with pytest.raises(ComponentError) as error_text:
         T.execute(runtime_params={"glunfump": 10.0}, input=2.0)
     assert ("Invalid specification in runtime_params arg for TransferMechanism" in str(error_text.value) and
             "'glunfump'" in str(error_text.value))
コード例 #27
0
    def test_configurable_params(self):
        old_value = 0.2
        new_value = 0.7
        T = TransferMechanism(function=Linear(slope=old_value,
                                              intercept=old_value),
                              noise=old_value,
                              integration_rate=old_value)

        # SLOPE - - - - - - - -

        assert np.allclose(T.user_params["function_params"]["slope"],
                           old_value)
        assert np.allclose(T.function.slope, old_value)
        assert np.allclose(T.mod_slope, old_value)

        T.function.slope = new_value

        # KAM changed 3/2/18 --
        # function_params looks at parameter state value, so this will not update until next execution
        assert np.allclose(T.user_params["function_params"]["slope"],
                           old_value)
        assert np.allclose(T.function.slope, new_value)
        assert np.allclose(T.mod_slope, old_value)

        # INTERCEPT - - - - - - - -

        assert np.allclose(T.user_params["function_params"]["intercept"],
                           old_value)
        assert np.allclose(T.function.intercept, old_value)
        assert np.allclose(T.mod_intercept, old_value)

        T.function.intercept = new_value

        # KAM changed 3/2/18 --
        # function_params looks at parameter state value, so this will not update until next execution
        assert np.allclose(T.user_params["function_params"]["intercept"],
                           old_value)
        assert np.allclose(T.function.intercept, new_value)
        assert np.allclose(T.mod_intercept, old_value)

        # SMOOTHING FACTOR - - - - - - - -

        assert np.allclose(T.user_params["integration_rate"], old_value)
        assert np.allclose(T.integration_rate, old_value)
        assert np.allclose(T.mod_integration_rate, old_value)

        T.integration_rate = new_value

        # KAM changed 3/2/18 --
        # function_params looks at parameter state value, so this will not update until next execution
        assert np.allclose(T.user_params["integration_rate"], old_value)
        assert np.allclose(T.integration_rate, new_value)
        assert np.allclose(T.mod_integration_rate, old_value)

        # NOISE - - - - - - - -

        assert np.allclose(T.user_params["noise"], old_value)
        assert np.allclose(T.noise, old_value)
        assert np.allclose(T.mod_noise, old_value)

        T.noise = new_value

        # KAM changed 3/2/18 --
        # function_params looks at parameter state value, so this will not update until next execution
        assert np.allclose(T.user_params["noise"], old_value)
        assert np.allclose(T.noise, new_value)
        assert np.allclose(T.mod_noise, old_value)

        T.execute(1.0)

        assert np.allclose(T.user_params["function_params"]["slope"],
                           new_value)
        assert np.allclose(T.function.slope, new_value)
        assert np.allclose(T.mod_slope, new_value)

        assert np.allclose(T.user_params["function_params"]["intercept"],
                           new_value)
        assert np.allclose(T.function.intercept, new_value)
        assert np.allclose(T.mod_intercept, new_value)

        assert np.allclose(T.user_params["integration_rate"], new_value)
        assert np.allclose(T.integration_rate, new_value)
        assert np.allclose(T.mod_integration_rate, new_value)

        assert np.allclose(T.user_params["noise"], new_value)
        assert np.allclose(T.noise, new_value)
        assert np.allclose(T.mod_noise, new_value)