Exemple #1
0
def test_tuning_curves_1d(Simulator, plt, seed):
    """For 1D ensembles, should be able to do plt.plot(*tuning_curves(...))."""
    model = nengo.Network(seed=seed)
    with model:
        ens_1d = nengo.Ensemble(10, dimensions=1, neuron_type=nengo.LIF())
    with Simulator(model) as sim:
        plt.plot(*tuning_curves(ens_1d, sim))
def test_tuning_curves_1d(Simulator, plt, seed):
    """For 1D ensembles, should be able to do plt.plot(*tuning_curves(...))."""
    model = nengo.Network(seed=seed)
    with model:
        ens_1d = nengo.Ensemble(10, dimensions=1, neuron_type=nengo.LIF())
    with Simulator(model) as sim:
        plt.plot(*tuning_curves(ens_1d, sim))
def test_tuning_curves(Simulator, nl_nodirect, plt, seed, dimensions):
    radius = 10
    max_rate = 400
    model = nengo.Network(seed=seed)
    with model:
        ens = nengo.Ensemble(
            10,
            dimensions=dimensions,
            neuron_type=nl_nodirect(),
            max_rates=Uniform(200, max_rate),
            radius=radius,
        )
    with Simulator(model) as sim:
        eval_points, activities = tuning_curves(ens, sim)

    plot_tuning_curves(plt, eval_points, activities)

    # Check that eval_points cover up to the radius.
    assert np.abs(radius - np.max(np.abs(eval_points))) <= (2 * radius /
                                                            dimensions)

    assert np.all(activities >= 0)

    d = np.sqrt(np.sum(np.asarray(eval_points)**2, axis=-1))
    assert np.all(activities[d <= radius] <= max_rate)
def test_tuning_curves_direct_mode(Simulator, plt, seed, dimensions):
    model = nengo.Network(seed=seed)
    with model:
        ens = nengo.Ensemble(10, dimensions, neuron_type=nengo.Direct())

    with Simulator(model) as sim:
        eval_points, activities = tuning_curves(ens, sim)

    plot_tuning_curves(plt, eval_points, activities)

    # eval_points is passed through in direct mode neurons
    assert np.allclose(eval_points, activities)
Exemple #5
0
def test_tuning_curves_direct_mode(Simulator, plt, seed, dimensions):
    model = nengo.Network(seed=seed)
    with model:
        ens = nengo.Ensemble(10, dimensions, neuron_type=nengo.Direct())

    with Simulator(model) as sim:
        eval_points, activities = tuning_curves(ens, sim)

    plot_tuning_curves(plt, eval_points, activities)

    # eval_points is passed through in direct mode neurons
    assert np.allclose(eval_points, activities)
def one_a():

	#ensemble parameters
	N=100
	dimensions=1
	tau_rc=0.02
	tau_ref=0.002
	noise=0.1
	lif_model=nengo.LIF(tau_rc=tau_rc,tau_ref=tau_ref)

	#model definition
	model = nengo.Network(label='1D Ensemble of LIF Neurons')
	with model:
		ens_1d = nengo.Ensemble(N,dimensions,
								intercepts=Uniform(-1.0,1.0),
								max_rates=Uniform(100,200),
								neuron_type=lif_model)

		#generate the decoders
		connection = nengo.Connection(ens_1d,ens_1d,
								solver=LstsqNoise(noise=noise))

	#create the simulator
	sim = nengo.Simulator(model)

	#retrieve evaluation points, activities, and decoders
	eval_points, activities = tuning_curves(ens_1d,sim)
	decoders = sim.data[connection].decoders.T

	#calculate the state estimate
	xhat = np.dot(activities,decoders)

	#plot tuning curves
	fig=plt.figure(figsize=(16,8))
	ax=fig.add_subplot(211)
	ax.plot(eval_points,activities)
	# ax.set_xlabel('$x$')
	ax.set_ylabel('Firing Rate $a$ (Hz)')

	#plot representational accuracy
	ax=fig.add_subplot(212)
	ax.plot(eval_points,eval_points)
	ax.plot(eval_points,xhat,
		label='RMSE=%f' %np.sqrt(np.average((eval_points-xhat)**2)))
	ax.set_xlabel('$x$')
	ax.set_ylabel('$\hat{x}$')
	legend=ax.legend(loc='best',shadow=True)
	plt.show()
Exemple #7
0
def plot_tuning_curves(neuron_params, ensemble_params, filename, show=False):
    """
    Plot tuning curves a neural population.
    """

    import nengo
    from nengo.utils.ensemble import tuning_curves
    import matplotlib as mpl
    if show:
        mpl.use('Qt4Agg')
    else:
        mpl.use('Agg')

    import matplotlib.pyplot as plt

    model = nengo.Network("Dummy Network")
    with model:
        neurons = nengo.LIF(**neuron_params),

        ensemble = nengo.Ensemble(neurons=neurons, **ensemble_params)

    sim = nengo.Simulator(model)
    eval_points, activities = tuning_curves(ensemble, sim)

    for neuron in activities.T:
        plt.plot(eval_points.T[0], neuron)

    plt.title("Tuning curves")
    plt.ylabel("Firing Rate (spikes/s)")
    plt.xlabel("ex")
    plt.ylim((0, 400))

    # We want different eval points for display purposes than for
    # optimization purposes
    # eval_points = Uniform(-radius, radius).sample(n_eval_points)
    # eval_points.sort()
    # eval_points = eval_points.reshape((n_eval_points, 1))

    # # have to divide by radius on our own since tuning_curves skips that step
    # _, activities = tuning_curves(standard, sim, eval_points/radius)
    # for neuron in activities.T:
    #     plt.plot(eval_points, neuron)

    if filename:
        plt.savefig(filename)

    if show:
        plt.show()
