示例#1
0
def test_storing_loading():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, '''v : volt
                           x : 1
                           n : integer
                           b : boolean''')
    v = np.arange(10)*volt
    x = np.arange(10, 20)
    n = np.arange(20, 30)
    b = np.array([True, False]).repeat(5)
    G.v = v
    G.x = x
    G.n = n
    G.b = b
    S = Synapses(G, G, '''v_syn : volt
                          x_syn : 1
                          n_syn : integer
                          b_syn : boolean''')
    S.connect(j='i')
    S.v_syn = v
    S.x_syn = x
    S.n_syn = n
    S.b_syn = b
    run(0*ms)
    device.build(directory=None, with_output=False)
    assert_allclose(G.v[:], v)
    assert_allclose(S.v_syn[:], v)
    assert_allclose(G.x[:], x)
    assert_allclose(S.x_syn[:], x)
    assert_allclose(G.n[:], n)
    assert_allclose(S.n_syn[:], n)
    assert_allclose(G.b[:], b)
    assert_allclose(S.b_syn[:], b)
    reset_device()
示例#2
0
def test_cpp_standalone():
    set_device('cpp_standalone', build_on_run=False)
    ##### Define the model
    tau = 1 * ms
    eqs = '''
    dV/dt = (-40*mV-V)/tau : volt (unless refractory)
    '''
    threshold = 'V>-50*mV'
    reset = 'V=-60*mV'
    refractory = 5 * ms
    N = 1000

    G = NeuronGroup(N,
                    eqs,
                    reset=reset,
                    threshold=threshold,
                    refractory=refractory,
                    name='gp')
    G.V = '-i*mV'
    M = SpikeMonitor(G)
    S = Synapses(G, G, 'w : volt', on_pre='V += w')
    S.connect('abs(i-j)<5 and i!=j')
    S.w = 0.5 * mV
    S.delay = '0*ms'

    net = Network(G, M, S)
    net.run(100 * ms)
    device.build(directory=None, with_output=False)
    # we do an approximate equality here because depending on minor details of how it was compiled, the results
    # may be slightly different (if -ffast-math is on)
    assert len(M.i) >= 17000 and len(M.i) <= 18000
    assert len(M.t) == len(M.i)
    assert M.t[0] == 0.
    reset_device()
def test_time_after_run(with_output=False):
    set_device('cpp_standalone', build_on_run=False)
    # Check that the clock and network time after a run is correct, even if we
    # have not actually run the code yet (via build)
    G = NeuronGroup(10, 'dv/dt = -v/(10*ms) : 1')
    net = Network(G)
    assert_allclose(defaultclock.dt, 0.1*ms)
    assert_allclose(defaultclock.t, 0.*ms)
    assert_allclose(G.t, 0.*ms)
    assert_allclose(net.t, 0.*ms)
    net.run(10*ms)
    assert_allclose(defaultclock.t, 10.*ms)
    assert_allclose(G.t, 10.*ms)
    assert_allclose(net.t, 10.*ms)
    net.run(10*ms)
    assert_allclose(defaultclock.t, 20.*ms)
    assert_allclose(G.t, 20.*ms)
    assert_allclose(net.t, 20.*ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, run=True, compile=True,
                 with_output=with_output)
    # Everything should of course still be accessible
    assert_allclose(defaultclock.t, 20.*ms)
    assert_allclose(G.t, 20.*ms)
    assert_allclose(net.t, 20.*ms)

    reset_device()
示例#4
0
def test_spikegenerator_standalone_change_spikes(with_output=False):
    '''
    Basic test for `SpikeGeneratorGroup`.
    '''
    set_device('cpp_standalone', build_on_run=False)
    indices1 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
    times1   = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2]) * ms
    SG = SpikeGeneratorGroup(5, indices1, times1)
    s_mon = SpikeMonitor(SG)
    net = Network(SG, s_mon)
    net.run(5*ms)

    indices2 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1, 3,   3,   3,   1,   2])
    times2   = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2, 4.5, 4.7, 4.8, 4.5, 4.7])*ms + 5*ms

    SG.set_spikes(indices2, times2)
    net.run(5*ms)

    indices3 = np.array([4, 1, 0])
    times3   = np.array([1, 3, 4])*ms + 10*ms

    SG.set_spikes(indices3, times3)
    net.run(5*ms)

    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True, run=True, with_output=with_output)

    _compare_spikes(5, indices1, times1, s_mon, end_time=5*ms)
    _compare_spikes(5, indices2, times2, s_mon, start_time=5*ms, end_time=10*ms)
    _compare_spikes(5, indices3, times3, s_mon, start_time=10*ms)

    reset_device()
def test_timedarray(with_output=True):
    set_device('cpp_standalone', build_on_run=False)

    defaultclock.dt = 0.1*ms
    ta1d = TimedArray(np.arange(10)*volt, dt=1*ms)
    ta2d = TimedArray(np.arange(300).reshape(3, 100).T, dt=defaultclock.dt)
    G = NeuronGroup(4, '''x = ta1d(t) : volt
                          y = ta2d(t, i) : 1''')
    mon = StateMonitor(G, ['x', 'y'], record=True)
    run(11*ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True,
                 run=True, with_output=with_output)

    for idx in xrange(4):
        # x variable should have neuron independent values
        assert_equal(mon[idx].x[:],
                     np.clip(np.arange(11).repeat(10), 0, 9)*volt)

    for idx in xrange(3):
        # y variable is neuron-specific
        assert_equal(mon[idx].y[:],
                     np.clip(np.arange(110), 0, 99) + idx*100)
    # the 2d array only has 3 columns, the last neuron should therefore contain
    # only NaN
    assert_equal(mon[3].y[:], np.nan)

    reset_device()
示例#6
0
def test_spikegenerator_standalone_change_period(with_output=False):
    '''
    Basic test for `SpikeGeneratorGroup`.
    '''
    set_device('cpp_standalone', build_on_run=False)
    indices1 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
    times1   = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2]) * ms
    SG = SpikeGeneratorGroup(5, indices1, times1, period=5*ms)
    s_mon = SpikeMonitor(SG)
    net = Network(SG, s_mon)
    net.run(10*ms)

    indices2 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1, 3,   3,   3,   1,   2])
    times2   = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2, 4.5, 4.7, 4.8, 4.5, 4.7])*ms + 10*ms

    SG.set_spikes(indices2, times2)
    net.run(10*ms)  # period should no longer be in effect

    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True, run=True, with_output=with_output)

    _compare_spikes(5, np.hstack([indices1, indices1]),
                    np.hstack([times1, times1+5*ms]),
                    s_mon, end_time=10*ms)
    _compare_spikes(5, indices2, times2, s_mon, start_time=10*ms)

    reset_device()
def test_sorted_indices_statemonitor():
    previous_device = get_device()

    results = {}

    n_cells = 5
    n_recorded = 10
    delay = np.arange(n_cells) * defaultclock.dt

    for devicename in ['cpp_standalone', 'cuda_standalone']:
        set_device(devicename, build_on_run=False, with_output=False)
        Synapses.__instances__().clear()
        reinit_devices()
        P = NeuronGroup(n_cells, model='', threshold='True')
        S = Synapses(P, P, model='''w : 1''', on_pre='''w += 1''')
        S.connect(j='i')
        S.pre.delay = delay

        state_mon = StateMonitor(S, 'w', record=range(n_recorded))

        run(defaultclock.dt * (n_cells + 1))

        device.build(directory=None, with_output=False)
        results[devicename] = state_mon.w.astype(int)

    assert_allclose(results['cpp_standalone'].sum(axis=0),
                    results['cuda_standalone'].sum(axis=0))
    assert_allclose(results['cpp_standalone'], results['cuda_standalone'])

    reset_device(previous_device)
示例#8
0
def test_time_after_run():
    set_device('cpp_standalone', build_on_run=False)
    # Check that the clock and network time after a run is correct, even if we
    # have not actually run the code yet (via build)
    G = NeuronGroup(10, 'dv/dt = -v/(10*ms) : 1')
    net = Network(G)
    assert_allclose(defaultclock.dt, 0.1 * ms)
    assert_allclose(defaultclock.t, 0. * ms)
    assert_allclose(G.t, 0. * ms)
    assert_allclose(net.t, 0. * ms)
    net.run(10 * ms)
    assert_allclose(defaultclock.t, 10. * ms)
    assert_allclose(G.t, 10. * ms)
    assert_allclose(net.t, 10. * ms)
    net.run(10 * ms)
    assert_allclose(defaultclock.t, 20. * ms)
    assert_allclose(G.t, 20. * ms)
    assert_allclose(net.t, 20. * ms)
    device.build(directory=None, with_output=False)
    # Everything should of course still be accessible
    assert_allclose(defaultclock.t, 20. * ms)
    assert_allclose(G.t, 20. * ms)
    assert_allclose(net.t, 20. * ms)

    reset_device()
示例#9
0
def test_storing_loading():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, '''v : volt
                           x : 1
                           n : integer
                           b : boolean''')
    v = np.arange(10)*volt
    x = np.arange(10, 20)
    n = np.arange(20, 30)
    b = np.array([True, False]).repeat(5)
    G.v = v
    G.x = x
    G.n = n
    G.b = b
    S = Synapses(G, G, '''v_syn : volt
                          x_syn : 1
                          n_syn : integer
                          b_syn : boolean''')
    S.connect(j='i')
    S.v_syn = v
    S.x_syn = x
    S.n_syn = n
    S.b_syn = b
    run(0*ms)
    device.build(directory=None, with_output=False)
    assert_allclose(G.v[:], v)
    assert_allclose(S.v_syn[:], v)
    assert_allclose(G.x[:], x)
    assert_allclose(S.x_syn[:], x)
    assert_allclose(G.n[:], n)
    assert_allclose(S.n_syn[:], n)
    assert_allclose(G.b[:], b)
    assert_allclose(S.b_syn[:], b)
    reset_device()
示例#10
0
def test_cpp_standalone():
    set_device('cpp_standalone', build_on_run=False)
    ##### Define the model
    tau = 1*ms
    eqs = '''
    dV/dt = (-40*mV-V)/tau : volt (unless refractory)
    '''
    threshold = 'V>-50*mV'
    reset = 'V=-60*mV'
    refractory = 5*ms
    N = 1000
    
    G = NeuronGroup(N, eqs,
                    reset=reset,
                    threshold=threshold,
                    refractory=refractory,
                    name='gp')
    G.V = '-i*mV'
    M = SpikeMonitor(G)
    S = Synapses(G, G, 'w : volt', on_pre='V += w')
    S.connect('abs(i-j)<5 and i!=j')
    S.w = 0.5*mV
    S.delay = '0*ms'

    net = Network(G, M, S)
    net.run(100*ms)
    device.build(directory=None, with_output=False)
    # we do an approximate equality here because depending on minor details of how it was compiled, the results
    # may be slightly different (if -ffast-math is on)
    assert len(M.i)>=17000 and len(M.i)<=18000
    assert len(M.t) == len(M.i)
    assert M.t[0] == 0.
    reset_device()
