예제 #1
0
def test_nrmse():
    assert np.allclose(nrmse(np.ones(100), np.ones(100)), 0)
    assert np.allclose(nrmse([0, 0, 0], [42, 9000, 0]), 1)

    ideal = sphere.sample(100, 3)
    r = np.asarray([1, 2, 3])
    assert np.allclose(nrmse(ideal, r * ideal, axis=0), 1 - 1. / r)
예제 #2
0
def test_hetero_neurons(Simulator, rng, seed):
    n_neurons = 100
    dt = 0.001
    T = 0.1
    dims_in = 2

    taus = nengo.dists.Uniform(0.001, 0.1).sample(n_neurons, rng=rng)
    synapses = [Lowpass(tau) for tau in taus]
    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    hs = HeteroSynapse(synapses, dt)

    def embed_encoders(x):
        # Reshapes the vectors to be the same dimensionality as the
        # encoders, and then takes the dot product row by row.
        # See http://stackoverflow.com/questions/26168363/ for a more
        # efficient solution.
        return np.sum(encoders * hs.from_vector(x), axis=1)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(nengo.Node(
                output=nengo.processes.WhiteSignal(T, high=10, seed=seed)),
                             stim[i],
                             synapse=None)

        # HeteroSynapse node
        syn = nengo.Node(size_in=dims_in, output=hs)

        # For comparing results
        x = [
            nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
            for _ in range(2)
        ]  # expected, actual

        # Expected
        for i, synapse in enumerate(synapses):
            t = np.zeros_like(encoders)
            t[i, :] = encoders[i, :]
            nengo.Connection(stim, x[0].neurons, transform=t, synapse=synapse)

        # Actual
        nengo.Connection(stim, syn, synapse=None)
        nengo.Connection(syn,
                         x[1].neurons,
                         function=embed_encoders,
                         synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0].neurons, synapse=None)
        p_act = nengo.Probe(x[1].neurons, synapse=None)

    # Check correctness
    with Simulator(model, dt=dt) as sim:
        sim.run(T)

    assert np.allclose(sim.data[p_act], sim.data[p_exp])
예제 #3
0
def test_hetero_neurons(Simulator, rng, seed):

    n_neurons = 100
    dt = 0.001
    T = 0.1
    dims_in = 2

    taus = nengo.dists.Uniform(0.001, 0.1).sample(n_neurons, rng=rng)
    synapses = [Lowpass(tau) for tau in taus]
    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    hs = HeteroSynapse(synapses, dt)

    def embed_encoders(x):
        # Reshapes the vectors to be the same dimensionality as the
        # encoders, and then takes the dot product row by row.
        # See http://stackoverflow.com/questions/26168363/ for a more
        # efficient solution.
        return np.sum(encoders * hs.from_vector(x), axis=1)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(
                nengo.Node(output=nengo.processes.WhiteSignal(T, high=10)),
                stim[i], synapse=None)

        # HeteroSynapse node
        syn = nengo.Node(size_in=dims_in, output=hs)

        # For comparing results
        x = [nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
             for _ in range(2)]  # expected, actual

        # Expected
        for i, synapse in enumerate(synapses):
            t = np.zeros_like(encoders)
            t[i, :] = encoders[i, :]
            nengo.Connection(stim, x[0].neurons, transform=t, synapse=synapse)

        # Actual
        nengo.Connection(stim, syn, synapse=None)
        nengo.Connection(
            syn, x[1].neurons, function=embed_encoders, synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0].neurons, synapse=None)
        p_act = nengo.Probe(x[1].neurons, synapse=None)

    # Check correctness
    sim = Simulator(model, dt=dt, seed=seed)
    sim.run(T)

    assert np.allclose(sim.data[p_act], sim.data[p_exp])
