Example #1
0
def test_poisson_vectorized_values_fixed_and_random_seed():

    if prefs.core.default_float_dtype is np.float32:
        # TODO: Make test single-precision compatible, see #262
        pytest.skip('Need double precision for this test')

    G = NeuronGroup(
        10, '''l: 1
                       dv/dt = -v/(10*ms) + 0.1*poisson(l)/ms : 1''')
    G.l = arange(10)
    mon = StateMonitor(G, 'v', record=True)

    # first run
    seed(13579)
    G.v = 'poisson(l)'
    seed()
    run(2 * defaultclock.dt)

    # second run
    seed(13579)
    G.v = 'poisson(l)'
    seed()
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])

    # First time step should be identical (same seed)
    assert_allclose(first_run_values[:, 0], second_run_values[:, 0])
    # Second should be different (random seed)
    with pytest.raises(AssertionError):
        assert_allclose(first_run_values[:, 1], second_run_values[:, 1])
Example #2
0
def test_random_values_fixed_and_random_seed():
    G = NeuronGroup(10, 'dv/dt = -v/(10*ms) + 0.1*xi/sqrt(ms) : 1')
    mon = StateMonitor(G, 'v', record=True)

    # first run
    seed(13579)
    G.v = 'rand()'
    seed()
    run(2 * defaultclock.dt)

    # second run
    seed(13579)
    G.v = 'rand()'
    seed()
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])

    # First time step should be identical (same seed)
    assert_allclose(first_run_values[:, 0], second_run_values[:, 0])
    # Second should be different (random seed)
    assert_raises(AssertionError, assert_allclose, first_run_values[:, 1],
                  second_run_values[:, 1])
Example #3
0
def test_multiple_runs_function_change():
    inp = TimedArray([1, 2], dt=defaultclock.dt)
    group = NeuronGroup(1, 'v = inp(t) : 1')
    mon = StateMonitor(group, 'v', record=0)
    run(2 * defaultclock.dt)
    inp = TimedArray([0, 0, 3, 4], dt=defaultclock.dt)
    run(2 * defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert_equal(mon.v[0], [1, 2, 3, 4])
Example #4
0
def test_multiple_runs_constant_change():
    const_v = 1
    group = NeuronGroup(1, 'v = const_v : 1')
    mon = StateMonitor(group, 'v', record=0)
    run(defaultclock.dt)
    const_v = 2
    run(defaultclock.dt)
    device.build(direct_call=False, **device.build_options)
    assert_equal(mon.v[0], [1, 2])
Example #5
0
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)
Example #6
0
def test_random_number_generation_with_multiple_runs():
    G = NeuronGroup(1000, 'dv/dt = rand() : second')
    mon = StateMonitor(G, 'v', record=True)

    run(1 * defaultclock.dt)
    run(2 * defaultclock.dt)
    device.build(direct_call=False, **device.build_options)

    assert_raises(AssertionError, assert_equal, mon.v[:, -1], 0)
    assert_raises(AssertionError, assert_equal, mon.v[:, -2], mon.v[:, -1])
Example #7
0
def test_dt_changes_between_runs():
    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)
    device.build(direct_call=False, **device.build_options)
    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)
Example #8
0
def test_binomial_values_synapse_dynamics_fixed_and_random_seed():

    if prefs.core.default_float_dtype is np.float32:
        # TODO: Make test single-precision compatible, see #262
        pytest.skip('Need double precision for this test')

    my_f = BinomialFunction(100, 0.1, approximate=False)
    my_f_approximated = BinomialFunction(100, 0.1, approximate=True)

    G = NeuronGroup(10, 'z : 1')
    S = Synapses(
        G, G,
        'dv/dt = -v/(10*ms) + 0.1*(my_f() + my_f_approximated())*xi/sqrt(ms) : 1'
    )
    S.connect()
    mon = StateMonitor(S, 'v', record=range(100))

    # first run
    S.v = 0
    seed()
    run(2 * defaultclock.dt)

    # third run
    S.v = 0
    seed()
    run(2 * defaultclock.dt)

    # third run
    S.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    # fourth run
    S.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])
    third_run_values = np.array(mon.v[:, [4, 5]])
    fourth_run_values = np.array(mon.v[:, [6, 7]])

    # First and second run should be different (random seed)
    with pytest.raises(AssertionError):
        assert_allclose(first_run_values, second_run_values)
    # Third and fourth run should be identical (same seed)
    assert_allclose(third_run_values, fourth_run_values)
