Ejemplo n.º 1
0
def test_parse_equations():
    ''' Test the parsing of equation strings '''
    # A simple equation
    eqs = parse_string_equations('dv/dt = -v / tau : 1')
    assert len(eqs.keys()
               ) == 1 and 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert get_dimensions(eqs['v'].unit) == DIMENSIONLESS

    # A complex one
    eqs = parse_string_equations('''dv/dt = -(v +
                                             ge + # excitatory conductance
                                             I # external current
                                             )/ tau : volt
                                    dge/dt = -ge / tau_ge : volt
                                    I = sin(2 * pi * f * t) : volt
                                    f : Hz (constant)
                                    b : boolean
                                    n : integer
                                 ''')
    assert len(eqs.keys()) == 6
    assert 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert 'ge' in eqs and eqs['ge'].type == DIFFERENTIAL_EQUATION
    assert 'I' in eqs and eqs['I'].type == SUBEXPRESSION
    assert 'f' in eqs and eqs['f'].type == PARAMETER
    assert 'b' in eqs and eqs['b'].type == PARAMETER
    assert 'n' in eqs and eqs['n'].type == PARAMETER
    assert eqs['f'].var_type == FLOAT
    assert eqs['b'].var_type == BOOLEAN
    assert eqs['n'].var_type == INTEGER
    assert get_dimensions(eqs['v'].unit) == volt.dim
    assert get_dimensions(eqs['ge'].unit) == volt.dim
    assert get_dimensions(eqs['I'].unit) == volt.dim
    assert get_dimensions(eqs['f'].unit) == Hz.dim
    assert eqs['v'].flags == []
    assert eqs['ge'].flags == []
    assert eqs['I'].flags == []
    assert eqs['f'].flags == ['constant']

    duplicate_eqs = '''
    dv/dt = -v / tau : 1
    v = 2 * t : 1
    '''
    assert_raises(EquationError, lambda: parse_string_equations(duplicate_eqs))
    parse_error_eqs = [
        '''dv/d = -v / tau : 1
        x = 2 * t : 1''', '''dv/dt = -v / tau : 1 : volt
    x = 2 * t : 1''', ''' dv/dt = -v / tau : 2 * volt''',
        'dv/dt = v / second : boolean'
    ]
    for error_eqs in parse_error_eqs:
        assert_raises((ValueError, EquationError, TypeError),
                      lambda: parse_string_equations(error_eqs))
Ejemplo n.º 2
0
def test_parse_equations():
    ''' Test the parsing of equation strings '''
    # A simple equation
    eqs = parse_string_equations('dv/dt = -v / tau : 1')
    assert len(eqs.keys()) == 1 and 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert get_dimensions(eqs['v'].unit) == DIMENSIONLESS

    # A complex one
    eqs = parse_string_equations('''dv/dt = -(v +
                                             ge + # excitatory conductance
                                             I # external current
                                             )/ tau : volt
                                    dge/dt = -ge / tau_ge : volt
                                    I = sin(2 * pi * f * t) : volt
                                    f : Hz (constant)
                                    b : boolean
                                    n : integer
                                 ''')
    assert len(eqs.keys()) == 6
    assert 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert 'ge' in eqs and eqs['ge'].type == DIFFERENTIAL_EQUATION
    assert 'I' in eqs and eqs['I'].type == SUBEXPRESSION
    assert 'f' in eqs and eqs['f'].type == PARAMETER
    assert 'b' in eqs and eqs['b'].type == PARAMETER
    assert 'n' in eqs and eqs['n'].type == PARAMETER
    assert eqs['f'].var_type == FLOAT
    assert eqs['b'].var_type == BOOLEAN
    assert eqs['n'].var_type == INTEGER
    assert get_dimensions(eqs['v'].unit) == volt.dim
    assert get_dimensions(eqs['ge'].unit) == volt.dim
    assert get_dimensions(eqs['I'].unit) == volt.dim
    assert get_dimensions(eqs['f'].unit) == Hz.dim
    assert eqs['v'].flags == []
    assert eqs['ge'].flags == []
    assert eqs['I'].flags == []
    assert eqs['f'].flags == ['constant']

    duplicate_eqs = '''
    dv/dt = -v / tau : 1
    v = 2 * t : 1
    '''
    assert_raises(EquationError, lambda: parse_string_equations(duplicate_eqs))
    parse_error_eqs = [
    '''dv/d = -v / tau : 1
        x = 2 * t : 1''',
    '''dv/dt = -v / tau : 1 : volt
    x = 2 * t : 1''',
    ''' dv/dt = -v / tau : 2 * volt''',
    'dv/dt = v / second : boolean']
    for error_eqs in parse_error_eqs:
        assert_raises((ValueError, EquationError, TypeError),
                      lambda: parse_string_equations(error_eqs))
