def test_nonlinearity_to_nonlinearity(self):
        N = 30

        m = nengo.Model('test_nonlinearity_to_nonlinearity', seed=123)
        a = m.make_ensemble('A', nengo.LIF(N), dimensions=1)
        m.make_node('in', output=np.sin)
        m.make_node('inh', piecewise({0:0,2.5:1}))
        m.make_node('ideal', piecewise({0:np.sin,2.5:0}))

        m.connect('in', 'A')

        con = m.connect('inh', a.neurons, transform=[[-2.5]]*N)

        m.probe('in')
        m.probe('A', filter=0.1)
        m.probe('inh')
        m.probe('ideal')

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(5.0)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approx, filter=0.1')
            plt.plot(t, sim.data('inh'), label='Inhib signal')
            plt.plot(t, sim.data('ideal'), label='Ideal output')
            plt.legend(loc=0, prop={'size':10})
            plt.savefig('test_tononlinearity_connection.test_nonlinearity_to_nonlinearity.pdf')
            plt.close()

        self.assertTrue(np.allclose(sim.data('A')[-10:], sim.data('ideal')[-10:],
                                    atol=.1, rtol=.01))
Esempio n. 2
0
    def test_multiplication(self):
        model = nengo.Model('multiplication')

        with declarative_syntax(model):
            ens.ensemble('A', nengo.LIF(100), dimensions=1, radius=10)
            ens.ensemble('B', nengo.LIF(100), dimensions=1, radius=10)
            ens.ensemble('Combined', nengo.LIF(100), dimensions=2, radius=15)
            ens.ensemble('D', nengo.LIF(100), dimensions=1, radius=20)

            ens.encoders(
                'Combined',
                np.tile([[1, 1], [-1, 1], [1, -1], [-1, -1]],
                        (ens.n_neurons('Combined') / 4, 1)))

            ens.node('Input A', piecewise({0: 0, 2.5: 10, 4: -10}))
            ens.node('Input B', piecewise({0: 10, 1.5: 2, 3: 0, 4.5: 2}))

            conn.connect('Input A', 'A')
            conn.connect('Input B', 'B')
            conn.connect('A', 'Combined', transform=[[1], [0]])
            conn.connect('B', 'Combined', transform=[[0], [1]])
            conn.connect('Combined', 'D', function=lambda x: x[0] * x[1])

            for name in 'Input A', 'Input B':
                probe(name)

            for name in 'A', 'B', 'Combined', 'D':
                probe(name, filter=0.01)

        sim = model.simulator()

        sim.run(5)

        import matplotlib.pyplot as plt

        # Plot the input signals and decoded ensemble values
        correct_ans = nengo.helpers.piecewise({
            0: 0,
            1.5: 0,
            2.5: 20,
            3: 0,
            4: 0,
            4.5: -20
        })
        t = sim.data(model.t)
        plt.plot(t, sim.data('A'), label="Decoded A")
        plt.plot(t, sim.data('B'), label="Decoded B")
        plt.plot(t, sim.data('D'), label="Decoded D")
        out = [0] * t.shape[0]
        for i in np.arange(t.shape[0]):
            out[i] = correct_ans(t[i])
        plt.plot(t, out)
        plt.legend()
        plt.ylim(-25, 25)
Esempio n. 3
0
    def test_decoder_to_nonlinearity(self):
        N = 30

        m = nengo.Model('test_decoder_to_nonlinearity', seed=123)
        a = m.make_ensemble('A', nengo.LIF(N), dimensions=1)
        b = m.make_ensemble('B', nengo.LIF(N), dimensions=1)
        m.make_node('in', output=np.sin)
        m.make_node('inh', piecewise({0: 0, 2.5: 1}))
        m.make_node('ideal', piecewise({0: np.sin, 2.5: 0}))

        m.connect('in', 'A')
        m.connect('inh', 'B')

        con = m.connect('B', a.neurons, transform=[[-2.5]] * N)

        m.probe('in')
        m.probe('A', filter=0.1)
        m.probe('B', filter=0.1)
        m.probe('inh')
        m.probe('ideal')

        sim = m.simulator(dt=0.001, sim_class=self.Simulator)
        sim.run(5.0)

        with Plotter(self.Simulator) as plt:
            t = sim.data(m.t)
            plt.plot(t, sim.data('in'), label='Input')
            plt.plot(t, sim.data('A'), label='Neuron approx, pstc=0.1')
            plt.plot(t,
                     sim.data('B'),
                     label='Neuron approx of inhib sig, pstc=0.1')
            plt.plot(t, sim.data('inh'), label='Inhib signal')
            plt.plot(t, sim.data('ideal'), label='Ideal output')
            plt.legend(loc=0, prop={'size': 10})
            plt.savefig(
                'test_tononlinearity_connection.test_decoder_to_nonlinearity.pdf'
            )
            plt.close()

        self.assertTrue(
            np.allclose(sim.data('A')[-10:],
                        sim.data('ideal')[-10:],
                        atol=.1,
                        rtol=.01))
        self.assertTrue(
            np.allclose(sim.data('B')[-10:],
                        sim.data('inh')[-10:],
                        atol=.1,
                        rtol=.01))
