def test_inputgatedmemory(Simulator, plt, seed): to_memorize = 0.5 start_memorizing = 0.4 with nengo.Network(seed=seed) as net: test_input = nengo.Node(piecewise( {0.0: 0, 0.1: to_memorize, start_memorizing + 0.1: 0})) gate_input = nengo.Node(piecewise({0.0: 0, start_memorizing: 1})) reset_input = nengo.Node(0) mem = nengo.networks.InputGatedMemory(100, 1, difference_gain=5.0) nengo.Connection(test_input, mem.input) nengo.Connection(gate_input, mem.gate) nengo.Connection(reset_input, mem.reset) mem_p = nengo.Probe(mem.output, synapse=0.01) sim = Simulator(net) sim.run(0.5) data = sim.data[mem_p] t = sim.trange() plt.title("gating at %.1f s" % start_memorizing) plt.plot(t, data, label="value in memory") plt.axhline(to_memorize, c='k', lw=2, label="value to remember") plt.axvline(start_memorizing, c='k', ls=':', label="start gating") plt.legend(loc='best') assert abs(np.mean(data[t < 0.1])) < 0.01 assert abs(np.mean(data[(t > 0.2) & (t <= 0.4)]) - 0.5) < 0.02 assert abs(np.mean(data[t > 0.4]) - 0.5) < 0.02
def test_inputgatedmemory(Simulator): with nengo.Network(seed=123) as net: test_input = nengo.Node(piecewise({0.0: 0, 0.3: 0.5, 1.0: 0})) gate_input = nengo.Node(piecewise({0.0: 0, 0.8: 1})) reset_input = nengo.Node(0) mem = nengo.networks.InputGatedMemory(100, 1, difference_gain=5.0) nengo.Connection(test_input, mem.input) nengo.Connection(gate_input, mem.gate) nengo.Connection(reset_input, mem.reset_node) mem_p = nengo.Probe(mem.output, synapse=0.01) sim = Simulator(net) sim.run(1.2) data = sim.data[mem_p] trange = sim.trange() with Plotter(Simulator) as plt: plt.plot(trange, data) plt.savefig('test_workingmemory.test_inputgatedmemory.pdf') plt.close() assert abs(np.mean(data[trange < 0.3])) < 0.01 assert abs(np.mean(data[(trange > 0.8) & (trange < 1.0)]) - 0.5) < 0.02 assert abs(np.mean(data[trange > 1.0]) - 0.5) < 0.02
def test_inputgatedmemory(Simulator, plt, seed): to_memorize = 0.5 start_memorizing = 0.4 with nengo.Network(seed=seed) as net: test_input = nengo.Node( piecewise({ 0.0: 0, 0.1: to_memorize, start_memorizing + 0.1: 0 })) gate_input = nengo.Node(piecewise({0.0: 0, start_memorizing: 1})) reset_input = nengo.Node(0) mem = nengo.networks.InputGatedMemory(100, 1, difference_gain=5.0) nengo.Connection(test_input, mem.input) nengo.Connection(gate_input, mem.gate) nengo.Connection(reset_input, mem.reset) mem_p = nengo.Probe(mem.output, synapse=0.01) sim = Simulator(net) sim.run(0.5) data = sim.data[mem_p] t = sim.trange() plt.title("gating at %.1f s" % start_memorizing) plt.plot(t, data, label="value in memory") plt.axhline(to_memorize, c='k', lw=2, label="value to remember") plt.axvline(start_memorizing, c='k', ls=':', label="start gating") plt.legend(loc='best') assert abs(np.mean(data[t < 0.1])) < 0.01 assert abs(np.mean(data[(t > 0.2) & (t <= 0.4)]) - 0.5) < 0.02 assert abs(np.mean(data[t > 0.4]) - 0.5) < 0.02
def test_integrator(Simulator, nl, plt): model = nengo.Network(label='Integrator', seed=892) with model: model.config[nengo.Ensemble].neuron_type = nl() inputs = {0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0} input = nengo.Node(piecewise(inputs)) tau = 0.1 T = nengo.networks.Integrator(tau, n_neurons=100, dimensions=1) nengo.Connection(input, T.input, synapse=tau) A = nengo.Ensemble(100, dimensions=1) nengo.Connection(A, A, synapse=tau) nengo.Connection(input, A, transform=tau, synapse=tau) input_p = nengo.Probe(input) A_p = nengo.Probe(A, synapse=0.01) T_p = nengo.Probe(T.ensemble, synapse=0.01) sim = Simulator(model, dt=0.001) sim.run(6.0) t = sim.trange() plt.plot(t, sim.data[A_p], label='Manual') plt.plot(t, sim.data[T_p], label='Template') plt.plot(t, sim.data[input_p], 'k', label='Input') plt.legend(loc=0) assert rmse(sim.data[A_p], sim.data[T_p]) < 0.2
def test_integrator(Simulator, nl): model = nengo.Network(label='Integrator', seed=892) with model: model.config[nengo.Ensemble].neuron_type = nl() inputs = {0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0} input = nengo.Node(piecewise(inputs)) tau = 0.1 T = nengo.networks.Integrator(tau, n_neurons=100, dimensions=1) nengo.Connection(input, T.input, synapse=tau) A = nengo.Ensemble(100, dimensions=1) nengo.Connection(A, A, synapse=tau) nengo.Connection(input, A, transform=tau, synapse=tau) input_p = nengo.Probe(input) A_p = nengo.Probe(A, synapse=0.01) T_p = nengo.Probe(T.ensemble, synapse=0.01) sim = Simulator(model, dt=0.001) sim.run(6.0) with Plotter(Simulator, nl) as plt: t = sim.trange() plt.plot(t, sim.data[A_p], label='Manual') plt.plot(t, sim.data[T_p], label='Template') plt.plot(t, sim.data[input_p], 'k', label='Input') plt.legend(loc=0) plt.savefig('test_integrator.test_integrator.pdf') plt.close() assert rmse(sim.data[A_p], sim.data[T_p]) < 0.2
def test_node_to_neurons(Simulator, nl_nodirect): name = "node_to_neurons" N = 30 m = nengo.Network(label=name, seed=123) with m: m.config[nengo.Ensemble].neuron_type = nl_nodirect() a = nengo.Ensemble(N, dimensions=1) inn = nengo.Node(output=np.sin) inh = nengo.Node(piecewise({0: 0, 2.5: 1})) nengo.Connection(inn, a) nengo.Connection(inh, a.neurons, transform=[[-2.5]] * N) inn_p = nengo.Probe(inn, "output") a_p = nengo.Probe(a, "decoded_output", synapse=0.1) inh_p = nengo.Probe(inh, "output") sim = Simulator(m) sim.run(5.0) t = sim.trange() ideal = np.sin(t) ideal[t >= 2.5] = 0 with Plotter(Simulator, nl_nodirect) as plt: plt.plot(t, sim.data[inn_p], label="Input") plt.plot(t, sim.data[a_p], label="Neuron approx, synapse=0.1") plt.plot(t, sim.data[inh_p], label="Inhib signal") plt.plot(t, ideal, label="Ideal output") plt.legend(loc=0, prop={"size": 10}) plt.savefig("test_connection.test_" + name + ".pdf") plt.close() assert np.allclose(sim.data[a_p][-10:], 0, atol=0.1, rtol=0.01)
def test_node_to_neurons(Simulator, nl_nodirect): name = 'node_to_neurons' N = 30 m = nengo.Network(label=name, seed=123) with m: m.config[nengo.Ensemble].neuron_type = nl_nodirect() a = nengo.Ensemble(N, dimensions=1) inn = nengo.Node(output=np.sin) inh = nengo.Node(piecewise({0: 0, 2.5: 1})) nengo.Connection(inn, a) nengo.Connection(inh, a.neurons, transform=[[-2.5]] * N) inn_p = nengo.Probe(inn, 'output') a_p = nengo.Probe(a, 'decoded_output', synapse=0.1) inh_p = nengo.Probe(inh, 'output') sim = Simulator(m) sim.run(5.0) t = sim.trange() ideal = np.sin(t) ideal[t >= 2.5] = 0 with Plotter(Simulator, nl_nodirect) as plt: plt.plot(t, sim.data[inn_p], label='Input') plt.plot(t, sim.data[a_p], label='Neuron approx, synapse=0.1') plt.plot(t, sim.data[inh_p], label='Inhib signal') plt.plot(t, ideal, label='Ideal output') plt.legend(loc=0, prop={'size': 10}) plt.savefig('test_connection.test_' + name + '.pdf') plt.close() assert np.allclose(sim.data[a_p][-10:], 0, atol=.1, rtol=.01)
def test_ensemble_to_neurons(Simulator, nl_nodirect, plt, seed): N = 30 m = nengo.Network(seed=seed) with m: m.config[nengo.Ensemble].neuron_type = nl_nodirect() a = nengo.Ensemble(N, dimensions=1) b = nengo.Ensemble(N, dimensions=1) inn = nengo.Node(output=np.sin) inh = nengo.Node(piecewise({0: 0, 0.5: 1})) nengo.Connection(inn, a) nengo.Connection(inh, b) nengo.Connection(b, a.neurons, transform=[[-2.5]] * N) inn_p = nengo.Probe(inn, 'output') a_p = nengo.Probe(a, 'decoded_output', synapse=0.1) b_p = nengo.Probe(b, 'decoded_output', synapse=0.1) inh_p = nengo.Probe(inh, 'output') with Simulator(m) as sim: sim.run(1.0) t = sim.trange() ideal = np.sin(t) ideal[t >= 0.5] = 0 plt.plot(t, sim.data[inn_p], label='Input') plt.plot(t, sim.data[a_p], label='Neuron approx, pstc=0.1') plt.plot(t, sim.data[b_p], label='Neuron approx of inhib sig, pstc=0.1') plt.plot(t, sim.data[inh_p], label='Inhib signal') plt.plot(t, ideal, label='Ideal output') plt.legend(loc=0, prop={'size': 10}) assert np.allclose(sim.data[a_p][-10:], 0, atol=.1, rtol=.01) assert np.allclose(sim.data[b_p][-10:], 1, atol=.1, rtol=.01)
def test_ensemble_to_neurons(Simulator, nl_nodirect, plt, seed): N = 30 m = nengo.Network(seed=seed) with m: m.config[nengo.Ensemble].neuron_type = nl_nodirect() a = nengo.Ensemble(N, dimensions=1) b = nengo.Ensemble(N, dimensions=1) inn = nengo.Node(output=np.sin) inh = nengo.Node(piecewise({0: 0, 0.5: 1})) nengo.Connection(inn, a) nengo.Connection(inh, b) nengo.Connection(b, a.neurons, transform=[[-2.5]] * N) inn_p = nengo.Probe(inn, 'output') a_p = nengo.Probe(a, 'decoded_output', synapse=0.1) b_p = nengo.Probe(b, 'decoded_output', synapse=0.1) inh_p = nengo.Probe(inh, 'output') sim = Simulator(m) sim.run(1.0) t = sim.trange() ideal = np.sin(t) ideal[t >= 0.5] = 0 plt.plot(t, sim.data[inn_p], label='Input') plt.plot(t, sim.data[a_p], label='Neuron approx, pstc=0.1') plt.plot(t, sim.data[b_p], label='Neuron approx of inhib sig, pstc=0.1') plt.plot(t, sim.data[inh_p], label='Inhib signal') plt.plot(t, ideal, label='Ideal output') plt.legend(loc=0, prop={'size': 10}) assert np.allclose(sim.data[a_p][-10:], 0, atol=.1, rtol=.01) assert np.allclose(sim.data[b_p][-10:], 1, atol=.1, rtol=.01)
def test_node_to_neurons(Simulator, nl_nodirect): name = 'node_to_neurons' N = 30 m = nengo.Model(name, seed=123) a = nengo.Ensemble(nl_nodirect(N), dimensions=1) inn = nengo.Node(output=np.sin) inh = nengo.Node(piecewise({0: 0, 2.5: 1})) nengo.Connection(inn, a) nengo.Connection(inh, a.neurons, transform=[[-2.5]]*N) inn_p = nengo.Probe(inn, 'output') a_p = nengo.Probe(a, 'decoded_output', filter=0.1) inh_p = nengo.Probe(inh, 'output') sim = Simulator(m) sim.run(5.0) t = sim.trange() ideal = np.sin(t) ideal[t >= 2.5] = 0 with Plotter(Simulator, nl_nodirect) as plt: plt.plot(t, sim.data(inn_p), label='Input') plt.plot(t, sim.data(a_p), label='Neuron approx, filter=0.1') plt.plot(t, sim.data(inh_p), label='Inhib signal') plt.plot(t, ideal, label='Ideal output') plt.legend(loc=0, prop={'size': 10}) plt.savefig('test_connection.test_' + name + '.pdf') plt.close() assert np.allclose(sim.data(a_p)[-10:], 0, atol=.1, rtol=.01)
def test_integrator(Simulator, plt, seed): model = nengo.Network(seed=seed) with model: inputs = {0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0} input = nengo.Node(piecewise(inputs)) tau = 0.1 T = nengo.networks.Integrator(tau, n_neurons=100, dimensions=1) nengo.Connection(input, T.input, synapse=tau) A = nengo.Ensemble(100, dimensions=1) nengo.Connection(A, A, synapse=tau) nengo.Connection(input, A, transform=tau, synapse=tau) input_p = nengo.Probe(input, sample_every=0.01) A_p = nengo.Probe(A, synapse=0.01, sample_every=0.01) T_p = nengo.Probe(T.ensemble, synapse=0.01, sample_every=0.01) sim = Simulator(model) sim.run(6.0) t = sim.trange(dt=0.01) plt.plot(t, sim.data[A_p], label='Manual') plt.plot(t, sim.data[T_p], label='Template') plt.plot(t, sim.data[input_p], 'k', label='Input') plt.legend(loc='best') assert rmse(sim.data[A_p], sim.data[T_p]) < 0.1
def test_integrator(Simulator, nl): model = nengo.Model('Integrator') inputs = {0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0} input = nengo.Node(piecewise(inputs)) tau = 0.1 T = nengo.networks.Integrator(tau, neurons=nl(100), dimensions=1) nengo.Connection(input, T.input, filter=tau) A = nengo.Ensemble(nl(100), dimensions=1) nengo.Connection(A, A, filter=tau) nengo.Connection(input, A, transform=tau, filter=tau) input_p = nengo.Probe(input, 'output') A_p = nengo.Probe(A, 'decoded_output', filter=0.01) T_p = nengo.Probe(T.ensemble, 'decoded_output', filter=0.01) sim = Simulator(model, dt=0.001) sim.run(6.0) with Plotter(Simulator, nl) as plt: t = sim.trange() plt.plot(t, sim.data(A_p), label='Manual') plt.plot(t, sim.data(T_p), label='Template') plt.plot(t, sim.data(input_p), 'k', label='Input') plt.legend(loc=0) plt.savefig('test_integrator.test_integrator.pdf') plt.close() assert rmse(sim.data(A_p), sim.data(T_p)) < 0.2
def test_oscillator(Simulator, plt, seed): model = nengo.Network(seed=seed) with model: inputs = {0: [1, 0], 0.5: [0, 0]} input = nengo.Node(piecewise(inputs), label='Input') tau = 0.1 freq = 5 T = nengo.networks.Oscillator(tau, freq, n_neurons=100) nengo.Connection(input, T.input) A = nengo.Ensemble(100, dimensions=2) nengo.Connection(A, A, synapse=tau, transform=[[1, -freq * tau], [freq * tau, 1]]) nengo.Connection(input, A) in_probe = nengo.Probe(input, sample_every=0.01) A_probe = nengo.Probe(A, synapse=0.01, sample_every=0.01) T_probe = nengo.Probe(T.ensemble, synapse=0.01, sample_every=0.01) with Simulator(model) as sim: sim.run(3.0) t = sim.trange(dt=0.01) plt.plot(t, sim.data[A_probe], label='Manual') plt.plot(t, sim.data[T_probe], label='Template') plt.plot(t, sim.data[in_probe], 'k', label='Input') plt.legend(loc='best') assert rmse(sim.data[A_probe], sim.data[T_probe]) < 0.2
def test_oscillator(Simulator, plt, seed): model = nengo.Network(seed=seed) with model: inputs = {0: [1, 0], 0.5: [0, 0]} input = nengo.Node(piecewise(inputs), label='Input') tau = 0.1 freq = 5 T = nengo.networks.Oscillator(tau, freq, n_neurons=100) nengo.Connection(input, T.input) A = nengo.Ensemble(100, dimensions=2) nengo.Connection(A, A, synapse=tau, transform=[[1, -freq*tau], [freq*tau, 1]]) nengo.Connection(input, A) in_probe = nengo.Probe(input, sample_every=0.01) A_probe = nengo.Probe(A, synapse=0.01, sample_every=0.01) T_probe = nengo.Probe(T.ensemble, synapse=0.01, sample_every=0.01) sim = Simulator(model) sim.run(3.0) t = sim.trange(dt=0.01) plt.plot(t, sim.data[A_probe], label='Manual') plt.plot(t, sim.data[T_probe], label='Template') plt.plot(t, sim.data[in_probe], 'k', label='Input') plt.legend(loc='best') assert rmse(sim.data[A_probe], sim.data[T_probe]) < 0.2
def test_oscillator(Simulator, nl): model = nengo.Network(label='Oscillator', seed=789) with model: inputs = {0: [1, 0], 0.5: [0, 0]} input = nengo.Node(piecewise(inputs), label='Input') tau = 0.1 freq = 5 T = nengo.networks.Oscillator( tau, freq, label="Oscillator", neurons=nl(100)) nengo.Connection(input, T.input) A = nengo.Ensemble(nl(100), label='A', dimensions=2) nengo.Connection(A, A, synapse=tau, transform=[[1, -freq*tau], [freq*tau, 1]]) nengo.Connection(input, A) in_probe = nengo.Probe(input, "output") A_probe = nengo.Probe(A, "decoded_output", synapse=0.01) T_probe = nengo.Probe(T.ensemble, "decoded_output", synapse=0.01) sim = Simulator(model) sim.run(3.0) with Plotter(Simulator, nl) as plt: t = sim.trange() plt.plot(t, sim.data[A_probe], label='Manual') plt.plot(t, sim.data[T_probe], label='Template') plt.plot(t, sim.data[in_probe], 'k', label='Input') plt.legend(loc=0) plt.savefig('test_oscillator.test_oscillator.pdf') plt.close() assert rmse(sim.data[A_probe], sim.data[T_probe]) < 0.3
def test_function(): f = piecewise({0: np.sin, 0.5: np.cos}) assert np.allclose(f(0), [np.sin(0)]) assert np.allclose(f(0.25), [np.sin(0.25)]) assert np.allclose(f(0.4999), [np.sin(0.4999)]) assert np.allclose(f(0.5), [np.cos(0.5)]) assert np.allclose(f(0.75), [np.cos(0.75)]) assert np.allclose(f(1.0), [np.cos(1.0)])
def test_lists(): f = piecewise({0.5: [1, 0], 1.0: [0, 1]}) assert np.allclose(f(-10), [0, 0]) assert np.allclose(f(0), [0, 0]) assert np.allclose(f(0.25), [0, 0]) assert np.allclose(f(0.5), [1, 0]) assert np.allclose(f(0.75), [1, 0]) assert np.allclose(f(1.0), [0, 1]) assert np.allclose(f(1.5), [0, 1]) assert np.allclose(f(100), [0, 1])
def test_basic(): f = piecewise({0.5: 1, 1.0: 0}) assert np.allclose(f(-10), [0]) assert np.allclose(f(0), [0]) assert np.allclose(f(0.25), [0]) assert np.allclose(f(0.5), [1]) assert np.allclose(f(0.75), [1]) assert np.allclose(f(1.0), [0]) assert np.allclose(f(1.5), [0]) assert np.allclose(f(100), [0])
def test_integrators(net): with net: piecewise_f = piecewise({0: 0, 0.2: 0.5, 1: 0, 2: -1, 3: 0, 4: 1, 5: 0}) piecewise_inp = nengo.Node(piecewise_f) nengo.Connection(piecewise_inp, net.pre_integrator.input) input_probe = nengo.Probe(piecewise_inp) pre_probe = nengo.Probe(net.pre_integrator.ensemble, synapse=0.01) post_probe = nengo.Probe(net.post_integrator.ensemble, synapse=0.01) with nengo.Simulator(net) as sim: sim.run(6) plt.plot(sim.trange(), sim.data[input_probe], color='k') plt.plot(sim.trange(), sim.data[pre_probe], color='b') plt.plot(sim.trange(), sim.data[post_probe], color='g')
def test_function_list(): def func1(t): return t, t ** 2, t ** 3 def func2(t): return t ** 4, t ** 5, t ** 6 f = piecewise({0: func1, 0.5: func2}) assert np.allclose(f(0), func1(0)) assert np.allclose(f(0.25), func1(0.25)) assert np.allclose(f(0.4999), func1(0.4999)) assert np.allclose(f(0.5), func2(0.5)) assert np.allclose(f(0.75), func2(0.75)) assert np.allclose(f(1.0), func2(1.0))
def test_function_list(): def func1(t): return t, t**2, t**3 def func2(t): return t**4, t**5, t**6 f = piecewise({0: func1, 0.5: func2}) assert np.allclose(f(0), func1(0)) assert np.allclose(f(0.25), func1(0.25)) assert np.allclose(f(0.4999), func1(0.4999)) assert np.allclose(f(0.5), func2(0.5)) assert np.allclose(f(0.75), func2(0.75)) assert np.allclose(f(1.0), func2(1.0))
def test_oscillator(Simulator, nl): model = nengo.Network(label='Oscillator', seed=789) with model: model.config[nengo.Ensemble].neuron_type = nl() inputs = {0: [1, 0], 0.5: [0, 0]} input = nengo.Node(piecewise(inputs), label='Input') tau = 0.1 freq = 5 T = nengo.networks.Oscillator(tau, freq, label="Oscillator", n_neurons=100) nengo.Connection(input, T.input) A = nengo.Ensemble(100, dimensions=2) nengo.Connection(A, A, synapse=tau, transform=[[1, -freq * tau], [freq * tau, 1]]) nengo.Connection(input, A) in_probe = nengo.Probe(input) A_probe = nengo.Probe(A, synapse=0.01) T_probe = nengo.Probe(T.ensemble, synapse=0.01) sim = Simulator(model) sim.run(3.0) with Plotter(Simulator, nl) as plt: t = sim.trange() plt.plot(t, sim.data[A_probe], label='Manual') plt.plot(t, sim.data[T_probe], label='Template') plt.plot(t, sim.data[in_probe], 'k', label='Input') plt.legend(loc=0) plt.savefig('test_oscillator.test_oscillator.pdf') plt.close() assert rmse(sim.data[A_probe], sim.data[T_probe]) < 0.3
def test_invalid_length(): with pytest.raises(Exception): f = piecewise({0.5: [1, 0], 1.0: [1, 0, 0]}) assert f
def test_invalid_key(): with pytest.raises(TypeError): f = piecewise({0.5: 1, 1: 0, 'a': 0.2}) assert f
def test_controlledoscillator(Simulator, plt, rng, seed, outfile): f_max = 2 T = 2 # time to hold each input for stims = np.array([1, 0.5, 0, -0.5, -1]) # control signal tau = 0.1 with nengo.Network(seed=seed) as model: state = nengo.Ensemble(n_neurons=500, dimensions=3, radius=1.7) def feedback(x): x0, x1, f = x w = f * f_max * 2 * np.pi return x0 + w * tau * x1, x1 - w * tau * x0 nengo.Connection(state, state[:2], function=feedback, synapse=tau) freq = nengo.Ensemble(n_neurons=100, dimensions=1) nengo.Connection(freq, state[2], synapse=tau) kick = nengo.Node(lambda t: 1 if t < 0.08 else 0) nengo.Connection(kick, state[0]) control = piecewise({i * T: stim for i, stim in enumerate(stims)}) freq_control = nengo.Node(control) nengo.Connection(freq_control, freq) p_state = nengo.Probe(state, synapse=0.03) if 'spinnaker' in Simulator.__module__: nengo_spinnaker.add_spinnaker_params(model.config) model.config[kick].function_of_time = True model.config[freq_control].function_of_time = True sim = Simulator(model) sim.run(len(stims) * T) data = sim.data[p_state][:, 1] ideal_freqs = f_max * stims # target frequencies dt = 0.001 steps = int(T / dt) freqs = np.fft.fftfreq(steps, d=dt) # compute fft for each input data.shape = len(stims), steps fft = np.fft.fft(data, axis=1) # compute ideal fft for each input ideal_data = np.zeros_like(data) for i in range(len(stims)): ideal_data[i] = np.cos(2 * np.pi * ideal_freqs[i] * np.arange(steps) * dt) ideal_fft = np.fft.fft(ideal_data, axis=1) # only consider the magnitude fft = np.abs(fft) ideal_fft = np.abs(ideal_fft) # compute the normalized dot product between the actual and ideal ffts score = np.zeros(len(stims)) for i in range(len(stims)): score[i] = np.dot(fft[i] / np.linalg.norm(fft[i]), ideal_fft[i] / np.linalg.norm(ideal_fft[i])) outfile.write('"n_neurons": %d,\n' % sum( e.n_neurons for e in model.all_ensembles)) outfile.write('"simtime": %f,\n' % (len(stims) * T)) outfile.write('"score": %f,\n' % np.mean(score)) figsize = (onecolumn, 4.0) if horizontal else (onecolumn * 2, 4.0) setup(figsize=figsize) lines = [] if type(plt).__name__ != 'Mock': for i, y in enumerate(np.fft.fftshift(fft, axes=1)): lines.append(plt.stem(np.fft.fftshift(freqs), y)) marker, stem, base = lines[-1] marker.set_color(colors[i]) marker.set_markersize(10.0) for s in stem: s.set_color(colors[i]) s.set_linewidth(1.0) base.set_visible(False) plt.xlim(-f_max * 2, f_max * 2) plt.xlabel('Frequency (Hz)') plt.ylabel('Power of decoded value') plt.legend(lines, ['%gHz' % f for f in ideal_freqs], loc='best', prop={'size': 8}) sns.despine() plt.saveas = 'results-3.svg' if hasattr(sim, 'close'): sim.close()
def test_invalid_function_length(): with pytest.raises(ValidationError): f = piecewise({0.5: 0, 1.0: lambda t: [t, t**2]}) assert f
def test_invalid_key(): with pytest.raises(ValidationError): f = piecewise({0.5: 1, 1: 0, 'a': 0.2}) assert f
from nengo.utils.functions import piecewise model = nengo.Network(label="Inhibitory Gating") n_neurons = 30 with model: A = nengo.Ensemble(n_neurons, dimensions=1, label="A") B = nengo.Ensemble(n_neurons, dimensions=1, label="B") C = nengo.Ensemble(n_neurons, dimensions=1, label="C") sin = nengo.Node(np.sin, label="sin") inhib = nengo.Node(piecewise({ 0: 0, 2.5: 1, 5: 0, 7.5: 1, 10: 0, 12.5: 1 }), label="inhibition") nengo.Connection(sin, A) nengo.Connection(sin, B) nengo.Connection(inhib, A.neurons, transform=[[-2.5]] * n_neurons) nengo.Connection(inhib, C) nengo.Connection(C, B.neurons, transform=[[-2.5]] * n_neurons) sin_probe = nengo.Probe(sin) inhib_probe = nengo.Probe(inhib) A_probe = nengo.Probe(A, synapse=0.01) B_probe = nengo.Probe(B, synapse=0.01)
import nengo import numpy as np from nengo.utils.functions import piecewise model = nengo.Network(label="Inhibitory Gating") n_neurons = 30 with model: A = nengo.Ensemble(n_neurons, dimensions=1, label="A") B = nengo.Ensemble(n_neurons, dimensions=1, label="B") C = nengo.Ensemble(n_neurons, dimensions=1, label="C") sin = nengo.Node(np.sin, label="sin") inhib = nengo.Node( piecewise({0: 0, 2.5: 1, 5: 0, 7.5: 1, 10: 0, 12.5: 1}), label="inhibition") nengo.Connection(sin, A) nengo.Connection(sin, B) nengo.Connection(inhib, A.neurons, transform=[[-2.5]] * n_neurons) nengo.Connection(inhib, C) nengo.Connection(C, B.neurons, transform=[[-2.5]] * n_neurons) sin_probe = nengo.Probe(sin) inhib_probe = nengo.Probe(inhib) A_probe = nengo.Probe(A, synapse=0.01) B_probe = nengo.Probe(B, synapse=0.01) C_probe = nengo.Probe(C, synapse=0.01)
from nengo.utils.functions import piecewise DURATION = 5.0 def product(x): return x[0] * x[1] def square(x): return x[0] * x[0] model = nengo.Network(label="sum") with model: input0 = nengo.Node(piecewise({0: -0.75, 1.25: 0.50, 2.5: -0.75, 3.75: 0})) input1 = nengo.Node(piecewise({0: 1.00, 1.25: 0.25, 2.5: -0.25, 3.75: 0})) ensemble0 = nengo.Ensemble(100, dimensions=1, radius=1) ensemble1 = nengo.Ensemble(100, dimensions=1, radius=1) ensemble_inp_2d = nengo.Ensemble(100, dimensions=2, radius=2) ensemble_prod = nengo.Ensemble(100, dimensions=1, radius=1) ensemble_sqr = nengo.Ensemble(100, dimensions=1, radius=1) nengo.Connection(input0, ensemble0) nengo.Connection(input1, ensemble1) nengo.Connection(ensemble0, ensemble_inp_2d[0]) nengo.Connection(ensemble1, ensemble_inp_2d[1]) nengo.Connection(ensemble_inp_2d, ensemble_prod, function=product) nengo.Connection(ensemble0, ensemble_sqr, function=square) input0_probe = nengo.Probe(input0) input1_probe = nengo.Probe(input1) ensemble0_probe = nengo.Probe(ensemble0, synapse=0.01)
import nengo tau = 0.1 model = nengo.Model('Integrator') integrator = nengo.networks.Integrator(tau, neurons=nengo.LIF(100), dimensions=1) from nengo.utils.functions import piecewise input = nengo.Node(piecewise({0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0})) nengo.Connection(input, integrator.input, filter=tau)
# (or any other linear system). # Create the model object import nengo from nengo.utils.functions import piecewise import matplotlib.pyplot as plt import numpy as np model = nengo.Network(label='Oscillator') with model: # Create the ensemble for the oscillator neurons = nengo.Ensemble(200, dimensions=2, label="neurons") # Create an input signal input = nengo.Node(piecewise({0: [1, 0], 0.1: [0, 0]}), label="input") # Connect the input signal to the neural ensemble nengo.Connection(input, neurons) # Create the feedback connection nengo.Connection(neurons, neurons, transform=[[1, 1], [-1, 1]], synapse=0.1) input_probe = nengo.Probe(input, 'output') neuron_probe = nengo.Probe(neurons, 'decoded_output', synapse=0.1) # start the simulator sim = nengo.Simulator(model) # dynamic plotting
# The ensemble for the oscillator oscillator = nengo.Ensemble(500, dimensions=3, radius=1.7, label="oscillator") # The feedback connection def feedback(x): x0, x1, w = x # These are the three variables stored in the ensemble return x0 + w*w_max*tau*x1, x1 - w*w_max*tau*x0, 0 nengo.Connection(oscillator, oscillator, function=feedback, synapse=tau) # The ensemble for controlling the speed of oscillation frequency = nengo.Ensemble(100, dimensions=1, label="frequency") nengo.Connection(frequency, oscillator[2]) # We need a quick input at the beginning to start the oscillator initial = nengo.Node(piecewise({0: [1, 0, 0], 0.15: [0, 0, 0]}), label="input") nengo.Connection(initial, oscillator) # Vary the speed over time input_frequency = nengo.Node( piecewise({0: 1, 1: 0.5, 2: 0, 3: -0.5, 4: -1}), label="frequency control") nengo.Connection(input_frequency, frequency) # Indicate which values to record oscillator_probe = nengo.Probe(oscillator, synapse=0.03) import nengo_gui
def test_invalid_function_length(): with pytest.raises(Exception): f = piecewise({0.5: 0, 1.0: lambda t: [t, t ** 2]}) assert f
# more directly to the differential equation used to describe an integrator: # $\dot{x} = \mathrm{Ax}(t) + \mathrm{Bu}(t)$ The control in this circuit is # A in that equation. This is also the controlled integrator described in the # book How to build a brain. import nengo from nengo.utils.functions import piecewise model = nengo.Network(label='Controlled Integrator 2') with model: # Make a population with 225 LIF neurons representing a 2 # dimensional signal, with a larger radius to accommodate large inputs A = nengo.Ensemble(225, dimensions=2, radius=1.5, label="A") # Create a piecewise step function for input input_func = piecewise( {0.2: 5, 0.3: 0, 0.44: -10, 0.54: 0, 0.8: 5, 0.9: 0}) inp = nengo.Node(output=input_func, label="input") # Connect the Input signal to ensemble A. tau = 0.1 nengo.Connection(inp, A, transform=[[tau], [0]], synapse=0.1) # Another piecewise function that changes half way through the run control_func = piecewise({0: 0, 0.6: -0.5}) control = nengo.Node(output=control_func, label="control") # Connect the "Control" signal to the second of A's two input channels nengo.Connection(control, A[1], synapse=0.005) # Note the changes from the previous example to the function being defined. nengo.Connection(A, A[0],
return -x[0] * x[1] # INPUTS / OUTPUTS # # Amy stim_amyg = nengo.Node([1]) # OFC possible_stim = np.vstack( (np.linspace(0.1, 0.9, num=7), np.linspace(0.05, 0.6, num=7) * (-1))) all_stim = input_stim(possible_stim, n_trials) # indicate number of trials cur_dic = stim_time(all_stim, len_stim, len_pause, len_fix) # (len_stim, len_pause,len_fix) # mod((length of stim),(number of features plus 1))!=0 cur_dic = collections.OrderedDict(sorted(cur_dic.items())) stim_ofc = nengo.Node(piecewise(cur_dic)) # ENSEMBLES # # AMYG amyg = nengo.Ensemble(N, 1, radius=brad, max_rates=get_maxrates(N, 100, 100), neuron_type=neurontype) # OFC ofc = nengo.Ensemble(N, 2, max_rates=get_maxrates(N, 100, 100), radius=brad, neuron_type=neurontype)
c1 = scipy.special.binom(n, k) c2 = scipy.special.binom((n+k-1)/2, n) coeffs.append(c1 * c2) def L(x): accumulator = np.zeros(x.shape) argument = np.ones(x.shape) for k in range(n+1): accumulator += np.multiply(argument, coeffs[k]) argument = np.multiply(argument, x) return 2.0**n * accumulator return L model = nengo.Network(label='Integrator') with model: A = nengo.Ensemble(100, dimensions=1) input = nengo.Node(piecewise({0:0, 0.2:1, 1:0, 2:-2, 3:0, 4:1, 5:0})) tau = 0.1 conn_recurrent = nengo.Connection(A, A, transform=[[1]], synapse=tau) conn_input = nengo.Connection(input, A, transform=[[tau]], synapse=tau) input_probe = nengo.Probe(input) A_probe = nengo.Probe(A, synapse=0.01) sim = nengo.Simulator(model) # find 1-D principal components and plot eval_points_in = np.linspace(-2.0, 2.0, 50) eval_points_in = np.ndarray(shape=(50,1), buffer=eval_points_in) eval_points, activities = tuning_curves(A, sim, eval_points_in) #plt.figure() #plt.title("Tuning curves") #plt.plot(eval_points, activities)
pre_ensemble = nengo.Ensemble(100, dimensions=1) nengo.Connection(pre_ensemble, pre_ensemble, synapse=0.1) nengo.Connection(pre_input, pre_ensemble, synapse=None, transform=0.1) with nengo.Network() as post_integrator: post_input = nengo.Node(size_in=1) post_ensemble = nengo.Ensemble(100, dimensions=1) nengo.Connection(post_ensemble, post_ensemble, synapse=0.1) nengo.Connection(post_input, post_ensemble, synapse=None, transform=0.1) nengo.Connection(pre_ensemble, post_input) # In[ ]: # Test the integrators by giving piecewise input and plotting with net: piecewise_f = piecewise({0: 0, 0.2: 0.5, 1: 0, 2: -1, 3: 0, 4: 1, 5: 0}) piecewise_inp = nengo.Node(piecewise_f) nengo.Connection(piecewise_inp, pre_input) input_probe = nengo.Probe(piecewise_inp) pre_probe = nengo.Probe(pre_ensemble, synapse=0.01) post_probe = nengo.Probe(post_ensemble, synapse=0.01) with nengo.Simulator(net) as sim: sim.run(6) plt.plot(sim.trange(), sim.data[input_probe], color='k', label="input") plt.plot(sim.trange(), sim.data[pre_probe], color='b', label="pre integrator") plt.plot(sim.trange(), sim.data[post_probe], color='g', label="post integrator") plt.legend(loc='best'); # Note that the `with` statements # are not just cosmetic;
# template which can also be used to quickly construct a harmonic oscillator # (or any other linear system). # Create the model object import nengo from nengo.utils.functions import piecewise import matplotlib.pyplot as plt import numpy as np model = nengo.Network(label='Oscillator') with model: # Create the ensemble for the oscillator neurons = nengo.Ensemble(200, dimensions=2, label="neurons") # Create an input signal input = nengo.Node(piecewise({0: [1, 0], 0.1: [0, 0]}), label="input") # Connect the input signal to the neural ensemble nengo.Connection(input, neurons) # Create the feedback connection nengo.Connection(neurons, neurons, transform=[[1, 1], [-1, 1]], synapse=0.1) input_probe = nengo.Probe(input, 'output') neuron_probe = nengo.Probe(neurons, 'decoded_output', synapse=0.1) # start the simulator sim = nengo.Simulator(model)
def test_invalid_length(): with pytest.raises(ValidationError): f = piecewise({0.5: [1, 0], 1.0: [1, 0, 0]}) assert f
manual = nengo.Node([0, 0]) #### actions / rewards actions = nengo.Ensemble(100, 2, radius=1.4) # which action to choose errors = nengo.networks.EnsembleArray(n_neurons=50, n_ensembles=2) act_times = {0: 1} pause = 0.5 choice = 0.2 time = 0 for i in range(500): act_times[time] = 1 time += pause act_times[time] = 0 time += choice act = nengo.Node(piecewise(act_times)) nengo.Connection(act, actions.neurons, transform=-2 * np.ones((actions.n_neurons, 1))) nengo.Connection(actions, reward) nengo.Connection(reward, errors.input, transform=-1) nengo.Connection(manual, actions) #### input to basal ganglia # random_thoughts = nengo.Node(np.sin) state = nengo.Ensemble(100, 2, radius=2) #state_integrator = nengo.Ensemble(100,1,radius=2) norm_state = nengo.Ensemble(100, 2, radius=2) # nengo.Connection(random_thoughts, state) nengo.Connection(state, norm_state, function=normalize)
from __future__ import division from nengo.utils.functions import piecewise import numpy as np import nengo import matplotlib.pyplot as plt import raphan_matrices as rmat # MODEL PARAMETERS # ===== TAU_RECUR = 0.1 input_func = input_func = piecewise({0: 0, 0.2: 5, 0.3: 0, 0.44: -10, 0.54: 0, 0.8: 5, 0.9: 0}) control_func = piecewise({0: 1, 0.6: 0.5}) # function to define integrator behaviour model = nengo.Network(label='1dinteg') with model: A = nengo.Ensemble(200, dimensions=2, radius=1.5) inp = nengo.Node(input_func) control = nengo.Node(output=control_func) # wire the stimulus input nengo.Connection(inp, A, transform=[[TAU_RECUR], [0]], synapse=TAU_RECUR) # wire the control quality input nengo.Connection(control, A[1], synapse=0.005) # set up the recurrent input nengo.Connection(A, A[0], function = lambda x: x[0] * x[1], synapse=TAU_RECUR)
import nengo tau = 0.1 # Post-synaptic time constant for feedback\n", w_max = 10 # Maximum frequency is w_max/(2*pi)\n", model = nengo.Model('Controlled Oscillator') oscillator = nengo.Ensemble(nengo.LIF(500), dimensions=3, radius=1.7) def feedback(x): x0, x1, w = x # These are the three variables stored in the ensemble\n", return x0 + w * w_max * tau * x1, x1 - w * w_max * tau * x0, 0 nengo.Connection(oscillator, oscillator, function=feedback, filter=tau) frequency = nengo.Ensemble(nengo.LIF(100), dimensions=1) nengo.Connection(frequency, oscillator, transform=[[0], [0], [1]]) from nengo.utils.functions import piecewise initial = nengo.Node(piecewise({0: [1, 0, 0], 0.15: [0, 0, 0]})) nengo.Connection(initial, oscillator) input_frequency = nengo.Node(piecewise({0: 1, 1: 0.5, 2: 0, 3: -0.5, 4: -1})) nengo.Connection(input_frequency, frequency)
import nengo tau = 0.1 # Post-synaptic time constant for feedback\n", w_max = 10 # Maximum frequency is w_max/(2*pi)\n", model = nengo.Model('Controlled Oscillator') oscillator = nengo.Ensemble(nengo.LIF(500), dimensions=3, radius=1.7) def feedback(x): x0, x1, w = x # These are the three variables stored in the ensemble\n", return x0 + w*w_max*tau*x1, x1 - w*w_max*tau*x0, 0 nengo.Connection(oscillator, oscillator, function=feedback, filter=tau) frequency = nengo.Ensemble(nengo.LIF(100), dimensions=1) nengo.Connection(frequency, oscillator, transform=[[0], [0], [1]]) from nengo.utils.functions import piecewise initial = nengo.Node(piecewise({0: [1, 0, 0], 0.15: [0, 0, 0]})) nengo.Connection(initial, oscillator) input_frequency = nengo.Node(piecewise({0: 1, 1: 0.5, 2: 0, 3: -0.5, 4: -1})) nengo.Connection(input_frequency, frequency)
import nengo model = nengo.Model("Inhibitory Gating") n_neurons = 30 A = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) B = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) C = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) import numpy as np from nengo.utils.functions import piecewise sin = nengo.Node(output=np.sin) inhib = nengo.Node(output=piecewise({ 0: 0, 2.5: 1, 5: 0, 7.5: 1, 10: 0, 12.5: 1 })) nengo.Connection(sin, A) nengo.Connection(sin, B) nengo.Connection(inhib, A.neurons, transform=[[-2.5]] * n_neurons) nengo.Connection(inhib, C) nengo.Connection(C, B.neurons, transform=[[-2.5]] * n_neurons)
import nengo model = nengo.Model('Oscillator') neurons = nengo.Ensemble(nengo.LIF(200), dimensions=2) from nengo.utils.functions import piecewise input = nengo.Node(output=piecewise({0: [1, 0], 0.1: [0, 0]})) nengo.Connection(input, neurons) nengo.Connection(neurons, neurons, transform=[[1, 1], [-1, 1]], filter=0.1)
# This next two lines make all of the encoders in the Combined population point at the # corners of the cube. This improves the quality of the computation. from nengo.dists import Choice # Comment out the line below for 'normal' encoders combined.encoders = Choice([[1, 1], [-1, 1], [1, -1], [-1, -1]]) # ## Step 2: Provide input to the model # We will use two varying scalar values for the two input signals that drive activity in ensembles A and B. # In[ ]: from nengo.utils.functions import piecewise with model: # Create a piecewise step function for input inputA = nengo.Node(piecewise({0: 0, 2.5: 10, 4: -10})) inputB = nengo.Node(piecewise({0: 10, 1.5: 2, 3: 0, 4.5: 2})) correct_ans = piecewise({0: 0, 1.5: 0, 2.5: 20, 3: 0, 4: 0, 4.5: -20}) # ## Step 3: Connect the elements of the model # In[ ]: with model: # Connect the input nodes to the appropriate ensembles nengo.Connection(inputA, A) nengo.Connection(inputB, B) # Connect input ensembles A and B to the 2D combined ensemble nengo.Connection(A, combined[0])
from nengo.utils.functions import piecewise __author__ = 'bogdanp' import nengo import nengo.spa as spa model = nengo.Network("Oscillator network") with model: input = nengo.Node(piecewise({0: [1, 0], 0.1: [0, 0]})) osc = nengo.networks.Oscillator(1, 1, 100) nengo.Connection(input, osc.input)
import nengo model = nengo.Model('Integrator') A = nengo.Ensemble(nengo.LIF(100), dimensions=1, label='Integrator') from nengo.utils.functions import piecewise input = nengo.Node(piecewise({0: 0, 0.2: 1, 1: 0, 2: -2, 3: 0, 4: 1, 5: 0}), label='Piecewise input') tau = 0.1 nengo.Connection(A, A, transform=[[1]], filter=tau) # Using a long time constant for stability\n" nengo.Connection(input, A, transform=[[tau]], filter=tau) # The same time constant as recurrent to make it more 'ideal'"
def test_invalid_key(): with pytest.raises(ValidationError): f = piecewise({0.5: 1, 1: 0, "a": 0.2}) assert f
# controlled integrator. We create a Network, and then we create a population # of neurons (called an *ensemble*). This population of neurons will # represent the state of our integrator, and the connections between # the neurons in the ensemble will define the dynamics of our integrator. import nengo from nengo.utils.functions import piecewise model = nengo.Network(label='Controlled Integrator') with model: # Make a population with 225 LIF neurons representing a 2 dimensional # signal, with a larger radius to accommodate large inputs A = nengo.Ensemble(225, dimensions=2, radius=1.5, label="A") # Create a piecewise step function for input input_func = piecewise( {0: 0, 0.2: 5, 0.3: 0, 0.44: -10, 0.54: 0, 0.8: 5, 0.9: 0}) # Define an input signal within our model inp = nengo.Node(input_func, label="input") # Connect the Input signal to ensemble A. # The `transform` argument means "connect real-valued signal "Input" to the # first of the two input channels of A." tau = 0.1 nengo.Connection(inp, A, transform=[[tau], [0]], synapse=tau) # Another piecewise step that changes half way through the run control_func = piecewise({0: 1, 0.6: 0.5}) control = nengo.Node(output=control_func, label="control") # Connect the "Control" signal to the second of A's two input channels.
import nengo model = nengo.Model("Inhibitory Gating") n_neurons = 30 A = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) B = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) C = nengo.Ensemble(nengo.LIF(n_neurons), dimensions=1) import numpy as np from nengo.utils.functions import piecewise sin = nengo.Node(output=np.sin) inhib = nengo.Node(output=piecewise({0: 0, 2.5: 1, 5: 0, 7.5: 1, 10: 0, 12.5: 1})) nengo.Connection(sin, A) nengo.Connection(sin, B) nengo.Connection(inhib, A.neurons, transform=[[-2.5]] * n_neurons) nengo.Connection(inhib, C) nengo.Connection(C, B.neurons, transform=[[-2.5]] * n_neurons)