Ejemplo n.º 3
0
def test_parse_equations():
    ''' Test the parsing of equation strings '''
    # A simple equation
    eqs = parse_string_equations('dv/dt = -v / tau : 1', {}, False, 0)    
    assert len(eqs.keys()) == 1 and 'v' in eqs and eqs['v'].eq_type == DIFFERENTIAL_EQUATION
    assert get_dimensions(eqs['v'].unit) == DIMENSIONLESS
    
    # A complex one
    eqs = parse_string_equations('''dv/dt = -(v +
                                             ge + # excitatory conductance
                                             I # external current
                                             )/ tau : volt
                                    dge/dt = -ge / tau_ge : volt
                                    I = sin(2 * pi * f * t) : volt
                                    f : Hz (constant)
                                 ''', 
                                 {}, False, 0)
    assert len(eqs.keys()) == 4
    assert 'v' in eqs and eqs['v'].eq_type == DIFFERENTIAL_EQUATION
    assert 'ge' in eqs and eqs['ge'].eq_type == DIFFERENTIAL_EQUATION
    assert 'I' in eqs and eqs['I'].eq_type == STATIC_EQUATION
    assert 'f' in eqs and eqs['f'].eq_type == PARAMETER
    assert get_dimensions(eqs['v'].unit) == volt.dim
    assert get_dimensions(eqs['ge'].unit) == volt.dim
    assert get_dimensions(eqs['I'].unit) == volt.dim
    assert get_dimensions(eqs['f'].unit) == Hz.dim
    assert eqs['v'].flags == []
    assert eqs['ge'].flags == []
    assert eqs['I'].flags == []
    assert eqs['f'].flags == ['constant']
    
    duplicate_eqs = '''
    dv/dt = -v / tau : 1
    v = 2 * t : 1
    '''
    assert_raises(SyntaxError, lambda: parse_string_equations(duplicate_eqs,
                                                              {}, False, 0))
    parse_error_eqs = [
    '''dv/d = -v / tau : 1
        x = 2 * t : 1''',
    '''dv/dt = -v / tau : 1 : volt
    x = 2 * t : 1''',
    ''' dv/dt = -v / tau : 2 * volt''']
    for error_eqs in parse_error_eqs:
        assert_raises((ValueError, SyntaxError), lambda: parse_string_equations(error_eqs,
                                                                                {}, False, 0))
Ejemplo n.º 4
0
def test_parse_equations():
    """ Test the parsing of equation strings """
    # A simple equation
    eqs = parse_string_equations('dv/dt = -v / tau : 1')
    assert len(
        eqs) == 1 and 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert eqs['v'].dim is DIMENSIONLESS

    # A complex one
    eqs = parse_string_equations("""dv/dt = -(v +
                                             ge + # excitatory conductance
                                             I # external current
                                             )/ tau : volt
                                    dge/dt = -ge / tau_ge : volt
                                    I = sin(2 * pi * f * t) : volt
                                    f : Hz (constant)
                                    b : boolean
                                    n : integer
                                 """)
    assert len(eqs) == 6
    assert 'v' in eqs and eqs['v'].type == DIFFERENTIAL_EQUATION
    assert 'ge' in eqs and eqs['ge'].type == DIFFERENTIAL_EQUATION
    assert 'I' in eqs and eqs['I'].type == SUBEXPRESSION
    assert 'f' in eqs and eqs['f'].type == PARAMETER
    assert 'b' in eqs and eqs['b'].type == PARAMETER
    assert 'n' in eqs and eqs['n'].type == PARAMETER
    assert eqs['f'].var_type == FLOAT
    assert eqs['b'].var_type == BOOLEAN
    assert eqs['n'].var_type == INTEGER
    assert eqs['v'].dim is volt.dim
    assert eqs['ge'].dim is volt.dim
    assert eqs['I'].dim is volt.dim
    assert eqs['f'].dim is Hz.dim
    assert eqs['v'].flags == []
    assert eqs['ge'].flags == []
    assert eqs['I'].flags == []
    assert eqs['f'].flags == ['constant']

    duplicate_eqs = """
    dv/dt = -v / tau : 1
    v = 2 * t : 1
    """
    with pytest.raises(EquationError):
        parse_string_equations(duplicate_eqs)
    parse_error_eqs = [
        """dv/d = -v / tau : 1
        x = 2 * t : 1""", """dv/dt = -v / tau : 1 : volt
    x = 2 * t : 1""", """ dv/dt = -v / tau : 2 * volt""",
        'dv/dt = v / second : boolean'
    ]
    for error_eqs in parse_error_eqs:
        with pytest.raises((ValueError, EquationError, TypeError)):
            parse_string_equations(error_eqs)