示例#11
0
def test_timedarray(with_output=True):
    set_device('cpp_standalone', build_on_run=False)

    defaultclock.dt = 0.1 * ms
    ta1d = TimedArray(np.arange(10) * volt, dt=1 * ms)
    ta2d = TimedArray(np.arange(300).reshape(3, 100).T, dt=defaultclock.dt)
    G = NeuronGroup(
        4, '''x = ta1d(t) : volt
                          y = ta2d(t, i) : 1''')
    mon = StateMonitor(G, ['x', 'y'], record=True)
    run(11 * ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir,
                 compile=True,
                 run=True,
                 with_output=with_output)

    for idx in xrange(4):
        # x variable should have neuron independent values
        assert_equal(mon[idx].x[:],
                     np.clip(np.arange(11).repeat(10), 0, 9) * volt)

    for idx in xrange(3):
        # y variable is neuron-specific
        assert_equal(mon[idx].y[:], np.clip(np.arange(110), 0, 99) + idx * 100)
    # the 2d array only has 3 columns, the last neuron should therefore contain
    # only NaN
    assert_equal(mon[3].y[:], np.nan)

    reset_device()
示例#12
0
def test_spikegenerator_standalone_change_period(with_output=False):
    '''
    Basic test for `SpikeGeneratorGroup`.
    '''
    set_device('cpp_standalone', build_on_run=False)
    indices1 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
    times1 = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2]) * ms
    SG = SpikeGeneratorGroup(5, indices1, times1, period=5 * ms)
    s_mon = SpikeMonitor(SG)
    net = Network(SG, s_mon)
    net.run(10 * ms)

    indices2 = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1, 3, 3, 3, 1, 2])
    times2 = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2, 4.5, 4.7, 4.8, 4.5, 4.7
                       ]) * ms + 10 * ms

    SG.set_spikes(indices2, times2)
    net.run(10 * ms)  # period should no longer be in effect

    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir,
                 compile=True,
                 run=True,
                 with_output=with_output)

    _compare_spikes(5,
                    np.hstack([indices1, indices1]),
                    np.hstack([times1, times1 + 5 * ms]),
                    s_mon,
                    end_time=10 * ms)
    _compare_spikes(5, indices2, times2, s_mon, start_time=10 * ms)

    reset_device()
示例#13
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)
示例#14
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)
    net = Network()
    # 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'

    set_device(name2)
    # 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'

    # 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)
示例#15
0
def test_multiple_connects():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, 'v:1')
    S = Synapses(G, G, 'w:1')
    S.connect(i=[0], j=[0])
    S.connect(i=[1], j=[1])
    run(0 * ms)
    device.build(directory=None, with_output=False)
    assert len(S) == 2 and len(S.w[:]) == 2
    reset_device()
示例#16
0
def test_multiple_connects():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, 'v:1')
    S = Synapses(G, G, 'w:1')
    S.connect(i=[0], j=[0])
    S.connect(i=[1], j=[1])
    run(0*ms)
    device.build(directory=None, with_output=False)
    assert len(S) == 2 and len(S.w[:]) == 2
    reset_device()
def test_cuda_scalar_writes():
    # Test that writing to a scalar variable only is done once in a cuda_standalone
    # setting
    set_device('cuda_standalone', build_on_run=False)
    G = NeuronGroup(10, 's : 1 (shared)')
    G.run_regularly('s += 1')
    run(defaultclock.dt)
    device.build(directory=None, with_output=False)
    assert_equal(G.s[:], 1.0)

    reset_device()
示例#18
0
def test_openmp_scalar_writes():
    # Test that writing to a scalar variable only is done once in an OpenMP
    # setting (see github issue #551)
    set_device('cpp_standalone', build_on_run=False)
    prefs.devices.cpp_standalone.openmp_threads = 4
    G = NeuronGroup(10, 's : 1 (shared)')
    G.run_regularly('s += 1')
    run(defaultclock.dt)
    device.build(directory=None, with_output=False)
    assert_equal(G.s[:], 1.0)

    reset_device()
示例#19
0
def test_openmp_scalar_writes():
    # Test that writing to a scalar variable only is done once in an OpenMP
    # setting (see github issue #551)
    set_device('cpp_standalone', build_on_run=False)
    prefs.devices.cpp_standalone.openmp_threads = 4
    G = NeuronGroup(10, 's : 1 (shared)')
    G.run_regularly('s += 1')
    run(defaultclock.dt)
    device.build(directory=None, with_output=False)
    assert_equal(G.s[:], 1.0)

    reset_device()
示例#20
0
def test_multiple_connects(with_output=False):
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, 'v:1')
    S = Synapses(G, G, 'w:1')
    S.connect(i=[0], j=[0])
    S.connect(i=[1], j=[1])
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    run(0 * ms)
    device.build(directory=tempdir, compile=True, run=True, with_output=True)
    assert len(S) == 2 and len(S.w[:]) == 2
    reset_device()
def test_multiple_connects(with_output=False):
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, 'v:1')
    S = Synapses(G, G, 'w:1')
    S.connect(i=[0], j=[0])
    S.connect(i=[1], j=[1])
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    run(0*ms)
    device.build(directory=tempdir, compile=True, run=True,
                 with_output=True)
    assert len(S) == 2 and len(S.w[:]) == 2
    reset_device()
def test_duplicate_names_across_nets(with_output=True):
    set_device('cpp_standalone', build_on_run=False)
    # In standalone mode, names have to be globally unique, not just unique
    # per network
    obj1 = BrianObject(name='name1')
    obj2 = BrianObject(name='name2')
    obj3 = BrianObject(name='name3')
    obj4 = BrianObject(name='name1')
    net1 = Network(obj1, obj2)
    net2 = Network(obj3, obj4)
    net1.run(0*ms)
    net2.run(0*ms)
    assert_raises(ValueError, lambda: device.build())

    reset_device()
示例#23
0
def test_state_monitor_record_single_timestep_cpp_standalone():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(1, 'dv/dt = -v/(5*ms) : 1')
    G.v = 1
    mon = StateMonitor(G, 'v', record=True)
    # Recording before a run should not work
    assert_raises(TypeError, lambda: mon.record_single_timestep())
    run(0.5 * ms)
    mon.record_single_timestep()
    tempdir = tempfile.mkdtemp()
    device.build(directory=tempdir, compile=True, run=True, with_output=False)
    assert_allclose(mon.t[-1], 0.5 * ms)
    assert len(mon.t) == 6
    assert mon[0].v[-1] == G.v
    reset_device()
示例#24
0
def test_duplicate_names_across_nets():
    set_device('cpp_standalone', build_on_run=False)
    # In standalone mode, names have to be globally unique, not just unique
    # per network
    obj1 = BrianObject(name='name1')
    obj2 = BrianObject(name='name2')
    obj3 = BrianObject(name='name3')
    obj4 = BrianObject(name='name1')
    net1 = Network(obj1, obj2)
    net2 = Network(obj3, obj4)
    net1.run(0*ms)
    net2.run(0*ms)
    assert_raises(ValueError, lambda: device.build())

    reset_device()
示例#25
0
def test_state_monitor_record_single_timestep_cpp_standalone():
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(1, 'dv/dt = -v/(5*ms) : 1')
    G.v = 1
    mon = StateMonitor(G, 'v', record=True)
    # Recording before a run should not work
    assert_raises(TypeError, lambda: mon.record_single_timestep())
    run(0.5*ms)
    mon.record_single_timestep()
    tempdir = tempfile.mkdtemp()
    device.build(directory=tempdir, compile=True, run=True,
                 with_output=False)
    assert_allclose(mon.t[-1], 0.5*ms)
    assert len(mon.t) == 6
    assert mon[0].v[-1] == G.v
    reset_device()
示例#26
0
def test_dt_changes_between_runs_standalone(with_output=False):
    set_device('cpp_standalone', build_on_run=False)
    defaultclock.dt = 0.1 * ms
    G = NeuronGroup(1, 'v:1')
    mon = StateMonitor(G, 'v', record=True)
    run(.5 * ms)
    defaultclock.dt = .5 * ms
    run(.5 * ms)
    defaultclock.dt = 0.1 * ms
    run(.5 * ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True, run=True, with_output=True)
    assert len(mon.t[:]) == 5 + 1 + 5
    assert_allclose(
        mon.t[:], [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 1., 1.1, 1.2, 1.3, 1.4] * ms)
    reset_device()
def test_dt_changes_between_runs_standalone(with_output=False):
    set_device('cpp_standalone', build_on_run=False)
    defaultclock.dt = 0.1*ms
    G = NeuronGroup(1, 'v:1')
    mon = StateMonitor(G, 'v', record=True)
    run(.5*ms)
    defaultclock.dt = .5*ms
    run(.5*ms)
    defaultclock.dt = 0.1*ms
    run(.5*ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True, run=True,
                 with_output=True)
    assert len(mon.t[:]) == 5 + 1 + 5
    assert_allclose(mon.t[:],
                    [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 1., 1.1, 1.2, 1.3, 1.4]*ms)
    reset_device()
示例#28
0
def test_spikegenerator_standalone(with_output=False):
    '''
    Basic test for `SpikeGeneratorGroup` in standalone.
    '''
    set_device('cpp_standalone', build_on_run=False)
    indices = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
    times   = np.array([1, 4, 4, 3, 2, 4, 2, 3, 2]) * ms
    SG = SpikeGeneratorGroup(5, indices, times)
    s_mon = SpikeMonitor(SG)
    net = Network(SG, s_mon)
    net.run(5*ms)
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, compile=True, run=True, with_output=with_output)

    _compare_spikes(5, indices, times, s_mon)

    reset_device()
示例#29
0
def example_run(debug=False, **build_options):
    '''
    Run a simple example simulation that test whether the Brian2/Brian2GeNN/GeNN
    pipeline is working correctly.

    Parameters
    ----------
    debug : bool
        Whether to display debug information (e.g. compilation output) during
        the run. Defaults to ``False``.
    build_options : dict
        Additional options that will be forwarded to the ``set_device`` call,
        e.g. ``use_GPU=False``.
    '''
    from brian2.devices.device import set_device, reset_device
    from brian2 import ms, NeuronGroup, run
    from brian2.utils.logger import std_silent
    import numpy as np
    from numpy.testing import assert_allclose
    from tempfile import mkdtemp
    import shutil
    with std_silent(debug):
        test_dir = mkdtemp(prefix='brian2genn_test')
        set_device('genn', directory=test_dir, debug=debug, **build_options)
        N = 100
        tau = 10 * ms
        eqs = '''
        dV/dt = -V/tau: 1
        '''
        G = NeuronGroup(N,
                        eqs,
                        threshold='V>1',
                        reset='V=0',
                        refractory=5 * ms,
                        method='linear')
        G.V = 'i/100.'
        run(1 * ms)
        assert_allclose(G.V, np.arange(100) / 100. * np.exp(-1 * ms / tau))
        shutil.rmtree(test_dir, ignore_errors=True)
        reset_device()
    print('Example run was successful.')