Exemple #8
0
def plot_tuning_curves(neuron_params, ensemble_params, filename, show=False):
    """
    Plot tuning curves a neural population.
    """

    import nengo
    from nengo.utils.ensemble import tuning_curves
    import matplotlib as mpl
    if show:
        mpl.use('Qt4Agg')
    else:
        mpl.use('Agg')

    import matplotlib.pyplot as plt

    model = nengo.Network("Dummy Network")
    with model:
        neurons = nengo.LIF(**neuron_params),

        ensemble = nengo.Ensemble(neurons=neurons, **ensemble_params)

    sim = nengo.Simulator(model)
    eval_points, activities = tuning_curves(ensemble, sim)

    for neuron in activities.T:
        plt.plot(eval_points.T[0], neuron)

    plt.title("Tuning curves")
    plt.ylabel("Firing Rate (spikes/s)")
    plt.xlabel("ex")
    plt.ylim((0, 400))

    # We want different eval points for display purposes than for
    # optimization purposes
    # eval_points = Uniform(-radius, radius).sample(n_eval_points)
    # eval_points.sort()
    # eval_points = eval_points.reshape((n_eval_points, 1))

    # # have to divide by radius on our own since tuning_curves skips that step
    # _, activities = tuning_curves(standard, sim, eval_points/radius)
    # for neuron in activities.T:
    #     plt.plot(eval_points, neuron)

    if filename:
        plt.savefig(filename)

    if show:
        plt.show()
Exemple #9
0
def plot_tuning_curves(model, ensemble):
    with nengo.Simulator(model) as sim:
        eval_points, activities = tuning_curves(ensemble, sim)

    plt.figure(figsize=(8, 8))
    ax = plt.subplot(111, projection="3d")
    ax.set_title("Tuning curves")
    for i in range(ensemble.n_neurons):
        ax.plot_surface(eval_points.T[0],
                        eval_points.T[1],
                        activities.T[i],
                        cmap=plt.cm.autumn)
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("Firing rate (Hz)")
    plt.show()
def ensemble_infomodal(ng, uid, conn_in_uids, conn_out_uids):
    ens = ng.uids[uid]

    params = [(attr, str(getattr(ens, attr)))
              for attr in ('n_neurons', 'dimensions', 'radius', 'encoders',
                           'intercepts', 'max_rates', 'eval_points',
                           'n_eval_points', 'neuron_type', 'noise', 'seed')
              if getattr(ens, attr) is not None]

    if ng.page.sim is None:
        plots = ("Simulation not yet running. "
                 "Start a simulation to see plots.")
    else:
        plots = []
        rc = PlotInfo("Response curves", plot="multiline")
        rc.x, rc.y = response_curves(ens, ng.page.sim)
        rc.y = rc.y.T
        if len(rc.y.shape) == 1:
            rc.y.shape = 1, rc.y.shape[0]
        if ens.n_neurons > 200:
            rc.warnings.append("Only showing the first 200 neurons.")
            rc.y = rc.y[:200]
        plots.append(rc.to_dict())

        tc = PlotInfo("Tuning curves")
        if ens.dimensions == 1:
            tc.plot = "multiline"
            tc.x, tc.y = tuning_curves(ens, ng.page.sim)
            tc.y = tc.y.T
            if ens.n_neurons > 200:
                tc.warnings.append("Only showing the first 200 neurons.")
                tc.y = tc.y[:200]
        else:
            tc.warnings.append("Tuning curves only shown for "
                               "one-dimensional ensembles.")
        plots.append(tc.to_dict())

    conninfo = conn_infomodal(ng, uid, conn_in_uids, conn_out_uids)

    js = ['Nengo.modal.title("Details for \'%s\'");' % ng.page.get_label(ens)]
    js.append('Nengo.modal.footer("close");')
    js.append(
        'Nengo.modal.ensemble_body("%s", %s, %s, %s);' %
        (uid, json.dumps(params), json.dumps(plots), json.dumps(conninfo)))
    js.append('Nengo.modal.show();')
    return '\n'.join(js)
Exemple #11
0
def tuning_curve_plot(ens, sim):
    tc = PlotInfo("Tuning curves")

    if ens.dimensions == 1:
        tc.plot = "multiline"
        tc.x, tc.y = tuning_curves(ens, sim)
        tc.x_label = "x"
        tc.y = tc.y.T
        tc.y_label = "Firing rate (Hz)"

        if ens.n_neurons > 200:
            tc.warnings.append("Only showing the first 200 neurons.")
            tc.y = tc.y[:200]
    else:
        tc.warnings.append("Tuning curves only shown for "
                           "one-dimensional ensembles.")
    return tc.to_dict()
def tuning_curve_plot(ens, sim):
    tc = PlotInfo("Tuning curves")

    if ens.dimensions == 1:
        tc.plot = "multiline"
        tc.x, tc.y = tuning_curves(ens, sim)
        tc.x_label = "x"
        tc.y = tc.y.T
        tc.y_label = "Firing rate (Hz)"

        if ens.n_neurons > 200:
            tc.warnings.append("Only showing the first 200 neurons.")
            tc.y = tc.y[:200]
    else:
        tc.warnings.append("Tuning curves only shown for "
                           "one-dimensional ensembles.")
    return tc.to_dict()