예제 #4
0
def test_hetero_multi_vector(Simulator, rng, seed):
    n_neurons = 20
    dt = 0.0005
    T = 0.1
    dims_in = 2
    synapses = [Alpha(0.1), Lowpass(0.005), Alpha(0.02)]

    dims_out = len(synapses)*dims_in
    encoders = sphere.sample(n_neurons, dims_out, rng=rng)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(
                nengo.Node(output=nengo.processes.WhiteSignal(T, high=10)),
                stim[i], synapse=None)

        # HeteroSynapse Nodes
        syn_dot = nengo.Node(
            size_in=dims_in, output=HeteroSynapse(synapses, dt))
        syn_elemwise = nengo.Node(
            size_in=dims_out, output=HeteroSynapse(
                np.repeat(synapses, dims_in), dt, elementwise=True))

        # For comparing results
        x = [nengo.Ensemble(n_neurons, dims_out, seed=0, encoders=encoders)
             for _ in range(3)]  # expected, actual 1, actual 2

        # Expected
        for j, synapse in enumerate(synapses):
            nengo.Connection(
                stim, x[0][j*dims_in:(j+1)*dims_in], synapse=synapse)

        # Actual (method #1 = matrix multiplies)
        nengo.Connection(stim, syn_dot, synapse=None)
        nengo.Connection(syn_dot, x[1], synapse=None)

        # Actual (method #2 = elementwise)
        for j in range(len(synapses)):
            nengo.Connection(
                stim, syn_elemwise[j*dims_in:(j+1)*dims_in], synapse=None)
        nengo.Connection(syn_elemwise, x[2], synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0], synapse=None)
        p_act_dot = nengo.Probe(x[1], synapse=None)
        p_act_elemwise = nengo.Probe(x[2], synapse=None)

    # Check correctness
    sim = Simulator(model, dt=dt, seed=seed)
    sim.run(T)

    assert np.allclose(sim.data[p_act_dot], sim.data[p_exp])
    assert np.allclose(sim.data[p_act_elemwise], sim.data[p_exp])
예제 #5
0
def test_hetero_vector(Simulator, rng, seed):
    n_neurons = 20
    dt = 0.0005
    T = 0.1
    dims_in = 2
    synapses = [Alpha(0.1), Lowpass(0.005)]
    assert dims_in == len(synapses)

    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(nengo.Node(
                output=nengo.processes.WhiteSignal(T, high=10, seed=seed)),
                             stim[i],
                             synapse=None)

        # HeteroSynapse Nodes
        syn_elemwise = nengo.Node(size_in=dims_in,
                                  output=HeteroSynapse(synapses,
                                                       dt,
                                                       elementwise=True))

        # For comparing results
        x = [
            nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
            for _ in range(2)
        ]  # expected, actual

        # Expected
        for j, synapse in enumerate(synapses):
            nengo.Connection(stim[j], x[0][j], synapse=synapse)

        # Actual
        nengo.Connection(stim, syn_elemwise, synapse=None)
        nengo.Connection(syn_elemwise, x[1], synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0], synapse=None)
        p_act_elemwise = nengo.Probe(x[1], synapse=None)

    # Check correctness
    with Simulator(model, dt=dt) as sim:
        sim.run(T)

    assert np.allclose(sim.data[p_act_elemwise], sim.data[p_exp])
예제 #6
0
def test_hetero_vector(Simulator, rng, seed):
    n_neurons = 20
    dt = 0.0005
    T = 0.1
    dims_in = 2
    synapses = [Alpha(0.1), Lowpass(0.005)]
    assert dims_in == len(synapses)

    encoders = sphere.sample(n_neurons, dims_in, rng=rng)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(
                nengo.Node(output=nengo.processes.WhiteSignal(T, high=10)),
                stim[i], synapse=None)

        # HeteroSynapse Nodes
        syn_elemwise = nengo.Node(
            size_in=dims_in,
            output=HeteroSynapse(synapses, dt, elementwise=True))

        # For comparing results
        x = [nengo.Ensemble(n_neurons, dims_in, seed=0, encoders=encoders)
             for _ in range(2)]  # expected, actual

        # Expected
        for j, synapse in enumerate(synapses):
            nengo.Connection(stim[j], x[0][j], synapse=synapse)

        # Actual
        nengo.Connection(stim, syn_elemwise, synapse=None)
        nengo.Connection(syn_elemwise, x[1], synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0], synapse=None)
        p_act_elemwise = nengo.Probe(x[1], synapse=None)

    # Check correctness
    sim = Simulator(model, dt=dt, seed=seed)
    sim.run(T)

    assert np.allclose(sim.data[p_act_elemwise], sim.data[p_exp])
