def test_recurrent_mech_inputs_list_of_strings(self): with pytest.raises(UtilitiesError) as error_text: R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], integrator_mode=True) R.execute(["one", "two", "three", "four"]) assert "has non-numeric entries" in str(error_text.value)
def test_recurrent_mech_inputs_list_of_ints(self): R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0]) val = R.execute([10, 12, 0, -1]) np.testing.assert_allclose(val, [[10.0, 12.0, 0, -1]]) val = R.execute([1, 2, 3, 0]) np.testing.assert_allclose( val, [[1, 2, 3, 0]] ) # because recurrent projection is not used when executing: mech is reset each time
def test_recurrent_mech_reduce_fun(self): with pytest.raises(TransferError) as error_text: R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Reduce(), time_constant=1.0, integrator_mode=True) R.execute([0, 0, 0, 0]) assert "must be a TRANSFER FUNCTION TYPE" in str(error_text.value)
def test_recurrent_mech_reinforcement_fun(self): with pytest.raises(TransferError) as error_text: R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Reinforcement(), smoothing_factor=1.0, integrator_mode=True) R.execute([0, 0, 0, 0]) assert "must be a TRANSFER FUNCTION TYPE" in str(error_text.value)
def test_recurrent_mech_time_constant_0_8(self): R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0.8, integrator_mode=True) val = R.execute([1, 1, 1, 1]) np.testing.assert_allclose(val, [[0.8, 0.8, 0.8, 0.8]]) val = R.execute([1, 1, 1, 1]) np.testing.assert_allclose(val, [[.96, .96, .96, .96]])
def test_recurrent_mech_time_constant_0_8_initial_0_5(self): R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0.8, initial_value=np.array( [[0.5, 0.5, 0.5, 0.5]]), integrator_mode=True) val = R.execute([1, 1, 1, 1]) np.testing.assert_allclose(val, [[0.9, 0.9, 0.9, 0.9]]) val = R.execute([1, 2, 3, 4]) np.testing.assert_allclose( val, [[.98, 1.78, 2.5800000000000005, 3.3800000000000003] ]) # due to inevitable floating point errors
def test_recurrent_mech_function_psyneulink(self): a = Logistic(gain=2, offset=1) R = RecurrentTransferMechanism(name='R', size=7, function=a) val = R.execute(np.zeros(7)) np.testing.assert_allclose(val, [np.full(7, 0.2689414213699951)])
def test_recurrent_mech_function_logistic(self): R = RecurrentTransferMechanism(name='R', size=10, function=Logistic(gain=2, offset=1)) val = R.execute(np.ones(10)) np.testing.assert_allclose(val, [np.full(10, 0.7310585786300049)])
def test_recurrent_mech_matrix_auto_hetero_spec_size_4(self): R = RecurrentTransferMechanism(name='R', size=4, auto=2.2, hetero=-3) val = R.execute([10, 10, 10, 10]) np.testing.assert_allclose(val, [[10., 10., 10., 10.]]) np.testing.assert_allclose(R.matrix, [[2.2, -3, -3, -3], [-3, 2.2, -3, -3], [-3, -3, 2.2, -3], [-3, -3, -3, 2.2]]) assert isinstance(R.matrix, np.ndarray)
def test_recurrent_mech_time_constant_0_8_initial_1_8(self): R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0.8, initial_value=np.array( [[1.8, 1.8, 1.8, 1.8]]), integrator_mode=True) val = R.execute([1, 1, 1, 1]) np.testing.assert_allclose(val, [[1.16, 1.16, 1.16, 1.16]]) val = R.execute([2, 2, 2, 2]) np.testing.assert_allclose(val, [[1.832, 1.832, 1.832, 1.832]]) val = R.execute([-4, -3, 0, 1]) np.testing.assert_allclose(val, [[ -2.8336, -2.0336000000000003, .36639999999999995, 1.1663999999999999 ]])
def test_clip_2d_array(self): R = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], clip=[-2.0, 2.0]) assert np.allclose( R.execute([[-5.0, -1.0, 5.0], [5.0, -5.0, 1.0], [1.0, 5.0, 5.0]]), [[-2.0, -1.0, 2.0], [2.0, -2.0, 1.0], [1.0, 2.0, 2.0]])
def test_recurrent_mech_hetero_matrix_matrix_spec(self): R = RecurrentTransferMechanism(name='R', size=4, hetero=np.array([[-4, -3, -2, -1]] * 4), matrix=[[1, 2, 3, 4]] * 4) val = R.execute([1, 2, 3, 4]) np.testing.assert_allclose(val, [[1., 2., 3., 4.]]) np.testing.assert_allclose(R.matrix, [[1, -3, -2, -1], [-4, 2, -2, -1], [-4, -3, 3, -1], [-4, -3, -2, 4]])
def test_recurrent_mech_auto_array_matrix_spec(self): R = RecurrentTransferMechanism(name='R', size=4, auto=[1.1, 2.2, 3.3, 4.4], matrix=[[1, 2, 3, 4]] * 4) val = R.execute([10, 11, 12, 13]) np.testing.assert_allclose(val, [[10., 11., 12., 13.]]) np.testing.assert_allclose( R.matrix, [[1.1, 2, 3, 4], [1, 2.2, 3, 4], [1, 2, 3.3, 4], [1, 2, 3, 4.4]])
def test_recurrent_mech_auto_hetero_matrix_spec_v3(self): R = RecurrentTransferMechanism(name='R', size=4, auto=[3], hetero=2, matrix=[[1, 2, 3, 4]] * 4) val = R.execute([1, 2, 3, 4]) np.testing.assert_allclose(val, [[1., 2., 3., 4.]]) np.testing.assert_allclose( R.matrix, [[3, 2, 2, 2], [2, 3, 2, 2], [2, 2, 3, 2], [2, 2, 2, 3]])
def test_recurrent_mech_matrix_keyword_spec(self): for m in MATRIX_KEYWORD_VALUES: if m == RANDOM_CONNECTIVITY_MATRIX: continue R = RecurrentTransferMechanism(name='R', size=4, matrix=m) val = R.execute([10, 10, 10, 10]) np.testing.assert_allclose(val, [[10., 10., 10., 10.]]) np.testing.assert_allclose(R.recurrent_projection.matrix, get_matrix(m, R.size[0], R.size[0]))
def test_recurrent_mech_hetero_float_matrix_spec(self): # hetero should override off-diagonal only R = RecurrentTransferMechanism(name='R', size=4, hetero=-2.2, matrix=[[1, 2, 3, 4]] * 4) val = R.execute([1, 2, 3, 4]) np.testing.assert_allclose(val, [[1., 2., 3., 4.]]) np.testing.assert_allclose( R.matrix, [[1, -2.2, -2.2, -2.2], [-2.2, 2, -2.2, -2.2], [-2.2, -2.2, 3, -2.2], [-2.2, -2.2, -2.2, 4]])
def test_recurrent_mech_auto_matrix_spec(self): # auto should override the diagonal only R = RecurrentTransferMechanism(name='R', size=4, auto=2.2, matrix=[[1, 2, 3, 4]] * 4) val = R.execute([10, 11, 12, 13]) np.testing.assert_allclose(val, [[10., 11., 12., 13.]]) np.testing.assert_allclose( R.matrix, [[2.2, 2, 3, 4], [1, 2.2, 3, 4], [1, 2, 2.2, 4], [1, 2, 3, 2.2]])
def test_recurrent_mech_time_constant_0_8_initial_1_2(self): R = RecurrentTransferMechanism(name='R', default_variable=[0, 0, 0, 0], function=Linear(), time_constant=0.8, initial_value=np.array([[-1, 1, -2, 2]]), integrator_mode=True) val = R.execute([3, 2, 1, 0]) np.testing.assert_allclose( val, [[2.2, 1.8, .40000000000000013, .3999999999999999]])
def test_recurrent_mech_matrix_hetero_spec(self): R = RecurrentTransferMechanism(name='R', size=3, hetero=-1) # (7/28/17 CW) these numbers assume that execute() leaves its value in the outputState of the mechanism: if # the behavior of execute() changes, feel free to change these numbers val = R.execute([-1, -2, -3]) np.testing.assert_allclose(val, [[-1, -2, -3]]) assert isinstance(R.matrix, np.ndarray) np.testing.assert_allclose(R.matrix, [[1, -1, -1], [-1, 1, -1], [-1, -1, 1]]) np.testing.assert_allclose( run_twice_in_system(R, [1, 2, 3], [10, 11, 12]), [8, 7, 6])
def test_recurrent_mech_auto_hetero_matrix_spec_v1(self): # auto and hetero should override matrix R = RecurrentTransferMechanism(name='R', size=4, auto=[1, 3, 5, 7], hetero=np.array([[-4, -3, -2, -1]] * 4), matrix=[[1, 2, 3, 4]] * 4) val = R.execute([1, 2, 3, 4]) np.testing.assert_allclose(val, [[1., 2., 3., 4.]]) np.testing.assert_allclose(R.matrix, [[1, -3, -2, -1], [-4, 3, -2, -1], [-4, -3, 5, -1], [-4, -3, -2, 7]])
def test_recurrent_mech_matrix_auto_hetero_matrix_spec(self): # when auto, hetero, and matrix are all specified, auto and hetero should take precedence R = RecurrentTransferMechanism(name='R', size=4, auto=2.2, hetero=-3, matrix=[[1, 2, 3, 4]] * 4) val = R.execute([10, 10, 10, 10]) np.testing.assert_allclose(val, [[10., 10., 10., 10.]]) np.testing.assert_allclose(R.matrix, [[2.2, -3, -3, -3], [-3, 2.2, -3, -3], [-3, -3, 2.2, -3], [-3, -3, -3, 2.2]]) assert isinstance(R.matrix, np.ndarray)
def test_recurrent_mech_matrix_other_spec(self): specs = [ np.matrix('1 2; 3 4'), np.array([[1, 2], [3, 4]]), [[1, 2], [3, 4]], '1 2; 3 4' ] for m in specs: R = RecurrentTransferMechanism(name='R', size=2, matrix=m) val = R.execute([10, 10]) np.testing.assert_allclose(val, [[10., 10.]]) assert isinstance(R.matrix, np.ndarray) np.testing.assert_allclose(R.matrix, [[1, 2], [3, 4]]) np.testing.assert_allclose(R.recurrent_projection.matrix, [[1, 2], [3, 4]]) assert isinstance(R.recurrent_projection.matrix, np.ndarray)
def test_recurrent_mech_matrix_hetero_spec(self): R = RecurrentTransferMechanism(name='R', size=3, hetero=-1) # (7/28/17 CW) these numbers assume that execute() leaves its value in the outputState of the mechanism: if # the behavior of execute() changes, feel free to change these numbers val = R.execute([-1, -2, -3]) np.testing.assert_allclose(val, [[-1, -2, -3]]) assert isinstance(R.matrix, np.ndarray) np.testing.assert_allclose(R.matrix, [[0, -1, -1], [-1, 0, -1], [-1, -1, 0]]) # Execution 1: # Recurrent input = [5, 4, 3] | New input = [1, 2, 3] | Total input = [6, 6, 6] # Output 1 = [6, 6, 6] # Execution 2: # Recurrent input =[-12, -12, -12] | New input = [10, 11, 12] | Total input = [-2, -1, 0] # Output 2 = [-2, -1, 0] np.testing.assert_allclose( run_twice_in_system(R, [1, 2, 3], [10, 11, 12]), [-2., -1., 0.])
def test_clip_float(self): R = RecurrentTransferMechanism(clip=[-2.0, 2.0]) assert np.allclose(R.execute(3.0), 2.0) assert np.allclose(R.execute(-3.0), -2.0)
def test_clip_array(self): R = RecurrentTransferMechanism(default_variable=[[0.0, 0.0, 0.0]], clip=[-2.0, 2.0]) assert np.allclose(R.execute([3.0, 0.0, -3.0]), [2.0, 0.0, -2.0])
def test_recurrent_mech_inputs_list_of_floats(self): R = RecurrentTransferMechanism(name='R', size=4) val = R.execute([10.0, 10.0, 10.0, 10.0]) np.testing.assert_allclose(val, [[10.0, 10.0, 10.0, 10.0]])
def test_recurrent_mech_no_inputs(self): R = RecurrentTransferMechanism(name='R') np.testing.assert_allclose(R.instance_defaults.variable, [[0]]) val = R.execute([10]) np.testing.assert_allclose(val, [[10.]])
def test_recurrent_mech_inputs_mismatched_with_default_shorter(self): with pytest.raises(MechanismError) as error_text: R = RecurrentTransferMechanism(name='R', size=6) R.execute([1, 2, 3, 4, 5]) assert "does not match required length" in str(error_text.value)
def test_recurrent_mech_matrix_auto_hetero_spec_size_1(self): R = RecurrentTransferMechanism(name='R', size=1, auto=-2, hetero=4.4) val = R.execute([10]) np.testing.assert_allclose(val, [[10.]]) assert isinstance(R.matrix, np.ndarray) np.testing.assert_allclose(R.matrix, [[-2]])