Example #1
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 #2
0
def test_poisson_scalar_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, 'dv/dt = -v/(10*ms) + 0.1*poisson(5)/ms : 1')
    mon = StateMonitor(G, 'v', record=True)

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

    # second run
    seed(13579)
    G.v = 'poisson(5)'
    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 #3
0
def test_linked_subexpression_2():
    '''
    Test a linked variable referring to a subexpression without indices
    '''
    G = NeuronGroup(2, '''dv/dt = 100*Hz : 1
                          I = clip(v, 0, inf) : 1''',
                    threshold='v>1', reset='v=0')
    G.v = [0, .5]
    G2 = NeuronGroup(2, '''I_l : 1 (linked) ''')

    G2.I_l = linked_var(G.I)
    mon1 = StateMonitor(G, 'I', record=True)
    mon = StateMonitor(G2, 'I_l', record=True)
    run(5*ms)

    assert all(mon[0].I_l == mon1[0].I)
    assert all(mon[1].I_l == mon1[1].I)
Example #4
0
def test_linked_variable_correct():
    '''
    Test correct uses of linked variables.
    '''
    tau = 10*ms
    G1 = NeuronGroup(10, 'dv/dt = -v / tau : volt')
    G1.v = linspace(0*mV, 20*mV, 10)
    G2 = NeuronGroup(10, 'v : volt (linked)')
    G2.v = linked_var(G1.v)
    mon1 = StateMonitor(G1, 'v', record=True)
    mon2 = StateMonitor(G2, 'v', record=True)
    run(10*ms)
    assert_equal(mon1.v[:, :], mon2.v[:, :])
    # Make sure that printing the variable values works
    assert len(str(G2.v)) > 0
    assert len(repr(G2.v)) > 0
    assert len(str(G2.v[:])) > 0
    assert len(repr(G2.v[:])) > 0
Example #5
0
def test_rand():
    G = NeuronGroup(1000, 'dv/dt = rand() : second')
    mon = StateMonitor(G, 'v', record=True)

    run(3 * defaultclock.dt)

    print(mon.v[:5, :])
    assert_raises(AssertionError, assert_equal, mon.v[:, -1], 0)
    assert_raises(AssertionError, assert_equal, mon.v[:, -2], mon.v[:, -1])
Example #6
0
def test_linked_subexpression_3():
    '''
    Test a linked variable referring to a subexpression with indices
    '''
    G = NeuronGroup(2, '''dv/dt = 100*Hz : 1
                          I = clip(v, 0, inf) : 1''',
                    threshold='v>1', reset='v=0')
    G.v = [0, .5]
    G2 = NeuronGroup(10, '''I_l : 1 (linked) ''')

    G2.I_l = linked_var(G.I, index=np.array([0, 1]).repeat(5))
    mon1 = StateMonitor(G, 'I', record=True)
    mon = StateMonitor(G2, 'I_l', record=True)
    run(5*ms)

    # Due to the linking, the first 5 and the second 5 recorded I vectors should
    # refer to the
    assert all((all(mon[i].I_l == mon1[0].I) for i in xrange(5)))
    assert all((all(mon[i+5].I_l == mon1[1].I) for i in xrange(5)))
Example #7
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 #8
0
def test_linked_variable_scalar():
    '''
    Test linked variable from a size 1 group.
    '''
    G1 = NeuronGroup(1, 'dx/dt = -x / (10*ms) : 1')
    G2 = NeuronGroup(10, '''dy/dt = (-y + x) / (20*ms) : 1
                            x : 1 (linked)''')
    G1.x = 1
    G2.y = np.linspace(0, 1, 10)
    G2.x = linked_var(G1.x)
    mon = StateMonitor(G2, 'y', record=True)
    net = Network(G1, G2, mon)
    net.run(10*ms)
Example #9
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 #10
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 #11
0
def test_linked_subexpression():
    '''
    Test a subexpression referring to a linked variable.
    '''
    G = NeuronGroup(2, 'dv/dt = 100*Hz : 1',
                    threshold='v>1', reset='v=0')
    G.v = [0, .5]
    G2 = NeuronGroup(10, '''I = clip(x, 0, inf) : 1
                            x : 1 (linked) ''')

    G2.x = linked_var(G.v, index=np.array([0, 1]).repeat(5))
    mon = StateMonitor(G2, 'I', record=True)
    run(5*ms)

    # Due to the linking, the first 5 and the second 5 recorded I vectors should
    # be identical
    assert all((all(mon[i].I == mon[0].I) for i in xrange(5)))
    assert all((all(mon[i+5].I == mon[5].I) for i in xrange(5)))
Example #12
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 #13
0
def test_linked_variable_scalar():
    '''
    Test linked variable from a size 1 group.
    '''
    G1 = NeuronGroup(1, 'dx/dt = -x / (10*ms) : 1')
    G2 = NeuronGroup(10, '''dy/dt = (-y + x) / (20*ms) : 1
                            x : 1 (linked)''')
    G1.x = 1
    G2.y = np.linspace(0, 1, 10)
    G2.x = linked_var(G1.x)
    mon = StateMonitor(G2, 'y', record=True)
    # We don't test anything for now, except that it runs without raising an
    # error
    run(10*ms)
    # Make sure that printing the variable values works
    assert len(str(G2.x)) > 0
    assert len(repr(G2.x)) > 0
    assert len(str(G2.x[:])) > 0
    assert len(repr(G2.x[:])) > 0
Example #14
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 #15
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 #16
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)