コード例 #1
0
 def test_transfer_mech_inputs_mismatched_with_default_shorter(self):
     with pytest.raises(MechanismError) as error_text:
         T = TransferMechanism(name='T',
                               default_variable=[0, 0, 0, 0, 0, 0],
                               integrator_mode=True)
         T.execute([1, 2, 3, 4, 5])
     assert "does not match required length" in str(error_text.value)
コード例 #2
0
    def test_runtime_params_reset_isolated(self):

        T = TransferMechanism()

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

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

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

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

        # Intercept attr NOT affected by runtime params reset
        assert T.function_object.intercept == 2.0
        assert T.value == 4.0
コード例 #3
0
 def test_transfer_mech_inputs_list_of_strings(self):
     with pytest.raises(UtilitiesError) as error_text:
         T = TransferMechanism(name='T',
                               default_variable=[0, 0, 0, 0],
                               integrator_mode=True)
         T.execute(["one", "two", "three", "four"])
     assert "has non-numeric entries" in str(error_text.value)
コード例 #4
0
 def test_projection_tuple_with_matrix_spec(self):
     R2 = TransferMechanism(size=3)
     T = TransferMechanism(size=2, input_states=[(R2, None, None, np.zeros((3, 2)))])
     np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(T.input_state.path_afferents[0].sender.instance_defaults.variable) == 3
     assert len(T.input_state.instance_defaults.variable) == 2
     T.execute()
コード例 #5
0
 def test_2_item_tuple_value_for_first_item(self):
     R2 = TransferMechanism(size=3)
     T = TransferMechanism(input_states=[([0,0], R2)])
     np.testing.assert_array_equal(T.instance_defaults.variable, np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(T.input_state.path_afferents[0].sender.instance_defaults.variable) == 3
     assert len(T.input_state.instance_defaults.variable) == 2
     T.execute()
コード例 #6
0
 def test_mech_spec_list(self):
     R1 = TransferMechanism(output_states=['FIRST', 'SECOND'])
     T = TransferMechanism(default_variable=[[0]], input_states=[R1])
     np.testing.assert_array_equal(T.instance_defaults.variable,
                                   np.array([[0]]))
     assert len(T.input_states) == 1
     assert T.input_state.path_afferents[0].sender == R1.output_state
     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.instance_defaults.variable, np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(T.input_state.path_afferents[0].sender.instance_defaults.variable) == 3
     assert T.input_state.socket_width == 2
     T.execute()
コード例 #8
0
    def test_reinitialize_not_integrator(self):

        with pytest.raises(MechanismError) as err_txt:
            T_not_integrator = TransferMechanism()
            T_not_integrator.execute(1.0)
            T_not_integrator.reinitialize(0.0)
        assert "not allowed because this Mechanism is not stateful." in str(err_txt) \
               and "try setting the integrator_mode argument to True." in str(err_txt)
コード例 #9
0
 def test_transfer_mech_reduce_fun(self):
     with pytest.raises(TransferError) as error_text:
         T = TransferMechanism(name='T',
                               default_variable=[0, 0, 0, 0],
                               function=Reduce(),
                               time_constant=1.0,
                               integrator_mode=True)
         T.execute([0, 0, 0, 0])
     assert "must be a TRANSFER FUNCTION TYPE" in str(error_text.value)
コード例 #10
0
 def test_output_state_spec_standalone(self):
     R1 = TransferMechanism(output_states=['FIRST', 'SECOND'])
     T = TransferMechanism(default_variable=[0],
                           input_states=R1.output_states['FIRST'])
     np.testing.assert_array_equal(T.instance_defaults.variable,
                                   np.array([[0]]))
     assert len(T.input_states) == 1
     assert T.input_states.names[0] == 'InputState-0'
     T.input_state.path_afferents[0].sender == R1.output_state
     T.execute()
コード例 #11
0
 def test_transfer_mech_time_constant_0_8(self):
     T = TransferMechanism(name='T',
                           default_variable=[0, 0, 0, 0],
                           function=Linear(),
                           time_constant=0.8,
                           integrator_mode=True)
     val = T.execute([1, 1, 1, 1])
     assert np.allclose(val, [[0.8, 0.8, 0.8, 0.8]])
     val = T.execute([1, 1, 1, 1])
     assert np.allclose(val, [[0.96, 0.96, 0.96, 0.96]])
コード例 #12
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))
コード例 #13
0
 def test_projection_in_tuple(self):
     R2 = TransferMechanism(size=3)
     P = MappingProjection(sender=R2)
     T = TransferMechanism(size=2, input_states=[(R2, None, None, P)])
     np.testing.assert_array_equal(T.instance_defaults.variable,
                                   np.array([[0, 0]]))
     assert len(T.input_states) == 1
     assert len(T.input_state.path_afferents[0].sender.instance_defaults.
                variable) == 3
     assert len(T.input_state.instance_defaults.variable) == 2
     T.execute()