Exemple #13
0
def test_alif_rate(Simulator):
    n = 100
    max_rates = 50 * np.ones(n)
    # max_rates = 200 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(n,
                           1,
                           max_rates=max_rates,
                           intercepts=intercepts,
                           encoders=encoders,
                           neuron_type=nengo.AdaptiveLIFRate())
        nengo.Connection(u, a, synapse=None)
        ap = nengo.Probe(a, "spikes", synapse=None)

    dt = 1e-3
    sim = Simulator(model, dt=dt)
    sim.run(2.)

    t = sim.trange()
    rates = sim.data[ap]
    _, ref = tuning_curves(a, sim, eval_points=0.5)

    with Plotter(Simulator) as plt:
        ax = plt.subplot(211)
        implot(plt, t, intercepts[::-1], rates.T / dt, ax=ax)
        ax.set_xlabel('time [s]')
        ax.set_ylabel('input')
        ax = plt.subplot(212)
        ax.plot(intercepts, ref[:, ::-1].T, 'k--')
        ax.plot(intercepts, rates[[1, 500, 1000, -1], ::-1].T / dt)
        ax.set_xlabel('input')
        ax.set_xlabel('rate')
        plt.savefig('test_neurons.test_alif_rate.pdf')
        plt.close()

    # check that initial tuning curve is the same as LIF rates
    assert np.allclose(rates[1] / dt, ref, atol=0.1, rtol=1e-3)

    # check that curves in firing region are monotonically decreasing
    assert np.all(np.diff(rates[1:, intercepts < 0.4], axis=0) < 0)
Exemple #14
0
def plot_tuning_curves(ensemble, sim, connection=None, ax=None):
    """Plot tuning curves for the given ensemble and simulator.

    If a connection is provided, the decoders will be used to set
    the colours of the tuning curves.
    """

    if ax is None:
        ax = plt.gca()

    evals, t_curves = tuning_curves(ensemble, sim)

    if connection is not None:
        if connection.dimensions > 1:
            warnings.warn("Ignoring dimensions > 1 in plot_tuning_curves")
        cm = plt.cm.ScalarMappable(cmap=plt.cm.coolwarm)
        ax.set_color_cycle(cm.to_rgba(sim.data[connection].decoders[0]))
    ax.plot(evals, t_curves)
Exemple #15
0
def plot_tuning_curves(ensemble, sim, connection=None, ax=None):
    """Plot tuning curves for the given ensemble and simulator.

    If a connection is provided, the decoders will be used to set
    the colours of the tuning curves.
    """

    if ax is None:
        ax = plt.gca()

    evals, t_curves = tuning_curves(ensemble, sim)

    if connection is not None:
        if connection.dimensions > 1:
            warnings.warn("Ignoring dimensions > 1 in plot_tuning_curves")
        cm = plt.cm.ScalarMappable(cmap=plt.cm.coolwarm)
        set_color_cycle(cm.to_rgba(sim.data[connection].decoders[0]), ax=ax)
    ax.plot(evals, t_curves)
Exemple #16
0
def ensemble_infomodal(ng, uid, conn_in_uids, conn_out_uids):
    ens = ng.uids[uid]

    params = [(attr, str(getattr(ens, attr))) for attr in (
        'n_neurons', 'dimensions', 'radius', 'encoders', 'intercepts',
        'max_rates', 'eval_points', 'n_eval_points', 'neuron_type',
        'noise', 'seed') if getattr(ens, attr) is not None]

    if ng.page.sim is None:
        plots = ("Simulation not yet running. "
                 "Start a simulation to see plots.")
    else:
        plots = []
        rc = PlotInfo("Response curves", plot="multiline")
        rc.x, rc.y = response_curves(ens, ng.page.sim)
        rc.y = rc.y.T
        if len(rc.y.shape) == 1:
            rc.y.shape = 1, rc.y.shape[0]
        if ens.n_neurons > 200:
            rc.warnings.append("Only showing the first 200 neurons.")
            rc.y = rc.y[:200]
        plots.append(rc.to_dict())

        tc = PlotInfo("Tuning curves")
        if ens.dimensions == 1:
            tc.plot = "multiline"
            tc.x, tc.y = tuning_curves(ens, ng.page.sim)
            tc.y = tc.y.T
            if ens.n_neurons > 200:
                tc.warnings.append("Only showing the first 200 neurons.")
                tc.y = tc.y[:200]
        else:
            tc.warnings.append("Tuning curves only shown for "
                               "one-dimensional ensembles.")
        plots.append(tc.to_dict())

    conninfo = conn_infomodal(ng, uid, conn_in_uids, conn_out_uids)

    js = ['Nengo.modal.title("Details for \'%s\'");' % ng.page.get_label(ens)]
    js.append('Nengo.modal.footer("close");')
    js.append('Nengo.modal.ensemble_body("%s", %s, %s, %s);' % (
        uid, json.dumps(params), json.dumps(plots), json.dumps(conninfo)))
    js.append('Nengo.modal.show();')
    return '\n'.join(js)
def test_alif_rate(Simulator):
    n = 100
    max_rates = 50 * np.ones(n)
    # max_rates = 200 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(n, 1,
                           max_rates=max_rates,
                           intercepts=intercepts,
                           encoders=encoders,
                           neuron_type=nengo.AdaptiveLIFRate())
        nengo.Connection(u, a, synapse=None)
        ap = nengo.Probe(a, "spikes", synapse=None)

    dt = 1e-3
    sim = Simulator(model, dt=dt)
    sim.run(2.)

    t = sim.trange()
    rates = sim.data[ap]
    _, ref = tuning_curves(a, sim, eval_points=0.5)

    with Plotter(Simulator) as plt:
        ax = plt.subplot(211)
        implot(plt, t, intercepts[::-1], rates.T / dt, ax=ax)
        ax.set_xlabel('time [s]')
        ax.set_ylabel('input')
        ax = plt.subplot(212)
        ax.plot(intercepts, ref[:, ::-1].T, 'k--')
        ax.plot(intercepts, rates[[1, 500, 1000, -1], ::-1].T / dt)
        ax.set_xlabel('input')
        ax.set_xlabel('rate')
        plt.savefig('test_neurons.test_alif_rate.pdf')
        plt.close()

    # check that initial tuning curve is the same as LIF rates
    assert np.allclose(rates[1] / dt, ref, atol=0.1, rtol=1e-3)

    # check that curves in firing region are monotonically decreasing
    assert np.all(np.diff(rates[1:, intercepts < 0.4], axis=0) < 0)
