copying to personal repo
This commit is contained in:
36
cnmodel/util/tests/test_expfitting.py
Normal file
36
cnmodel/util/tests/test_expfitting.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from __future__ import print_function
|
||||
from cnmodel.util import ExpFitting
|
||||
import numpy as np
|
||||
|
||||
|
||||
def test_fit1():
|
||||
fit = ExpFitting(nexp=1)
|
||||
x = np.linspace(0.0, 50, 500)
|
||||
p = [-50.0, 4.0, 5.0]
|
||||
y = p[0] + p[1] * np.exp(-x / p[2])
|
||||
res = fit.fit(x, y, fit.fitpars)
|
||||
pr = [float(res[k].value) for k in res.keys()]
|
||||
print("\noriginal: ", p)
|
||||
print("fit res: ", pr)
|
||||
for i, v in enumerate(p):
|
||||
assert np.allclose(v, pr[i])
|
||||
|
||||
|
||||
def test_fit2():
|
||||
fit = ExpFitting(nexp=2)
|
||||
x = np.linspace(0.0, 50, 500)
|
||||
p = [
|
||||
-50.0,
|
||||
4.0,
|
||||
5.0,
|
||||
1.0,
|
||||
4.5,
|
||||
] # last term is ratio of the two time constants (t2 = delta*t1)
|
||||
y = p[0] + p[1] * np.exp(-x / p[2]) + p[3] * np.exp(-x / (p[2] * p[4]))
|
||||
res = fit.fit(x, y, fit.fitpars)
|
||||
pr = [float(res[k].value) for k in res.keys()]
|
||||
print("\noriginal: ", p)
|
||||
print("fit res: ", pr)
|
||||
# we can only do this approximately for 2 exp fits
|
||||
for i, v in enumerate(p): # test each one individually
|
||||
assert np.allclose(pr[i] / v, 1.0, atol=1e-4, rtol=1e-2)
|
||||
40
cnmodel/util/tests/test_matlab.py
Normal file
40
cnmodel/util/tests/test_matlab.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
|
||||
# from cnmodel.util.matlab_proc import MatlabProcess
|
||||
import matlab.engine
|
||||
|
||||
|
||||
def test_matlab():
|
||||
global proc
|
||||
try:
|
||||
# proc = MatlabProcess()
|
||||
proc = matlab.engine.start_matlab()
|
||||
except RuntimeError:
|
||||
# no matlab available; skip this test
|
||||
pytest.skip("MATLAB unavailable")
|
||||
|
||||
base_vcount = proc.who(nargout=1) # .shape[0]
|
||||
assert len(base_vcount) == 0
|
||||
|
||||
e4 = np.array(proc.eye(4, nargout=1))
|
||||
|
||||
assert isinstance(e4, np.ndarray)
|
||||
assert np.all(e4 == np.eye(4))
|
||||
# assert proc.who().shape[0] == base_vcount
|
||||
|
||||
o6_ref = np.array(proc.ones(6, nargout=1)) # _transfer=False))
|
||||
# o6 = o6_ref.get()
|
||||
o6 = np.array(proc.ones(6, nargout=1))
|
||||
assert np.all(o6 == np.ones(6))
|
||||
# print(proc.who(nargout=1))
|
||||
# assert proc.who(nargout=1) == base_vcount + 1
|
||||
|
||||
del o6_ref
|
||||
# assert proc.who().shape[0] == base_vcount
|
||||
|
||||
proc.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_matlab()
|
||||
98
cnmodel/util/tests/test_sound.py
Normal file
98
cnmodel/util/tests/test_sound.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import numpy as np
|
||||
from cnmodel.util import sound
|
||||
|
||||
|
||||
def test_conversions():
|
||||
pa = np.array([3990.5, 20, 0.3639, 2e-5])
|
||||
db = np.array([166, 120, 85.2, 0])
|
||||
|
||||
assert np.allclose(sound.pa_to_dbspl(pa), db, atol=0.1, rtol=0.002)
|
||||
assert np.allclose(sound.dbspl_to_pa(db), pa, atol=0.1, rtol=0.002)
|
||||
|
||||
|
||||
def test_tonepip():
|
||||
rate = 100000
|
||||
dur = 0.1
|
||||
ps = 0.01
|
||||
rd = 0.02
|
||||
pd = 0.08
|
||||
db = 60
|
||||
s1 = sound.TonePip(
|
||||
rate=rate,
|
||||
duration=dur,
|
||||
f0=5321,
|
||||
dbspl=db,
|
||||
pip_duration=pd,
|
||||
pip_start=[ps],
|
||||
ramp_duration=rd,
|
||||
)
|
||||
|
||||
# test array sizes
|
||||
assert s1.sound.size == s1.time.size == int(dur * rate) + 1
|
||||
|
||||
# test for consistency
|
||||
assert np.allclose(
|
||||
[s1.sound.min(), s1.sound.mean(), s1.sound.max()],
|
||||
[-0.028284253158247834, -1.0954891976168953e-10, 0.028284270354167296],
|
||||
)
|
||||
|
||||
# test that we got the requested amplitude
|
||||
assert np.allclose(s1.measure_dbspl(ps + rd, ps + pd - rd), db, atol=0.1, rtol=0.01)
|
||||
|
||||
# test for quiet before and after pip
|
||||
assert np.all(s1.sound[: int(ps * rate) - 1] == 0)
|
||||
assert np.all(s1.sound[int((ps + pd) * rate) + 1 :] == 0)
|
||||
|
||||
# test the sound can be recreated from its key
|
||||
key = s1.key()
|
||||
s2 = sound.create(**key)
|
||||
assert np.all(s1.time == s2.time)
|
||||
assert np.all(s1.sound == s2.sound)
|
||||
|
||||
|
||||
def test_noisepip():
|
||||
rate = 100000
|
||||
dur = 0.1
|
||||
ps = 0.01
|
||||
rd = 0.02
|
||||
pd = 0.08
|
||||
db = 60
|
||||
s1 = sound.NoisePip(
|
||||
rate=rate,
|
||||
duration=dur,
|
||||
seed=184724,
|
||||
dbspl=db,
|
||||
pip_duration=pd,
|
||||
pip_start=[ps],
|
||||
ramp_duration=rd,
|
||||
)
|
||||
|
||||
# test array sizes
|
||||
assert s1.sound.size == s1.time.size == int(dur * rate) + 1
|
||||
|
||||
# test for consistency
|
||||
assert np.allclose(
|
||||
[s1.sound.min(), s1.sound.mean(), s1.sound.max()],
|
||||
[-0.082260796003197786, -0.00018484322982972046, 0.069160217220832404],
|
||||
)
|
||||
|
||||
# test that we got the requested amplitude
|
||||
assert np.allclose(s1.measure_dbspl(ps + rd, ps + pd - rd), db, atol=0.1, rtol=0.01)
|
||||
|
||||
# test for quiet before and after pip
|
||||
assert np.all(s1.sound[: int(ps * rate) - 1] == 0)
|
||||
assert np.all(s1.sound[int((ps + pd) * rate) + 1 :] == 0)
|
||||
|
||||
# test the sound can be recreated from its key
|
||||
key = s1.key()
|
||||
s2 = sound.create(**key)
|
||||
# also test new seed works, and does not affect other sounds
|
||||
key["seed"] += 1
|
||||
s3 = sound.create(**key)
|
||||
s3.sound # generate here to advance rng before generating s2
|
||||
|
||||
assert np.all(s1.time == s2.time)
|
||||
assert np.all(s1.sound == s2.sound)
|
||||
start = int(ps * rate) + 1
|
||||
end = int((ps + pd) * rate) - 1
|
||||
assert not np.any(s1.sound[start:end] == s3.sound[start:end])
|
||||
29
cnmodel/util/tests/test_stim.py
Normal file
29
cnmodel/util/tests/test_stim.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import numpy as np
|
||||
from numpy.testing import assert_raises
|
||||
|
||||
from cnmodel.util import stim
|
||||
from neuron import h
|
||||
|
||||
h.dt = 0.025
|
||||
|
||||
|
||||
def test_make_pulse():
|
||||
params = dict(delay=10, Sfreq=50, dur=1, amp=15, PT=0, NP=5)
|
||||
|
||||
assert_raises(Exception, lambda: stim.make_pulse(params))
|
||||
params["dt"] = 0.025
|
||||
|
||||
w, maxt, times = stim.make_pulse(params)
|
||||
|
||||
assert w.min() == 0.0
|
||||
assert w.max() == 15
|
||||
assert w.dtype == np.float64
|
||||
|
||||
triggers = np.argwhere(np.diff(w) > 0)[:, 0] + 1
|
||||
assert np.all(triggers == times)
|
||||
assert w.sum() == 15 * len(times) * int(1 / h.dt)
|
||||
|
||||
params["PT"] = 100
|
||||
w, maxt, times = stim.make_pulse(params)
|
||||
triggers = np.argwhere(np.diff(w) > 0)[:, 0] + 1
|
||||
assert triggers[-1] - triggers[-2] == 100 / h.dt
|
||||
Reference in New Issue
Block a user