Example #9
0
def test_poisson_variable_lambda_values_fixed_and_random_seed():

    if prefs.core.default_float_dtype is np.float32:
        # TODO: Make test single-precision compatible, see #262
        pytest.skip('Need double precision for this test')

    G = NeuronGroup(
        10, '''l : 1
                       dv/dt = -v/(10*ms) + 0.1*poisson(l)*xi/sqrt(ms) : 1''')
    G.l = arange(10)

    mon = StateMonitor(G, 'v', record=True)

    # first run
    G.v = 0
    seed()
    run(2 * defaultclock.dt)

    # second run
    G.v = 0
    seed()
    run(2 * defaultclock.dt)

    # third run
    G.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    # fourth run
    G.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])
    third_run_values = np.array(mon.v[:, [4, 5]])
    fourth_run_values = np.array(mon.v[:, [6, 7]])

    # First and second run should be different (random seed)
    with pytest.raises(AssertionError):
        assert_allclose(first_run_values, second_run_values)
    # Third and fourth run should be identical (same seed)
    assert_allclose(third_run_values, fourth_run_values)
Example #10
0
def test_binomial_values_synapse_dynamics_fixed_and_random_seed():
    my_f = BinomialFunction(100, 0.1, approximate=False)
    my_f_approximated = BinomialFunction(100, 0.1, approximate=True)

    G = NeuronGroup(10, 'z : 1')
    S = Synapses(
        G, G,
        'dv/dt = -v/(10*ms) + 0.1*(my_f() + my_f_approximated())*xi/sqrt(ms) : 1'
    )
    S.connect()
    mon = StateMonitor(S, 'v', record=range(100))

    # first run
    S.v = 0
    seed()
    run(2 * defaultclock.dt)

    # third run
    S.v = 0
    seed()
    run(2 * defaultclock.dt)

    # third run
    S.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    # fourth run
    S.v = 0
    seed(13579)
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])
    third_run_values = np.array(mon.v[:, [4, 5]])
    fourth_run_values = np.array(mon.v[:, [6, 7]])

    # First and second run should be different (random seed)
    assert_raises(AssertionError, assert_allclose, first_run_values,
                  second_run_values)
    # Third and fourth run should be identical (same seed)
    assert_allclose(third_run_values, fourth_run_values)
Example #11
0
def example_run(device_name="cuda_standalone",
                directory=None,
                **build_options):
    """
    Run a simple example simulation to test whether Brian2CUDA is correctly set up.

    Parameters
    ----------
    device_name : str
        What device to use (default: "cuda_standalone").
    directory : str ,optional
        The output directory to write the project to, any existing files will be
        overwritten. If the given directory name is ``None`` (default for this example
        run), then a temporary directory will be used.
    build_options : dict, optional
        Additional options that will be forwarded to the ``device.build`` call,
    """
    from brian2.devices.device import device, set_device
    from brian2 import ms, NeuronGroup, run
    import brian2cuda
    import numpy as np
    from numpy.testing import assert_allclose

    set_device(device_name, build_on_run=False)
    N = 100
    tau = 10 * ms
    G = NeuronGroup(
        N,
        "dv/dt = -v / tau: 1",
        threshold="v > 1",
        reset="v = 0",
        refractory=5 * ms,
        method="linear",
    )
    G.v = "i / 100."
    run(1 * ms)
    device.build(direct_call=False, directory=directory, **build_options)
    assert_allclose(G.v, np.arange(N) / N * np.exp(-1 * ms / tau))
    device.reinit()
    device.activate()
    print("\nExample run was successful.")