Exemple #18
0
def test_alif_rate(Simulator, plt, allclose):
    n = 100
    max_rates = 50 * np.ones(n)
    intercepts = np.linspace(-0.99, 0.99, n)
    encoders = np.ones((n, 1))

    model = nengo.Network()
    with model:
        u = nengo.Node(output=0.5)
        a = nengo.Ensemble(
            n,
            1,
            max_rates=max_rates,
            intercepts=intercepts,
            encoders=encoders,
            neuron_type=AdaptiveLIFRate(),
        )
        nengo.Connection(u, a, synapse=None)
        ap = nengo.Probe(a.neurons)

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

    t = sim.trange()
    rates = sim.data[ap]
    _, ref = tuning_curves(a, sim, inputs=0.5)

    ax = plt.subplot(211)
    implot(plt, t, intercepts[::-1], rates.T, ax=ax)
    ax.set_xlabel("time [s]")
    ax.set_ylabel("input")
    ax = plt.subplot(212)
    ax.plot(intercepts, ref[::-1].T, "k--")
    ax.plot(intercepts, rates[[1, 500, 1000, -1], ::-1].T)
    ax.set_xlabel("input")
    ax.set_xlabel("rate")

    # check that initial tuning curve is the same as LIF rates
    assert allclose(rates[1], ref, atol=0.1, rtol=1e-3)

    # check that curves in firing region are monotonically decreasing
    assert np.all(np.diff(rates[1:, intercepts < 0.4], axis=0) < 0)
Exemple #19
0
def test_tuning_curves(Simulator, nl_nodirect, plt, seed, dimensions):
    radius = 10
    max_rate = 400
    model = nengo.Network(seed=seed)
    with model:
        ens = nengo.Ensemble(
            10, dimensions=dimensions, neuron_type=nl_nodirect(),
            max_rates=Uniform(200, max_rate), radius=radius)
    with Simulator(model) as sim:
        eval_points, activities = tuning_curves(ens, sim)

    plot_tuning_curves(plt, eval_points, activities)

    # Check that eval_points cover up to the radius.
    assert np.abs(radius - np.max(np.abs(eval_points))) <= (
        2 * radius / dimensions)

    assert np.all(activities >= 0)

    d = np.sqrt(np.sum(np.asarray(eval_points) ** 2, axis=-1))
    assert np.all(activities[d <= radius] <= max_rate)
Exemple #20
0
    def retrieve_recorded_weights(self, sim):
        '''
		Reconstructs the weight matrix for the underlying mixture model from the
		decoders recorded over time.

		sim: simulator object from which holds the probed decoders.
		'''

        from nengo.utils.ensemble import tuning_curves

        # Make sure data was recorded
        assert (self.record_learning)

        # Fetch the decoders over time for each basis ensemble
        decoders = [
            sim.data[probe] for probe in self.learning_connection_probes
        ]

        # Fetch the tuning curves for each basis ensemble
        xs = np.linspace(0, 1, 100).reshape(-1, 1)
        activities = [
            tuning_curves(ens, sim, xs)[1]
            for ens in self.ens_basis.ea_ensembles
        ]

        # Calculate the weights
        N = decoders[0].shape[0]  # Number of timesteps
        ws = np.zeros((N, self.n_basis))
        for i in range(N):  # Iterate over each timestep
            for j in range(self.n_basis):  # Iterate over each basis
                # Decode the current function that is being computed
                fs = activities[j]
                dec = decoders[j][i]
                f = fs @ dec.T

                # Obtain the weight value by fitting a linear function to the
                # decoded function
                ws[i, j] = np.polyfit(xs[:, 0], f[:, 0], 1)[0]
        return ws
Exemple #21
0
def test_tuning_curves(Simulator, plt, seed, dimensions):
    radius = 10
    max_rate = 400
    model = nengo.Network(seed=seed)
    with model:
        ens = nengo.Ensemble(
            10, dimensions=dimensions, neuron_type=nengo.LIF(),
            max_rates=Uniform(200, max_rate), radius=radius)
    sim = Simulator(model)

    eval_points, activities = tuning_curves(ens, sim)

    plt.saveas = 'utils.test_ensemble.test_tuning_curves_%d.pdf' % dimensions
    plot_tuning_curves(plt, eval_points, activities)

    # Check that eval_points cover up to the radius.
    assert np.abs(radius - np.max(np.abs(eval_points))) <= (
        2 * radius / dimensions)

    assert np.all(activities >= 0)

    d = np.sqrt(np.sum(np.asarray(eval_points) ** 2, axis=0))
    assert np.all(activities[:, d <= radius] <= max_rate)
def plot(model, neurons, cos_probe, spikes, filtered):
    # run the simulation for one second
    with nengo.Simulator(model) as sim:
        eval_points, activities = tuning_curves(neurons, sim)
        sim.run(1)
    # Plot tuning-curves
    plt.figure()
    plt.plot(eval_points, activities)
    plt.ylabel("Firing rate (Hz)")
    plt.xlabel("Input scalar, x")
    # Plot the decoded output of the ensemble
    plt.figure()
    plt.plot(sim.trange(), sim.data[filtered])
    plt.plot(sim.trange(), sim.data[cos_probe])
    plt.ylabel("Signal")
    plt.xlabel("Time")
    plt.xlim(0, 1)
    # Plot the spiking output of the ensemble
    plt.figure()
    rasterplot(sim.trange(), sim.data[spikes])
    plt.ylabel("Neuron")
    plt.xlabel("Time of Spike-Occurrence")
    plt.xlim(0, 1)
    plt.show()
