def test_variableview_calculations(): # Check that you can directly calculate with "variable views" G = NeuronGroup(10, '''x : 1 y : volt''') G.x = np.arange(10) G.y = np.arange(10)[::-1]*mV assert_allclose(G.x * G.y, np.arange(10)*np.arange(10)[::-1]*mV) assert_allclose(-G.x, -np.arange(10)) assert_allclose(-G.y, -np.arange(10)[::-1]*mV) assert_allclose(3 * G.x, 3 * np.arange(10)) assert_allclose(3 * G.y, 3 *np.arange(10)[::-1]*mV) assert_allclose(G.x * 3, 3 * np.arange(10)) assert_allclose(G.y * 3, 3 *np.arange(10)[::-1]*mV) assert_allclose(G.x / 2.0, np.arange(10)/2.0) assert_allclose(G.y / 2, np.arange(10)[::-1]*mV/2) assert_allclose(G.x + 2, 2 + np.arange(10)) assert_allclose(G.y + 2*mV, 2*mV + np.arange(10)[::-1]*mV) assert_allclose(2 + G.x, 2 + np.arange(10)) assert_allclose(2*mV + G.y, 2*mV + np.arange(10)[::-1]*mV) assert_allclose(G.x - 2, np.arange(10) - 2) assert_allclose(G.y - 2*mV, np.arange(10)[::-1]*mV - 2*mV) assert_allclose(2 - G.x, 2 - np.arange(10)) assert_allclose(2*mV - G.y, 2*mV - np.arange(10)[::-1]*mV) # incorrect units assert_raises(DimensionMismatchError, lambda: G.x + G.y) assert_raises(DimensionMismatchError, lambda: G.x[:] + G.y) assert_raises(DimensionMismatchError, lambda: G.x + G.y[:]) assert_raises(DimensionMismatchError, lambda: G.x + 3*mV) assert_raises(DimensionMismatchError, lambda: 3*mV + G.x) assert_raises(DimensionMismatchError, lambda: G.y + 3) assert_raises(DimensionMismatchError, lambda: 3 + G.y)
def test_linked_subgroup2(): ''' Test linking a variable from a subgroup with indexing ''' G1 = NeuronGroup(10, 'x : 1') G1.x = np.arange(10) * 0.1 G2 = G1[3:8] G3 = NeuronGroup(10, 'y:1 (linked)') G3.y = linked_var(G2.x, index=np.arange(5).repeat(2)) assert_equal(G3.y[:], (np.arange(5)+3).repeat(2)*0.1)
def test_linked_subgroup(): ''' Test linking a variable from a subgroup ''' G1 = NeuronGroup(10, 'x : 1') G1.x = np.arange(10) * 0.1 G2 = G1[3:8] G3 = NeuronGroup(5, 'y:1 (linked)') G3.y = linked_var(G2.x) assert_equal(G3.y[:], (np.arange(5)+3)*0.1)
def test_linked_variable_indexed(): ''' Test linking a variable with an index specified as an array ''' G = NeuronGroup(10, '''x : 1 y : 1 (linked)''') G.x = np.arange(10)*0.1 G.y = linked_var(G.x, index=np.arange(10)[::-1]) # G.y should refer to an inverted version of G.x assert_equal(G.y[:], np.arange(10)[::-1]*0.1)
def test_linked_var_in_reset_size_1(): G1 = NeuronGroup(1, 'x:1') G2 = NeuronGroup(1, '''x_linked : 1 (linked) y:1''', threshold='y>1', reset='y=0; x_linked += 1') G2.x_linked = linked_var(G1, 'x') G2.y = 1.1 # In this context, x_linked should not be considered as a scalar variable # and therefore the reset statement should be allowed run(3*defaultclock.dt) assert_equal(G1.x[:], 1)
def test_linked_double_linked4(): ''' Linked to a linked variable, both use indices ''' G1 = NeuronGroup(5, 'x : 1') G2 = NeuronGroup(10, 'y : 1 (linked)') G2.y = linked_var(G1.x, index=np.arange(5).repeat(2)) G3 = NeuronGroup(10, 'z: 1 (linked)') G3.z = linked_var(G2.y, index=np.arange(10)[::-1]) G1.x = np.arange(5)*0.1 assert_equal(G3.z[:], np.arange(5).repeat(2)[::-1]*0.1)
def test_linked_double_linked3(): ''' Linked to a linked variable, first with indices, second without indices ''' G1 = NeuronGroup(5, 'x : 1') G2 = NeuronGroup(10, 'y : 1 (linked)') G2.y = linked_var(G1.x, index=np.arange(5).repeat(2)) G3 = NeuronGroup(10, 'z: 1 (linked)') G3.z = linked_var(G2.y) G1.x = np.arange(5)*0.1 assert_equal(G3.z[:], np.arange(5).repeat(2)*0.1)
def test_linked_double_linked1(): ''' Linked to a linked variable, without indices ''' G1 = NeuronGroup(10, 'x : 1') G2 = NeuronGroup(10, 'y : 1 (linked)') G2.y = linked_var(G1.x) G3 = NeuronGroup(10, 'z: 1 (linked)') G3.z = linked_var(G2.y) G1.x = np.arange(10) assert_equal(G3.z[:], np.arange(10))
def test_linked_variable_scalar(): ''' Test linked variable from a size 1 group. ''' G1 = NeuronGroup(1, 'dx/dt = -x / (10*ms) : 1') G2 = NeuronGroup(10, '''dy/dt = (-y + x) / (20*ms) : 1 x : 1 (linked)''') G1.x = 1 G2.y = np.linspace(0, 1, 10) G2.x = linked_var(G1.x) mon = StateMonitor(G2, 'y', record=True) net = Network(G1, G2, mon) net.run(10*ms)
def test_linked_var_in_reset_incorrect(): # Raise an error if a scalar variable (linked variable from a group of size # 1 is set in a reset statement of a group with size > 1) G1 = NeuronGroup(1, 'x:1') G2 = NeuronGroup(2, '''x_linked : 1 (linked) y:1''', threshold='y>1', reset='y=0; x_linked += 1') G2.x_linked = linked_var(G1, 'x') G2.y = 1.1 net = Network(G1, G2) # It is not well-defined what x_linked +=1 means in this context # (as for any other shared variable) assert_raises(SyntaxError, lambda: net.run(0*ms))
def test_linked_variable_scalar(): ''' Test linked variable from a size 1 group. ''' G1 = NeuronGroup(1, 'dx/dt = -x / (10*ms) : 1') G2 = NeuronGroup(10, '''dy/dt = (-y + x) / (20*ms) : 1 x : 1 (linked)''') G1.x = 1 G2.y = np.linspace(0, 1, 10) G2.x = linked_var(G1.x) mon = StateMonitor(G2, 'y', record=True) # We don't test anything for now, except that it runs without raising an # error run(10*ms) # Make sure that printing the variable values works assert len(str(G2.x)) > 0 assert len(repr(G2.x)) > 0 assert len(str(G2.x[:])) > 0 assert len(repr(G2.x[:])) > 0