示例#30
0
def test_set_reset_device_implicit():

    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    assert get_device() is test_device2
    assert get_device()._options['my_opt'] == 2
    assert get_device().build_on_run

    reset_device()
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    reset_device()
    assert get_device() is runtime_device

    reset_device()  # If there is no previous device, will reset to runtime device
    assert get_device() is runtime_device
    del all_devices['test1']
    del all_devices['test2']
示例#31
0
def test_set_reset_device_implicit():
    import brian2.devices.device as device_module
    old_prev_devices = list(device_module.previous_devices)
    device_module.previous_devices = []
    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    assert get_device() is test_device2
    assert get_device()._options['my_opt'] == 2
    assert get_device().build_on_run

    reset_device()
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    reset_device()
    assert get_device() is runtime_device

    reset_device(
    )  # If there is no previous device, will reset to runtime device
    assert get_device() is runtime_device
    del all_devices['test1']
    del all_devices['test2']
    device_module.previous_devices = old_prev_devices
示例#32
0
def test_set_reset_device_implicit():

    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    assert get_device() is test_device2
    assert get_device()._options['my_opt'] == 2
    assert get_device().build_on_run

    reset_device()
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    reset_device()
    assert get_device() is runtime_device

    reset_device()  # If there is no previous device, will reset to runtime device
    assert get_device() is runtime_device
    del all_devices['test1']
    del all_devices['test2']
示例#33
0
def test_set_reset_device_implicit():
    import brian2.devices.device as device_module
    old_prev_devices = list(device_module.previous_devices)
    device_module.previous_devices = []
    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    assert get_device() is test_device2
    assert get_device()._options['my_opt'] == 2
    assert get_device().build_on_run

    reset_device()
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    reset_device()
    assert get_device() is runtime_device

    reset_device()  # If there is no previous device, will reset to runtime device
    assert get_device() is runtime_device
    del all_devices['test1']
    del all_devices['test2']
    device_module.previous_devices = old_prev_devices
示例#34
0
def test_set_reset_device_explicit():
    original_device = get_device()
    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2
    test_device3 = ATestDevice()
    all_devices['test3'] = test_device3

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    set_device('test3', build_on_run=False, my_opt=3)

    reset_device('test1')  # Directly jump back to the first device
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    del all_devices['test1']
    del all_devices['test2']
    del all_devices['test3']
    reset_device(original_device)
示例#35
0
def test_set_reset_device_explicit():
    original_device = get_device()
    test_device1 = ATestDevice()
    all_devices['test1'] = test_device1
    test_device2 = ATestDevice()
    all_devices['test2'] = test_device2
    test_device3 = ATestDevice()
    all_devices['test3'] = test_device3

    set_device('test1', build_on_run=False, my_opt=1)
    set_device('test2', build_on_run=True, my_opt=2)
    set_device('test3', build_on_run=False, my_opt=3)

    reset_device('test1')  # Directly jump back to the first device
    assert get_device() is test_device1
    assert get_device()._options['my_opt'] == 1
    assert not get_device().build_on_run

    del all_devices['test1']
    del all_devices['test2']
    del all_devices['test3']
    reset_device(original_device)
示例#36
0
def generate_results(num_repeats):
    results = {}

    for name in ['CUBA', 'COBA']:
        for target in ['numpy', 'cython', 'weave']:
            for dtype in [float32, float64]:
                prefs.codegen.target = target
                prefs.core.default_float_dtype = dtype
                times = [run_benchmark(name) for repeat in range(num_repeats)]
                results[name, target, dtype.__name__] = amin(times)

    for name in ['CUBA', 'COBA']:
        for dtype in [float32, float64]:
            times = []
            for _ in range(num_repeats):
                reset_device()
                reinit_devices()
                set_device('cpp_standalone', directory=None, with_output=False)
                prefs.core.default_float_dtype = dtype
                times.append(run_benchmark(name))
            results[name, 'cpp_standalone', dtype.__name__] = amin(times)

    return results
示例#37
0
def example_run(debug=False, **build_options):
    '''
    Run a simple example simulation that test whether the Brian2/Brian2GeNN/GeNN
    pipeline is working correctly.

    Parameters
    ----------
    debug : bool
        Whether to display debug information (e.g. compilation output) during
        the run. Defaults to ``False``.
    build_options : dict
        Additional options that will be forwarded to the ``set_device`` call,
        e.g. ``use_GPU=False``.
    '''
    from brian2.devices.device import set_device, reset_device
    from brian2 import ms, NeuronGroup, run
    from brian2.utils.logger import std_silent
    import numpy as np
    from numpy.testing import assert_allclose
    from tempfile import mkdtemp
    import shutil
    with std_silent(debug):
        test_dir = mkdtemp(prefix='brian2genn_test')
        set_device('genn', directory=test_dir, debug=debug, **build_options)
        N = 100
        tau = 10*ms
        eqs = '''
        dV/dt = -V/tau: 1
        '''
        G = NeuronGroup(N, eqs, threshold='V>1', reset='V=0', refractory=5 * ms,
                        method='linear')
        G.V = 'i/100.'
        run(1*ms)
        assert_allclose(G.V, np.arange(100)/100.*np.exp(-1*ms/tau))
        shutil.rmtree(test_dir, ignore_errors=True)
        reset_device()
    print('Example run was successful.')
示例#38
0
        # backup prefs such that reinit_device in the pytest test teardown resets
        # the preferences to what was set above (in restore_initial_state())
        # TODO after change to pytest, might just use that pytest prefs plugin?
        prefs._backup()

        if args.only is None or args.only == 'single-run':
            print ("Running standalone-compatible standard tests "
                   "(single run statement)\n")
            sys.stdout.flush()
            pref_plugin.device_options = {'directory': None, 'with_output': with_output}
            argv = make_argv(tests, markers='standalone_compatible and not multiple_runs')
            exit_code = pytest.main(argv + additional_args, plugins=[pref_plugin])
            pref_success = pref_success and exit_code in [0, 5]

            clear_caches()
            reset_device()

        if args.only is None or args.only == 'multi-run':
            print ("Running standalone-compatible standard tests "
                   "(multiple run statements)\n")
            sys.stdout.flush()
            pref_plugin.device_options = {'directory': None, 'with_output': with_output,
                                          'build_on_run': False}
            argv = make_argv(tests, markers='standalone_compatible and multiple_runs')
            exit_code = pytest.main(argv + additional_args, plugins=[pref_plugin])
            pref_success = pref_success and exit_code in [0, 5]

            clear_caches()
            reset_device()

        if args.only is None or args.only == 'standalone-only':
