def test_noise(Simulator, nl_nodirect, seed, plt): """Ensure that setting Ensemble.noise generates noise.""" with nengo.Network(seed=seed) as model: inp, gain, bias = 1, 5, 2 neg_noise, pos_noise = -4, 20 model.config[nengo.Ensemble].neuron_type = nl_nodirect() model.config[nengo.Ensemble].encoders = Choice([[1]]) model.config[nengo.Ensemble].gain = Choice([gain]) model.config[nengo.Ensemble].bias = Choice([bias]) const = nengo.Node(output=inp) pos = nengo.Ensemble( 1, 1, noise=StochasticProcess(Choice([pos_noise]))) normal = nengo.Ensemble(1, 1) neg = nengo.Ensemble( 1, 1, noise=StochasticProcess(Choice([neg_noise]))) nengo.Connection(const, pos) nengo.Connection(const, normal) nengo.Connection(const, neg) pos_p = nengo.Probe(pos.neurons, synapse=0.1) normal_p = nengo.Probe(normal.neurons, synapse=0.1) neg_p = nengo.Probe(neg.neurons, synapse=0.1) sim = Simulator(model) sim.run(0.06) t = sim.trange() plt.title("input=%d, bias=%d, gain=%d" % (inp, bias, gain)) plt.plot(t, sim.data[pos_p], c='b', label="noise=%d" % pos_noise) plt.plot(t, sim.data[normal_p], c='k', label="no noise") plt.plot(t, sim.data[neg_p], c='r', label="noise=%d" % neg_noise) plt.legend(loc="best") assert np.all(sim.data[pos_p] >= sim.data[normal_p]) assert np.all(sim.data[normal_p] >= sim.data[neg_p]) assert not np.all(sim.data[normal_p] == sim.data[pos_p]) assert not np.all(sim.data[normal_p] == sim.data[neg_p])
def test_stochasticprocess(rng): dist = DistributionMock(42) process = StochasticProcess(dist) samples = nengo.processes.sample(5, process, d=2, rng=rng) assert np.all(samples == 42 * np.ones((5, 2))) assert nengo.processes.sample(5, process).shape == (5, ) assert nengo.processes.sample(1, process, d=1).shape == (1, 1) assert nengo.processes.sample(2, process, d=3).shape == (2, 3)
def test_gaussian_whitenoise(rms, rng, plt): d = 500 t = 100 dt = 0.001 process = StochasticProcess(Gaussian(0., rms)) values = nengo.processes.sample(t, process, dt=dt, d=d, rng=rng) freq, val_psd = psd(values) trange = np.arange(1, t + 1) * dt plt.subplot(2, 1, 1) plt.title("First two dimensions of white noise process, rms=%.1f" % rms) plt.plot(trange, values[:, :2]) plt.xlim(right=trange[-1]) plt.subplot(2, 1, 2) plt.title("Power spectrum") plt.plot(freq, val_psd, drawstyle='steps') assert np.allclose(np.std(values), rms, rtol=0.02) assert np.allclose(val_psd[1:-1], rms, rtol=0.2)
def test_noise_copies_ok(Simulator, nl_nodirect, seed, plt): """Make sure the same noise process works in multiple ensembles. We test this both with the default system and without. """ process = StochasticProcess(Choice([0.5]), synapse=nengo.Alpha(1.)) with nengo.Network(seed=seed) as model: inp, gain, bias = 1, 5, 2 model.config[nengo.Ensemble].neuron_type = nl_nodirect() model.config[nengo.Ensemble].encoders = Choice([[1]]) model.config[nengo.Ensemble].gain = Choice([gain]) model.config[nengo.Ensemble].bias = Choice([bias]) model.config[nengo.Ensemble].noise = process const = nengo.Node(output=inp) a = nengo.Ensemble(1, 1, noise=process) b = nengo.Ensemble(1, 1, noise=process) c = nengo.Ensemble(1, 1) # defaults to noise=process nengo.Connection(const, a) nengo.Connection(const, b) nengo.Connection(const, c) ap = nengo.Probe(a.neurons, synapse=0.01) bp = nengo.Probe(b.neurons, synapse=0.01) cp = nengo.Probe(c.neurons, synapse=0.01) sim = Simulator(model) sim.run(0.06) t = sim.trange() plt.subplot(2, 1, 1) plt.plot(t, sim.data[ap], lw=3) plt.plot(t, sim.data[bp], lw=2) plt.plot(t, sim.data[cp]) plt.subplot(2, 1, 2) plt.plot(*nengo.utils.ensemble.tuning_curves(a, sim), lw=3) plt.plot(*nengo.utils.ensemble.tuning_curves(b, sim), lw=2) plt.plot(*nengo.utils.ensemble.tuning_curves(c, sim)) assert np.allclose(sim.data[ap], sim.data[bp]) assert np.allclose(sim.data[bp], sim.data[cp])