Esempio n. 4
0
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)
Esempio n. 5
0
    def test_multiplication(self):
        model = nengo.Model('multiplication')

        with declarative_syntax(model):
            ens.ensemble('A', nengo.LIF(100), dimensions=1, radius=10)
            ens.ensemble('B', nengo.LIF(100), dimensions=1, radius=10)
            ens.ensemble('Combined', nengo.LIF(100), dimensions=2, radius=15)
            ens.ensemble('D', nengo.LIF(100), dimensions=1, radius=20)

            ens.encoders('Combined',
                 np.tile([[1,1],[-1,1],[1,-1],[-1,-1]],
                     (ens.n_neurons('Combined')/4, 1)))

            ens.node('Input A', piecewise({0:0, 2.5:10, 4:-10}))
            ens.node('Input B', piecewise({0:10, 1.5:2, 3:0, 4.5:2}))

            conn.connect('Input A', 'A')
            conn.connect('Input B', 'B')
            conn.connect('A','Combined', transform=[[1], [0]])
            conn.connect('B','Combined', transform=[[0], [1]])
            conn.connect('Combined', 'D', function=lambda x: x[0] * x[1])

            for name in 'Input A', 'Input B':
                probe(name)

            for name in 'A', 'B', 'Combined', 'D':
                probe(name, filter=0.01)

        sim = model.simulator()

        sim.run(5)

        import matplotlib.pyplot as plt

        # Plot the input signals and decoded ensemble values
        correct_ans = nengo.helpers.piecewise({0:0, 1.5:0, 2.5:20, 3:0, 4:0, 4.5:-20})
        t = sim.data(model.t)
        plt.plot(t, sim.data('A'), label="Decoded A")
        plt.plot(t, sim.data('B'), label="Decoded B")
        plt.plot(t, sim.data('D'), label="Decoded D")
        out = [0] * t.shape[0]
        for i in np.arange(t.shape[0]):
            out[i] = correct_ans(t[i])
        plt.plot(t, out)
        plt.legend()
        plt.ylim(-25,25);
Esempio n. 6
0
 def test_function(self):
     f = piecewise({0: np.sin, 0.5: np.cos})
     self.assertEqual(f(0), [np.sin(0)])
     self.assertEqual(f(0.25), [np.sin(0.25)])
     self.assertEqual(f(0.4999), [np.sin(0.4999)])
     self.assertEqual(f(0.5), [np.cos(0.5)])
     self.assertEqual(f(0.75), [np.cos(0.75)])
     self.assertEqual(f(1.0), [np.cos(1.0)])
Esempio n. 7
0
 def test_function(self):
     f = piecewise({0: np.sin, 0.5: np.cos})
     self.assertEqual(f(0), [np.sin(0)])
     self.assertEqual(f(0.25), [np.sin(0.25)])
     self.assertEqual(f(0.4999), [np.sin(0.4999)])
     self.assertEqual(f(0.5), [np.cos(0.5)])
     self.assertEqual(f(0.75), [np.cos(0.75)])
     self.assertEqual(f(1.0), [np.cos(1.0)])
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])
Esempio n. 10
0
 def test_basic(self):
     f = piecewise({0.5: 1, 1.0: 0})
     self.assertEqual(f(-10), [0])
     self.assertEqual(f(0), [0])
     self.assertEqual(f(0.25), [0])
     self.assertEqual(f(0.5), [1])
     self.assertEqual(f(0.75), [1])
     self.assertEqual(f(1.0), [0])
     self.assertEqual(f(1.5), [0])
     self.assertEqual(f(100), [0])