Example #12
0
def test_random_values_codeobject_every_tick():
    G = NeuronGroup(10, 'dv/dt = -v/(10*ms) + 0.1*xi/sqrt(ms) : 1')
    mon = StateMonitor(G, 'v', record=True)

    # first run
    seed(10124)
    G.v = 'rand()'
    run(2 * defaultclock.dt)

    # second run
    seed(10124)
    G.v = 'rand()'
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    first_run_values = np.array(mon.v[:, [0, 1]])
    second_run_values = np.array(mon.v[:, [2, 3]])

    # First time step should be identical (same seed)
    assert_allclose(first_run_values[:, 0], second_run_values[:, 0])
    # Second should also be identical (same seed)
    assert_allclose(first_run_values[:, 1], second_run_values[:, 1])
Example #13
0
def test_multiple_runs_report_standalone_3():
    group = NeuronGroup(1, 'dv/dt = 1*Hz : 1')
    run(1 * ms, report='text')
    run(1 * ms, report='text')
    device.build(direct_call=False, **device.build_options)
Example #14
0
def test_binomial_values():

    if prefs.core.default_float_dtype is np.float32:
        # TODO: Make test single-precision compatible, see #262
        pytest.skip('Need double precision for this test')

    # On Denis' local computer this test blows up all available RAM + SWAP when
    # compiling with all threads in parallel. Use half the threads instead.
    import socket
    if socket.gethostname() == 'selene':
        prefs.devices.cpp_standalone.extra_make_args_unix = ['-j4']

    my_f_approximated = BinomialFunction(100, 0.1, approximate=True)
    my_f = BinomialFunction(100, 0.1, approximate=False)

    # Test neurongroup every tick objects
    G = NeuronGroup(10,
                    '''dx/dt = my_f_approximated()/ms: 1
                          dy/dt = my_f()/ms: 1''',
                    threshold='True')
    G.run_regularly('''x = my_f_approximated()
                       y = my_f()''')

    # Test synapses every tick objects (where N is not known at compile time)
    syn = Synapses(
        G,
        G,
        model='''
                         dw/dt = my_f()/ms: 1
                         dv/dt = my_f_approximated()/ms: 1
                         ''',
        on_pre='''
                          x += w
                          y += v
                          '''

        # TODO: fails when having binomial here, why?
        #on_pre='''
        #       x += w * my_f()
        #       y += v * my_f_approximated()
        #       '''
    )
    # Test synapses generation, which needs host side binomial
    syn.connect(condition='my_f_approximated() < 100')

    mon = StateMonitor(G, ['x', 'y'], record=True)
    w_mon = StateMonitor(syn, ['w', 'v'], record=np.arange(100))

    def init_group_variables(my_f, my_f_approximated):
        # Test codeobjects run only once outside the network,
        # G.x uses group_variable_set_conditional, G.x[:5] uses group_variable_set
        # Synapse objects (N not known at compile time)
        syn.w = 'my_f()'
        syn.w['i < j'] = 'my_f_approximated()'
        syn.v = 'my_f_approximated()'
        syn.v['i < j'] = 'my_f()'
        # Neurongroup object
        G.x = 'my_f_approximated()'
        G.x[:5] = 'my_f()'
        G.y = 'my_f()'
        G.y[:5] = 'my_f_approximated()'

    # first run
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # second run
    seed(11400)
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # third run
    seed()
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # forth run
    seed(11400)
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    run_values_1 = np.vstack([
        mon.x[:, [0, 1]], mon.y[:, [0, 1]], w_mon.w[:, [0, 1]], w_mon.v[:,
                                                                        [0, 1]]
    ])
    run_values_2 = np.vstack([
        mon.x[:, [2, 3]], mon.y[:, [2, 3]], w_mon.w[:, [2, 3]], w_mon.v[:,
                                                                        [2, 3]]
    ])
    run_values_3 = np.vstack([
        mon.x[:, [4, 5]], mon.y[:, [4, 5]], w_mon.w[:, [4, 5]], w_mon.v[:,
                                                                        [4, 5]]
    ])
    run_values_4 = np.vstack([
        mon.x[:, [6, 7]], mon.y[:, [6, 7]], w_mon.w[:, [6, 7]], w_mon.v[:,
                                                                        [6, 7]]
    ])

    # Two calls to binomial functions should return different numbers in all runs
    for n, values in enumerate(
        [run_values_1, run_values_2, run_values_3, run_values_4]):
        with pytest.raises(AssertionError):
            assert_allclose(values[:, 0], values[:, 1])

    # 2. and 4. run set the same seed
    assert_allclose(run_values_2, run_values_4)
    # all other combinations should be different
    with pytest.raises(AssertionError):
        assert_allclose(run_values_1, run_values_2)
    with pytest.raises(AssertionError):
        assert_allclose(run_values_1, run_values_3)
    with pytest.raises(AssertionError):
        assert_allclose(run_values_2, run_values_3)