Exemple #23
0
def run_nengo_realtime(nengo_sim,
                       print_runtime_stats=True,
                       save_runtime_stats=None,
                       save_probe_data=None,
                       save_tuning_curves=None):
    """Runs a nengo simulation in as close to real time as possible

    If a simulation step runs faster than real time, throttle
    the step taking until the simulation runs in close to real time

    If the simulation step runs slower than real time, let the simulation
    run as fast as possible, and report that simulation ran slower than
    real time

    Inputs
    ------
    nengo_sim: nengo.Simulator instance
        simulator to run
    print_runtime_stats: bool
        whether to print out the runtime stats after the simulation ends
    save_runtime_stats: string or None
        if not None, filename of location to save runtime stats
    save_probe_data: dict or None
        if not None, dictionary of {nengo probe object: filename to save to, ...}
    save_tuning_curves: {ensemble object: file, ...} or None
        if not None, dictionary of {nengo ensemble object: filename to save to, ...}
    """
    assert isinstance(nengo_sim, nengo.Simulator), (
        "must pass in a nengo.Simulator instance to run_nengo_realtime")
    if save_probe_data is not None:
        assert isinstance(save_probe_data, dict), (
            "save_probe_data must a dictionary mapping probe objects to filenames"
        )
    if save_tuning_curves is not None:
        assert isinstance(save_tuning_curves, dict), (
            "save_tuning_curves must a dictionary mapping ensemble objects to filenames"
        )

    dt_target = nengo_sim.dt
    dt_sim_measured = []  # measure the time each sim step takes
    real_times = []

    try:
        time0 = timeit.default_timer()
        real_time = 0
        sim_time = 0
        while True:
            sim_real_gap = sim_time - real_time
            if sim_real_gap >= MIN_SLEEP:
                time.sleep(sim_real_gap)
            sim_start = timeit.default_timer()
            nengo_sim.step()
            sim_time += dt_target
            dt_sim_measured.append(timeit.default_timer() - sim_start)
            real_time = timeit.default_timer() - time0
            real_times.append(real_time)
    except KeyboardInterrupt:
        print("\nkeyboard interrupt detected; ending simulation")

    if print_runtime_stats:
        _print_run_nengo_realtime_stats(dt_target, np.array(dt_sim_measured),
                                        save_runtime_stats)

    if save_probe_data is not None:
        sim_time = nengo_sim.trange().reshape(-1, 1)
        real_times = np.array(real_times).reshape(-1, 1)
        len_sim_time = len(sim_time)
        len_real_times = len(real_times)
        for p_obj, fname in save_probe_data.items():
            probe_data = nengo_sim.data[p_obj]
            clip_idx = np.min(
                (len_sim_time, len_real_times, probe_data.shape[0]))
            np.savetxt(fname,
                       np.hstack((sim_time[:clip_idx], real_times[:clip_idx],
                                  probe_data[:clip_idx])),
                       fmt="%.4f")

    if save_tuning_curves:
        for ens_obj, fname in save_tuning_curves.items():
            inputs = np.sort(
                np.linspace(-1, 1, 200).tolist() +
                nengo_sim.data[ens_obj].intercepts.tolist())
            inputs = inputs.reshape((-1, 1))
            tuning_data = tuning_curves(ens_obj, nengo_sim, inputs=inputs)
            np.savetxt(fname,
                       np.hstack((tuning_data[0], tuning_data[1])),
                       fmt="%.3f")
plt.plot(sim.trange(), sim.data[input_probe], lw=2)
plt.title("input Signal")
plt.xlabel("Time (s)")
plt.xlim(0, 1)
# plt.show()

intercepts, encoders = aligned(8)
with model:
    A = nengo.Ensemble(8,
                       dimensions=1,
                       intercepts=intercepts,
                       max_rates=Uniform(80, 100),
                       encoders=encoders)

with nengo.Simulator(model) as sim:
    eval_points, activities = tuning_curves(A, sim)

plt.figure()
plt.plot(eval_points, activities, lw=2)
plt.xlabel("Input Signal")
plt.ylabel("Firing rate (Hz)")
# plt.show()

with model:
    nengo.Connection(input, A)
    A_spikes = nengo.Probe(A.neurons)

with nengo.Simulator(model) as sim:
    sim.run(1)