def test_openmp_consistency(with_output=False):
    previous_device = get_device()
    n_cells    = 100
    n_recorded = 10
    numpy.random.seed(42)
    taum       = 20 * ms
    taus       = 5 * ms
    Vt         = -50 * mV
    Vr         = -60 * mV
    El         = -49 * mV
    fac        = (60 * 0.27 / 10)
    gmax       = 20*fac
    dApre      = .01
    taupre     = 20 * ms
    taupost    = taupre
    dApost     = -dApre * taupre / taupost * 1.05
    dApost    *=  0.1*gmax
    dApre     *=  0.1*gmax

    connectivity = numpy.random.randn(n_cells, n_cells)
    sources      = numpy.random.random_integers(0, n_cells-1, 10*n_cells)
    # Only use one spike per time step (to rule out that a single source neuron
    # has more than one spike in a time step)
    times        = numpy.random.choice(numpy.arange(10*n_cells), 10*n_cells,
                                       replace=False)*ms
    v_init       = Vr + numpy.random.rand(n_cells) * (Vt - Vr)

    eqs  = Equations('''
    dv/dt = (g-(v-El))/taum : volt
    dg/dt = -g/taus         : volt
    ''')

    results = {}

    for (n_threads, devicename) in [(0, 'runtime'),
                                    (0, 'cpp_standalone'),
                                    (1, 'cpp_standalone'),
                                    (2, 'cpp_standalone'),
                                    (3, 'cpp_standalone'),
                                    (4, 'cpp_standalone')]:
        set_device(devicename, build_on_run=False, with_output=False)
        Synapses.__instances__().clear()
        if devicename=='cpp_standalone':
            reinit_devices()
        prefs.devices.cpp_standalone.openmp_threads = n_threads
        P    = NeuronGroup(n_cells, model=eqs, threshold='v>Vt', reset='v=Vr', refractory=5 * ms)
        Q    = SpikeGeneratorGroup(n_cells, sources, times)
        P.v  = v_init
        P.g  = 0 * mV
        S    = Synapses(P, P, 
                            model = '''dApre/dt=-Apre/taupre    : 1 (event-driven)    
                                       dApost/dt=-Apost/taupost : 1 (event-driven)
                                       w                        : 1''', 
                            pre = '''g     += w*mV
                                     Apre  += dApre
                                     w      = w + Apost''',
                            post = '''Apost += dApost
                                      w      = w + Apre''')
        S.connect()
        
        S.w       = fac*connectivity.flatten()

        T         = Synapses(Q, P, model = "w : 1", on_pre="g += w*mV")
        T.connect(j='i')
        T.w       = 10*fac

        spike_mon = SpikeMonitor(P)
        rate_mon  = PopulationRateMonitor(P)
        state_mon = StateMonitor(S, 'w', record=range(n_recorded), dt=0.1*second)
        v_mon     = StateMonitor(P, 'v', record=range(n_recorded))

        run(0.2 * second, report='text')

        if devicename=='cpp_standalone':
            tempdir = tempfile.mkdtemp()
            if with_output:
                print tempdir
            device.build(directory=tempdir, compile=True,
                         run=True, with_output=with_output)

        results[n_threads, devicename]      = {}
        results[n_threads, devicename]['w'] = state_mon.w
        results[n_threads, devicename]['v'] = v_mon.v
        results[n_threads, devicename]['s'] = spike_mon.num_spikes
        results[n_threads, devicename]['r'] = rate_mon.rate[:]

    for key1, key2 in [((0, 'runtime'), (0, 'cpp_standalone')),
                       ((1, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((2, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((3, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((4, 'cpp_standalone'), (0, 'cpp_standalone'))
                       ]:
        assert_allclose(results[key1]['w'], results[key2]['w'])
        assert_allclose(results[key1]['v'], results[key2]['v'])
        assert_allclose(results[key1]['r'], results[key2]['r'])
        assert_allclose(results[key1]['s'], results[key2]['s'])
    reset_device(previous_device)
def test_array_cache(with_output=False):
    # Check that variables are only accessible from Python when they should be
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10, '''dv/dt = -v / (10*ms) : 1
                           w : 1
                           x : 1
                           y : 1
                           z : 1 (shared)''',
                    threshold='v>1')
    S = Synapses(G, G, 'weight: 1', on_pre='w += weight')
    S.connect(p=0.2)
    S.weight = 7
    # All neurongroup values should be known
    assert_allclose(G.v, 0)
    assert_allclose(G.w, 0)
    assert_allclose(G.x, 0)
    assert_allclose(G.y, 0)
    assert_allclose(G.z, 0)
    assert_allclose(G.i, np.arange(10))

    # But the synaptic variable is not -- we don't know the number of synapses
    assert_raises(NotImplementedError, lambda: S.weight[:])

    # Setting variables with explicit values should not change anything
    G.v = np.arange(10)+1
    G.w = 2
    G.y = 5
    G.z = 7
    assert_allclose(G.v, np.arange(10)+1)
    assert_allclose(G.w, 2)
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)

    # But setting with code should invalidate them
    G.x = 'i*2'
    assert_raises(NotImplementedError, lambda: G.x[:])

    # Make sure that the array cache does not allow to use incorrectly sized
    # values to pass
    assert_raises(ValueError, lambda: setattr(G, 'w', [0, 2]))
    assert_raises(ValueError, lambda: G.w.__setitem__(slice(0, 4), [0, 2]))

    run(10*ms)
    # v is now no longer known without running the network
    assert_raises(NotImplementedError, lambda: G.v[:])
    # Neither is w, it is updated in the synapse
    assert_raises(NotImplementedError, lambda: G.w[:])
    # However, no code touches y or z
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)
    # i is read-only anyway
    assert_allclose(G.i, np.arange(10))

    # After actually running the network, everything should be accessible
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir, run=True, compile=True,
                 with_output=with_output)
    assert all(G.v > 0)
    assert all(G.w > 0)
    assert_allclose(G.x, np.arange(10)*2)
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)
    assert_allclose(G.i, np.arange(10))
    assert_allclose(S.weight, 7)

    reset_device()
示例#41
0
def test_openmp_consistency():
    previous_device = get_device()
    n_cells = 100
    n_recorded = 10
    numpy.random.seed(42)
    taum = 20 * ms
    taus = 5 * ms
    Vt = -50 * mV
    Vr = -60 * mV
    El = -49 * mV
    fac = (60 * 0.27 / 10)
    gmax = 20 * fac
    dApre = .01
    taupre = 20 * ms
    taupost = taupre
    dApost = -dApre * taupre / taupost * 1.05
    dApost *= 0.1 * gmax
    dApre *= 0.1 * gmax

    connectivity = numpy.random.randn(n_cells, n_cells)
    sources = numpy.random.randint(0, n_cells - 1, 10 * n_cells)
    # Only use one spike per time step (to rule out that a single source neuron
    # has more than one spike in a time step)
    times = numpy.random.choice(
        numpy.arange(10 * n_cells), 10 * n_cells, replace=False) * ms
    v_init = Vr + numpy.random.rand(n_cells) * (Vt - Vr)

    eqs = Equations('''
    dv/dt = (g-(v-El))/taum : volt
    dg/dt = -g/taus         : volt
    ''')

    results = {}

    for (n_threads,
         devicename) in [(0, 'runtime'), (0, 'cpp_standalone'),
                         (1, 'cpp_standalone'), (2, 'cpp_standalone'),
                         (3, 'cpp_standalone'), (4, 'cpp_standalone')]:
        set_device(devicename, build_on_run=False, with_output=False)
        Synapses.__instances__().clear()
        if devicename == 'cpp_standalone':
            reinit_and_delete()
        prefs.devices.cpp_standalone.openmp_threads = n_threads
        P = NeuronGroup(n_cells,
                        model=eqs,
                        threshold='v>Vt',
                        reset='v=Vr',
                        refractory=5 * ms)
        Q = SpikeGeneratorGroup(n_cells, sources, times)
        P.v = v_init
        P.g = 0 * mV
        S = Synapses(P,
                     P,
                     model='''dApre/dt=-Apre/taupre    : 1 (event-driven)    
                                       dApost/dt=-Apost/taupost : 1 (event-driven)
                                       w                        : 1''',
                     pre='''g     += w*mV
                                     Apre  += dApre
                                     w      = w + Apost''',
                     post='''Apost += dApost
                                      w      = w + Apre''')
        S.connect()

        S.w = fac * connectivity.flatten()

        T = Synapses(Q, P, model="w : 1", on_pre="g += w*mV")
        T.connect(j='i')
        T.w = 10 * fac

        spike_mon = SpikeMonitor(P)
        rate_mon = PopulationRateMonitor(P)
        state_mon = StateMonitor(S,
                                 'w',
                                 record=np.arange(n_recorded),
                                 dt=0.1 * second)
        v_mon = StateMonitor(P, 'v', record=np.arange(n_recorded))

        run(0.2 * second, report='text')

        if devicename == 'cpp_standalone':
            device.build(directory=None, with_output=False)

        results[n_threads, devicename] = {}
        results[n_threads, devicename]['w'] = state_mon.w
        results[n_threads, devicename]['v'] = v_mon.v
        results[n_threads, devicename]['s'] = spike_mon.num_spikes
        results[n_threads, devicename]['r'] = rate_mon.rate[:]

    for key1, key2 in [((0, 'runtime'), (0, 'cpp_standalone')),
                       ((1, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((2, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((3, 'cpp_standalone'), (0, 'cpp_standalone')),
                       ((4, 'cpp_standalone'), (0, 'cpp_standalone'))]:
        assert_allclose(results[key1]['w'], results[key2]['w'])
        assert_allclose(results[key1]['v'], results[key2]['v'])
        assert_allclose(results[key1]['r'], results[key2]['r'])
        assert_allclose(results[key1]['s'], results[key2]['s'])
    reset_device(previous_device)
示例#42
0
def run(codegen_targets=None, long_tests=False, test_codegen_independent=True,
        test_standalone=None, test_openmp=False,
        test_in_parallel=['codegen_independent', 'numpy', 'cython', 'cpp_standalone'],
        reset_preferences=True, fail_for_not_implemented=True):
    '''
    Run brian's test suite. Needs an installation of the nose testing tool.

    For testing, the preferences will be reset to the default preferences.
    After testing, the user preferences will be restored.

    Parameters
    ----------
    codegen_targets : list of str or str
        A list of codegeneration targets or a single target, e.g.
        ``['numpy', 'weave']`` to test. The whole test suite will be repeatedly
        run with `codegen.target` set to the respective value. If not
        specified, all available code generation targets will be tested.
    long_tests : bool, optional
        Whether to run tests that take a long time. Defaults to ``False``.
    test_codegen_independent : bool, optional
        Whether to run tests that are independent of code generation. Defaults
        to ``True``.
    test_standalone : str, optional
        Whether to run tests for a standalone mode. Should be the name of a
        standalone mode (e.g. ``'cpp_standalone'``) and expects that a device
        of that name and an accordingly named "simple" device (e.g.
        ``'cpp_standalone_simple'`` exists that can be used for testing (see
        `CPPStandaloneSimpleDevice` for details. Defaults to ``None``, meaning
        that no standalone device is tested.
    test_openmp : bool, optional
        Whether to test standalone test with multiple threads and OpenMP. Will
        be ignored if ``cpp_standalone`` is not tested. Defaults to ``False``.
    reset_preferences : bool, optional
        Whether to reset all preferences to the default preferences before
        running the test suite. Defaults to ``True`` to get test results
        independent of the user's preference settings but can be switched off
        when the preferences are actually necessary to pass the tests (e.g. for
        device-specific settings).
    fail_for_not_implemented : bool, optional
        Whether to fail for tests raising a `NotImplementedError`. Defaults to
        ``True``, but can be switched off for devices known to not implement
        all of Brian's features.
    '''
    if nose is None:
        raise ImportError('Running the test suite requires the "nose" package.')
    
    if os.name == 'nt':
        test_in_parallel = []

    multiprocess_arguments = ['--processes=-1',
                              '--process-timeout=3600',  # we don't want them to time out
                              '--process-restartworker']

    if codegen_targets is None:
        codegen_targets = ['numpy']
        try:
            import scipy.weave
            codegen_targets.append('weave')
        except ImportError:
            try:
                import weave
                codegen_targets.append('weave')
            except ImportError:
                pass
        try:
            import Cython
            codegen_targets.append('cython')
        except ImportError:
            pass
    elif isinstance(codegen_targets, basestring):  # allow to give a single target
        codegen_targets = [codegen_targets]

    dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    # We write to stderr since nose does all of its output on stderr as well
    sys.stderr.write('Running tests in "%s" ' % dirname)
    if codegen_targets:
        sys.stderr.write('for targets %s' % (', '.join(codegen_targets)))
    ex_in = 'including' if long_tests else 'excluding'
    sys.stderr.write(' (%s long tests)\n' % ex_in)

    all_targets = set(codegen_targets)

    if test_standalone:
        if not isinstance(test_standalone, basestring):
            raise ValueError('test_standalone argument has to be the name of a '
                             'standalone device (e.g. "cpp_standalone")')
        if test_standalone not in all_devices:
            raise ValueError('test_standalone argument "%s" is not a known '
                             'device. Known devices are: '
                             '%s' % (test_standalone,
                                     ', '.join(repr(d) for d in all_devices)))
        sys.stderr.write('Testing standalone \n')
        all_targets.add(test_standalone)
    if test_codegen_independent:
        sys.stderr.write('Testing codegen-independent code \n')
        all_targets.add('codegen_independent')

    parallel_tests = all_targets.intersection(set(test_in_parallel))
    if parallel_tests:
        sys.stderr.write('Testing with multiple processes for %s\n' % ', '.join(parallel_tests))

    if reset_preferences:
        sys.stderr.write('Resetting to default preferences\n')

    sys.stderr.write('\n')

    if reset_preferences:
        # Store the currently set preferences and reset to default preferences
        stored_prefs = prefs.as_file
        prefs.read_preference_file(StringIO(prefs.defaults_as_file))

    # Suppress INFO log messages during testing
    from brian2.utils.logger import BrianLogger, LOG_LEVELS
    log_level = BrianLogger.console_handler.level
    BrianLogger.console_handler.setLevel(LOG_LEVELS['WARNING'])

    # Switch off code optimization to get faster compilation times
    prefs['codegen.cpp.extra_compile_args_gcc'].extend(['-w', '-O0'])
    prefs['codegen.cpp.extra_compile_args_msvc'].extend(['/Od'])

    if fail_for_not_implemented:
        not_implemented_plugin = NotImplementedPlugin
    else:
        not_implemented_plugin = NotImplementedNoFailurePlugin
    # This hack is needed to get the NotImplementedPlugin working for multiprocessing
    import nose.plugins.multiprocess as multiprocess
    multiprocess._instantiate_plugins = [not_implemented_plugin]

    plugins = [not_implemented_plugin()]

    try:
        success = []
        if test_codegen_independent:
            sys.stderr.write('Running tests that do not use code generation\n')
            # Some doctests do actually use code generation, use numpy for that
            prefs.codegen.target = 'numpy'
            prefs._backup()
            argv = ['nosetests', dirname,
                    '-c=',  # no config file loading
                    '-I', '^hears\.py$',
                    '-I', '^\.',
                    '-I', '^_',
                    '--with-doctest',
                    "-a", "codegen-independent",
                    '--nologcapture',
                    '--exe']
            if 'codegen_independent' in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))

        for target in codegen_targets:
            sys.stderr.write('Running tests for target %s:\n' % target)
            prefs.codegen.target = target
            # Also set the target for string-expressions -- otherwise we'd only
            # ever test numpy for those
            prefs.codegen.string_expression_target = target
            prefs._backup()
            exclude_str = "!standalone-only,!codegen-independent"
            if not long_tests:
                exclude_str += ',!long'
            # explicitly ignore the brian2.hears file for testing, otherwise the
            # doctest search will import it, failing on Python 3
            argv = ['nosetests', dirname,
                    '-c=',  # no config file loading
                    '-I', '^hears\.py$',
                    '-I', '^\.',
                    '-I', '^_',
                    # Do not run standalone or
                    # codegen-independent tests
                    "-a", exclude_str,
                    '--nologcapture',
                    '--exe']
            if target in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))

        if test_standalone:
            from brian2.devices.device import get_device, set_device
            set_device(test_standalone, directory=None,  # use temp directory
                       with_output=False)
            sys.stderr.write('Testing standalone device "%s"\n' % test_standalone)
            sys.stderr.write('Running standalone-compatible standard tests\n')
            exclude_str = ',!long' if not long_tests else ''
            argv = ['nosetests', dirname,
                    '-c=',  # no config file loading
                    '-I', '^hears\.py$',
                    '-I', '^\.',
                    '-I', '^_',
                    # Only run standalone tests
                    '-a', 'standalone-compatible'+exclude_str,
                    '--nologcapture',
                    '--exe']
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))

            if test_openmp and test_standalone == 'cpp_standalone':
                # Run all the standalone compatible tests again with 4 threads
                prefs.devices.cpp_standalone.openmp_threads = 4
                prefs._backup()
                sys.stderr.write('Running standalone-compatible standard tests with OpenMP\n')
                exclude_str = ',!long' if not long_tests else ''
                argv = ['nosetests', dirname,
                        '-c=',  # no config file loading
                        '-I', '^hears\.py$',
                        '-I', '^\.',
                        '-I', '^_',
                        # Only run standalone tests
                        '-a', 'standalone-compatible'+exclude_str,
                        '--nologcapture',
                        '--exe']
                success.append(nose.run(argv=argv,
                                        addplugins=plugins))
                prefs.devices.cpp_standalone.openmp_threads = 0
                prefs._backup()

            reset_device()

            sys.stderr.write('Running standalone-specific tests\n')
            exclude_openmp = ',!openmp' if not test_openmp else ''
            argv = ['nosetests', dirname,
                    '-c=',  # no config file loading
                    '-I', '^hears\.py$',
                    '-I', '^\.',
                    '-I', '^_',
                    # Only run standalone tests
                    '-a', test_standalone+exclude_openmp,
                    '--nologcapture',
                    '--exe']
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
        all_success = all(success)
        if not all_success:
            sys.stderr.write(('ERROR: %d/%d test suite(s) did not complete '
                              'successfully (see above).\n') % (len(success) - sum(success),
                                                                len(success)))
        else:
            sys.stderr.write(('OK: %d/%d test suite(s) did complete '
                              'successfully.\n') % (len(success), len(success)))
        return all_success

    finally:
        BrianLogger.console_handler.setLevel(log_level)
        if reset_preferences:
            # Restore the user preferences
            prefs.read_preference_file(StringIO(stored_prefs))
            prefs._backup()