예제 #7
0
def test_hetero_multi_vector(Simulator, rng, seed):
    n_neurons = 20
    dt = 0.0005
    T = 0.1
    dims_in = 2
    synapses = [Alpha(0.1), Lowpass(0.005), Alpha(0.02)]

    dims_out = len(synapses) * dims_in
    encoders = sphere.sample(n_neurons, dims_out, rng=rng)

    with Network(seed=seed) as model:
        # Input stimulus
        stim = nengo.Node(size_in=dims_in)
        for i in range(dims_in):
            nengo.Connection(nengo.Node(
                output=nengo.processes.WhiteSignal(T, high=10, seed=seed)),
                             stim[i],
                             synapse=None)

        # HeteroSynapse Nodes
        syn_dot = nengo.Node(size_in=dims_in,
                             output=HeteroSynapse(synapses, dt))
        syn_elemwise = nengo.Node(size_in=dims_out,
                                  output=HeteroSynapse(np.repeat(
                                      synapses, dims_in),
                                                       dt,
                                                       elementwise=True))

        # For comparing results
        x = [
            nengo.Ensemble(n_neurons, dims_out, seed=0, encoders=encoders)
            for _ in range(3)
        ]  # expected, actual 1, actual 2

        # Expected
        for j, synapse in enumerate(synapses):
            nengo.Connection(stim,
                             x[0][j * dims_in:(j + 1) * dims_in],
                             synapse=synapse)

        # Actual (method #1 = matrix multiplies)
        nengo.Connection(stim, syn_dot, synapse=None)
        nengo.Connection(syn_dot, x[1], synapse=None)

        # Actual (method #2 = elementwise)
        for j in range(len(synapses)):
            nengo.Connection(stim,
                             syn_elemwise[j * dims_in:(j + 1) * dims_in],
                             synapse=None)
        nengo.Connection(syn_elemwise, x[2], synapse=None)

        # Probes
        p_exp = nengo.Probe(x[0], synapse=None)
        p_act_dot = nengo.Probe(x[1], synapse=None)
        p_act_elemwise = nengo.Probe(x[2], synapse=None)

    # Check correctness
    with Simulator(model, dt=dt) as sim:
        sim.run(T)

    assert np.allclose(sim.data[p_act_dot], sim.data[p_exp])
    assert np.allclose(sim.data[p_act_elemwise], sim.data[p_exp])
예제 #8
0
def figure_time_cells(targets, order):
    npfile = np.load(TIME_CELLS_SIM % order)
    t = npfile['t']
    x = npfile['x']
    spikes = npfile['a']

    mus = []
    stds = []
    for i in range(spikes.shape[1]):
        spike_times = t[spikes[:, i] > 0]
        spike_times = spike_times[(spike_times >= 1)] - 1
        total = len(spike_times)
        if total > 0:
            within = len(np.where(spike_times < 4.784)[0])
            if within / float(total) >= 0.9:
                mus.append(np.mean(spike_times))
                stds.append(np.std(spike_times))
    logging.info("Remaining: %d", len(mus))
    logging.info("Pearson's correlation: %s", pearsonr(mus, stds))

    model = sm.OLS(stds, sm.add_constant(mus))
    res = model.fit()
    logging.info("Intercept / Slope: %s", res.params)
    logging.info("Standard errors: %s", res.bse)
    logging.info("p-values: %s", res.pvalues)

    sample_rate = 10
    t = t[::sample_rate]  # downsample PDFs to avoid reader lag
    x = x[::sample_rate]

    n_cells = 73
    intercept = -1

    rng = np.random.RandomState(seed=2)  # chosen to get a good spread
    encoders = []
    while len(encoders) < n_cells:
        e = sphere.sample(1, x.shape[1], rng=rng).squeeze()
        s = np.dot(x, e)
        if np.max(s) > intercept and 1.1 <= t[np.argmax(s)] <= 5.5:
            encoders.append(e)
    encoders = np.asarray(encoders)

    # Compute heat plot (right subfigure)
    a = np.dot(x[t >= 1.0], encoders.T)
    a = a.clip(intercept)
    a -= np.min(a, axis=0)
    assert np.all(np.sum(a, axis=0) > 0)
    normed_a = a / np.max(a, axis=0)
    assert np.allclose(np.min(normed_a, axis=0), 0)
    assert np.allclose(np.max(normed_a, axis=0), 1)

    t_peak = np.argmax(normed_a, axis=0)
    order = np.argsort(t_peak)
    heat = normed_a[:, order]

    # Compute cosine similarities (left subfigure)
    sorted_a = a[:, order]
    norms = np.linalg.norm(sorted_a, axis=1)
    assert np.isfinite(norms).all()
    cos = sorted_a.dot(sorted_a.T) / norms[:, None] / norms[None, :]
    cos -= np.min(cos)  # normalize the color map
    cos /= np.max(cos)

    cmap = 'jet'  # bad, but consistent with Zoran's figure

    with sns.axes_style('white'):
        with sns.plotting_context('paper', font_scale=3.0):
            aspect = 0.790
            t_ticks = ['', 0, 1, 2, 3, 4, '']

            pylab.figure(figsize=(22, 7.4))
            gs = gridspec.GridSpec(1, 2)
            ax1 = plt.subplot(gs[0])
            ax2 = plt.subplot(gs[1])

            ax1.imshow(cos, cmap=cmap, aspect=aspect, interpolation='none')
            ax1.set_xticklabels(t_ticks)
            ax1.set_yticklabels(t_ticks)
            ax1.set_xlabel("Time [s]", labelpad=20)
            ax1.set_ylabel("Time [s]", labelpad=20)
            ax1.set_anchor('S')

            ax2.imshow(heat.T,
                       cmap=cmap,
                       aspect=aspect * heat.shape[0] / float(heat.shape[1]),
                       interpolation='none')
            ax2.set_xticklabels(t_ticks)
            ax2.set_xlabel("Time [s]", labelpad=20)
            ax2.set_ylabel(r"Cell \#", labelpad=20)
            ax2.set_anchor('S')

            savefig(targets[0])