plt.figure()
def one_b():

	#ensemble parameters
	N=100
	dimensions=1
	tau_rc=0.02
	tau_ref=0.002
	noise=0.1
	averages=5
	seed=3

	#objects and lists
	radii=np.logspace(-2,2,20)
	RMSE_list=[]
	RMSE_stddev_list=[]

	for i in range(len(radii)):

		RMSE_list_i=[]
		for a in range(averages):

			seed=3+a+i*len(radii)
			rng1=np.random.RandomState(seed=seed)
			lif_model=nengo.LIF(tau_rc=tau_rc,tau_ref=tau_ref)

			#model definition
			model = nengo.Network(label='1D LIF Ensemble',seed=seed)
			with model:
				ens_1d = nengo.Ensemble(N,dimensions,
										intercepts=Uniform(-1.0,1.0),
										max_rates=Uniform(100,200),
										radius=radii[i],
										#encoders=encoders,
										neuron_type=lif_model)

				#generate the decoders
				connection = nengo.Connection(ens_1d,ens_1d,
										solver=LstsqNoise(noise=noise))

			#create the simulator
			sim = nengo.Simulator(model)

			#retrieve evaluation points, activities, and decoders
			eval_points, activities = tuning_curves(ens_1d,sim)
			activities_noisy = activities + rng1.normal(
										scale=noise*np.max(activities),
										size=activities.shape)
			decoders = sim.data[connection].decoders.T

			#calculate the state estimate
			xhat = np.dot(activities_noisy,decoders)

			#calculate RMSE
			RMSE_list_i.append(np.sqrt(np.average((eval_points-xhat)**2)))

		RMSE_list.append(np.average(RMSE_list_i))
		RMSE_stddev_list.append(np.std(RMSE_list_i))

	#plot RMSE vs radius
	fig=plt.figure(figsize=(16,8))
	ax=fig.add_subplot(111)
	ax.plot(np.log10(radii),RMSE_list)
	ax.fill_between(np.log10(radii),
		np.subtract(RMSE_list,RMSE_stddev_list),np.add(RMSE_list,RMSE_stddev_list),
		color='lightgray')
	ax.set_xlabel('log(radius)')
	ax.set_ylabel('RMSE')
	plt.show()
Exemple #26
0
# In[ ]:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

import nengo
from nengo.utils.ensemble import tuning_curves

# In[ ]:

model = nengo.Network()
with model:
    ens_1d = nengo.Ensemble(15, dimensions=1)
with nengo.Simulator(model) as sim:
    eval_points, activities = tuning_curves(ens_1d, sim)

plt.plot(eval_points, activities)
# We could have alternatively shortened this to
# plt.plot(*tuning_curves(ens_1d, sim))
plt.ylabel("Firing rate (Hz)")
plt.xlabel("Input scalar, x")

# Each coloured line represents the response on one neuron.
# As you can see, the neurons cover the space pretty well,
# but there is no clear pattern to their responses.
#
# If there is some biological or functional reason
# to impose some pattern to their responses,
# we can do so by changing the parameters
# of the ensemble.
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)

u, s, v = np.linalg.svd(activities.transpose())
# plot first 'npc' principal components
npc = 7
S = np.zeros((u.shape[0], v.shape[0]), dtype=complex)
S[:s.shape[0], :s.shape[0]] = np.diag(s)
usi = np.linalg.pinv(np.dot(u, S))
principal_components = np.real(np.dot(usi[0:npc, :], activities.transpose()))
principal_component_functions = []
for n in range(npc):
    pc = principal_components[n]
    f = scipy.interpolate.interp1d(eval_points[:,0], pc, kind='cubic')
Exemple #28
0
def plot_tuning_curves(filename, plot_decoding=False, show=False):
    """
    Plot tuning curves for an association population and for a standard
    subpopulation (of the neural extraction network).
    """
    import matplotlib as mpl
    mpl.rcParams['font.size'] = '10'

    if show:
        mpl.use('Qt4Agg')
    else:
        mpl.use('Agg')

    import matplotlib.pyplot as plt

    plt.figure(figsize=(5, 3))

    neurons_per_item = 20
    neurons_per_dim = 50
    intercepts_low = 0.29
    intercepts_range = 0.00108

    intercepts = Uniform(intercepts_low, intercepts_low + intercepts_range)

    tau_rc = 0.034
    tau_ref = 0.0026
    radius = 1.0
    assoc_encoders = np.ones((neurons_per_item, 1))
    standard_encoders = np.ones((neurons_per_dim, 1))

    threshold = 0.3
    threshold_func = lambda x: 1 if x > threshold else 0

    max_rates = Uniform(200, 350)

    model = nengo.Network("Associative Memory")
    with model:
        neuron_type = nengo.LIF(
            tau_rc=tau_rc, tau_ref=tau_ref)

        assoc = nengo.Ensemble(
            n_neurons=neurons_per_item, dimensions=1, intercepts=intercepts,
            encoders=assoc_encoders, label="assoc", radius=radius,
            max_rates=max_rates, neuron_type=neuron_type)

        n_eval_points = 750
        eval_points = np.random.normal(0, 0.06, (n_eval_points, 1))
        eval_points.T[0].sort()
        radius = 5.0 / np.sqrt(512)
        standard = nengo.Ensemble(n_neurons=neurons_per_dim, dimensions=1,
                                  eval_points=eval_points, radius=radius,
                                  encoders=standard_encoders)

        if plot_decoding:
            dummy = nengo.Ensemble(1, 1)
            conn = nengo.Connection(assoc, dummy, function=threshold_func)
            dummy2 = nengo.Ensemble(1, 1)
            conn2 = nengo.Connection(standard, dummy2)

    sim = nengo.Simulator(model)

    if plot_decoding:
        gs = gridspec.GridSpec(3, 2)
    else:
        gs = gridspec.GridSpec(2, 2)

    plt.subplot(gs[0:2, 0])

    assoc_eval_points, assoc_activities = tuning_curves(assoc, sim)

    for neuron in assoc_activities.T:
        plt.plot(assoc_eval_points.T[0], neuron)
    plt.title("Association")
    plt.ylabel("Firing Rate (spikes/s)")
    plt.xlabel(r"$e_ix$")
    plt.ylim((0, 400))
    plt.yticks([0, 100, 200, 300, 400])

    ax = plt.subplot(gs[0:2, 1])

    # We want different eval points for display purposes than for
    # optimization purposes
    eval_points = Uniform(-radius, radius).sample(n_eval_points)
    eval_points.sort()
    eval_points = eval_points.reshape((n_eval_points, 1))

    # have to divide by radius on our own since tuning_curves skips that step
    _, activities = tuning_curves(standard, sim, eval_points/radius)
    for neuron in activities.T:
        plt.plot(eval_points, neuron)

    plt.title("Standard")
    plt.xlabel(r"$e_ix$")
    plt.xlim((-radius, radius))
    plt.ylim((0, 400))
    plt.setp(ax, yticks=[])

    if plot_decoding:
        plt.subplot(gs[2, 0])
        decoders = sim.data[conn].decoders
        plt.plot(assoc_eval_points.T[0],
                 0.001 * np.dot(assoc_activities, decoders.T))
        plt.axhline(y=1.0, ls='--')

        plt.subplot(gs[2, 1])
        x, activities2 = tuning_curves(standard, sim, assoc_eval_points/radius)
        decoders = sim.data[conn2].decoders
        plt.plot(
            assoc_eval_points.T[0],
            0.001 * np.dot(activities2, decoders.T))
        plt.plot([-1.0, 1.0], [-1.0, 1.0], c='k', ls='--')
        plt.axvline(x=radius, c='k', ls='--')
        plt.axvline(x=-radius, c='k', ls='--')

    plt.tight_layout()

    plt.subplots_adjust(right=0.89, left=0.11)

    if filename:
            plt.savefig(filename)
    if show:
            plt.show()