Example #15
0
def test_binomial_values():
    my_f_approximated = BinomialFunction(100, 0.1, approximate=True)
    my_f = BinomialFunction(100, 0.1, approximate=False)

    # Test neurongroup every tick objects
    G = NeuronGroup(10,
                    '''dx/dt = my_f_approximated()/ms: 1
                          dy/dt = my_f()/ms: 1''',
                    threshold='True')
    G.run_regularly('''x = my_f_approximated()
                       y = my_f()''')

    # Test synapses every tick objects (where N is not known at compile time)
    syn = Synapses(
        G,
        G,
        model='''
                         dw/dt = my_f()/ms: 1
                         dv/dt = my_f_approximated()/ms: 1
                         ''',
        on_pre='''
                          x += w
                          y += v
                          '''

        # TODO: fails when having binomial here, why?
        #on_pre='''
        #       x += w * my_f()
        #       y += v * my_f_approximated()
        #       '''
    )
    # Test synapses generation, which needs host side binomial
    syn.connect(condition='my_f_approximated() < 100')

    mon = StateMonitor(G, ['x', 'y'], record=True)
    w_mon = StateMonitor(syn, ['w', 'v'], record=np.arange(100))

    def init_group_variables(my_f, my_f_approximated):
        # Test codeobjects run only once outside the network,
        # G.x uses group_variable_set_conditional, G.x[:5] uses group_variable_set
        # Synapse objects (N not known at compile time)
        syn.w = 'my_f()'
        syn.w['i < j'] = 'my_f_approximated()'
        syn.v = 'my_f_approximated()'
        syn.v['i < j'] = 'my_f()'
        # Neurongroup object
        G.x = 'my_f_approximated()'
        G.x[:5] = 'my_f()'
        G.y = 'my_f()'
        G.y[:5] = 'my_f_approximated()'

    # first run
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # second run
    seed(11400)
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # third run
    seed()
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    # forth run
    seed(11400)
    init_group_variables(my_f, my_f_approximated)
    run(2 * defaultclock.dt)

    device.build(direct_call=False, **device.build_options)

    run_values_1 = np.vstack([
        mon.x[:, [0, 1]], mon.y[:, [0, 1]], w_mon.w[:, [0, 1]], w_mon.v[:,
                                                                        [0, 1]]
    ])
    run_values_2 = np.vstack([
        mon.x[:, [2, 3]], mon.y[:, [2, 3]], w_mon.w[:, [2, 3]], w_mon.v[:,
                                                                        [2, 3]]
    ])
    run_values_3 = np.vstack([
        mon.x[:, [4, 5]], mon.y[:, [4, 5]], w_mon.w[:, [4, 5]], w_mon.v[:,
                                                                        [4, 5]]
    ])
    run_values_4 = np.vstack([
        mon.x[:, [6, 7]], mon.y[:, [6, 7]], w_mon.w[:, [6, 7]], w_mon.v[:,
                                                                        [6, 7]]
    ])

    # Two calls to binomial functions should return different numbers in all runs
    for n, values in enumerate(
        [run_values_1, run_values_2, run_values_3, run_values_4]):
        assert_raises(AssertionError, assert_allclose, values[:, 0], values[:,
                                                                            1])

    # 2. and 4. run set the same seed
    assert_allclose(run_values_2, run_values_4)
    # all other combinations should be different
    assert_raises(AssertionError, assert_allclose, run_values_1, run_values_2)
    assert_raises(AssertionError, assert_allclose, run_values_1, run_values_3)
    assert_raises(AssertionError, assert_allclose, run_values_2, run_values_3)