예제 #9
0
def figure_delay_example(targets):
    npfile = np.load(DELAY_EXAMPLE_SIM)
    theta = npfile['theta']
    dt = npfile['dt']
    t = npfile['t']
    u = npfile['u']
    x = npfile['x']
    a = npfile['a']
    y = npfile['y']
    T = t[-1]

    ideal = ideal_delay(u, theta, dt)
    logging.info("Paper constant: NRMSE: %s", nrmse(y, target=ideal))

    n_encoders = 1000
    rng = np.random.RandomState(seed=0)
    encoders = sphere.sample(n_encoders, x.shape[1], rng=rng)
    sims = np.dot(x, encoders.T)
    order = np.argsort(np.argmax(sims, axis=0))
    intercept = -1
    sims = sims.clip(intercept)
    sims -= np.min(sims, axis=0)
    sims /= np.max(sims, axis=0)
    a = sims[:, order]
    assert np.isfinite(a).all()

    sample_rate = 50  # downsample PDFs to avoid reader lag
    t = t[::sample_rate]
    u = u[::sample_rate]
    x = x[::sample_rate]
    a = a[::sample_rate]
    y = y[::sample_rate]
    ideal = ideal[::sample_rate]

    cmap = sns.diverging_palette(220, 20, as_cmap=True)

    with sns.axes_style('ticks'):
        with sns.plotting_context('paper', font_scale=2.8):
            f, (ax1, ax2, ax3) = pylab.subplots(3, 1, figsize=(18, 16))

            cpal = sns.color_palette(None, 2)
            ax1.plot(t, u, label=r"$u(t)$", c=cpal[0], lw=4, alpha=0.8)
            ax1.plot(t,
                     ideal,
                     label=r"$u(t - %s)$" % theta,
                     c=cpal[0],
                     lw=4,
                     linestyle='--',
                     zorder=2)  # on top
            ax1.plot(t, y, c=cpal[1], lw=4, label=r"$y(t)$", zorder=1)
            ax1.set_xlim(t[0], t[-1])
            ax1.set_ylim(-1, 1)
            ax1.set_xticks([])
            ax1.set_ylabel("Input / Output", labelpad=20)
            ax1.legend(bbox_to_anchor=(1.02, 1), loc=2, borderaxespad=0.)

            for i in range(x.shape[1]):
                ax2.plot(t,
                         x[:, -i],
                         label=r"$\hat{x}_%d(t)$" % (i + 1),
                         lw=2,
                         alpha=0.8)
            ax2.set_xlim(t[0], t[-1])
            ax2.set_ylim(-1, 1)
            ax2.set_xticks([])
            ax2.set_ylabel("Decoded State", labelpad=20)
            ax2.legend(bbox_to_anchor=(1.02, 1), loc=2, borderaxespad=0.)

            ax3.imshow(a.T, cmap=cmap, aspect='auto', interpolation='none')
            ax3.set_xlim(0, len(t))
            ax3.set_xticklabels(np.linspace(0, T, 9))
            ax3.set_xlabel("Time [s]", labelpad=20)
            ax3.set_ylabel(r"Cell \#", labelpad=20)

            segs = 100
            lc = LineCollection(segs * [[(0, 0)]],
                                lw=10,
                                colors=cmap(np.linspace(0, 1, segs)))
            ax3.legend([lc], ['Activity'],
                       handlelength=2,
                       handler_map={type(lc): HandlerDashedLines()},
                       bbox_to_anchor=(1.02, 1),
                       loc=2,
                       borderaxespad=0.)

            sns.despine(offset=15)

            savefig(targets[0])