示例#43
0
def run(codegen_targets=None, long_tests=False, test_codegen_independent=True,
        test_standalone=None, test_openmp=False,
        test_in_parallel=['codegen_independent', 'numpy', 'cython', 'cpp_standalone'],
        reset_preferences=True, fail_for_not_implemented=True,
        build_options=None, extra_test_dirs=None, float_dtype=None):
    '''
    Run brian's test suite. Needs an installation of the nose testing tool.

    For testing, the preferences will be reset to the default preferences.
    After testing, the user preferences will be restored.

    Parameters
    ----------
    codegen_targets : list of str or str
        A list of codegeneration targets or a single target, e.g.
        ``['numpy', 'weave']`` to test. The whole test suite will be repeatedly
        run with `codegen.target` set to the respective value. If not
        specified, all available code generation targets will be tested.
    long_tests : bool, optional
        Whether to run tests that take a long time. Defaults to ``False``.
    test_codegen_independent : bool, optional
        Whether to run tests that are independent of code generation. Defaults
        to ``True``.
    test_standalone : str, optional
        Whether to run tests for a standalone mode. Should be the name of a
        standalone mode (e.g. ``'cpp_standalone'``) and expects that a device
        of that name and an accordingly named "simple" device (e.g.
        ``'cpp_standalone_simple'`` exists that can be used for testing (see
        `CPPStandaloneSimpleDevice` for details. Defaults to ``None``, meaning
        that no standalone device is tested.
    test_openmp : bool, optional
        Whether to test standalone test with multiple threads and OpenMP. Will
        be ignored if ``cpp_standalone`` is not tested. Defaults to ``False``.
    reset_preferences : bool, optional
        Whether to reset all preferences to the default preferences before
        running the test suite. Defaults to ``True`` to get test results
        independent of the user's preference settings but can be switched off
        when the preferences are actually necessary to pass the tests (e.g. for
        device-specific settings).
    fail_for_not_implemented : bool, optional
        Whether to fail for tests raising a `NotImplementedError`. Defaults to
        ``True``, but can be switched off for devices known to not implement
        all of Brian's features.
    build_options : dict, optional
        Non-default build options that will be passed as arguments to the
        `set_device` call for the device specified in ``test_standalone``.
    extra_test_dirs : list of str or str, optional
        Additional directories as a list of strings (or a single directory as
        a string) that will be searched for additional tests.
    float_dtype : np.dtype, optional
        Set the dtype to use for floating point variables to a value different
        from the default `core.default_float_dtype` setting.
    '''
    if nose is None:
        raise ImportError('Running the test suite requires the "nose" package.')

    if build_options is None:
        build_options = {}

    if os.name == 'nt':
        test_in_parallel = []

    if extra_test_dirs is None:
        extra_test_dirs = []
    elif isinstance(extra_test_dirs, basestring):
        extra_test_dirs = [extra_test_dirs]

    multiprocess_arguments = ['--processes=-1',
                              '--process-timeout=3600',  # we don't want them to time out
                              '--process-restartworker']

    if codegen_targets is None:
        codegen_targets = ['numpy']
        try:
            import scipy.weave
            codegen_targets.append('weave')
        except ImportError:
            try:
                import weave
                codegen_targets.append('weave')
            except ImportError:
                pass
        try:
            import Cython
            codegen_targets.append('cython')
        except ImportError:
            pass
    elif isinstance(codegen_targets, basestring):  # allow to give a single target
        codegen_targets = [codegen_targets]

    dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    dirnames = [dirname] + extra_test_dirs
    # We write to stderr since nose does all of its output on stderr as well
    sys.stderr.write('Running tests in %s ' % (', '.join(dirnames)))
    if codegen_targets:
        sys.stderr.write('for targets %s' % (', '.join(codegen_targets)))
    ex_in = 'including' if long_tests else 'excluding'
    sys.stderr.write(' (%s long tests)\n' % ex_in)

    all_targets = set(codegen_targets)

    if test_standalone:
        if not isinstance(test_standalone, basestring):
            raise ValueError('test_standalone argument has to be the name of a '
                             'standalone device (e.g. "cpp_standalone")')
        if test_standalone not in all_devices:
            raise ValueError('test_standalone argument "%s" is not a known '
                             'device. Known devices are: '
                             '%s' % (test_standalone,
                                     ', '.join(repr(d) for d in all_devices)))
        sys.stderr.write('Testing standalone \n')
        all_targets.add(test_standalone)
    if test_codegen_independent:
        sys.stderr.write('Testing codegen-independent code \n')
        all_targets.add('codegen_independent')

    parallel_tests = all_targets.intersection(set(test_in_parallel))
    if parallel_tests:
        sys.stderr.write('Testing with multiple processes for %s\n' % ', '.join(parallel_tests))

    if reset_preferences:
        sys.stderr.write('Resetting to default preferences\n')

    if reset_preferences:
        # Store the currently set preferences and reset to default preferences
        stored_prefs = prefs.as_file
        prefs.read_preference_file(StringIO(prefs.defaults_as_file))

    # Avoid failures in the tests for user-registered units
    import copy
    import brian2.units.fundamentalunits as fundamentalunits
    old_unit_registry = copy.copy(fundamentalunits.user_unit_register)
    fundamentalunits.user_unit_register = fundamentalunits.UnitRegistry()

    if float_dtype is not None:
        sys.stderr.write('Setting dtype for floating point variables to: '
                         '{}\n'.format(float_dtype.__name__))
        prefs['core.default_float_dtype'] = float_dtype
    prefs._backup()

    sys.stderr.write('\n')

    # Suppress INFO log messages during testing
    from brian2.utils.logger import BrianLogger, LOG_LEVELS
    log_level = BrianLogger.console_handler.level
    BrianLogger.console_handler.setLevel(LOG_LEVELS['WARNING'])

    # Switch off code optimization to get faster compilation times
    prefs['codegen.cpp.extra_compile_args_gcc'].extend(['-w', '-O0'])
    prefs['codegen.cpp.extra_compile_args_msvc'].extend(['/Od'])

    if fail_for_not_implemented:
        not_implemented_plugin = NotImplementedPlugin
    else:
        not_implemented_plugin = NotImplementedNoFailurePlugin
    # This hack is needed to get the NotImplementedPlugin working for multiprocessing
    import nose.plugins.multiprocess as multiprocess
    multiprocess._instantiate_plugins = [not_implemented_plugin]

    plugins = [not_implemented_plugin()]

    from brian2.devices import set_device
    set_device('runtime')
    try:
        success = []
        if test_codegen_independent:
            sys.stderr.write('Running tests that do not use code generation\n')
            # Some doctests do actually use code generation, use numpy for that
            prefs.codegen.target = 'numpy'
            prefs._backup()
            # Print output changed in numpy 1.14, stick with the old format to
            # avoid doctest failures
            import numpy as np
            try:
                np.set_printoptions(legacy='1.13')
            except TypeError:
                pass  # using a numpy version < 1.14
            argv = make_argv(dirnames, "codegen-independent", doctests=True)
            if 'codegen_independent' in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
            clear_caches()

        for target in codegen_targets:
            sys.stderr.write('Running tests for target %s:\n' % target)
            prefs.codegen.target = target
            # Also set the target for string-expressions -- otherwise we'd only
            # ever test numpy for those
            prefs.codegen.string_expression_target = target
            prefs._backup()
            exclude_str = "!standalone-only,!codegen-independent"
            if not long_tests:
                exclude_str += ',!long'
            # explicitly ignore the brian2.hears file for testing, otherwise the
            # doctest search will import it, failing on Python 3
            argv = make_argv(dirnames, exclude_str)
            if target in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
            clear_caches()

        if test_standalone:
            from brian2.devices.device import get_device, set_device
            set_device(test_standalone, directory=None,  # use temp directory
                       with_output=False, **build_options)
            sys.stderr.write('Testing standalone device "%s"\n' % test_standalone)
            sys.stderr.write('Running standalone-compatible standard tests (single run statement)\n')
            exclude_str = ',!long' if not long_tests else ''
            exclude_str += ',!multiple-runs'
            argv = make_argv(dirnames, 'standalone-compatible'+exclude_str)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
            clear_caches()

            reset_device()

            sys.stderr.write('Running standalone-compatible standard tests (multiple run statements)\n')
            set_device(test_standalone, directory=None,  # use temp directory
                       with_output=False, build_on_run=False, **build_options)
            exclude_str = ',!long' if not long_tests else ''
            exclude_str += ',multiple-runs'
            argv = make_argv(dirnames, 'standalone-compatible'+exclude_str)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
            clear_caches()
            reset_device()

            if test_openmp and test_standalone == 'cpp_standalone':
                # Run all the standalone compatible tests again with 4 threads
                set_device(test_standalone, directory=None, # use temp directory
                           with_output=False, **build_options)
                prefs.devices.cpp_standalone.openmp_threads = 4
                prefs._backup()
                sys.stderr.write('Running standalone-compatible standard tests with OpenMP (single run statements)\n')
                exclude_str = ',!long' if not long_tests else ''
                exclude_str += ',!multiple-runs'
                argv = make_argv(dirnames,
                                 'standalone-compatible' + exclude_str)
                success.append(nose.run(argv=argv,
                                        addplugins=plugins))
                clear_caches()
                reset_device()

                set_device(test_standalone, directory=None, # use temp directory
                           with_output=False, build_on_run=False, **build_options)
                sys.stderr.write('Running standalone-compatible standard tests with OpenMP (multiple run statements)\n')
                exclude_str = ',!long' if not long_tests else ''
                exclude_str += ',multiple-runs'
                argv = make_argv(dirnames,
                                 'standalone-compatible' + exclude_str)
                success.append(nose.run(argv=argv,
                                        addplugins=plugins))
                clear_caches()
                prefs.devices.cpp_standalone.openmp_threads = 0
                prefs._backup()

                reset_device()

            sys.stderr.write('Running standalone-specific tests\n')
            exclude_openmp = ',!openmp' if not test_openmp else ''
            argv = make_argv(dirnames, test_standalone+exclude_openmp)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv,
                                    addplugins=plugins))
            clear_caches()

        all_success = all(success)
        if not all_success:
            sys.stderr.write(('ERROR: %d/%d test suite(s) did not complete '
                              'successfully (see above).\n') % (len(success) - sum(success),
                                                                len(success)))
        else:
            sys.stderr.write(('OK: %d/%d test suite(s) did complete '
                              'successfully.\n') % (len(success), len(success)))
        return all_success

    finally:
        BrianLogger.console_handler.setLevel(log_level)

        if reset_preferences:
            # Restore the user preferences
            prefs.read_preference_file(StringIO(stored_prefs))
            prefs._backup()

        fundamentalunits.user_unit_register = old_unit_registry