Ejemplo n.º 5
0
def test_simple_neurongroup():
    """
    Test dictionary representation of simple NeuronGroup
    """
    # example 1
    eqn = '''dv/dt = (1 - v) / tau : volt'''
    tau = 10 * ms
    size = 1

    grp = NeuronGroup(size, eqn, method='exact')
    neuron_dict = collect_NeuronGroup(grp, get_local_namespace(0))

    assert neuron_dict['N'] == size
    assert neuron_dict['user_method'] == 'exact'
    assert neuron_dict['equations']['v']['type'] == DIFFERENTIAL_EQUATION
    assert neuron_dict['equations']['v']['unit'] == volt
    assert neuron_dict['equations']['v']['var_type'] == FLOAT

    with pytest.raises(KeyError):
        neuron_dict['equations']['tau']

    with pytest.raises(KeyError):
        neuron_dict['run_regularly']

    eqn_obj = Equations(eqn)
    assert neuron_dict['equations']['v']['expr'] == eqn_obj['v'].expr.code
    assert neuron_dict['identifiers']['tau'] == 10 * ms
    with pytest.raises(KeyError):
        neuron_dict['identifiers']['size']
    assert neuron_dict['when'] == 'groups'
    assert neuron_dict['order'] == 0

    # example 2
    start_scope()
    area = 100 * umetre**2
    g_L = 1e-2 * siemens * cm**-2 * area
    E_L = 1000
    div_2 = 2
    dim_2 = 0.02 * amp
    Cm = 1 * ufarad * cm**-2 * area
    grp = NeuronGroup(
        10, '''dv/dt = I_leak / Cm : volt
                        I_leak = g_L*(E_L - v) : amp''')
    grp.run_regularly('v = v / div_2', dt=20 * ms, name='i_am_run_reg_senior')
    grp.run_regularly('I_leak = I_leak + dim_2',
                      dt=10 * ms,
                      name='i_am_run_reg_junior')

    neuron_dict = collect_NeuronGroup(grp, get_local_namespace(0))

    assert neuron_dict['N'] == 10
    assert neuron_dict['user_method'] is None
    assert neuron_dict['when'] == 'groups'
    assert neuron_dict['order'] == 0
    eqn_str = '''
    dv/dt = I_leak / Cm : volt
    I_leak = g_L*(E_L - v) : amp
    '''
    assert neuron_dict['equations']['v']['type'] == DIFFERENTIAL_EQUATION
    assert neuron_dict['equations']['v']['unit'] == volt
    assert neuron_dict['equations']['v']['var_type'] == FLOAT

    parsed = parse_string_equations(eqn_str)
    assert neuron_dict['equations']['v']['expr'] == parsed['v'].expr.code

    assert neuron_dict['equations']['I_leak']['type'] == SUBEXPRESSION
    assert neuron_dict['equations']['I_leak']['unit'] == amp
    assert neuron_dict['equations']['I_leak']['var_type'] == FLOAT
    assert neuron_dict['equations']['I_leak']['expr'] == 'g_L*(E_L - v)'
    assert neuron_dict['identifiers']['g_L'] == g_L
    assert neuron_dict['identifiers']['Cm'] == Cm
    assert neuron_dict['identifiers']['div_2'] == div_2
    assert neuron_dict['identifiers']['dim_2'] == dim_2

    with pytest.raises(KeyError):
        neuron_dict['events']
    with pytest.raises(KeyError):
        neuron_dict['identifiers']['area']

    assert neuron_dict['run_regularly'][0]['name'] == 'i_am_run_reg_senior'
    assert neuron_dict['run_regularly'][1]['name'] == 'i_am_run_reg_junior'
    assert neuron_dict['run_regularly'][0]['code'] == 'v = v / div_2'
    assert (
        neuron_dict['run_regularly'][1]['code'] == 'I_leak = I_leak + dim_2')
    assert neuron_dict['run_regularly'][0]['dt'] == 20 * ms
    assert neuron_dict['run_regularly'][1]['dt'] == 10 * ms
    assert neuron_dict['run_regularly'][0]['when'] == 'start'
    assert neuron_dict['run_regularly'][1]['when'] == 'start'
    assert neuron_dict['run_regularly'][0]['order'] == 0
    assert neuron_dict['run_regularly'][1]['order'] == 0

    with pytest.raises(IndexError):
        neuron_dict['run_regularly'][2]
Ejemplo n.º 6
0
m1 = StateMonitor(G,'v',record=9)
M = StateMonitor(G, 'I', record=9)
m2 = SpikeMonitor(P)

net = Network(collect())
net.run(100*ms)
visualise_connectivity(S)

# store("test4","../../Data/test4")

# start_scope()
# G1 = NeuronGroup(10, equ, threshold='v > 0.9', reset='v = 0', method='linear',refractory=1*ms )
# m_g1=StateMonitor(G,'v',record=9)
# net.add(m_g1)
from brian2.equations.equations import parse_string_equations
G.equations._equations['I'] = parse_string_equations("I = (g-h)*30 : 1")['I']
G.equations._equations.pop('I')
G.equations = G.equations+("I = (g-h)*40 : 1")
G.variables._variables['I'].expr = '(g-h)*10'
M.variables._variables['_source_I'].expr = '(__source_I_neurongroup_g-__source_I_neurongroup_h)*10'

S.pre.code = on_pre
# net.run(100*ms)


# net.remove(G)
# net.remove(S)
# net.remove(m1)
# net.remove(M)
# net.remove(m_g1)