|
| 1 | +# %% |
| 2 | + |
| 3 | +from jax import numpy as jnp, random, jit |
| 4 | +from ngcsimlib.context import Context |
| 5 | +import numpy as np |
| 6 | +np.random.seed(42) |
| 7 | +from ngclearn.components import RateCell |
| 8 | +from ngcsimlib.compilers import compile_command, wrap_command |
| 9 | +from numpy.testing import assert_array_equal |
| 10 | + |
| 11 | +from ngcsimlib.compilers.process import Process, transition |
| 12 | +from ngcsimlib.component import Component |
| 13 | +from ngcsimlib.compartment import Compartment |
| 14 | +from ngcsimlib.context import Context |
| 15 | +from ngcsimlib.utils.compartment import Get_Compartment_Batch |
| 16 | + |
| 17 | + |
| 18 | +def test_RateCell1(): |
| 19 | + name = "rate_ctx" |
| 20 | + dkey = random.PRNGKey(42) |
| 21 | + dkey, *subkeys = random.split(dkey, 100) |
| 22 | + dt = 1. # ms |
| 23 | + with Context(name) as ctx: |
| 24 | + a = RateCell( |
| 25 | + name="a", n_units=1, tau_m=50., prior=("gaussian", 0.), act_fx="identity", |
| 26 | + threshold=("none", 0.), integration_type="euler", |
| 27 | + batch_size=1, resist_scale=1., shape=None, is_stateful=True |
| 28 | + ) |
| 29 | + advance_process = (Process() >> a.advance_state) |
| 30 | + ctx.wrap_and_add_command(jit(advance_process.pure), name="run") |
| 31 | + reset_process = (Process() >> a.reset) |
| 32 | + ctx.wrap_and_add_command(jit(reset_process.pure), name="reset") |
| 33 | + |
| 34 | + # reset_cmd, reset_args = ctx.compile_by_key(a, compile_key="reset") |
| 35 | + # ctx.add_command(wrap_command(jit(ctx.reset)), name="reset") |
| 36 | + # advance_cmd, advance_args = ctx.compile_by_key(a, compile_key="advance_state") |
| 37 | + # ctx.add_command(wrap_command(jit(ctx.advance_state)), name="run") |
| 38 | + |
| 39 | + @Context.dynamicCommand |
| 40 | + def clamp(x): |
| 41 | + a.j.set(x) |
| 42 | + |
| 43 | + ## input spike train |
| 44 | + x_seq = jnp.ones((1, 10)) |
| 45 | + ## desired output/epsp pulses |
| 46 | + y_seq = jnp.asarray([[0.02, 0.04, 0.06, 0.08, 0.09999999999999999, 0.11999999999999998, 0.13999999999999999, 0.15999999999999998, 0.17999999999999998, 0.19999999999999998]], dtype=jnp.float32) |
| 47 | + |
| 48 | + outs = [] |
| 49 | + ctx.reset() |
| 50 | + for ts in range(x_seq.shape[1]): |
| 51 | + x_t = jnp.array([[x_seq[0, ts]]]) ## get data at time t |
| 52 | + ctx.clamp(x_t) |
| 53 | + ctx.run(t=ts * 1., dt=dt) |
| 54 | + outs.append(a.z.value) |
| 55 | + outs = jnp.concatenate(outs, axis=1) |
| 56 | + |
| 57 | + ## output should equal input |
| 58 | + # assert_array_equal(outs, y_seq, tol=1e-3) |
| 59 | + np.testing.assert_allclose(outs, y_seq, atol=1e-3) |
| 60 | + |
0 commit comments