示例#44
0
def run(codegen_targets=None,
        long_tests=False,
        test_codegen_independent=True,
        test_standalone=None,
        test_openmp=False,
        test_in_parallel=[
            'codegen_independent', 'numpy', 'cython', 'cpp_standalone'
        ],
        reset_preferences=True,
        fail_for_not_implemented=True,
        build_options=None,
        extra_test_dirs=None,
        float_dtype=None):
    '''
    Run brian's test suite. Needs an installation of the nose testing tool.

    For testing, the preferences will be reset to the default preferences.
    After testing, the user preferences will be restored.

    Parameters
    ----------
    codegen_targets : list of str or str
        A list of codegeneration targets or a single target, e.g.
        ``['numpy', 'weave']`` to test. The whole test suite will be repeatedly
        run with `codegen.target` set to the respective value. If not
        specified, all available code generation targets will be tested.
    long_tests : bool, optional
        Whether to run tests that take a long time. Defaults to ``False``.
    test_codegen_independent : bool, optional
        Whether to run tests that are independent of code generation. Defaults
        to ``True``.
    test_standalone : str, optional
        Whether to run tests for a standalone mode. Should be the name of a
        standalone mode (e.g. ``'cpp_standalone'``) and expects that a device
        of that name and an accordingly named "simple" device (e.g.
        ``'cpp_standalone_simple'`` exists that can be used for testing (see
        `CPPStandaloneSimpleDevice` for details. Defaults to ``None``, meaning
        that no standalone device is tested.
    test_openmp : bool, optional
        Whether to test standalone test with multiple threads and OpenMP. Will
        be ignored if ``cpp_standalone`` is not tested. Defaults to ``False``.
    reset_preferences : bool, optional
        Whether to reset all preferences to the default preferences before
        running the test suite. Defaults to ``True`` to get test results
        independent of the user's preference settings but can be switched off
        when the preferences are actually necessary to pass the tests (e.g. for
        device-specific settings).
    fail_for_not_implemented : bool, optional
        Whether to fail for tests raising a `NotImplementedError`. Defaults to
        ``True``, but can be switched off for devices known to not implement
        all of Brian's features.
    build_options : dict, optional
        Non-default build options that will be passed as arguments to the
        `set_device` call for the device specified in ``test_standalone``.
    extra_test_dirs : list of str or str, optional
        Additional directories as a list of strings (or a single directory as
        a string) that will be searched for additional tests.
    float_dtype : np.dtype, optional
        Set the dtype to use for floating point variables to a value different
        from the default `core.default_float_dtype` setting.
    '''
    if nose is None:
        raise ImportError(
            'Running the test suite requires the "nose" package.')

    if build_options is None:
        build_options = {}

    if os.name == 'nt':
        test_in_parallel = []

    if extra_test_dirs is None:
        extra_test_dirs = []
    elif isinstance(extra_test_dirs, basestring):
        extra_test_dirs = [extra_test_dirs]

    multiprocess_arguments = [
        '--processes=-1',
        '--process-timeout=3600',  # we don't want them to time out
        '--process-restartworker'
    ]

    if codegen_targets is None:
        codegen_targets = ['numpy']
        try:
            import scipy.weave
            codegen_targets.append('weave')
        except ImportError:
            try:
                import weave
                codegen_targets.append('weave')
            except ImportError:
                pass
        try:
            import Cython
            codegen_targets.append('cython')
        except ImportError:
            pass
    elif isinstance(codegen_targets,
                    basestring):  # allow to give a single target
        codegen_targets = [codegen_targets]

    dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    dirnames = [dirname] + extra_test_dirs
    # We write to stderr since nose does all of its output on stderr as well
    sys.stderr.write('Running tests in %s ' % (', '.join(dirnames)))
    if codegen_targets:
        sys.stderr.write('for targets %s' % (', '.join(codegen_targets)))
    ex_in = 'including' if long_tests else 'excluding'
    sys.stderr.write(' (%s long tests)\n' % ex_in)

    all_targets = set(codegen_targets)

    if test_standalone:
        if not isinstance(test_standalone, basestring):
            raise ValueError(
                'test_standalone argument has to be the name of a '
                'standalone device (e.g. "cpp_standalone")')
        if test_standalone not in all_devices:
            raise ValueError(
                'test_standalone argument "%s" is not a known '
                'device. Known devices are: '
                '%s' %
                (test_standalone, ', '.join(repr(d) for d in all_devices)))
        sys.stderr.write('Testing standalone \n')
        all_targets.add(test_standalone)
    if test_codegen_independent:
        sys.stderr.write('Testing codegen-independent code \n')
        all_targets.add('codegen_independent')

    parallel_tests = all_targets.intersection(set(test_in_parallel))
    if parallel_tests:
        sys.stderr.write('Testing with multiple processes for %s\n' %
                         ', '.join(parallel_tests))

    if reset_preferences:
        sys.stderr.write('Resetting to default preferences\n')

    if reset_preferences:
        # Store the currently set preferences and reset to default preferences
        stored_prefs = prefs.as_file
        prefs.read_preference_file(StringIO(prefs.defaults_as_file))

    # Avoid failures in the tests for user-registered units
    import copy
    import brian2.units.fundamentalunits as fundamentalunits
    old_unit_registry = copy.copy(fundamentalunits.user_unit_register)
    fundamentalunits.user_unit_register = fundamentalunits.UnitRegistry()

    if float_dtype is not None:
        sys.stderr.write('Setting dtype for floating point variables to: '
                         '{}\n'.format(float_dtype.__name__))
        prefs['core.default_float_dtype'] = float_dtype
    prefs._backup()

    sys.stderr.write('\n')

    # Suppress INFO log messages during testing
    from brian2.utils.logger import BrianLogger, LOG_LEVELS
    log_level = BrianLogger.console_handler.level
    BrianLogger.console_handler.setLevel(LOG_LEVELS['WARNING'])

    # Switch off code optimization to get faster compilation times
    prefs['codegen.cpp.extra_compile_args_gcc'].extend(['-w', '-O0'])
    prefs['codegen.cpp.extra_compile_args_msvc'].extend(['/Od'])

    if fail_for_not_implemented:
        not_implemented_plugin = NotImplementedPlugin
    else:
        not_implemented_plugin = NotImplementedNoFailurePlugin
    # This hack is needed to get the NotImplementedPlugin working for multiprocessing
    import nose.plugins.multiprocess as multiprocess
    multiprocess._instantiate_plugins = [not_implemented_plugin]

    plugins = [not_implemented_plugin()]

    from brian2.devices import set_device
    set_device('runtime')
    try:
        success = []
        if test_codegen_independent:
            sys.stderr.write('Running tests that do not use code generation\n')
            # Some doctests do actually use code generation, use numpy for that
            prefs.codegen.target = 'numpy'
            prefs._backup()
            # Print output changed in numpy 1.14, stick with the old format to
            # avoid doctest failures
            import numpy as np
            try:
                np.set_printoptions(legacy='1.13')
            except TypeError:
                pass  # using a numpy version < 1.14
            argv = make_argv(dirnames, "codegen-independent", doctests=True)
            if 'codegen_independent' in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv, addplugins=plugins))
            clear_caches()

        for target in codegen_targets:
            sys.stderr.write('Running tests for target %s:\n' % target)
            prefs.codegen.target = target
            # Also set the target for string-expressions -- otherwise we'd only
            # ever test numpy for those
            prefs.codegen.string_expression_target = target
            prefs._backup()
            exclude_str = "!standalone-only,!codegen-independent"
            if not long_tests:
                exclude_str += ',!long'
            # explicitly ignore the brian2.hears file for testing, otherwise the
            # doctest search will import it, failing on Python 3
            argv = make_argv(dirnames, exclude_str)
            if target in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv, addplugins=plugins))
            clear_caches()

        if test_standalone:
            from brian2.devices.device import get_device, set_device
            set_device(
                test_standalone,
                directory=None,  # use temp directory
                with_output=False,
                **build_options)
            sys.stderr.write('Testing standalone device "%s"\n' %
                             test_standalone)
            sys.stderr.write(
                'Running standalone-compatible standard tests (single run statement)\n'
            )
            exclude_str = ',!long' if not long_tests else ''
            exclude_str += ',!multiple-runs'
            argv = make_argv(dirnames, 'standalone-compatible' + exclude_str)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv, addplugins=plugins))
            clear_caches()

            reset_device()

            sys.stderr.write(
                'Running standalone-compatible standard tests (multiple run statements)\n'
            )
            set_device(
                test_standalone,
                directory=None,  # use temp directory
                with_output=False,
                build_on_run=False,
                **build_options)
            exclude_str = ',!long' if not long_tests else ''
            exclude_str += ',multiple-runs'
            argv = make_argv(dirnames, 'standalone-compatible' + exclude_str)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv, addplugins=plugins))
            clear_caches()
            reset_device()

            if test_openmp and test_standalone == 'cpp_standalone':
                # Run all the standalone compatible tests again with 4 threads
                set_device(
                    test_standalone,
                    directory=None,  # use temp directory
                    with_output=False,
                    **build_options)
                prefs.devices.cpp_standalone.openmp_threads = 4
                prefs._backup()
                sys.stderr.write(
                    'Running standalone-compatible standard tests with OpenMP (single run statements)\n'
                )
                exclude_str = ',!long' if not long_tests else ''
                exclude_str += ',!multiple-runs'
                argv = make_argv(dirnames,
                                 'standalone-compatible' + exclude_str)
                success.append(nose.run(argv=argv, addplugins=plugins))
                clear_caches()
                reset_device()

                set_device(
                    test_standalone,
                    directory=None,  # use temp directory
                    with_output=False,
                    build_on_run=False,
                    **build_options)
                sys.stderr.write(
                    'Running standalone-compatible standard tests with OpenMP (multiple run statements)\n'
                )
                exclude_str = ',!long' if not long_tests else ''
                exclude_str += ',multiple-runs'
                argv = make_argv(dirnames,
                                 'standalone-compatible' + exclude_str)
                success.append(nose.run(argv=argv, addplugins=plugins))
                clear_caches()
                prefs.devices.cpp_standalone.openmp_threads = 0
                prefs._backup()

                reset_device()

            sys.stderr.write('Running standalone-specific tests\n')
            exclude_openmp = ',!openmp' if not test_openmp else ''
            argv = make_argv(dirnames, test_standalone + exclude_openmp)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(nose.run(argv=argv, addplugins=plugins))
            clear_caches()

        all_success = all(success)
        if not all_success:
            sys.stderr.write(('ERROR: %d/%d test suite(s) did not complete '
                              'successfully (see above).\n') %
                             (len(success) - sum(success), len(success)))
        else:
            sys.stderr.write(
                ('OK: %d/%d test suite(s) did complete '
                 'successfully.\n') % (len(success), len(success)))
        return all_success

    finally:
        BrianLogger.console_handler.setLevel(log_level)

        if reset_preferences:
            # Restore the user preferences
            prefs.read_preference_file(StringIO(stored_prefs))
            prefs._backup()

        fundamentalunits.user_unit_register = old_unit_registry
