def test_init_lif_rng(Simulator, seed): with nengo.Network() as model: x = nengo.Ensemble(100, 1) with Simulator(model) as sim: v1, r1 = init_lif(sim, x, rng=np.random.RandomState(seed=seed)) v2, r2 = init_lif(sim, x, rng=np.random.RandomState(seed=seed)) assert np.allclose(v1, v2) assert np.allclose(r1, r2) assert v1.shape == (x.n_neurons, ) assert r1.shape == (x.n_neurons, )
def test_init_lif_invalid(Simulator): with nengo.Network() as model: x = nengo.Ensemble(100, 1, neuron_type=nengo.Direct()) with Simulator(model) as sim: with pytest.raises(ValueError): init_lif(sim, x) with nengo.Network() as model: x = nengo.Ensemble(100, 2) with Simulator(model) as sim: with pytest.raises(ValueError): init_lif(sim, x, x0=0)
def go(freq, neuron_type, n_neurons_over_freq=50, # scale-invariant n_steps=1000, tau_times_freq=0.01, # dimensionless dt_times_freq=0.001, # dimensionless max_rates=nengo.dists.Uniform(20, 40), seed=0, ): n_neurons = int(n_neurons_over_freq * freq) tau = tau_times_freq / freq dt = dt_times_freq / freq print(n_neurons) with nengo.Network(seed=seed) as model: u = nengo.Node(lambda t: np.sin(freq*2*np.pi*t)) x = nengo.Ensemble(n_neurons, 1, neuron_type=neuron_type, max_rates=max_rates) nengo.Connection(u, x, synapse=None) p_actual = nengo.Probe(x, synapse=tau) p_ideal = nengo.Probe(u, synapse=tau) with nengo.Simulator(model, dt=dt) as sim: rng = np.random.RandomState(seed=seed) if isinstance(neuron_type, nengo.LIF): from nengolib.neurons import init_lif init_lif(sim, x, rng=rng) elif isinstance(neuron_type, nengo.SpikingRectifiedLinear): # https://github.com/nengo/nengo/issues/1415 sim.signals[sim.model.sig[x.neurons]['voltage']] = ( rng.rand(x.n_neurons)) sim.run_steps(n_steps) return nengo.utils.numpy.rmse( sim.data[p_actual], sim.data[p_ideal])
def go(freq, max_rates, n_neurons=2500, n_steps=10000, dt=1e-4, sample_every=1e-4, tau=0.02, seed=0): with nengo.Network(seed=seed) as model: u = nengo.Node(output=lambda t: np.sin(freq * 2 * np.pi * t)) x = nengo.Ensemble(n_neurons, 1, max_rates=max_rates, seed=seed, neuron_type=nengo.LIF()) x_rate = nengo.Ensemble(n_neurons, 1, max_rates=max_rates, seed=seed, neuron_type=nengo.LIFRate()) nengo.Connection(u, x, synapse=None) nengo.Connection(u, x_rate, synapse=None) p_u = nengo.Probe(u, synapse=None, sample_every=sample_every) p_v = nengo.Probe(x.neurons, 'voltage', synapse=None, sample_every=sample_every) p_j = nengo.Probe(x.neurons, 'input', synapse=None, sample_every=sample_every) p_r = nengo.Probe(x.neurons, 'refractory_time', synapse=None, sample_every=sample_every) p_ideal = nengo.Probe(x_rate, synapse=tau, sample_every=sample_every) p_actual = nengo.Probe(x, synapse=tau, sample_every=sample_every) with nengo.Simulator(model, dt=dt) as sim: init_lif(sim, x, x0=0) sim.run_steps(n_steps) A = get_activities(sim.model, x, sim.data[p_u]) assert A.shape == sim.data[p_v].shape == sim.data[p_j].shape a_flatten = A.flatten() sl = a_flatten > 0 v = sim.data[p_v].flatten()[sl] j = sim.data[p_j].flatten()[sl] a = a_flatten[sl] r = (sim.data[p_r].flatten()[sl] - dt).clip(0) s = (x.neuron_type.tau_rc * np.log1p((1 - v) / (j - 1)) + r) * a actual = sim.data[p_actual] ideal = sim.data[p_ideal] assert ideal.shape == actual.shape return ( kstest(s, 'uniform'), rmse(actual, ideal), )
def test_init_lif(Simulator, seed, x0): u = 0 if x0 is None else x0 n_neurons = 1000 t = 2.0 ens_kwargs = dict( n_neurons=n_neurons, dimensions=1, max_rates=nengo.dists.Uniform(10, 100), seed=seed, ) with nengo.Network(seed=seed) as model: stim = nengo.Node(u) zero = nengo.Ensemble(**ens_kwargs) init = nengo.Ensemble(**ens_kwargs) nengo.Connection(stim, zero, synapse=None) nengo.Connection(stim, init, synapse=None) p_zero_spikes = nengo.Probe(zero.neurons, 'spikes', synapse=None) p_zero_v = nengo.Probe(zero.neurons, 'voltage', synapse=None) p_init_spikes = nengo.Probe(init.neurons, 'spikes', synapse=None) p_init_v = nengo.Probe(init.neurons, 'voltage', synapse=None) with Simulator(model, seed=seed) as sim: init_lif(sim, init, x0=x0) sim.run(t) # same tuning curves a = get_activities(sim.model, zero, [u]) assert np.allclose(a, get_activities(sim.model, init, [u])) # calculate difference between actual spike counts and ideal count_zero = np.count_nonzero(sim.data[p_zero_spikes], axis=0) count_init = np.count_nonzero(sim.data[p_init_spikes], axis=0) e_zero = count_zero - a * t e_init = count_init - a * t # initialized error is close to zero, better than uninitialized, # with std. dev. close to the uninitialized assert np.abs(np.mean(e_init)) < 0.05 assert np.abs(np.mean(e_zero)) > 0.1 assert np.abs(np.std(e_init) - np.std(e_zero)) < 0.05 # subthreshold neurons are the same between populations subthresh = np.all(sim.data[p_init_spikes] == 0, axis=0) assert np.allclose(subthresh, np.all(sim.data[p_zero_spikes] == 0, axis=0)) assert 0 < np.count_nonzero(subthresh) < n_neurons is_active = ~subthresh # uninitialized always under-counts (unless subthreshold) # the other exception is when a neuron spikes at the very end # since the simulation does not start in its refractory assert np.allclose(e_zero[subthresh], 0) very_end = sim.trange() >= t - init.neuron_type.tau_ref exception = np.any(sim.data[p_zero_spikes][very_end, :] > 0, axis=0) # no more than 10% should be exceptions (heuristic) assert np.count_nonzero(exception) < 0.1 * n_neurons assert np.all(e_zero[is_active & (~exception)] < 0) # uninitialized voltages start at 0 (plus first time-step) assert np.all(sim.data[p_zero_v][0, :] < 0.2) # initialized sub-threshold voltages remain constant # (steady-state input) assert np.allclose(sim.data[p_init_v][0, subthresh], sim.data[p_init_v][-1, subthresh]) def uniformity_test(spikes): # test uniformity of ISIs # returns (r, d, p) where r is the [0, 1) relative # position of the first spike-time within the ISI # d is the KS D-statistic which is the absolute max # distance from the uniform distribution, and p # is the p-value of this statistic t_spike = sim.trange()[[ np.where(s > 0)[0][0] for s in spikes[:, is_active].T ]] assert t_spike.shape == (np.count_nonzero(is_active), ) isi_location = (t_spike - sim.dt) * a[is_active] return (isi_location, ) + kstest(isi_location, 'uniform') r, d, p = uniformity_test(sim.data[p_init_spikes]) assert np.all(r >= 0) assert np.all(r < 1) assert d < 0.1 r, d, p = uniformity_test(sim.data[p_zero_spikes]) assert np.all(r >= 0.7) # heuristic assert np.all(r < 1) assert d > 0.7 assert p < 1e-5