Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
189 changes: 104 additions & 85 deletions examples/extra_models_examples/IF_cond_exp_stoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,88 +33,107 @@
from pyNN.utility.plotting import Figure, Panel
import matplotlib.pyplot as plt

sim.setup(timestep=1.0, min_delay=1.0)

stoc_cell = sim.Population(1, sim.extra_models.IFCondExpStoc(**{
'i_offset': 0.1,
'tau_refrac': 3.0,
'v_thresh': -51.0,
'v_reset': -70.0,
'tau_syn_E': 5.0,
'tau_syn_I': 5.0}))

exp_cell = sim.Population(1, sim.IF_cond_exp(**{
'i_offset': 0.1,
'tau_refrac': 3.0,
'v_thresh': -51.0,
'v_reset': -70.0,
'tau_syn_E': 5.0,
'tau_syn_I': 5.0}))


spike_sourceE = sim.Population(1, sim.SpikeSourceArray(**{
'spike_times': [float(i) for i in range(5, 105, 10)]}))
spike_sourceI = sim.Population(1, sim.SpikeSourceArray(**{
'spike_times': [float(i) for i in range(155, 255, 10)]}))

sim.Projection(spike_sourceE, exp_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=0.15, delay=2.0),
receptor_type='excitatory')
sim.Projection(spike_sourceI, exp_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=-0.15, delay=4.0),
receptor_type='inhibitory')
sim.Projection(spike_sourceE, stoc_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=0.15, delay=2.0),
receptor_type='excitatory')
sim.Projection(spike_sourceI, stoc_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=-0.15, delay=4.0),
receptor_type='inhibitory')

stoc_cell.record('all')
exp_cell.record('all')

runtime = 200.0

sim.run(runtime)

stoc_data = stoc_cell.get_data()
exp_data = exp_cell.get_data()

