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_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_triple_linked(): ''' Link to a linked variable that links to a linked variable, all use indices ''' G1 = NeuronGroup(2, 'a : 1') G2 = NeuronGroup(4, 'b : 1 (linked)') G2.b = linked_var(G1.a, index=np.arange(2).repeat(2)) G3 = NeuronGroup(4, 'c: 1 (linked)') G3.c = linked_var(G2.b, index=np.arange(4)[::-1]) G4 = NeuronGroup(8, 'd: 1 (linked)') G4.d = linked_var(G3.c, index=np.arange(4).repeat(2)) G1.a = np.arange(2)*0.1 assert_equal(G4.d[:], np.arange(2).repeat(2)[::-1].repeat(2)*0.1)
def test_linked_synapses(): ''' Test linking to a synaptic variable (should raise an error). ''' G = NeuronGroup(10, '') S = Synapses(G, G, 'w:1', connect=True) G2 = NeuronGroup(100, 'x : 1 (linked)') assert_raises(NotImplementedError, lambda: setattr(G2, 'x', linked_var(S, 'w')))
def test_linked_variable_repeat(): ''' Test a "repeat"-like connection between two groups of different size ''' G1 = NeuronGroup(5, 'w : 1') G2 = NeuronGroup(10, 'v : 1 (linked)') G2.v = linked_var(G1.w, index=np.arange(5).repeat(2)) G1.w = np.arange(5) * 0.1 assert_equal(G2.v[:], np.arange(5).repeat(2) * 0.1)
def test_linked_variable_incorrect(): ''' Test incorrect uses of linked variables. ''' G1 = NeuronGroup(10, '''x : volt y : 1''') G2 = NeuronGroup(20, '''x: volt''') G3 = NeuronGroup(10, '''l : volt (linked) not_linked : volt''') # incorrect unit assert_raises(DimensionMismatchError, lambda: setattr(G3, 'l', linked_var(G1.y))) # incorrect group size assert_raises(ValueError, lambda: setattr(G3, 'l', linked_var(G2.x))) # incorrect use of linked_var assert_raises(ValueError, lambda: setattr(G3, 'l', linked_var(G1.x, 'x'))) assert_raises(ValueError, lambda: setattr(G3, 'l', linked_var(G1))) # Not a linked variable assert_raises(TypeError, lambda: setattr(G3, 'not_linked', linked_var(G1.x)))
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_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_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_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) net = Network(G1, G2, mon) net.run(10*ms)
def test_linked_variable_indexed_incorrect(): ''' Test errors when providing incorrect index arrays ''' G = NeuronGroup(10, '''x : 1 y : 1 (linked)''') G.x = np.arange(10) * 0.1 assert_raises( TypeError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10) * 1.0))) assert_raises( TypeError, lambda: setattr( G, 'y', linked_var(G.x, index=np.arange(10).reshape(5, 2)))) assert_raises(TypeError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(5)))) assert_raises( ValueError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10) - 1))) assert_raises( ValueError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10) + 1)))
def test_linked_variable_indexed_incorrect(): ''' Test errors when providing incorrect index arrays ''' G = NeuronGroup(10, '''x : 1 y : 1 (linked)''') G.x = np.arange(10)*0.1 assert_raises(TypeError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10)*1.0))) assert_raises(TypeError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10).reshape(5, 2)))) assert_raises(TypeError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(5)))) assert_raises(ValueError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10)-1))) assert_raises(ValueError, lambda: setattr(G, 'y', linked_var(G.x, index=np.arange(10)+1)))
def test_linked_variable_correct(): ''' Test correct uses of linked variables. ''' tau = 10*ms G1 = NeuronGroup(10, 'dv/dt = -v / tau : volt') G1.v = np.linspace(0*mV, 20*mV, 10) G2 = NeuronGroup(10, 'v : volt (linked)') G2.v = linked_var(G1.v) mon1 = StateMonitor(G1, 'v', record=True) mon2 = StateMonitor(G2, 'v', record=True) net = Network(G1, G2, mon1, mon2) net.run(10*ms) assert_equal(mon1.v[:, :], mon2.v[:, :])
def test_linked_subexpression_2(): ''' Test a linked variable referring to a subexpression without indices ''' G = NeuronGroup(2, '''dv/dt = 100*Hz : 1 I = clip(v, 0, inf) : 1''', threshold='v>1', reset='v=0') G.v = [0, .5] G2 = NeuronGroup(2, '''I_l : 1 (linked) ''') G2.I_l = linked_var(G.I) mon1 = StateMonitor(G, 'I', record=True) mon = StateMonitor(G2, 'I_l', record=True) run(5*ms) assert all(mon[0].I_l == mon1[0].I) assert all(mon[1].I_l == mon1[1].I)
def test_linked_subexpression(): ''' Test a subexpression referring to a linked variable. ''' G = NeuronGroup(2, 'dv/dt = 100*Hz : 1', threshold='v>1', reset='v=0') G.v = [0, .5] G2 = NeuronGroup(10, '''I = clip(x, 0, inf) : 1 x : 1 (linked) ''') G2.x = linked_var(G.v, index=np.array([0, 1]).repeat(5)) mon = StateMonitor(G2, 'I', record=True) run(5*ms) # Due to the linking, the first 5 and the second 5 recorded I vectors should # be identical assert all((all(mon[i].I == mon[0].I) for i in xrange(5))) assert all((all(mon[i+5].I == mon[5].I) for i in xrange(5)))
def test_linked_variable_correct(): ''' Test correct uses of linked variables. ''' tau = 10*ms G1 = NeuronGroup(10, 'dv/dt = -v / tau : volt') G1.v = np.linspace(0*mV, 20*mV, 10) G2 = NeuronGroup(10, 'v : volt (linked)') G2.v = linked_var(G1.v) mon1 = StateMonitor(G1, 'v', record=True) mon2 = StateMonitor(G2, 'v', record=True) run(10*ms) assert_equal(mon1.v[:, :], mon2.v[:, :]) # Make sure that printing the variable values works assert len(str(G2.v)) > 0 assert len(repr(G2.v)) > 0 assert len(str(G2.v[:])) > 0 assert len(repr(G2.v[:])) > 0
def test_linked_variable_correct(): ''' Test correct uses of linked variables. ''' tau = 10*ms G1 = NeuronGroup(10, 'dv/dt = -v / tau : volt') G1.v = linspace(0*mV, 20*mV, 10) G2 = NeuronGroup(10, 'v : volt (linked)') G2.v = linked_var(G1.v) mon1 = StateMonitor(G1, 'v', record=True) mon2 = StateMonitor(G2, 'v', record=True) run(10*ms) assert_equal(mon1.v[:, :], mon2.v[:, :]) # Make sure that printing the variable values works assert len(str(G2.v)) > 0 assert len(repr(G2.v)) > 0 assert len(str(G2.v[:])) > 0 assert len(repr(G2.v[:])) > 0
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
def test_linked_subexpression_3(): ''' Test a linked variable referring to a subexpression with indices ''' G = NeuronGroup(2, '''dv/dt = 100*Hz : 1 I = clip(v, 0, inf) : 1''', threshold='v>1', reset='v=0') G.v = [0, .5] G2 = NeuronGroup(10, '''I_l : 1 (linked) ''') G2.I_l = linked_var(G.I, index=np.array([0, 1]).repeat(5)) mon1 = StateMonitor(G, 'I', record=True) mon = StateMonitor(G2, 'I_l', record=True) run(5*ms) # Due to the linking, the first 5 and the second 5 recorded I vectors should # refer to the assert all((all(mon[i].I_l == mon1[0].I) for i in xrange(5))) assert all((all(mon[i+5].I_l == mon1[1].I) for i in xrange(5)))
def test_linked_subexpression_synapse(): ''' Test a complicated setup (not unlikely when using brian hears) ''' G = NeuronGroup(2, 'dv/dt = 100*Hz : 1', threshold='v>1', reset='v=0') G.v = [0, .5] G2 = NeuronGroup(10, '''I = clip(x, 0, inf) : 1 x : 1 (linked) ''') # This will not be able to include references to `I` as `I_pre` etc., since # the indirect indexing would have to change depending on the synapses G2.x = linked_var(G.v, index=np.array([0, 1]).repeat(5)) S = Synapses(G2, G2, '') S.connect('i==j') assert 'I' not in S.variables assert 'I_pre' not in S.variables assert 'I_post' not in S.variables assert 'x' not in S.variables assert 'x_pre' not in S.variables assert 'x_post' not in S.variables