コード例 #14
0
 def test_transfer_mech_normal_fun(self):
     with pytest.raises(TransferError) as error_text:
         T = TransferMechanism(
             name='T',
             default_variable=[0, 0, 0, 0],
             function=NormalDist(),
             integration_rate=1.0,
             integrator_mode=True
         )
         T.execute([0, 0, 0, 0])
     assert "must be a TRANSFER FUNCTION TYPE" in str(error_text.value)
コード例 #15
0
 def test_transfer_mech_mismatched_shape_noise(self):
     with pytest.raises(MechanismError) as error_text:
         T = TransferMechanism(name='T',
                               default_variable=[0, 0],
                               function=Linear(),
                               noise=[5.0, 5.0, 5.0],
                               time_constant=0.1,
                               integrator_mode=True)
         T.execute()
     assert 'Noise parameter' in str(
         error_text.value) and "does not match default variable" in str(
             error_text.value)
コード例 #16
0
 def test_transfer_mech_integration_rate_0_8(self):
     T = TransferMechanism(
         name='T',
         default_variable=[0 for i in range(VECTOR_SIZE)],
         function=Linear(),
         integration_rate=0.8,
         integrator_mode=True
     )
     val = T.execute([1 for i in range(VECTOR_SIZE)])
     assert np.allclose(val, [[0.8 for i in range(VECTOR_SIZE)]])
     val = T.execute([1 for i in range(VECTOR_SIZE)])
     assert np.allclose(val, [[0.96 for i in range(VECTOR_SIZE)]])
コード例 #17
0
 def test_transfer_mech_time_constant_0_8_initial_0_5(self):
     T = TransferMechanism(name='T',
                           default_variable=[0, 0, 0, 0],
                           function=Linear(),
                           time_constant=0.8,
                           initial_value=np.array([[.5, .5, .5, .5]]),
                           integrator_mode=True)
     val = T.execute([1, 1, 1, 1])
     assert np.allclose(val, [[0.9, 0.9, 0.9, 0.9]])
     T.noise = 10
     val = T.execute([1, 2, -3, 0])
     assert np.allclose(val, [[10.98, 11.78, 7.779999999999999, 10.18]
                              ])  # testing noise changes to an integrator
コード例 #18
0
    def test_default_variable_override_mech_list(self):

        R2 = TransferMechanism(size=3)

        # default_variable override of OutputState.value
        T = TransferMechanism(default_variable=[[0, 0]], input_states=[R2])
        np.testing.assert_array_equal(T.instance_defaults.variable,
                                      np.array([[0, 0]]))
        assert len(T.input_states) == 1
        assert len(T.input_state.path_afferents[0].sender.instance_defaults.
                   variable) == 3
        assert len(T.input_state.instance_defaults.variable) == 2
        T.execute()
コード例 #19
0
 def test_transfer_mech_integration_rate_0_8_list(self):
     with pytest.raises(TransferError) as error_text:
         T = TransferMechanism(
             name='T',
             default_variable=[0, 0, 0, 0],
             function=Linear(),
             integration_rate=[0.8, 0.8, 0.8, 0.8],
             integrator_mode=True
         )
         T.execute([1, 1, 1, 1])
     assert (
         "integration_rate parameter" in str(error_text.value)
         and "must be a float" in str(error_text.value)
     )