Exemple #29
0
DURATION = 3 * math.pi

model = nengo.Network(label="single_neuron")
with model:
  input = nengo.Node(output=np.sin)
  ensemble = nengo.Ensemble(1, dimensions=1, encoders=[[1]])
  nengo.Connection(input, ensemble)
  input_probe = nengo.Probe(input)
  spikes_probe = nengo.Probe(ensemble.neurons)
  voltage_probe = nengo.Probe(ensemble.neurons, 'voltage')
  ensemble_probe = nengo.Probe(ensemble, synapse=0.01)

if __name__ == "__main__":
  with nengo.Simulator(model) as sim:
    sim.run(DURATION)
    eval_points, activities = tuning_curves(ensemble, sim)

  plt.figure()
  plt.subplot(2, 2, 1)
  plt.plot(sim.trange(), sim.data[ensemble_probe])
  plt.plot(sim.trange(), sim.data[input_probe])
  plt.xlim(0, DURATION)

  plt.subplot(2, 2, 2)
  rasterplot(sim.trange(), sim.data[spikes_probe])
  plt.xlim(0, DURATION)

  plt.subplot(2, 2, 3)
  plt.plot(sim.trange(), sim.data[voltage_probe][:, 0], 'r')
  plt.xlim(0, DURATION)
Exemple #30
0
def plot_tuning_curves(filename, plot_decoding=False, show=False):
    """
    Plot tuning curves for an association population and for a standard
    subpopulation (of the neural extraction network).
    """
    import matplotlib as mpl
    mpl.rcParams['font.size'] = '10'

    if show:
        mpl.use('Qt4Agg')
    else:
        mpl.use('Agg')

    import matplotlib.pyplot as plt

    plt.figure(figsize=(5, 3))

    neurons_per_item = 20
    neurons_per_dim = 50
    intercepts_low = 0.29
    intercepts_range = 0.00108

    intercepts = Uniform(intercepts_low, intercepts_low + intercepts_range)

    tau_rc = 0.034
    tau_ref = 0.0026
    radius = 1.0
    assoc_encoders = np.ones((neurons_per_item, 1))
    standard_encoders = np.ones((neurons_per_dim, 1))

    threshold = 0.3
    threshold_func = lambda x: 1 if x > threshold else 0

    max_rates = Uniform(200, 350)

    model = nengo.Network("Associative Memory")
    with model:
        neuron_type = nengo.LIF(tau_rc=tau_rc, tau_ref=tau_ref)

        assoc = nengo.Ensemble(n_neurons=neurons_per_item,
                               dimensions=1,
                               intercepts=intercepts,
                               encoders=assoc_encoders,
                               label="assoc",
                               radius=radius,
                               max_rates=max_rates,
                               neuron_type=neuron_type)

        n_eval_points = 750
        eval_points = np.random.normal(0, 0.06, (n_eval_points, 1))
        eval_points.T[0].sort()
        radius = 5.0 / np.sqrt(512)
        standard = nengo.Ensemble(n_neurons=neurons_per_dim,
                                  dimensions=1,
                                  eval_points=eval_points,
                                  radius=radius,
                                  encoders=standard_encoders)

        if plot_decoding:
            dummy = nengo.Ensemble(1, 1)
            conn = nengo.Connection(assoc, dummy, function=threshold_func)
            dummy2 = nengo.Ensemble(1, 1)
            conn2 = nengo.Connection(standard, dummy2)

    sim = nengo.Simulator(model)

    if plot_decoding:
        gs = gridspec.GridSpec(3, 2)
    else:
        gs = gridspec.GridSpec(2, 2)

    plt.subplot(gs[0:2, 0])

    assoc_eval_points, assoc_activities = tuning_curves(assoc, sim)

    for neuron in assoc_activities.T:
        plt.plot(assoc_eval_points.T[0], neuron)
    plt.title("Association")
    plt.ylabel("Firing Rate (spikes/s)")
    plt.xlabel(r"$e_ix$")
    plt.ylim((0, 400))
    plt.yticks([0, 100, 200, 300, 400])

    ax = plt.subplot(gs[0:2, 1])

    # We want different eval points for display purposes than for
    # optimization purposes
    eval_points = Uniform(-radius, radius).sample(n_eval_points)
    eval_points.sort()
    eval_points = eval_points.reshape((n_eval_points, 1))

    # have to divide by radius on our own since tuning_curves skips that step
    _, activities = tuning_curves(standard, sim, eval_points / radius)
    for neuron in activities.T:
        plt.plot(eval_points, neuron)

    plt.title("Standard")
    plt.xlabel(r"$e_ix$")
    plt.xlim((-radius, radius))
    plt.ylim((0, 400))
    plt.setp(ax, yticks=[])

    if plot_decoding:
        plt.subplot(gs[2, 0])
        decoders = sim.data[conn].decoders
        plt.plot(assoc_eval_points.T[0],
                 0.001 * np.dot(assoc_activities, decoders.T))
        plt.axhline(y=1.0, ls='--')

        plt.subplot(gs[2, 1])
        x, activities2 = tuning_curves(standard, sim,
                                       assoc_eval_points / radius)
        decoders = sim.data[conn2].decoders
        plt.plot(assoc_eval_points.T[0],
                 0.001 * np.dot(activities2, decoders.T))
        plt.plot([-1.0, 1.0], [-1.0, 1.0], c='k', ls='--')
        plt.axvline(x=radius, c='k', ls='--')
        plt.axvline(x=-radius, c='k', ls='--')

    plt.tight_layout()

    plt.subplots_adjust(right=0.89, left=0.11)

    if filename:
        plt.savefig(filename)
    if show:
        plt.show()
