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_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()
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()
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()
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)
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']
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
def test_default_function_convertion_preference(): if prefs.core.default_float_dtype is np.float32: raise SkipTest('Need double precision for this test') set_device('cuda_standalone', directory=None) unrepresentable_int = 2**24 + 1 # can't be represented as 32bit float prefs.codegen.generators.cuda.default_functions_integral_convertion = 'float32' G = NeuronGroup(1, 'v: 1') G.variables.add_array('myarr', dtype=np.int32, size=1) G.variables['myarr'].set_value(unrepresentable_int) G.v = 'floor(myarr)'.format(unrepresentable_int) prefs.codegen.generators.cuda.default_functions_integral_convertion = 'float64' G2 = NeuronGroup(1, 'v: 1') G2.variables.add_array('myarr', dtype=np.int32, size=1) G2.variables['myarr'].set_value(unrepresentable_int) G2.v = 'floor(myarr)'.format(unrepresentable_int) run(0*ms) assert G.v[0] != unrepresentable_int, G.v[0] assert G2.v[0] == unrepresentable_int, '{} != {}'.format(G2.v[0], unrepresentable_int)
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
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_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()
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_profile_error(): # Test that profiling info is not 0 set_device('cuda_standalone', directory=None) G = NeuronGroup(1, 'v:1', threshold='True') assert_raises(ValueError, run(defaultclock.dt, profile='error'))
def test_run_with_synapses_and_profile(): set_device('cpp_standalone', build_on_run=True, directory=None) group = NeuronGroup(1, 'v: 1', threshold='False', reset='') syn = Synapses(group, group, on_pre='v += 1') syn.connect() mon = SpikeMonitor(group) run(defaultclock.dt, profile=True)
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()
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_profile_in_run_raises(): set_device('cuda_standalone', directory=None, build_on_run=False) G = NeuronGroup(1, 'v:1', threshold='True') assert_raises(TypeError, lambda: run(defaultclock.dt, profile=False)) assert_raises(TypeError, lambda: run(defaultclock.dt, profile='string')) run(defaultclock.dt, profile=True)
def test_profile_wrong_raises(): # Test that profiling info is not 0 set_device('cuda_standalone', profile='wrong', directory=None) G = NeuronGroup(1, 'v:1', threshold='True') # error raised in device.build() assert_raises(TypeError, lambda: run(defaultclock.dt))
def test_constant_replacement(): # see github issue #1276 set_device('cpp_standalone') x = 42 G = NeuronGroup(1, 'y : 1') G.y = 'x' run(0*ms) assert G.y[0] == 42.
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)
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)
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_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_multiple_runs_report_standalone_3(with_output=False): set_device('cpp_standalone', build_on_run=False) group = NeuronGroup(1, 'dv/dt = 1*Hz : 1') run(1*ms, report='text') run(1*ms, report='text') tempdir = tempfile.mkdtemp() if with_output: print tempdir device.build(directory=tempdir, compile=True, run=True, with_output=with_output)
def test_run_with_debug(): # We just want to make sure that it works for now (i.e. not fails with a # compilation or runtime error), capturing the output is actually # a bit involved to get right. set_device('cpp_standalone', build_on_run=True, debug=True, directory=None) group = NeuronGroup(1, 'v: 1', threshold='False') syn = Synapses(group, group, on_pre='v += 1') syn.connect() mon = SpikeMonitor(group) run(defaultclock.dt)
def test_profile_False(): # Test that profiling info is 0 set_device('cuda_standalone', profile=False, directory=None) G = NeuronGroup(1, 'v:1', threshold='True') run(defaultclock.dt) prof_info = magic_network.profiling_info assert all([prof_info[n][1] for n in range(len(prof_info))])
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()
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()
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_changing_profile_arg(): set_device('cpp_standalone', build_on_run=False) G = NeuronGroup(10000, 'v : 1') op1 = G.run_regularly('v = exp(-v)', name='op1') op2 = G.run_regularly('v = exp(-v)', name='op2') op3 = G.run_regularly('v = exp(-v)', name='op3') op4 = G.run_regularly('v = exp(-v)', name='op4') # Op 1 is active only during the first profiled run # Op 2 is active during both profiled runs # Op 3 is active only during the second profiled run # Op 4 is never active (only during the unprofiled run) op1.active = True op2.active = True op3.active = False op4.active = False run(1000 * defaultclock.dt, profile=True) op1.active = True op2.active = True op3.active = True op4.active = True run(1000 * defaultclock.dt, profile=False) op1.active = False op2.active = True op3.active = True op4.active = False run(1000 * defaultclock.dt, profile=True) device.build(directory=None, with_output=False) profiling_dict = dict(magic_network.profiling_info) # Note that for now, C++ standalone creates a new CodeObject for every run, # which is most of the time unnecessary (this is partly due to the way we # handle constants: they are included as literals in the code but they can # change between runs). Therefore, the profiling info is potentially # difficult to interpret assert len(profiling_dict) == 4 # 2 during first run, 2 during last run # The two code objects that were executed during the first run assert ('op1_codeobject' in profiling_dict and profiling_dict['op1_codeobject'] > 0 * second) assert ('op2_codeobject' in profiling_dict and profiling_dict['op2_codeobject'] > 0 * second) # Four code objects were executed during the second run, but no profiling # information was saved for name in [ 'op1_codeobject_1', 'op2_codeobject_1', 'op3_codeobject', 'op4_codeobject' ]: assert name not in profiling_dict # Two code objects were exectued during the third run assert ('op2_codeobject_2' in profiling_dict and profiling_dict['op2_codeobject_2'] > 0 * second) assert ('op3_codeobject_1' in profiling_dict and profiling_dict['op3_codeobject_1'] > 0 * second)
def test_continued_standalone_runs(): # see github issue #1237 set_device('cpp_standalone', build_on_run=False) source = SpikeGeneratorGroup(1, [0], [0]*ms) target = NeuronGroup(1, 'v : 1') C_ee = Synapses(source, target, on_pre='v += 1', delay=2*ms) C_ee.connect() run(1*ms) # Spike has not been delivered yet run(2*ms) device.build(directory=None) assert target.v[0] == 1 # Make sure the spike got delivered
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_changing_profile_arg(): set_device('cpp_standalone', build_on_run=False) G = NeuronGroup(10000, 'v : 1') op1 = G.run_regularly('v = exp(-v)', name='op1') op2 = G.run_regularly('v = exp(-v)', name='op2') op3 = G.run_regularly('v = exp(-v)', name='op3') op4 = G.run_regularly('v = exp(-v)', name='op4') # Op 1 is active only during the first profiled run # Op 2 is active during both profiled runs # Op 3 is active only during the second profiled run # Op 4 is never active (only during the unprofiled run) op1.active = True op2.active = True op3.active = False op4.active = False run(100*defaultclock.dt, profile=True) op1.active = True op2.active = True op3.active = True op4.active = True run(100*defaultclock.dt, profile=False) op1.active = False op2.active = True op3.active = True op4.active = False run(100*defaultclock.dt, profile=True) device.build(directory=None, with_output=False) profiling_dict = dict(magic_network.profiling_info) # Note that for now, C++ standalone creates a new CodeObject for every run, # which is most of the time unnecessary (this is partly due to the way we # handle constants: they are included as literals in the code but they can # change between runs). Therefore, the profiling info is potentially # difficult to interpret assert len(profiling_dict) == 4 # 2 during first run, 2 during last run # The two code objects that were executed during the first run assert ('op1_codeobject' in profiling_dict and profiling_dict['op1_codeobject'] > 0*second) assert ('op2_codeobject' in profiling_dict and profiling_dict['op2_codeobject'] > 0*second) # Four code objects were executed during the second run, but no profiling # information was saved for name in ['op1_codeobject_1', 'op2_codeobject_1', 'op3_codeobject', 'op4_codeobject']: assert name not in profiling_dict # Two code objects were exectued during the third run assert ('op2_codeobject_2' in profiling_dict and profiling_dict['op2_codeobject_2'] > 0*second) assert ('op3_codeobject_1' in profiling_dict and profiling_dict['op3_codeobject_1'] > 0*second)
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()
def variableview_get_subexpression_with_index_array( self, variableview, item, level=0, run_namespace=None): if not self.has_been_run: raise NotImplementedError( 'Cannot retrieve the values of state ' 'variables in standalone code before the ' 'simulation has been run.') # Temporarily switch to the runtime device to evaluate the subexpression # (based on the values stored on disk) backup_device = get_device() set_device('runtime') result = VariableView.get_subexpression_with_index_array( variableview, item, level=level + 2, run_namespace=run_namespace) set_device(backup_device) return result
def test_profile_warning(): # Test that profiling info is not 0 set_device('cuda_standalone', directory=None) prefs.devices.cuda_standalone.profile = True G = NeuronGroup(1, 'v:1', threshold='True') BrianLogger._log_messages.clear() with catch_logs() as logs: run(defaultclock.dt, profile=True) assert len(logs) == 1, len(logs) assert logs[0][0] == 'WARNING' assert logs[0][1] == 'brian2.devices.cuda_standalone'
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()
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()
def variableview_get_subexpression_with_index_array(self, variableview, item, level=0, run_namespace=None): if not self.has_been_run: raise NotImplementedError('Cannot retrieve the values of state ' 'variables in standalone code before the ' 'simulation has been run.') # Temporarily switch to the runtime device to evaluate the subexpression # (based on the values stored on disk) backup_device = get_device() set_device('runtime') result = VariableView.get_subexpression_with_index_array(variableview, item, level=level+2, run_namespace=run_namespace) set_device(backup_device) return result
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()
def test_delete_directory(): set_device('cpp_standalone', build_on_run=True, directory=None) group = NeuronGroup(10, 'dv/dt = -v / (10*ms) : volt', method='exact') group.v = np.arange(10) * mV # uses the static array mechanism run(defaultclock.dt) # Add a new file dummy_file = os.path.join(device.project_dir, 'results', 'dummy.txt') open(dummy_file, 'w').flush() assert os.path.isfile(dummy_file) with catch_logs() as logs: device.delete(directory=True) assert len(logs) == 1 assert os.path.isfile(dummy_file) with catch_logs() as logs: device.delete(directory=True, force=True) assert len(logs) == 0 # everything should be deleted assert not os.path.exists(device.project_dir)
def test_delete_code_data(): set_device('cpp_standalone', build_on_run=True, directory=None) group = NeuronGroup(10, 'dv/dt = -v / (10*ms) : volt', method='exact') group.v = np.arange(10)*mV # uses the static array mechanism run(defaultclock.dt) results_dir = os.path.join(device.project_dir, 'results') assert os.path.exists(results_dir) and os.path.isdir(results_dir) # There should be 3 files for the clock, 2 for the neurongroup (index + v), # and the "last_run_info.txt" file assert len(os.listdir(results_dir)) == 6 device.delete(data=True, code=False, directory=False) assert os.path.exists(results_dir) and os.path.isdir(results_dir) assert len(os.listdir(results_dir)) == 0 assert len(os.listdir(os.path.join(device.project_dir, 'static_arrays'))) > 0 assert len(os.listdir(os.path.join(device.project_dir, 'code_objects'))) > 0 device.delete(data=False, code=True, directory=False) assert len(os.listdir(os.path.join(device.project_dir, 'static_arrays'))) == 0 assert len(os.listdir(os.path.join(device.project_dir, 'code_objects'))) == 0
def test_active_flag_standalone(with_output=True): set_device('cpp_standalone', build_on_run=False) G = NeuronGroup(1, 'dv/dt = 1/ms : 1') mon = StateMonitor(G, 'v', record=0) mon.active = False run(1*ms) mon.active = True G.active = False run(1*ms) tempdir = tempfile.mkdtemp() if with_output: print tempdir device.build(directory=tempdir) # Monitor should start recording at 1ms # Neurongroup should not integrate after 1ms (but should have integrated before) assert_allclose(mon[0].t[0], 1*ms) assert_allclose(mon[0].v, 1.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()
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 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.')
def test_schedule_warning(): previous_device = get_device() from uuid import uuid4 # TestDevice1 supports arbitrary schedules, TestDevice2 does not class TestDevice1(Device): pass class TestDevice2(Device): 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') set_device(previous_device)
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)
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()
test_magic_collect, test_progress_report, test_progress_report_incorrect, test_multiple_runs_report_standalone, test_multiple_runs_report_standalone_2, test_multiple_runs_report_standalone_3, test_multiple_runs_report_standalone_incorrect, test_store_restore, test_store_restore_to_file, test_store_restore_to_file_new_objects, test_store_restore_to_file_differing_nets, test_store_restore_magic, test_store_restore_magic_to_file, test_defaultclock_dt_changes, test_dt_changes_between_runs, test_dt_restore, test_continuation, test_get_set_states, test_multiple_runs_defaultclock, test_multiple_runs_defaultclock_incorrect, test_profile, test_profile_ipython_html, test_magic_scope, test_runtime_rounding, test_small_runs, test_long_run_dt_change, ]: set_device(all_devices['runtime']) t() reinit_devices()
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 run(codegen_targets=None, long_tests=False, test_codegen_independent=True, test_standalone=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. ''' try: import nose except ImportError: raise ImportError('Running the test suite requires the "nose" package.') 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) 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') if test_codegen_independent: sys.stderr.write('Testing codegen-independent code \n') sys.stderr.write('\n') # Store the currently set preferences and reset to default preferences stored_prefs = prefs.as_file prefs.read_preference_file(StringIO(prefs.defaults_as_file)) # Switch off code optimization to get faster compilation times prefs['codegen.cpp.extra_compile_args_gcc'] = ['-w', '-O0'] 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() success.append(nose.run(argv=['', dirname, '-c=', # no config file loading '-I', '^hears\.py$', '-I', '^\.', '-I', '^_', '--with-doctest', "-a", "codegen-independent", '--nologcapture', '--exe'])) for target in codegen_targets: sys.stderr.write('Running tests for target %s:\n' % target) prefs.codegen.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 success.append(nose.run(argv=['', 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 test_standalone: from brian2.devices.device import get_device, set_device previous_device = get_device() set_device(test_standalone + '_simple') 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 '' success.append(nose.run(argv=['', dirname, '-c=', # no config file loading '-I', '^hears\.py$', '-I', '^\.', '-I', '^_', # Only run standalone tests '-a', 'standalone-compatible'+exclude_str, '--nologcapture', '--exe'])) set_device(previous_device) sys.stderr.write('Running standalone-specific tests\n') success.append(nose.run(argv=['', dirname, '-c=', # no config file loading '-I', '^hears\.py$', '-I', '^\.', '-I', '^_', # Only run standalone tests '-a', test_standalone+exclude_str, '--nologcapture', '--exe'])) 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: # Restore the user preferences prefs.read_preference_file(StringIO(stored_prefs)) prefs._backup()
def test_multiple_runs_report_standalone_incorrect(with_output=False): set_device('cpp_standalone', build_on_run=False) group = NeuronGroup(1, 'dv/dt = 1*Hz : 1') run(1*ms, report='text') assert_raises(NotImplementedError, lambda: run(1*ms, report='stderr'))
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()