def test_linear_network(Simulator, plt, seed, rng, neuron_type, atol, atol_x): n_neurons = 500 dt = 0.001 T = 1.0 sys = Lowpass(0.1) scale_input = 2.0 synapse = 0.02 tau_probe = 0.005 with Network(seed=seed) as model: stim = nengo.Node( output=nengo.processes.WhiteSignal(T, high=10, seed=seed)) subnet = LinearNetwork(sys, n_neurons_per_ensemble=n_neurons, synapse=synapse, input_synapse=synapse, dt=dt, neuron_type=neuron_type) nengo.Connection(stim, subnet.input, synapse=None, transform=scale_input) assert subnet.synapse == subnet.input_synapse assert subnet.output_synapse is None p_input = nengo.Probe(subnet.input, synapse=tau_probe) p_x = nengo.Probe(subnet.state.output, synapse=tau_probe) p_output = nengo.Probe(subnet.output, synapse=tau_probe) with Simulator(model, dt=dt) as sim: sim.run(T) ideal_output = shift(sys.filt(sim.data[p_input])) ideal_x = shift(subnet.realization.X.filt(sim.data[p_input])) plt.plot(sim.trange(), sim.data[p_input], label="Input", alpha=0.5) plt.plot(sim.trange(), sim.data[p_output], label="Actual y", alpha=0.5) plt.plot(sim.trange(), ideal_output, label="Expected y", alpha=0.5, linestyle='--') plt.plot(sim.trange(), sim.data[p_x], label="Actual x", alpha=0.5) plt.plot(sim.trange(), ideal_x, label="Expected x", alpha=0.5, linestyle='--') plt.legend() assert nrmse(sim.data[p_output], ideal_output) < atol assert nrmse(sim.data[p_x].squeeze(), ideal_x.squeeze()) < atol_x
def test_sim_new_synapse(Simulator): # Create a new synapse object and simulate it synapse = Lowpass(0.1) - Lowpass(0.01) with Network() as model: stim = nengo.Node(output=np.sin) x = nengo.Node(size_in=1) nengo.Connection(stim, x, synapse=synapse) p_stim = nengo.Probe(stim, synapse=None) p_x = nengo.Probe(x, synapse=None) with Simulator(model) as sim: sim.run(0.1) assert np.allclose(shift(synapse.filt(sim.data[p_stim])), sim.data[p_x])
def do_trial(name, seed, length=2000, dt=0.001, tau_probe=0.02, sanity=False, **kwargs): # Note: depends on the globals, (factory, C, model, u, p_u, P, sys) process = nengo.processes.WhiteSignal( period=length*dt, rms=power, high=freq, y0=0, seed=seed) test_u = process.run_steps(length, dt=dt) x_ideal = sys.X.filt(test_u, dt=dt) if sanity: analyze("ideal-%s" % name, t=process.ntrange(length, dt=dt), u=test_u, x_hat=x_ideal, x_ideal=x_ideal, C=C, theta=theta, dump_file=False, do_plot=False) u.output = process with factory(network=model, dt=dt) as sim: sim.run(length*dt) if post_fixture: post_fixture(sim) assert np.allclose(test_u, np.asarray(sim.data[p_u])) # Use discrete principle 3, offline, to get x_hat # from the unfiltered spikes representing x. # This is analagous to probing the PSC, pre-encoding. syn_probe = Lowpass(tau_probe) map_out = ss2sim(sys, synapse=syn_probe, dt=dt) x_raw = np.asarray([sim.data[p] for p in P]).squeeze() f = map_out.A.dot(x_raw) + map_out.B.dot(test_u.T) x_hat = syn_probe.filt(f, axis=1, dt=dt).T return analyze( name=name, t=sim.trange(), u=test_u, x_hat=x_hat, x_ideal=x_ideal, C=C, theta=theta, **kwargs)
def test_impulse(): dt = 0.001 tau = 0.005 length = 500 delta = np.zeros(length) delta[0] = 1. / dt sys = Lowpass(tau) response = sys.impulse(length, dt) assert not np.allclose(response[0], 0) # should give the same result as using filt assert np.allclose(response, sys.filt(delta, dt)) # and should default to the same dt assert sys.default_dt == dt assert np.allclose(response, sys.impulse(length)) # should also accept discrete systems dss = cont2discrete(sys, dt=dt) assert not dss.analog assert np.allclose(response, dss.impulse(length) / dt) assert np.allclose(response, dss.impulse(length, dt=dt))