Ejemplo n.º 1
0
def test_schedule_warning():
    previous_device = get_device()
    from uuid import uuid4
    # TestDevice1 supports arbitrary schedules, TestDevice2 does not
    class TestDevice1(Device):
        # These functions are needed during the setup of the defaultclock
        def get_value(self, var):
            return np.array([0.0001])
        def add_array(self, var):
            pass
        def init_with_zeros(self, var, dtype):
            pass
        def fill_with_array(self, var, arr):
            pass
    class TestDevice2(TestDevice1):
        def __init__(self):
            super(TestDevice2, self).__init__()
            self.network_schedule = ['start', 'groups', 'synapses',
                                     'thresholds', 'resets', 'end']

    # Unique names are important for getting the warnings again for multiple
    # runs of the test suite
    name1 = 'testdevice_' + str(uuid4())
    name2 = 'testdevice_' + str(uuid4())
    all_devices[name1] = TestDevice1()
    all_devices[name2] = TestDevice2()

    set_device(name1)
    assert schedule_propagation_offset() == 0*ms
    net = Network()
    assert schedule_propagation_offset(net) == 0*ms

    # Any schedule should work
    net.schedule = list(reversed(net.schedule))
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 0, 'did not expect a warning'

    assert schedule_propagation_offset(net) == defaultclock.dt

    set_device(name2)
    assert schedule_propagation_offset() == defaultclock.dt

    # Using the correct schedule should work
    net.schedule = ['start', 'groups', 'synapses', 'thresholds', 'resets', 'end']
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 0, 'did not expect a warning'
    assert schedule_propagation_offset(net) == defaultclock.dt

    # Using another (e.g. the default) schedule should raise a warning
    net.schedule = None
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 1 and l[0][1].endswith('schedule_conflict')
    reset_device(previous_device)
Ejemplo n.º 2
0
def test_schedule_warning():
    previous_device = get_device()
    from uuid import uuid4
    # TestDevice1 supports arbitrary schedules, TestDevice2 does not
    class TestDevice1(Device):
        # These functions are needed during the setup of the defaultclock
        def get_value(self, var):
            return np.array([0.0001])
        def add_array(self, var):
            pass
        def init_with_zeros(self, var, dtype):
            pass
        def fill_with_array(self, var, arr):
            pass
    class TestDevice2(TestDevice1):
        def __init__(self):
            super(TestDevice2, self).__init__()
            self.network_schedule = ['start', 'groups', 'synapses',
                                     'thresholds', 'resets', 'end']

    # Unique names are important for getting the warnings again for multiple
    # runs of the test suite
    name1 = 'testdevice_' + str(uuid4())
    name2 = 'testdevice_' + str(uuid4())
    all_devices[name1] = TestDevice1()
    all_devices[name2] = TestDevice2()

    set_device(name1)
    assert schedule_propagation_offset() == 0*ms
    net = Network()
    assert schedule_propagation_offset(net) == 0*ms

    # Any schedule should work
    net.schedule = list(reversed(net.schedule))
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 0, 'did not expect a warning'

    assert schedule_propagation_offset(net) == defaultclock.dt

    set_device(name2)
    assert schedule_propagation_offset() == defaultclock.dt

    # Using the correct schedule should work
    net.schedule = ['start', 'groups', 'synapses', 'thresholds', 'resets', 'end']
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 0, 'did not expect a warning'
    assert schedule_propagation_offset(net) == defaultclock.dt

    # Using another (e.g. the default) schedule should raise a warning
    net.schedule = None
    with catch_logs() as l:
        net.run(0*ms)
        assert len(l) == 1 and l[0][1].endswith('schedule_conflict')
    reset_device(previous_device)
Ejemplo n.º 3
0
def test_spikegenerator_connected():
    '''
    Test that `SpikeGeneratorGroup` connects properly.
    '''
    G = NeuronGroup(10, 'v:1')
    mon = StateMonitor(G, 'v', record=True, when='end')
    indices = np.array([3, 2, 1, 1, 4, 5])
    times = np.array([6, 5, 4, 3, 3, 1]) * ms
    SG = SpikeGeneratorGroup(10, indices, times)
    S = Synapses(SG, G, on_pre='v+=1')
    S.connect(j='i')
    run(7 * ms)
    # The following neurons should not receive any spikes
    for idx in [0, 6, 7, 8, 9]:
        assert all(mon[idx].v == 0)
    offset = schedule_propagation_offset()
    # The following neurons should receive a single spike
    for idx, time in zip([2, 3, 4, 5], [5, 6, 3, 1] * ms):
        assert all(mon[idx].v[mon.t < time + offset] == 0)
        assert all(mon[idx].v[mon.t >= time + offset] == 1)
    # This neuron receives two spikes
    assert all(mon[1].v[mon.t < 3 * ms + offset] == 0)
    assert all(mon[1].v[(mon.t >= 3 * ms + offset)
                        & (mon.t < 4 * ms + offset)] == 1)
    assert all(mon[1].v[(mon.t >= 4 * ms + offset)] == 2)
