def compile_script(argv=None): from spyks import __version__ from spyks.core import load_model import spyks.validate as spkv import argparse p = argparse.ArgumentParser(description="compile a spyks model file into a python extension module") p.add_argument("model", help="the model descriptor file to compile") p.add_argument("target", help="the path to put the module (default same as model file)", nargs='?') p.add_argument("--version", action="version", version="%(prog)s {}".format(__version__)) p.add_argument("--skip-compile", help="skip compilation step", action="store_true") p.add_argument("--skip-codegen", help="skip code generation step", action="store_true") args = p.parse_args(argv) ch = logging.StreamHandler() formatter = logging.Formatter("[%(name)s] %(message)s") loglevel = logging.INFO log.setLevel(loglevel) ch.setLevel(loglevel) # change ch.setFormatter(formatter) log.addHandler(ch) model = load_model(args.model) log.info("%s: validating model", model["name"]) spkv.check_symbols(model) spkv.check_equations(model) path = args.target or os.path.dirname(args.model) cppfile = os.path.join(path, model["name"] + ".cpp") if not args.skip_codegen: write_cppfile(model, cppfile) if not args.skip_compile: build_module(cppfile, model["name"], path, version=model["version"])
def test_biocm(): pymodel = s.load_model("models/biocm.yml") biocm = s.load_module(pymodel, "models") params = s.to_array(pymodel['parameters']) forcing = s.to_array(pymodel['forcing']) state = s.to_array(pymodel['state']) model = biocm.model(params, forcing, 0.05) dXdt = model(state.tolist(), 0) pydXdt = s.to_array(sv.deriv(pymodel)) assert_true(nx.allclose(dXdt, pydXdt))
def test_biocm_integration(I=20): N = 5000 dt = 0.05 pymodel = s.load_model("models/biocm.yml") biocm = s.load_module(pymodel, "models") params = s.to_array(pymodel['parameters']) x0 = s.to_array(pymodel['state']) data = nx.ones(N) * I model = biocm.model(params, data, dt) X = biocm.integrate(params, x0, data, dt, dt) X2 = biocm.integrate(model, x0, data.size * dt, dt) assert_true(nx.allclose(X, X2)) return X
def test_nakl_integration(I=50): pymodel = s.load_model("models/nakl.yml") nakl = s.load_module(pymodel, "models") N = 10000 dt = 0.05 params = nakl_params[0]['params'] x0 = nakl_params[0]['state'] data = nx.ones(N) * I model = nakl.model(params, data, dt) X = nakl.integrate(params, x0, data, dt, dt) X2 = nakl.integrate(model, x0, data.size * dt, dt) assert_true(nx.allclose(X, X2)) return X
def test_passive_ei_deriv(): pymodel = s.load_model("models/passive_ei.yml") passive_ei = s.load_module(pymodel, "models") def compare(params, forcing, state): model = passive_ei.model(params, forcing, 0.05) dXdt = model(state, 0) s.update_model(pymodel, state=state, forcing=forcing[0], parameters=params) pydXdt = s.to_array(sv.deriv(pymodel)) assert_true(nx.allclose(dXdt, pydXdt)) for tvals in passive_params: yield compare, tvals['params'], tvals['forcing'], tvals['state']
def test_nakl_dxdt(): pymodel = s.load_model("models/nakl.yml") nakl = s.load_module(pymodel, "models") def compare_nakl(params, forcing, state): model = nakl.model(params, forcing, 0.05) dXdt = model(state, 0) s.update_model(pymodel, state=state, forcing=forcing, parameters=params) pydXdt = s.to_array(sv.deriv(pymodel)) assert_true(nx.allclose(dXdt, pydXdt)) for tvals in nakl_params: yield compare_nakl, tvals['params'], tvals['forcing'], tvals['state']
def test_adex_dxdt(): pymodel = s.load_model("models/adex.yml") adex = s.load_module(pymodel, "models") def compare_adex(params, forcing, state): # inj = models.timeseries(forcing, 0.05) model = adex.model(params, forcing, 0.05) dXdt = model(state, 0) s.update_model(pymodel, state=state, forcing=forcing, parameters=params) pydXdt = s.to_array(sv.deriv(pymodel)) assert_true(nx.allclose(dXdt, pydXdt)) for tvals in adex_params: yield compare_adex, tvals['params'], tvals['forcing'], tvals['state']
def test_adex_integration(): pymodel = s.load_model("models/adex.yml") adex = s.load_module(pymodel, "models") I = 500 N = 1000 dt = 0.05 params = adex_params[0]['params'] x0 = adex_params[0]['state'] data = nx.ones(N) * I model = adex.model(params, data, dt) X = adex.integrate(params, x0, data, dt, dt) X2 = adex.integrate(model, x0, data.size * dt, dt) assert_true(nx.allclose(X, X2)) # with these parameters, there should be exactly one spike at 555 events = (X[:, 0] > 29.9).nonzero()[0] assert_equal(events.size, 1) assert_equal(events[0], 555) return X
def test_passive_ei_integration(): pymodel = s.load_model("models/passive_ei.yml") passive_ei = s.load_module(pymodel, "models") g_ex = 1.0 g_inh = 0.5 N = 2000 dt = 0.05 params = s.to_array(pymodel['parameters']) forcing = nx.column_stack([nx.ones(N) * g_ex, nx.ones(N) * g_inh]) x0 = s.to_array(pymodel['state']) model = passive_ei.model(params, forcing, dt) X = passive_ei.integrate(params, x0, forcing, dt, dt) X2 = passive_ei.integrate(model, x0, N * dt, dt) assert_true(nx.allclose(X, X2)) # steady-state V should be a weighted average of the reversal potentials g_l = s.get_param_value(pymodel, 'g_l') Evals = [s.get_param_value(pymodel, n) for n in ('E_l', 'E_ex', 'E_inh')] gvals = [g_l, g_ex * g_l.units, g_inh * g_l.units] V_steady = sum(g * e for e, g in zip(Evals, gvals)) / sum(gvals) events = (X[:, 0] > 29.9).nonzero()[0] assert_almost_equal(V_steady.magnitude, X[-1, 0], places=2)
def test_adex_reset(): pymodel = s.load_model("models/adex.yml") adex = s.load_module(pymodel, "models") def compare_reset(params, forcing, state): model = adex.model(params, forcing, 0.05) reset, new_state = model.reset(state) s.update_model(pymodel, state=state, forcing=forcing, parameters=params) pypost_state = sv.reset(pymodel) if reset: pypost_state = s.to_array(pypost_state) assert_true(nx.allclose(new_state, pypost_state)) else: assert_equal(reset, pypost_state) assert_true(nx.allclose(state, new_state)) for tvals in adex_params: yield compare_reset, tvals['params'], tvals['forcing'], tvals['state']