# Plot
Figure(
# raster plot of the presynaptic neuron spike times
Panel(stoc_data.segments[0].spiketrains,
yticks=True, markersize=0.2, xlim=(0, runtime)),
Panel(exp_data.segments[0].spiketrains,
yticks=True, markersize=0.2, xlim=(0, runtime)),
# membrane potential of the postsynaptic neuron
Panel(stoc_data.segments[0].filter(name='v')[0],
ylabel="Membrane potential (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
Panel(stoc_data.segments[0].filter(name='gsyn_exc')[0],
ylabel="gsyn excitatory (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
Panel(stoc_data.segments[0].filter(name='gsyn_inh')[0],
ylabel="gsyn inhibitory (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
# membrane potential of the postsynaptic neuron
Panel(exp_data.segments[0].filter(name='v')[0],
ylabel="Membrane potential (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
Panel(exp_data.segments[0].filter(name='gsyn_exc')[0],
ylabel="gsyn excitatory (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
Panel(exp_data.segments[0].filter(name='gsyn_inh')[0],
ylabel="gsyn inhibitory (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
title="IF_cond_exp_stoc example",
annotations=f"Simulated with {sim.name()}"
)
plt.show()

sim.end()
pylab.show()

def run_script(*, split: bool = False) -> None:
"""
Runs the example script

:param split: If True will split the Populations that receive data
into synapse and neuron cores.
This requires more cores but allows more spikes to be received.
"""
sim.setup(timestep=1.0, min_delay=1.0)

if split:
sim.extra_models.IFCondExpStoc.set_model_n_synapse_cores(1)
sim.IF_cond_exp.set_model_n_synapse_cores(1)

stoc_cell = sim.Population(1, sim.extra_models.IFCondExpStoc(**{
'i_offset': 0.1,
'tau_refrac': 3.0,
'v_thresh': -51.0,
'v_reset': -70.0,
'tau_syn_E': 5.0,
'tau_syn_I': 5.0}))

exp_cell = sim.Population(1, sim.IF_cond_exp(**{
'i_offset': 0.1,
'tau_refrac': 3.0,
'v_thresh': -51.0,
'v_reset': -70.0,
'tau_syn_E': 5.0,
'tau_syn_I': 5.0}))

spike_sourceE = sim.Population(1, sim.SpikeSourceArray(**{
'spike_times': [float(i) for i in range(5, 105, 10)]}))
spike_sourceI = sim.Population(1, sim.SpikeSourceArray(**{
'spike_times': [float(i) for i in range(155, 255, 10)]}))

sim.Projection(spike_sourceE, exp_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=0.15, delay=2.0),
receptor_type='excitatory')
sim.Projection(spike_sourceI, exp_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=-0.15, delay=4.0),
receptor_type='inhibitory')
sim.Projection(spike_sourceE, stoc_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=0.15, delay=2.0),
receptor_type='excitatory')
sim.Projection(spike_sourceI, stoc_cell,
sim.OneToOneConnector(),
synapse_type=sim.StaticSynapse(weight=-0.15, delay=4.0),
receptor_type='inhibitory')

stoc_cell.record('all')
exp_cell.record('all')

runtime = 200.0

sim.run(runtime)

stoc_data = stoc_cell.get_data()
exp_data = exp_cell.get_data()

# Plot
Figure(
# raster plot of the presynaptic neuron spike times
Panel(stoc_data.segments[0].spiketrains,
yticks=True, markersize=0.2, xlim=(0, runtime)),
Panel(exp_data.segments[0].spiketrains,
yticks=True, markersize=0.2, xlim=(0, runtime)),
# membrane potential of the postsynaptic neuron
Panel(stoc_data.segments[0].filter(name='v')[0],
ylabel="Membrane potential (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
Panel(stoc_data.segments[0].filter(name='gsyn_exc')[0],
ylabel="gsyn excitatory (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
Panel(stoc_data.segments[0].filter(name='gsyn_inh')[0],
ylabel="gsyn inhibitory (mV)",
data_labels=[stoc_cell.label], yticks=True, xlim=(0, runtime)),
# membrane potential of the postsynaptic neuron
Panel(exp_data.segments[0].filter(name='v')[0],
ylabel="Membrane potential (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
Panel(exp_data.segments[0].filter(name='gsyn_exc')[0],
ylabel="gsyn excitatory (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
Panel(exp_data.segments[0].filter(name='gsyn_inh')[0],
ylabel="gsyn inhibitory (mV)",
data_labels=[exp_cell.label], yticks=True, xlim=(0, runtime)),
title="IF_cond_exp_stoc example",
annotations=f"Simulated with {sim.name()}"
)
plt.show()

sim.end()
pylab.show()

# combined binaries [IF_cond_exp_stoc.aplx, IF_cond_exp.aplx]
# split binaries [IF_cond_exp_stoc_neuron.aplx, IF_cond_exp_neuron.aplx]


if __name__ == "__main__":
run_script()
196 changes: 108 additions & 88 deletions examples/extra_models_examples/IF_curr_exp_ca2_adaptive.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,111 @@
import matplotlib.pyplot as pylab
import pyNN.spiNNaker as sim

# Timestep (ms)
# **NOTE** the 2.5Khz input frequency is not going to work particularly well
# at 1ms
dt = 0.1

# Number of neurons - used to gather enough ISIs to get a smooth estimate
N = 300

# Time (ms) to simulate for
T = 250

# Setup simulator
sim.setup(timestep=dt, min_delay=1.0)

# Create population of neurons
cell = sim.Population(N, sim.extra_models.IFCurrExpCa2Adaptive(**{
"tau_m": 20.0, "cm": 0.5,
"v_rest": -70.0,
"v_reset": -60.0,
"v_thresh": -54.0,
"i_alpha": 0.1,
"tau_ca2": 50.0}))

# Create poisson spike source
spike_source = sim.Population(N, sim.SpikeSourcePoisson(rate=2500.0))

sim.Projection(spike_source, cell,
sim.OneToOneConnector(),
sim.StaticSynapse(weight=0.1, delay=0.1),
receptor_type="excitatory")

cell.record('spikes')
# cell.record_gsyn()

sim.run(T)

spike_times = cell.spinnaker_get_data('spikes')
# ca2 = cell.get_gsyn(compatible_output=True)

# Split into list of spike times for each neuron
neuron_spikes = [spike_times[spike_times[:, 0] == n, 1] for n in range(N)]

# Calculate interspike intervals
# and pair these with bin index of last spike time in pair
# **NOTE** using first spike time in pair leads to a dip at the
# end as the end of long pairs goes off the end of the simulation
binned_isis = numpy.hstack([
numpy.vstack(
(t[1:] - t[:-1],
numpy.digitize(t[1:], numpy.arange(T)) - 1))
for t in neuron_spikes])

# Split interspike intervals into separate array for each time bin
time_binned_isis = [binned_isis[0, binned_isis[1] == t] for t in range(T)]

# Create a dictionary of non-empty time bins to mean interspike interval
mean_isis = {t: numpy.average(i)
for (t, i) in enumerate(time_binned_isis) if len(i) > 0}

# Calculate the coefficient of variance for each time bin
isi_cv = {t: math.sqrt(
numpy.sum(numpy.power(time_binned_isis[t] - mean_isi, 2)) /
float(len(time_binned_isis[t]))) / mean_isi
for (t, mean_isi) in mean_isis.items()}

# Take average CA2 level across all neurons
# average_ca2 = numpy.average(numpy.reshape(ca2[:,2], (N, int(T / dt))),
# axis=0)

# Plot
fig, axes = pylab.subplots(2, sharex=True)

axes[0].scatter(mean_isis.keys(),
[1000.0 / i for i in mean_isis.values()], s=2)
axes[0].set_ylabel("Firing rate/Hz")

# axes[1].scatter(numpy.arange(0.0, T, dt), average_ca2, s=2)
# axes[1].set_ylabel("CA2/mA")
# axes[1].set_ylim((0.0, numpy.amax(average_ca2) * 1.25))

axes[1].scatter(isi_cv.keys(), isi_cv.values(), s=2)
axes[1].set_ylabel("Coefficient of ISI variance")

axes[1].set_xlim((0.0, T))
axes[1].set_xlabel("Time/ms")

sim.end()
pylab.show()

def run_script(*, split: bool = False) -> None:
"""
Runs the example script

:param split: If True will split the Populations that receive data
into synapse and neuron cores.
This requires more cores but allows more spikes to be received.
"""
# Timestep (ms)
# **NOTE**
# the 2.5Khz input frequency is not going to work particularly well at 1ms
dt = 0.1

# Number of neurons - used to gather enough ISIs to get a smooth estimate
N = 300

# Time (ms) to simulate for
T = 250

# Setup simulator
sim.setup(timestep=dt, min_delay=1.0)

if split:
sim.extra_models.IFCurrExpCa2Adaptive.set_model_n_synapse_cores(1)

# Create population of neurons
cell = sim.Population(N, sim.extra_models.IFCurrExpCa2Adaptive(**{
"tau_m": 20.0, "cm": 0.5,
"v_rest": -70.0,
"v_reset": -60.0,
"v_thresh": -54.0,
"i_alpha": 0.1,
"tau_ca2": 50.0}))

# Create poisson spike source
spike_source = sim.Population(N, sim.SpikeSourcePoisson(rate=2500.0))

sim.Projection(spike_source, cell,
sim.OneToOneConnector(),
sim.StaticSynapse(weight=0.1, delay=0.1),
receptor_type="excitatory")

cell.record('spikes')
# cell.record_gsyn()

sim.run(T)

spike_times = cell.spinnaker_get_data('spikes')
# ca2 = cell.get_gsyn(compatible_output=True)

# Split into list of spike times for each neuron
neuron_spikes = [spike_times[spike_times[:, 0] == n, 1] for n in range(N)]

# Calculate interspike intervals
# and pair these with bin index of last spike time in pair
# **NOTE** using first spike time in pair leads to a dip at the
# end as the end of long pairs goes off the end of the simulation
binned_isis = numpy.hstack([
numpy.vstack(
(t[1:] - t[:-1],
numpy.digitize(t[1:], numpy.arange(T)) - 1))
for t in neuron_spikes])

# Split interspike intervals into separate array for each time bin
time_binned_isis = [binned_isis[0, binned_isis[1] == t] for t in range(T)]

# Create a dictionary of non-empty time bins to mean interspike interval
mean_isis = {t: numpy.average(i)
for (t, i) in enumerate(time_binned_isis) if len(i) > 0}

# Calculate the coefficient of variance for each time bin
isi_cv = {t: math.sqrt(
numpy.sum(numpy.power(time_binned_isis[t] - mean_isi, 2)) /
float(len(time_binned_isis[t]))) / mean_isi
for (t, mean_isi) in mean_isis.items()}

# Take average CA2 level across all neurons
# average_ca2 = numpy.average(numpy.reshape(ca2[:,2], (N, int(T / dt))),
# axis=0)

# Plot
_, axes = pylab.subplots(2, sharex=True)

axes[0].scatter(mean_isis.keys(),
[1000.0 / i for i in mean_isis.values()], s=2)
axes[0].set_ylabel("Firing rate/Hz")

# axes[1].scatter(numpy.arange(0.0, T, dt), average_ca2, s=2)
# axes[1].set_ylabel("CA2/mA")
# axes[1].set_ylim((0.0, numpy.amax(average_ca2) * 1.25))

axes[1].scatter(isi_cv.keys(), isi_cv.values(), s=2)
axes[1].set_ylabel("Coefficient of ISI variance")

axes[1].set_xlim((0.0, T))
axes[1].set_xlabel("Time/ms")

sim.end()
pylab.show()


# combined binaries [IF_curr_exp_ca2_adaptive.aplx]
# split binaries [IF_curr_exp_ca2_adaptive_neuron.aplx]


if __name__ == "__main__":
run_script()
Loading
Loading