Ejemplo n.º 4
0
def test_propagation():
    # Using a PoissonGroup as a source for Synapses should work as expected
    P = PoissonGroup(2, np.array([0, 1./defaultclock.dt])*Hz)
    G = NeuronGroup(2, 'v:1')
    S = Synapses(P, G, on_pre='v+=1')
    S.connect(j='i')
    run(2*defaultclock.dt + schedule_propagation_offset())

    assert_equal(G.v[:], np.array([0., 2.]))
Ejemplo n.º 5
0
def test_propagation():
    # Using a PoissonGroup as a source for Synapses should work as expected
    P = PoissonGroup(2, np.array([0, 1. / defaultclock.dt]) * Hz)
    G = NeuronGroup(2, 'v:1')
    S = Synapses(P, G, on_pre='v+=1')
    S.connect(j='i')
    run(2 * defaultclock.dt + schedule_propagation_offset())

    assert_equal(G.v[:], np.array([0., 2.]))
Ejemplo n.º 6
0
def test_synaptic_propagation_2():
    # This tests for the bug in github issue #461
    source = NeuronGroup(100, '', threshold='True')
    sub_source = source[99:]
    target = NeuronGroup(1, 'v:1')
    syn = Synapses(sub_source, target, on_pre='v+=1')
    syn.connect()
    run(defaultclock.dt + schedule_propagation_offset())
    assert target.v[0] == 1.0
Ejemplo n.º 7
0
def test_synaptic_propagation_2():
    # This tests for the bug in github issue #461
    source = NeuronGroup(100, '', threshold='True')
    sub_source = source[99:]
    target = NeuronGroup(1, 'v:1')
    syn = Synapses(sub_source, target, on_pre='v+=1')
    syn.connect()
    run(defaultclock.dt + schedule_propagation_offset())
    assert target.v[0] == 1.0
Ejemplo n.º 8
0
def test_no_reference_3():
    '''
    Using subgroups without keeping an explicit reference. Monitors
    '''
    G = NeuronGroup(2, 'v:1', threshold='v>1', reset='v=0')
    G.v = [1.1, 0]
    S = Synapses(G[:1], G[1:], on_pre='v+=1')
    S.connect()
    run(defaultclock.dt + schedule_propagation_offset())
    assert_equal(G.v[:], np.array([0, 1]))
Ejemplo n.º 9
0
def test_no_reference_3():
    '''
    Using subgroups without keeping an explicit reference. Monitors
    '''
    G = NeuronGroup(2, 'v:1', threshold='v>1', reset='v=0')
    G.v = [1.1, 0]
    S = Synapses(G[:1], G[1:], on_pre='v+=1')
    S.connect()
    run(defaultclock.dt + schedule_propagation_offset())
    assert_equal(G.v[:], np.array([0, 1]))
Ejemplo n.º 10
0
def test_poissoninput_refractory():
    eqs = '''
    dv/dt = 0/second : 1 (unless refractory)
    '''
    G = NeuronGroup(10, eqs, reset='v=0', threshold='v>4.5', refractory=5*defaultclock.dt)
    # Will increase the value by 1.0 at each time step
    P = PoissonInput(G, 'v', 1, 1/defaultclock.dt, weight=1.0)
    mon = StateMonitor(G, 'v', record=5)
    run(10*defaultclock.dt)
    expected = np.arange(10, dtype=np.float)
    expected[6-int(schedule_propagation_offset()/defaultclock.dt):] = 0
    assert_allclose(mon[5].v[:], expected)
Ejemplo n.º 11
0
def test_poissoninput_refractory():
    eqs = '''
    dv/dt = 0/second : 1 (unless refractory)
    '''
    G = NeuronGroup(10, eqs, reset='v=0', threshold='v>4.5', refractory=5*defaultclock.dt)
    # Will increase the value by 1.0 at each time step
    P = PoissonInput(G, 'v', 1, 1/defaultclock.dt, weight=1.0)
    mon = StateMonitor(G, 'v', record=5)
    run(10*defaultclock.dt)
    expected = np.arange(10, dtype=np.float)
    expected[6-int(schedule_propagation_offset()/defaultclock.dt):] = 0
    assert_allclose(mon[5].v[:], expected)
Ejemplo n.º 12
0
def test_synaptic_propagation():
    G1 = NeuronGroup(10, 'v:1', threshold='v>1', reset='v=0')
    G1.v['i%2==1'] = 1.1  # odd numbers should spike
    G2 = NeuronGroup(20, 'v:1')
    SG1 = G1[1:6]
    SG2 = G2[10:]
    S = Synapses(SG1, SG2, on_pre='v+=1')
    S.connect('i==j')
    run(defaultclock.dt + schedule_propagation_offset())
    expected = np.zeros(len(G2))
    # Neurons 1, 3, 5 spiked and are connected to 10, 12, 14
    expected[[10, 12, 14]] = 1
    assert_equal(np.asarray(G2.v).flatten(), expected)