Esempio n. 11
0
 def test_lists(self):
     f = piecewise({0.5: [1, 0], 1.0: [0, 1]})
     self.assertEqual(f(-10), [0, 0])
     self.assertEqual(f(0), [0, 0])
     self.assertEqual(f(0.25), [0, 0])
     self.assertEqual(f(0.5), [1, 0])
     self.assertEqual(f(0.75), [1, 0])
     self.assertEqual(f(1.0), [0, 1])
     self.assertEqual(f(1.5), [0, 1])
     self.assertEqual(f(100), [0, 1])
Esempio n. 12
0
 def test_basic(self):
     f = piecewise({0.5: 1, 1.0: 0})
     self.assertEqual(f(-10), [0])
     self.assertEqual(f(0), [0])
     self.assertEqual(f(0.25), [0])
     self.assertEqual(f(0.5), [1])
     self.assertEqual(f(0.75), [1])
     self.assertEqual(f(1.0), [0])
     self.assertEqual(f(1.5), [0])
     self.assertEqual(f(100), [0])
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])
Esempio n. 14
0
 def test_lists(self):
     f = piecewise({0.5: [1, 0], 1.0: [0, 1]})
     self.assertEqual(f(-10), [0, 0])
     self.assertEqual(f(0), [0, 0])
     self.assertEqual(f(0.25), [0, 0])
     self.assertEqual(f(0.5), [1, 0])
     self.assertEqual(f(0.75), [1, 0])
     self.assertEqual(f(1.0), [0, 1])
     self.assertEqual(f(1.5), [0, 1])
     self.assertEqual(f(100), [0, 1])
Esempio n. 15
0
 def test_function_list(self):
     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})
     self.assertEqual(f(0), func1(0))
     self.assertEqual(f(0.25), func1(0.25))
     self.assertEqual(f(0.4999), func1(0.4999))
     self.assertEqual(f(0.5), func2(0.5))
     self.assertEqual(f(0.75), func2(0.75))
     self.assertEqual(f(1.0), func2(1.0))
Esempio n. 16
0
    def test_function_list(self):
        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})
        self.assertEqual(f(0), func1(0))
        self.assertEqual(f(0.25), func1(0.25))
        self.assertEqual(f(0.4999), func1(0.4999))
        self.assertEqual(f(0.5), func2(0.5))
        self.assertEqual(f(0.75), func2(0.75))
        self.assertEqual(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))
Esempio n. 18
0
 def test_invalid_length(self):
     with self.assertRaises(Exception):
         f = piecewise({0.5: [1, 0], 1.0: [1, 0, 0]})
Esempio n. 19
0
 def test_invalid_function_length(self):
     with self.assertRaises(Exception):
         f = piecewise({0.5: 0, 1.0: lambda t: [t, t ** 2]})
Esempio n. 20
0
 def test_invalid_length(self):
     with self.assertRaises(Exception):
         f = piecewise({0.5: [1, 0], 1.0: [1, 0, 0]})
Esempio n. 21
0
 def test_invalid_key(self):
     with self.assertRaises(TypeError):
         f = piecewise({0.5: 1, 1: 0, 'a': 0.2})
Esempio n. 22
0
 def test_invalid_function_length(self):
     with self.assertRaises(Exception):
         f = piecewise({0.5: 0, 1.0: lambda t: [t, t**2]})
def test_invalid_key():
    with pytest.raises(TypeError):
        f = piecewise({0.5: 1, 1: 0, 'a': 0.2})
        assert f
Esempio n. 24
0
 def test_invalid_key(self):
     with self.assertRaises(TypeError):
         f = piecewise({0.5: 1, 1: 0, 'a': 0.2})
def test_invalid_length():
    with pytest.raises(Exception):
        f = piecewise({0.5: [1, 0], 1.0: [1, 0, 0]})
        assert f
def test_invalid_function_length():
    with pytest.raises(Exception):
        f = piecewise({0.5: 0, 1.0: lambda t: [t, t ** 2]})
        assert f