コード例 #1
0
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
コード例 #2
0
def test_direct_window(legendre, Simulator, seed, plt):
    theta = 1.0
    T = theta

    assert np.allclose(t_default, np.linspace(0, 1, 1000))

    with Network() as model:
        stim = nengo.Node(output=lambda t: t)
        rw = RollingWindow(theta, n_neurons=1, dimensions=12,
                           neuron_type=nengo.Direct(), process=None,
                           legendre=legendre)
        assert rw.theta == theta
        assert rw.dt == 0.001
        assert rw.process is None
        assert rw.synapse == nengo.Lowpass(0.1)
        assert rw.input_synapse == nengo.Lowpass(0.1)

        nengo.Connection(stim, rw.input, synapse=None)
        output = rw.add_output(function=lambda w: np.sum(w**3)**2)
        p_output = nengo.Probe(output, synapse=None)

    with Simulator(model) as sim:
        sim.run(T)

    actual = sim.data[p_output].squeeze()
    t = sim.trange()
    ideal = shift(np.cumsum(t**3)**2)

    plt.figure()
    plt.plot(t, actual, label="Output")
    plt.plot(t, ideal, label="Ideal", linestyle='--')
    plt.legend()

    assert nrmse(actual, ideal) < 0.005
コード例 #3
0
def test_modred(rng):
    dt = 0.001
    isys = Lowpass(0.05)
    noise = 0.5 * Lowpass(0.01) + 0.5 * Alpha(0.005)
    p = 0.999
    sys = p * isys + (1 - p) * noise

    T, Tinv, S = balanced_transformation(sys)
    balsys = sys.transform(T, Tinv)

    # Keeping just the best state should remove the 3 noise dimensions
    # Discarding the lowest state should do at least as well
    for keep_states in (S.argmax(),
                        list(set(range(len(sys))) - set((S.argmin(), )))):
        delsys = modred(balsys, keep_states)
        assert delsys.order_den == np.asarray(keep_states).size

        u = rng.normal(size=2000)

        expected = sys.filt(u, dt)
        actual = delsys.filt(u, dt)
        assert rmse(expected, actual) < 1e-4

        step = np.zeros(2000)
        step[50:] = 1.0
        dcsys = modred(balsys, keep_states, method='dc')
        assert np.allclose(dcsys.dcgain, balsys.dcgain)

        # use of shift related to nengo issue #938
        assert not sys.has_passthrough
        assert dcsys.has_passthrough
        expected = shift(sys.filt(step, dt))
        actual = dcsys.filt(step, dt)
        assert rmse(expected, actual) < 1e-4
コード例 #4
0
ファイル: test_mapping.py プロジェクト: emaballarin/nengolib
def test_mapping(Simulator, plt, seed):
    sys = Alpha(0.1)
    syn = Lowpass(0.01)
    gsyn = 2*syn  # scaled lowpass
    isyn = 2/s  # scaled integrator
    dt = 0.001

    ss = ss2sim(sys, syn, None)  # normal lowpass, continuous
    dss = ss2sim(sys, syn, dt)  # normal lowpass, discrete
    gss = ss2sim(sys, gsyn, None)  # scaled lowpass, continuous
    gdss = ss2sim(sys, gsyn, dt)  # scaled lowpass, discrete
    iss = ss2sim(sys, isyn, None)  # scaled integrator, continuous
    idss = ss2sim(sys, isyn, dt)  # scaled integrator, discrete
    assert ss.analog and gss.analog and iss.analog
    assert not (dss.analog or gdss.analog or idss.analog)

    with Network(seed=seed) as model:
        stim = nengo.Node(output=lambda t: np.sin(20*np.pi*t))

        probes = []
        for mapped, synapse in ((ss, syn), (dss, syn), (gss, gsyn),
                                (gdss, gsyn), (iss, isyn), (idss, isyn)):
            A, B, C, D = mapped.ss
            x = nengo.Node(size_in=2)
            y = nengo.Node(size_in=1)

            nengo.Connection(stim, x, transform=B, synapse=synapse)
            nengo.Connection(x, x, transform=A, synapse=synapse)
            nengo.Connection(x, y, transform=C, synapse=None)
            nengo.Connection(stim, y, transform=D, synapse=None)

            probes.append(nengo.Probe(y))

        p_stim = nengo.Probe(stim)

    pss, pdss, pgss, pgdss, piss, pidss = probes

    with Simulator(model, dt=dt) as sim:
        sim.run(1.0)

    expected = shift(sys.filt(sim.data[p_stim], dt))

    plt.plot(sim.trange(), sim.data[pss], label="Continuous", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pdss], label="Discrete", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pgss], label="Gain Cont.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pgdss], label="Gain Disc.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[piss], label="Integ Cont.", alpha=0.5)
    plt.plot(sim.trange(), sim.data[pidss], label="Integ Disc.", alpha=0.5)
    plt.plot(sim.trange(), expected, label="Expected", linestyle='--')
    plt.legend()

    assert np.allclose(sim.data[pss], expected, atol=0.01)
    assert np.allclose(sim.data[pdss], expected)
    assert np.allclose(sim.data[pgss], expected, atol=0.01)
    assert np.allclose(sim.data[pgdss], expected)
    assert np.allclose(sim.data[piss], expected, atol=0.01)
    assert np.allclose(sim.data[pidss], expected)
コード例 #5
0
def test_non_siso_filtering(rng):
    sys = PadeDelay(0.1, order=4)
    length = 1000

    SIMO = sys.X
    assert not SIMO.is_SISO
    assert SIMO.size_in == 1
    assert SIMO.size_out == len(sys)

    x = SIMO.impulse(length)
    for i, (sub1, sub2) in enumerate(zip(sys, SIMO)):
        assert sub1 == sub2
        y1 = sub1.impulse(length)
        y2 = sub2.impulse(length)
        _transclose(shift(y1), shift(y2), x[:, i])

    B = np.asarray([[1, 2, 3], [0, 0, 0], [0, 0, 0], [0, 0, 0]]) * sys.B
    u = rng.randn(length, 3)

    Bu = u.dot([1, 2, 3])
    assert Bu.shape == (length,)
    MISO = LinearSystem((sys.A, B, sys.C, np.zeros((1, 3))), analog=True)
    assert not MISO.is_SISO
    assert MISO.size_in == 3
    assert MISO.size_out == 1

    y = cont2discrete(MISO, dt=0.001).filt(u)
    assert y.shape == (length,)
    assert np.allclose(shift(sys.filt(Bu)), y)

    MIMO = MISO.X
    assert not MIMO.is_SISO
    assert MIMO.size_in == 3
    assert MIMO.size_out == 4

    y = MIMO.filt(u)
    I = np.eye(len(sys))
    for i, sub1 in enumerate(MIMO):
        sub2 = LinearSystem((sys.A, B, I[i:i+1], np.zeros((1, 3))))
        _transclose(sub1.filt(u), sub2.filt(u), y[:, i])
コード例 #6
0
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])