Skip to content

Commit 6d77b8e

Browse files
Add test cases
1 parent 3ac0adf commit 6d77b8e

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

brian2/groups/neurongroup.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -944,15 +944,19 @@ def resting_state(self, x0 = {}):
944944
Dictioary with pair of state variables and resting state values. Returned values
945945
are represented in SI units.
946946
'''
947+
# check whether the model is currently unsupported
948+
if self.thresholder != {} or self.events != {}:
949+
raise NotImplementedError('Event based and Neuron-specific models are currently not supported for resting state calculation')
947950

948951
if(x0.keys() - self.equations.diff_eq_names):
949-
raise KeyError("Unknown State Variable: {}".format(next(iter(x0.keys() - self.equations.diff_eq_names))))
952+
raise KeyError("Unknown State Variable: {}".format(next(iter(x0.keys() -
953+
self.equations.diff_eq_names))))
950954

951955
# Add 0 as the intial value for non-mentioned state variables in x0
952956
x0.update({name : 0 for name in self.equations.diff_eq_names - x0.keys()})
953-
954-
return dict(zip(sorted(self.equations.diff_eq_names), root(_wrapper, list(dict(sorted(x0.items())).values()),
955-
args = (self.equations, get_local_namespace(1))).x))
957+
sorted_variable_values = list(dict(sorted(x0.items())).values())
958+
result = root(_wrapper, sorted_variable_values, args = (self.equations, get_local_namespace(1)))
959+
return dict(zip(sorted(self.equations.diff_eq_names), result.x))
956960

957961
def _evaluate_rhs(eqs, values, namespace=None):
958962
"""

brian2/tests/test_neurongroup.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import division
22
from __future__ import absolute_import
33
import uuid
4-
4+
import sys
55
import sympy
66
import numpy as np
77
from numpy.testing.utils import assert_raises, assert_equal
@@ -22,8 +22,8 @@
2222
from brian2.units.fundamentalunits import (DimensionMismatchError,
2323
have_same_dimensions)
2424
from brian2.units.unitsafefunctions import linspace
25-
from brian2.units.allunits import second, volt
26-
from brian2.units.stdunits import ms, mV, Hz
25+
from brian2.units.allunits import second, volt, umetre, siemens, ufarad
26+
from brian2.units.stdunits import ms, mV, Hz, cm
2727
from brian2.utils.logger import catch_logs
2828

2929
from brian2.tests.utils import assert_allclose
@@ -1639,6 +1639,36 @@ def test_semantics_mod():
16391639
assert_allclose(G.x[:], float_values % 3)
16401640
assert_allclose(G.y[:], float_values % 3)
16411641

1642+
def test_resting_value():
1643+
"""
1644+
Test the resting state values of the system
1645+
"""
1646+
# simple model with single dependent variable, here it is not necessary
1647+
# to run the model as the resting value is certain
1648+
epsilon = sys.float_info.epsilon
1649+
El = - 100
1650+
tau = 1 * ms
1651+
eqs = '''
1652+
dv/dt = (El - v)/tau : 1
1653+
'''
1654+
grp = NeuronGroup(1, eqs, method = 'exact')
1655+
resting_state = grp.resting_state()
1656+
assert abs(resting_state['v'] - El) < epsilon * max(abs(resting_state['v']), abs(El))
1657+
1658+
# one more example
1659+
area = 100 * umetre ** 2
1660+
g_L = 1e-2 * siemens * cm ** -2 * area
1661+
E_L = 1000
1662+
Cm = 1 * ufarad * cm ** -2 * area
1663+
grp = NeuronGroup(10, '''dv/dt = I_leak / Cm : volt
1664+
I_leak = g_L*(E_L - v) : amp''')
1665+
resting_state = grp.resting_state({'v': float(10000)})
1666+
assert abs(resting_state['v'] - E_L) < epsilon * max(abs(resting_state['v']), abs(E_L))
1667+
1668+
# check unsupported models are identified
1669+
tau = 10 * ms
1670+
grp = NeuronGroup(1, 'dv/dt = -v/tau : volt', threshold='v > -50*mV', reset='v = -70*mV')
1671+
assert_raises(NotImplementedError, lambda: grp.resting_state())
16421672

16431673
if __name__ == '__main__':
16441674
test_set_states()
@@ -1714,3 +1744,4 @@ def test_semantics_mod():
17141744
test_semantics_floor_division()
17151745
test_semantics_floating_point_division()
17161746
test_semantics_mod()
1747+
test_resting_value()

0 commit comments

Comments
 (0)