コード例 #20
0
 def test_transfer_mech_size_int_inputs_floats(self):
     T = TransferMechanism(
         name='T',
         size=VECTOR_SIZE
     )
     val = T.execute([10.0 for i in range(VECTOR_SIZE)])
     assert np.allclose(val, [[10.0 for i in range(VECTOR_SIZE)]])
コード例 #21
0
 def test_multiple_output_states_for_multiple_input_states(self):
     T = TransferMechanism(input_states=['a', 'b', 'c'])
     val = T.execute([[1], [2], [3]])
     assert len(T.variable) == 3
     assert all(a == b for a, b in zip(val, [[1.], [2.], [3.]]))
     assert len(T.output_states) == 3
     assert all(a == b for a, b in zip(T.output_values, val))
コード例 #22
0
 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]])
コード例 #23
0
 def test_output_state_spec_list_two_items(self):
     R1 = TransferMechanism(output_states=['FIRST', 'SECOND'])
     T = TransferMechanism(default_variable=[[0], [0]],
                           input_states=[
                               R1.output_states['FIRST'],
                               R1.output_states['SECOND']
                           ])
     np.testing.assert_array_equal(T.instance_defaults.variable,
                                   np.array([[0], [0]]))
     assert len(T.input_states) == 2
     assert T.input_states.names[0] == 'InputState-0'
     assert T.input_states.names[1] == 'InputState-1'
     for input_state in T.input_states:
         for projection in input_state.path_afferents:
             assert projection.sender.owner is R1
     T.execute()
コード例 #24
0
    def test_transfer_mech_inputs_list_of_floats(self):

        T = TransferMechanism(name='T',
                              default_variable=[0, 0, 0, 0],
                              integrator_mode=True)
        val = T.execute([10.0, 10.0, 10.0, 10.0])
        assert np.allclose(val, [[10.0, 10.0, 10.0, 10.0]])
コード例 #25
0
 def test_transfer_mech_size_float_inputs_floats(self):
     T = TransferMechanism(
         name='T',
         size=4.0
     )
     val = T.execute([10.0, 10.0, 10.0, 10.0])
     assert np.allclose(val, [[10.0, 10.0, 10.0, 10.0]])
コード例 #26
0
 def test_projection_in_specification_dict(self):
     R1 = TransferMechanism(output_states=['FIRST', 'SECOND'])
     T = TransferMechanism(input_states=[{
         NAME:
         'My InputState with Two Projections',
         PROJECTIONS:
         [R1.output_states['FIRST'], R1.output_states['SECOND']]
     }])
     np.testing.assert_array_equal(T.instance_defaults.variable,
                                   np.array([[0]]))
     assert len(T.input_states) == 1
     assert T.input_state.name == 'My InputState with Two Projections'
     for input_state in T.input_states:
         for projection in input_state.path_afferents:
             assert projection.sender.owner is R1
     T.execute()
コード例 #27
0
 def test_transfer_mech_var_1D_size_wrong_2(self):
     T = TransferMechanism(name='T',
                           default_variable=[1, 2, 3, 4],
                           size=[2, 3, 4])
     assert len(T.instance_defaults.variable) == 1 and (
         T.instance_defaults.variable[0] == [1, 2, 3, 4]).all()
     val = T.execute([10.0, 10.0, 10.0, 10.0])
     assert np.allclose(val, [[10.0, 10.0, 10.0, 10.0]])
コード例 #28
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]])
コード例 #29
0
    def test_transfer_mech_softmax_fun(self):

        T = TransferMechanism(name='T',
                              default_variable=[0, 0, 0, 0],
                              function=SoftMax(),
                              time_constant=1.0,
                              integrator_mode=True)
        val = T.execute([0, 0, 0, 0])
        assert np.allclose(val, [[0.25, 0.25, 0.25, 0.25]])
コード例 #30
0
    def test_transfer_mech_exponential_fun(self):

        T = TransferMechanism(name='T',
                              default_variable=[0, 0, 0, 0],
                              function=Exponential(),
                              time_constant=1.0,
                              integrator_mode=True)
        val = T.execute([0, 0, 0, 0])
        assert np.allclose(val, [[1.0, 1.0, 1.0, 1.0]])