Ejemplo n.º 13
0
def test_synaptic_propagation():
    G1 = NeuronGroup(10, 'v:1', threshold='v>1', reset='v=0')
    G1.v['i%2==1'] = 1.1 # odd numbers should spike
    G2 = NeuronGroup(20, 'v:1')
    SG1 = G1[1:6]
    SG2 = G2[10:]
    S = Synapses(SG1, SG2, on_pre='v+=1')
    S.connect('i==j')
    run(defaultclock.dt + schedule_propagation_offset())
    expected = np.zeros(len(G2))
    # Neurons 1, 3, 5 spiked and are connected to 10, 12, 14
    expected[[10, 12, 14]] = 1
    assert_equal(np.asarray(G2.v).flatten(), expected)
Ejemplo n.º 14
0
def test_no_reference_4():
    '''
    Using subgroups without keeping an explicit reference. Synapses
    '''
    G1 = NeuronGroup(10, 'v:1', threshold='v>1', reset='v=0')
    G1.v['i%2==1'] = 1.1  # odd numbers should spike
    G2 = NeuronGroup(20, 'v:1')
    S = Synapses(G1[1:6], G2[10:], on_pre='v+=1')
    S.connect('i==j')
    run(defaultclock.dt + schedule_propagation_offset())
    expected = np.zeros(len(G2))
    # Neurons 1, 3, 5 spiked and are connected to 10, 12, 14
    expected[[10, 12, 14]] = 1
    assert_equal(np.asarray(G2.v).flatten(), expected)
Ejemplo n.º 15
0
def test_poissongroup_subgroup():
    # It should be possible to take a subgroup of a PoissonGroup
    P = PoissonGroup(4, [0, 0, 0, 0]*Hz)
    P1 = P[:2]
    P2 = P[2:]
    P2.rates = 1./defaultclock.dt
    G = NeuronGroup(4, 'v:1')
    S1 = Synapses(P1, G[:2], on_pre='v+=1')
    S1.connect(j='i')
    S2 = Synapses(P2, G[2:], on_pre='v+=1')
    S2.connect(j='i')
    run(2*defaultclock.dt + schedule_propagation_offset())

    assert_equal(G.v[:], np.array([0., 0., 2., 2.]))
Ejemplo n.º 16
0
def test_no_reference_4():
    '''
    Using subgroups without keeping an explicit reference. Synapses
    '''
    G1 = NeuronGroup(10, 'v:1', threshold='v>1', reset='v=0')
    G1.v['i%2==1'] = 1.1 # odd numbers should spike
    G2 = NeuronGroup(20, 'v:1')
    S = Synapses(G1[1:6], G2[10:], on_pre='v+=1')
    S.connect('i==j')
    run(defaultclock.dt + schedule_propagation_offset())
    expected = np.zeros(len(G2))
    # Neurons 1, 3, 5 spiked and are connected to 10, 12, 14
    expected[[10, 12, 14]] = 1
    assert_equal(np.asarray(G2.v).flatten(), expected)
Ejemplo n.º 17
0
def test_poissongroup_subgroup():
    # It should be possible to take a subgroup of a PoissonGroup
    P = PoissonGroup(4, [0, 0, 0, 0] * Hz)
    P1 = P[:2]
    P2 = P[2:]
    P2.rates = 1. / defaultclock.dt
    G = NeuronGroup(4, 'v:1')
    S1 = Synapses(P1, G[:2], on_pre='v+=1')
    S1.connect(j='i')
    S2 = Synapses(P2, G[2:], on_pre='v+=1')
    S2.connect(j='i')
    run(2 * defaultclock.dt + schedule_propagation_offset())

    assert_equal(G.v[:], np.array([0., 0., 2., 2.]))
Ejemplo n.º 18
0
def test_spikegenerator_connected():
    '''
    Test that `SpikeGeneratorGroup` connects properly.
    '''
    G = NeuronGroup(10, 'v:1')
    mon = StateMonitor(G, 'v', record=True, when='end')
    indices = np.array([3, 2, 1, 1, 4, 5])
    times =   np.array([6, 5, 4, 3, 3, 1]) * ms
    SG = SpikeGeneratorGroup(10, indices, times)
    S = Synapses(SG, G, on_pre='v+=1')
    S.connect(j='i')
    run(7*ms)
    # The following neurons should not receive any spikes
    for idx in [0, 6, 7, 8, 9]:
        assert all(mon[idx].v == 0)
    offset = schedule_propagation_offset()
    # The following neurons should receive a single spike
    for idx, time in zip([2, 3, 4, 5], [5, 6, 3, 1]*ms):
        assert all(mon[idx].v[mon.t<time+offset] == 0)
        assert all(mon[idx].v[mon.t>=time+offset] == 1)
    # This neuron receives two spikes
    assert all(mon[1].v[mon.t<3*ms+offset] == 0)
    assert all(mon[1].v[(mon.t>=3*ms+offset) & (mon.t<4*ms+offset)] == 1)
    assert all(mon[1].v[(mon.t>=4*ms+offset)] == 2)