def simple_model(N,
                 params,
                 record=None,
                 update_progress=None,
                 fm=None,
                 minimum_initial_time=100 * ms,
                 use_standalone_openmp=False):
    if use_standalone_openmp:
        prefs.devices.cpp_standalone.openmp_threads = multiprocessing.cpu_count(
        ) / 2  # assume hyperthreading
        set_device('cpp_standalone', with_output=True)
    seed(3402348923)  # for reproducibility
    if fm is None:
        fm = dietz_fm
    orig_fm = fm
    min_tauihc = 0.1 * ms
    eqs = '''
    carrier = clip(cos(2*pi*fc*t), 0, Inf) : 1
    A_raw = (carrier*gain*0.5*(1-cos(2*pi*fm*t)))**gamma : 1
    dA_filt/dt = (A_raw-A)/(int(tauihc<min_tauihc)*1*second+tauihc) : 1
    A = A_raw*int(tauihc<min_tauihc)+A_filt*int(tauihc>=min_tauihc) : 1
    dQ/dt = -k*Q*A+R*(1-Q) : 1
    AQ = A*Q : 1
    dAe/dt = (AQ-Ae)/taue : 1
    dAi/dt = (AQ-Ai)/taui : 1
    out = clip(Ae-beta*Ai, 0, Inf) : 1
    gain = 10**(level/20.) : 1
    R = (1-alpha)/taua : Hz
    k = alpha/taua : Hz
    fc = fc_Hz*Hz : Hz
    fc_Hz : 1
    fm : Hz
    tauihc = tauihc_ms*ms : second
    taue = taue_ms*ms : second
    taui = taui_ms*ms : second
    taua = taua_ms*ms : second
    tauihc_ms : 1
    taue_ms : 1
    taui_ms : 1
    taua_ms : 1
    alpha : 1
    beta : 1
    gamma : 1
    level : 1
    # Accumulation variables
    start_time : second
    end_time : second
    do_accumulate = 1.0*int(t>=start_time and t<end_time) : 1
    accum_sum_out : 1
    accum_sum_out_rising : 1
    accum_sum_out_falling : 1
    accum_argmax_out : second
    accum_max_out : 1
    accum_weighted_sum_cos_phase : 1
    accum_weighted_sum_sin_phase : 1
    '''
    G = NeuronGroup(N * len(fm), eqs, method='euler', dt=0.1 * ms)
    rr = G.run_regularly('''
        accum_sum_out += out*do_accumulate
        phase = (2*pi*fm*t)%(2*pi)
        accum_sum_out_rising += out*int(phase<pi)*do_accumulate
        accum_sum_out_falling += out*int(phase>=pi)*do_accumulate
        accum_weighted_sum_cos_phase += out*cos(phase)*do_accumulate
        accum_weighted_sum_sin_phase += out*sin(phase)*do_accumulate
        is_larger = out>accum_max_out and do_accumulate>0
        accum_max_out = int(not is_larger)*accum_max_out+int(is_larger)*out
        accum_argmax_out = int(not is_larger)*accum_argmax_out+int(is_larger)*t
        ''',
                         when='end')
    params = params.copy()
    for k, v in params.items():
        if isinstance(v, tuple) and len(v) == 2:
            low, high = v
            params[k] = v = rand(N) * (high - low) + low
    params2d = params.copy()
    for k, v in params2d.items():
        if isinstance(v, ndarray) and v.size > 1:
            v = reshape(v, v.size)
            fm, v = meshgrid(orig_fm,
                             v)  # fm and v have shape (N, len(dietz_fm))
            fm.shape = fm.size
            v.shape = v.size
            params2d[k] = v
    params2d['fm'] = fm
    G.set_states(params2d)
    G.tauihc_ms['tauihc_ms<min_tauihc/ms'] = 0
    G.Q = 1
    net = Network(G, rr)
    # Calculate how long to run the simulation
    period = 1 / fm
    num_initial_cycles = ceil(
        minimum_initial_time /
        period)  # at least one period and at least that time
    start_time = num_initial_cycles * period
    end_time = (num_initial_cycles + 1) * period
    duration = amax(end_time)
    G.start_time = start_time
    G.end_time = end_time
    # Run the simulation
    if isinstance(update_progress, basestring):
        report_period = 10 * second
    else:
        report_period = 1 * second
    if record:
        M = StateMonitor(G, record, record=True)
        net.add(M)
    net.run(duration, report=update_progress, report_period=report_period)
    G.accum_sum_out['accum_sum_out<1e-10'] = 1
    for name in [
            'accum_sum_out_rising', 'accum_sum_out_falling',
            'accum_argmax_out', 'accum_max_out',
            'accum_weighted_sum_cos_phase', 'accum_weighted_sum_sin_phase'
    ]:
        if name == 'accum_argmax_out':
            u = second
        else:
            u = 1
        getattr(G, name)['accum_sum_out>1e10'] = 0 * u
    G.accum_sum_out['accum_sum_out>1e10'] = 1
    c = G.accum_weighted_sum_cos_phase[:]
    s = G.accum_weighted_sum_sin_phase[:]
    weighted_phase = (angle(c + 1j * s) + 2 * pi) % (2 * pi)
    vs = sqrt(c**2 + s**2) / G.accum_sum_out[:]
    mean_fr = G.accum_sum_out[:] / ((end_time - start_time) / G.dt)
    onsettiness = 0.5 * (1 + (G.accum_sum_out_rising[:] -
                              G.accum_sum_out_falling[:]) / G.accum_sum_out[:])
    res = Results(
        accum_argmax_out=G.accum_argmax_out[:],
        accum_max_out=G.accum_max_out[:],
        weighted_phase=weighted_phase,
        vs=vs,
        mean_fr=mean_fr,
        onsettiness=onsettiness,
        params=params,
        start_time=start_time,
        end_time=end_time,
    )
    if record:
        for name in record:
            v = getattr(M, name)[:, :]
            v.shape = (N, len(dietz_fm), -1)
            setattr(res, name, v)
        res.t = M.t[:]
    if use_standalone_openmp:
        reset_device()
        reinit_devices()
    return res
