def test_kwta_matrix_keyword_spec(self): for m in MATRIX_KEYWORD_VALUES: if m != RANDOM_CONNECTIVITY_MATRIX: K = KWTAMechanism(name='K', size=4, matrix=m) val = K.execute([10, 10, 10, 10]) assert (np.allclose(val, [[.5, .5, .5, .5]]))
def test_kwta_no_inputs(self): K = KWTAMechanism( name='K' ) assert(K.defaults.variable == [[0]]) val = K.execute([10]) assert(np.allclose(val, [[0.5]]))
def test_kwta_log_gain_offset(self): K = KWTAMechanism(name='K', size=2, function=Logistic(gain=-.2, offset=4), k_value=1) val = K.execute(input=[.1, -4]) assert np.allclose(val, [[0.017636340339722684, 0.039165722796764356]])
def test_kwta_inputs_list_of_strings(self): with pytest.raises(KWTAError) as error_text: K = KWTAMechanism( name='K', size = 4, ) K.execute(["one", "two", "three", "four"]) assert("which is not supported for KWTA" in str(error_text.value))
def test_kwta_linear_slope(self): K = KWTAMechanism(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]])
def test_recurrent_mech_inputs_mismatched_with_default_shorter(self): with pytest.raises(MechanismError) as error_text: K = KWTAMechanism( name='K', size=6 ) K.execute([1, 2, 3, 4, 5]) assert("does not match required length" in str(error_text.value))
def test_kwta_function_various_spec(self): specs = [ Logistic, Linear, Linear(slope=3), Logistic(gain=2, offset=-4.2) ] for s in specs: K = KWTAMechanism(name='K', size=5, function=s, k_value=4) K.execute([1, 2, 5, -2, .3])
def test_kwta_log_offset(self): K = KWTAMechanism( name='K', size=3, function=Logistic(offset=-.2), k_value=2 ) val = K.execute(input=[1, 2, 3]) assert np.allclose(val, [[0.425557483188341, 0.6681877721681662, 0.84553473491646523]])
def test_kwta_inputs_list_of_ints(self): K = KWTAMechanism( name='K', default_variable=[0, 0, 0, 0] ) val = K.execute([10, 12, 0, -1]) assert(np.allclose(val, [[0.9933071490757153, 0.9990889488055994, 0.0066928509242848554, 0.0024726231566347743]])) val = K.execute([1, 2, 3, 0]) assert(np.allclose(val, [[0.3775406687981454, 0.6224593312018546, 0.8175744761936437, 0.18242552380635635]]))
def test_kwta_log_gain(self): K = KWTAMechanism( name='K', size=3, function=Logistic(gain=2), k_value=2 ) val = K.execute(input = [1, 2, 3]) assert np.allclose(val, [[0.2689414213699951, 0.7310585786300049, 0.9525741268224334]])
def test_kwta_linear(self): # inhibition would be positive: so instead it is set to zero K = KWTAMechanism( name='K', threshold=3, size=3, k_value=2, function=Linear ) val = K.execute(input=[1, 3, 4]) assert np.allclose(val, [[1, 3, 4]])
def test_kwta_linear_system(self): K=KWTAMechanism( name='K', size=4, k_value=3, function=Linear )
def test_kwta_size_10_k_3_threshold_1(self): K = KWTAMechanism( name='K', size=10, k_value=3, threshold=1, ) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) kwta_input = {K: [[-1, -.5, 0, 0, 0, 1, 1, 2, 3, 3]]} print("") for i in range(20): c.run(inputs=kwta_input) print('\ntrial number', i) print('K.parameters.value.get(c): ', K.parameters.value.get(c)) assert np.allclose(K.parameters.value.get(c), [[0.012938850123312412, 0.022127587008877226, 0.039010157367582114, 0.039010157367582114, 0.039010157367582114, 0.19055156271846602, 0.19055156271846602, 0.969124504436019, 0.9895271824560731, 0.9895271824560731]]) kwta_input2 = {K: [0] * 10} print('\n\nturning to zero-inputs now:') for i in range(20): c.run(inputs=kwta_input2) print('\ntrial number', i) print('K.parameters.value.get(c): ', K.parameters.value.get(c)) assert np.allclose(K.parameters.value.get(c), [[0.13127237999481228, 0.13130057846907178, 0.1313653354768465, 0.1313653354768465, 0.1313653354768465, 0.5863768938723602, 0.5863768938723602, 0.8390251365605804, 0.8390251603214743, 0.8390251603214743]])
def test_kwta_matrix_hetero_spec(self): K = KWTAMechanism( name='K', size=3, hetero=-.5, ) assert(np.allclose(K.recurrent_projection.matrix, [[5, -.5, -.5], [-.5, 5, -.5], [-.5, -.5, 5]]))
def test_kwta_k_value_int_size_5(self): K = KWTAMechanism( name='K', size=5, k_value=3 ) assert K.k_value == 3
def test_kwta_matrix_auto_spec(self): K = KWTAMechanism( name='K', size=3, auto=-.5, ) assert(np.allclose(K.recurrent_projection.matrix, [[-.5, 0, 0], [0, -.5, 0], [0, 0, -.5]]))
def test_kwta_k_value_too_low(self): with pytest.raises(KWTAError) as error_text: K = KWTAMechanism( name='K', size=4, k_value=-5 ) assert "was larger than the total number of elements" in str(error_text.value)
def test_kwta_k_value_list(self): with pytest.raises(KWTAError) as error_text: K = KWTAMechanism( name='K', size=4, k_value=[1, 2] ) assert "must be a single number" in str(error_text.value)
def test_kwta_k_value_bad_float(self): with pytest.raises(KWTAError) as error_text: K = KWTAMechanism( name='K', size=4, k_value=2.5 ) assert "must be an integer, or between 0 and 1." in str(error_text.value)
def test_kwta_var_list_of_strings(self): with pytest.raises(ParameterError) as error_text: K = KWTAMechanism( name='K', default_variable=['a', 'b', 'c', 'd'], integrator_mode=True ) assert("non-numeric entries" in str(error_text.value))
def test_kwta_ratio_neg_1(self): with pytest.raises(KWTAError) as error_text: K = KWTAMechanism( name='K', size=4, ratio=-1 ) assert "must be between 0 and 1" in str(error_text.value)
def test_kwta_matrix_auto_hetero_spec(self): K = KWTAMechanism( name='K', size=4, auto=3, hetero=2 ) assert(np.allclose(K.recurrent_projection.matrix, [[3, 2, 2, 2], [2, 3, 2, 2], [2, 2, 3, 2], [2, 2, 2, 3]]))
def test_kwta_check_attrs(self): K = KWTAMechanism(name='K', size=3) np.testing.assert_allclose(K.value, K.defaults.value) assert (np.allclose(K.defaults.variable, [[0., 0., 0.]])) assert (K.size == [3]) assert (np.allclose(K.matrix.base, [[5, 0, 0], [0, 5, 0], [0, 0, 5]])) assert (K.recurrent_projection.sender is K.output_port) assert (K.recurrent_projection.receiver is K.input_port)
def test_kwta_threshold_float(self): K = KWTAMechanism(name='K', size=4, threshold=0.5) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 3, 3]}) assert np.allclose( K.parameters.value.get(c), [[0.2689414213699951, 0.5, 0.7310585786300049, 0.7310585786300049] ])
def test_kwta_threshold_int(self): K = KWTAMechanism(name='K', size=4, threshold=-1) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(c), [[ 0.07585818002124355, 0.18242552380635635, 0.3775406687981454, 0.6224593312018546 ]])
def test_kwta_k_value_empty_size_6(self): K = KWTAMechanism(name='K', size=6) assert K.k_value.base == 0.5 c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) c.run(inputs={K: [1, 2, 2, 3, 3, 4]}) assert np.allclose(K.parameters.value.get(c), [[ 0.18242552380635635, 0.3775406687981454, 0.3775406687981454, 0.6224593312018546, 0.6224593312018546, 0.8175744761936437 ]])
def test_kwta_threshold_float(self): K = KWTAMechanism(name='K', size=4, threshold=0.5) p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs) s = System(processes=[p], prefs=TestKWTARatio.simple_prefs) s.run(inputs={K: [1, 2, 3, 3]}) assert np.allclose( K.parameters.value.get(s), [[0.2689414213699951, 0.5, 0.7310585786300049, 0.7310585786300049] ])
def test_kwta_threshold_int(self): K = KWTAMechanism(name='K', size=4, threshold=-1) p = Process(pathway=[K], prefs=TestKWTAThreshold.simple_prefs) s = System(processes=[p], prefs=TestKWTAThreshold.simple_prefs) s.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[ 0.07585818002124355, 0.18242552380635635, 0.3775406687981454, 0.6224593312018546 ]])
def test_kwta_k_value_empty_size_4(self): K = KWTAMechanism( name='K', size=4 ) assert K.k_value == 0.5 p = Process(pathway=[K], prefs=TestKWTARatio.simple_prefs) s = System(processes=[p], prefs=TestKWTARatio.simple_prefs) s.run(inputs={K: [1, 2, 3, 4]}) assert np.allclose(K.parameters.value.get(s), [[0.18242552380635635, 0.3775406687981454, 0.6224593312018546, 0.8175744761936437]])
def test_kwta_average_k_1(self): K = KWTAMechanism(name='K', size=4, k_value=1, threshold=0, function=Linear, average_based=True) c = Composition(pathways=[K], prefs=TestKWTARatio.simple_prefs) kwta_input = {K: [[1, 2, 3, 4]]} c.run(inputs=kwta_input) assert np.allclose(K.parameters.value.get(c), [[-2, -1, 0, 1]])