Exemple #31
0
dt = 0.0001  # use finer resolution cz of high bursting

with nengo.Network() as model:
    # parameters tuned for EBN/IBN
    neuron = nengo.Izhikevich(reset_voltage=-70, reset_recovery=2,
                              tau_recovery=0.005)

    ens = nengo.Ensemble(n_neurons=1, dimensions=1, encoders=[[1]],
                         intercepts=[-0.9], max_rates=[150],
                         neuron_type=neuron)
    # ens = nengo.Ensemble(n_neurons=1, dimensions=1, encoders=[[1]],
    # intercepts=[-0.9])
    node = nengo.Node(output=lambda t: 1 if t < dur else -1)
    nengo.Connection(node, ens)
    sp = nengo.Probe(ens.neurons, attr='spikes')
    cp = nengo.Probe(ens, attr='input')

s = nengo.Simulator(model, dt=dt)
s.run(0.1)

plt.figure()
plt.subplot(221)
plt.plot(s.trange(), s.data[sp])
plt.subplot(222)
plt.plot(s.trange(), rates_isi(s.trange(), s.data[sp]))
plt.subplot(223)
plt.plot(s.trange(), s.data[cp])
plt.subplot(224)
plt.plot(*tuning_curves(ens, s))
plt.show()
def one_c():

	#ensemble parameters
	N=100
	dimensions=1
	tau_rc=0.02
	tau_ref=0.002
	noise=0.1
	averages=10
	seed=3

	#objects and lists
	tau_refs=np.logspace(-6,-2.333,10)
	# tau_refs=np.linspace(0.0002,0.0049,10)
	RMSE_list=[]
	RMSE_stddev_list=[]
	eval_points_list=[]
	activities_list=[]

	for i in range(len(tau_refs)):

		RMSE_list_i=[]
		for a in range(averages):

			seed=3+a+i*len(tau_refs) #unique seed for each iteration
			rng1=np.random.RandomState(seed=seed)
			lif_model=nengo.LIF(tau_rc=tau_rc,tau_ref=tau_refs[i])

			#model definition
			model = nengo.Network(label='1D LIF Ensemble',seed=seed)
			with model:
				ens_1d = nengo.Ensemble(N,dimensions,
										intercepts=Uniform(-1.0,1.0),
										max_rates=Uniform(100,200),
										neuron_type=lif_model)

				#generate the decoders
				connection = nengo.Connection(ens_1d,ens_1d,
										solver=LstsqNoise(noise=noise))

			#create the simulator
			sim = nengo.Simulator(model)

			#retrieve evaluation points, activities, and decoders
			eval_points, activities = tuning_curves(ens_1d,sim)
			eval_points_list.append(eval_points)
			activities_list.append(activities)
			activities_noisy = activities + rng1.normal(
										scale=noise*np.max(activities),
										size=activities.shape)
			decoders = sim.data[connection].decoders.T

			#calculate the state estimate
			xhat = np.dot(activities_noisy,decoders)

			#calculate RMSE
			RMSE_list_i.append(np.sqrt(np.average((eval_points-xhat)**2)))

		RMSE_list.append(np.average(RMSE_list_i))
		RMSE_stddev_list.append(np.std(RMSE_list_i))

	#plot RMSE vs radius
	fig=plt.figure(figsize=(16,8))
	ax=fig.add_subplot(111)
	ax.plot(np.log10(tau_refs),RMSE_list)
	ax.fill_between(np.log10(tau_refs),
		np.subtract(RMSE_list,RMSE_stddev_list),np.add(RMSE_list,RMSE_stddev_list),
		color='lightgray')
	ax.set_xlabel('log($\\tau_{ref}$)')
	ax.set_ylabel('RMSE')
	plt.show()

	# plot tuning curves
	fig=plt.figure(figsize=(16,8))
	ax=fig.add_subplot(211)
	ax.plot(eval_points_list[0],activities_list[0])
	ax.set_title('$\\tau_{ref}=%f$' %tau_refs[0])
	# ax.set_xlabel('$x$')
	ax.set_ylabel('Firing Rate $a$ (Hz)')
	ax=fig.add_subplot(212)
	ax.plot(eval_points_list[-1],activities_list[-1])
	ax.set_title('$\\tau_{ref}=%f$' %tau_refs[-1])
	ax.set_xlabel('$x$')
	ax.set_ylabel('Firing Rate $a$ (Hz)')
	plt.show()