示例#46
0
def run(codegen_targets=None,
        long_tests=False,
        test_codegen_independent=True,
        test_standalone=None,
        test_openmp=False,
        test_in_parallel=[
            'codegen_independent', 'numpy', 'cython', 'cpp_standalone'
        ],
        reset_preferences=True,
        fail_for_not_implemented=True,
        build_options=None,
        extra_test_dirs=None,
        float_dtype=None,
        additional_args=None):
    '''
    Run brian's test suite. Needs an installation of the pytest testing tool.

    For testing, the preferences will be reset to the default preferences.
    After testing, the user preferences will be restored.

    Parameters
    ----------
    codegen_targets : list of str or str
        A list of codegeneration targets or a single target, e.g.
        ``['numpy', 'weave']`` to test. The whole test suite will be repeatedly
        run with `codegen.target` set to the respective value. If not
        specified, all available code generation targets will be tested.
    long_tests : bool, optional
        Whether to run tests that take a long time. Defaults to ``False``.
    test_codegen_independent : bool, optional
        Whether to run tests that are independent of code generation. Defaults
        to ``True``.
    test_standalone : str, optional
        Whether to run tests for a standalone mode. Should be the name of a
        standalone mode (e.g. ``'cpp_standalone'``) and expects that a device
        of that name and an accordingly named "simple" device (e.g.
        ``'cpp_standalone_simple'`` exists that can be used for testing (see
        `CPPStandaloneSimpleDevice` for details. Defaults to ``None``, meaning
        that no standalone device is tested.
    test_openmp : bool, optional
        Whether to test standalone test with multiple threads and OpenMP. Will
        be ignored if ``cpp_standalone`` is not tested. Defaults to ``False``.
    reset_preferences : bool, optional
        Whether to reset all preferences to the default preferences before
        running the test suite. Defaults to ``True`` to get test results
        independent of the user's preference settings but can be switched off
        when the preferences are actually necessary to pass the tests (e.g. for
        device-specific settings).
    fail_for_not_implemented : bool, optional
        Whether to fail for tests raising a `NotImplementedError`. Defaults to
        ``True``, but can be switched off for devices known to not implement
        all of Brian's features.
    build_options : dict, optional
        Non-default build options that will be passed as arguments to the
        `set_device` call for the device specified in ``test_standalone``.
    extra_test_dirs : list of str or str, optional
        Additional directories as a list of strings (or a single directory as
        a string) that will be searched for additional tests.
    float_dtype : np.dtype, optional
        Set the dtype to use for floating point variables to a value different
        from the default `core.default_float_dtype` setting.
    additional_args : list of str, optional
        Optional command line arguments to pass to ``pytest``
    '''
    if pytest is None:
        raise ImportError(
            'Running the test suite requires the "pytest" package.')

    if build_options is None:
        build_options = {}

    if os.name == 'nt':
        test_in_parallel = []

    if extra_test_dirs is None:
        extra_test_dirs = []
    elif isinstance(extra_test_dirs, basestring):
        extra_test_dirs = [extra_test_dirs]
    if additional_args is None:
        additional_args = []

    multiprocess_arguments = ['-n', 'auto']

    if codegen_targets is None:
        codegen_targets = ['numpy']
        try:
            import scipy.weave
            codegen_targets.append('weave')
        except ImportError:
            try:
                import weave
                codegen_targets.append('weave')
            except ImportError:
                pass
        try:
            import Cython
            codegen_targets.append('cython')
        except ImportError:
            pass
    elif isinstance(codegen_targets,
                    basestring):  # allow to give a single target
        codegen_targets = [codegen_targets]

    dirname = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    dirnames = [dirname] + extra_test_dirs

    print('Running tests in %s ' % (', '.join(dirnames)), end='')
    if codegen_targets:
        print('for targets %s' % (', '.join(codegen_targets)), end='')
    ex_in = 'including' if long_tests else 'excluding'
    print(' (%s long tests)' % ex_in)

    print("Running Brian version {} "
          "from '{}'".format(brian2.__version__,
                             os.path.dirname(brian2.__file__)))

    all_targets = set(codegen_targets)

    if test_standalone:
        if not isinstance(test_standalone, basestring):
            raise ValueError(
                'test_standalone argument has to be the name of a '
                'standalone device (e.g. "cpp_standalone")')
        if test_standalone not in all_devices:
            raise ValueError(
                'test_standalone argument "%s" is not a known '
                'device. Known devices are: '
                '%s' %
                (test_standalone, ', '.join(repr(d) for d in all_devices)))
        print('Testing standalone')
        all_targets.add(test_standalone)
    if test_codegen_independent:
        print('Testing codegen-independent code')
        all_targets.add('codegen_independent')

    parallel_tests = all_targets.intersection(set(test_in_parallel))
    if parallel_tests:
        try:
            import xdist
            print('Testing with multiple processes for %s' %
                  ', '.join(parallel_tests))
        except ImportError:
            test_in_parallel = []

    if reset_preferences:
        print('Resetting to default preferences')

    if reset_preferences:
        stored_prefs = prefs.as_file
        prefs.read_preference_file(StringIO(prefs.defaults_as_file))

    # Avoid failures in the tests for user-registered units
    import copy
    import brian2.units.fundamentalunits as fundamentalunits
    old_unit_registry = copy.copy(fundamentalunits.user_unit_register)
    fundamentalunits.user_unit_register = fundamentalunits.UnitRegistry()

    if float_dtype is not None:
        print('Setting dtype for floating point variables to: '
              '{}'.format(float_dtype.__name__))

        prefs['core.default_float_dtype'] = float_dtype

    print()

    # Suppress INFO log messages during testing
    from brian2.utils.logger import BrianLogger, LOG_LEVELS
    log_level = BrianLogger.console_handler.level
    BrianLogger.console_handler.setLevel(LOG_LEVELS['WARNING'])

    # Switch off code optimization to get faster compilation times
    prefs['codegen.cpp.extra_compile_args_gcc'].extend(['-w', '-O0'])
    prefs['codegen.cpp.extra_compile_args_msvc'].extend(['-w', '-O0'])

    from brian2.devices import set_device
    set_device('runtime')
    pref_plugin = PreferencePlugin(prefs, fail_for_not_implemented)
    try:
        success = []
        if test_codegen_independent:
            print('Running doctests')
            # Some doctests do actually use code generation, use numpy for that
            prefs['codegen.target'] = 'numpy'
            # Always test doctests with 64 bit, to avoid differences in print output
            if float_dtype is not None:
                prefs['core.default_float_dtype'] = np.float64
            sphinx_dir = os.path.abspath(
                os.path.join(os.path.dirname(__file__), '..', '..',
                             'docs_sphinx'))
            if os.path.exists(sphinx_dir):
                sphinx_doc_dir = [sphinx_dir]
            else:
                # When running on travis, the source directory is in the SRCDIR
                # environment variable
                if 'SRCDIR' in os.environ:
                    sphinx_dir = os.path.abspath(
                        os.path.join(os.environ['SRCDIR'], 'docs_sphinx'))
                    if os.path.exists(sphinx_dir):
                        sphinx_doc_dir = [sphinx_dir]
                    else:
                        sphinx_doc_dir = []
                else:
                    sphinx_doc_dir = []
            argv = make_argv(dirnames + sphinx_doc_dir, doctests=True)
            if 'codegen_independent' in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            # Set float_dtype back again if necessary
            if float_dtype is not None:
                prefs['core.default_float_dtype'] = float_dtype

            print('Running tests that do not use code generation')
            argv = make_argv(dirnames, "codegen_independent")
            if 'codegen_independent' in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            clear_caches()

        for target in codegen_targets:
            print('Running tests for target %s:' % target)
            # Also set the target for string-expressions -- otherwise we'd only
            # ever test numpy for those
            prefs['codegen.target'] = target

            markers = "not standalone_only and not codegen_independent"
            if not long_tests:
                markers += ' and not long'
            # explicitly ignore the brian2.hears file for testing, otherwise the
            # doctest search will import it, failing on Python 3
            argv = make_argv(dirnames, markers)
            if target in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            clear_caches()

        if test_standalone:
            from brian2.devices.device import get_device, set_device
            set_device(
                test_standalone,
                directory=None,  # use temp directory
                with_output=False,
                **build_options)
            print('Testing standalone device "%s"' % test_standalone)
            print(
                'Running standalone-compatible standard tests (single run statement)'
            )
            markers = 'and not long' if not long_tests else ''
            markers += ' and not multiple_runs'
            argv = make_argv(dirnames, 'standalone_compatible ' + markers)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            clear_caches()

            reset_device()

            print(
                'Running standalone-compatible standard tests (multiple run statements)'
            )
            set_device(
                test_standalone,
                directory=None,  # use temp directory
                with_output=False,
                build_on_run=False,
                **build_options)
            markers = ' and not long' if not long_tests else ''
            markers += ' and multiple_runs'
            argv = make_argv(dirnames, 'standalone_compatible' + markers)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            clear_caches()
            reset_device()

            if test_openmp and test_standalone == 'cpp_standalone':
                # Run all the standalone compatible tests again with 4 threads
                set_device(
                    test_standalone,
                    directory=None,  # use temp directory
                    with_output=False,
                    **build_options)
                prefs['devices.cpp_standalone.openmp_threads'] = 4
                print(
                    'Running standalone-compatible standard tests with OpenMP (single run statements)'
                )
                markers = ' and not long' if not long_tests else ''
                markers += ' and not multiple_runs'
                argv = make_argv(dirnames, 'standalone_compatible' + markers)
                success.append(
                    pytest.main(argv +
                                additional_args, plugins=[pref_plugin]) == 0)
                clear_caches()
                reset_device()

                set_device(
                    test_standalone,
                    directory=None,  # use temp directory
                    with_output=False,
                    build_on_run=False,
                    **build_options)
                print(
                    'Running standalone-compatible standard tests with OpenMP (multiple run statements)'
                )
                markers = ' and not long' if not long_tests else ''
                markers += ' and multiple_runs'
                argv = make_argv(dirnames, 'standalone_compatible' + markers)
                success.append(
                    pytest.main(argv +
                                additional_args, plugins=[pref_plugin]) == 0)
                clear_caches()
                prefs['devices.cpp_standalone.openmp_threads'] = 0

                reset_device()

            print('Running standalone-specific tests')
            exclude_openmp = ' and not openmp' if not test_openmp else ''
            argv = make_argv(dirnames, test_standalone + exclude_openmp)
            if test_standalone in test_in_parallel:
                argv.extend(multiprocess_arguments)
            success.append(
                pytest.main(argv +
                            additional_args, plugins=[pref_plugin]) == 0)
            clear_caches()

        all_success = all(success)
        if not all_success:
            print(('ERROR: %d/%d test suite(s) did not complete '
                   'successfully (see above).') %
                  (len(success) - sum(success), len(success)))
        else:
            print(('OK: %d/%d test suite(s) did complete '
                   'successfully.') % (len(success), len(success)))
        return all_success

    finally:
        BrianLogger.console_handler.setLevel(log_level)

        if reset_preferences:
            # Restore the user preferences
            prefs.read_preference_file(StringIO(stored_prefs))
            prefs._backup()

        fundamentalunits.user_unit_register = old_unit_registry
示例#47
0
def test_array_cache(with_output=False):
    # Check that variables are only accessible from Python when they should be
    set_device('cpp_standalone', build_on_run=False)
    G = NeuronGroup(10,
                    '''dv/dt = -v / (10*ms) : 1
                           w : 1
                           x : 1
                           y : 1
                           z : 1 (shared)''',
                    threshold='v>1')
    S = Synapses(G, G, 'weight: 1', on_pre='w += weight')
    S.connect(p=0.2)
    S.weight = 7
    # All neurongroup values should be known
    assert_allclose(G.v, 0)
    assert_allclose(G.w, 0)
    assert_allclose(G.x, 0)
    assert_allclose(G.y, 0)
    assert_allclose(G.z, 0)
    assert_allclose(G.i, np.arange(10))

    # But the synaptic variable is not -- we don't know the number of synapses
    assert_raises(NotImplementedError, lambda: S.weight[:])

    # Setting variables with explicit values should not change anything
    G.v = np.arange(10) + 1
    G.w = 2
    G.y = 5
    G.z = 7
    assert_allclose(G.v, np.arange(10) + 1)
    assert_allclose(G.w, 2)
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)

    # But setting with code should invalidate them
    G.x = 'i*2'
    assert_raises(NotImplementedError, lambda: G.x[:])

    # Make sure that the array cache does not allow to use incorrectly sized
    # values to pass
    assert_raises(ValueError, lambda: setattr(G, 'w', [0, 2]))
    assert_raises(ValueError, lambda: G.w.__setitem__(slice(0, 4), [0, 2]))

    run(10 * ms)
    # v is now no longer known without running the network
    assert_raises(NotImplementedError, lambda: G.v[:])
    # Neither is w, it is updated in the synapse
    assert_raises(NotImplementedError, lambda: G.w[:])
    # However, no code touches y or z
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)
    # i is read-only anyway
    assert_allclose(G.i, np.arange(10))

    # After actually running the network, everything should be accessible
    tempdir = tempfile.mkdtemp()
    if with_output:
        print tempdir
    device.build(directory=tempdir,
                 run=True,
                 compile=True,
                 with_output=with_output)
    assert all(G.v > 0)
    assert all(G.w > 0)
    assert_allclose(G.x, np.arange(10) * 2)
    assert_allclose(G.y, 5)
    assert_allclose(G.z, 7)
    assert_allclose(G.i, np.arange(10))
    assert_allclose(S.weight, 7)

    reset_device()