Skip to content

Commit 7dc7092

Browse files
committed
test split and combined
1 parent 5406aad commit 7dc7092

5 files changed

Lines changed: 422 additions & 306 deletions

File tree

examples/extra_models_examples/IF_curr_exp_ca2_adaptive.py

Lines changed: 107 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,110 @@
2525
import matplotlib.pyplot as pylab
2626
import pyNN.spiNNaker as sim
2727

28-
# Timestep (ms)
29-
# **NOTE** the 2.5Khz input frequency is not going to work particularly well
30-
# at 1ms
31-
dt = 0.1
32-
33-
# Number of neurons - used to gather enough ISIs to get a smooth estimate
34-
N = 300
35-
36-
# Time (ms) to simulate for
37-
T = 250
38-
39-
# Setup simulator
40-
sim.setup(timestep=dt, min_delay=1.0)
41-
42-
# Create population of neurons
43-
cell = sim.Population(N, sim.extra_models.IFCurrExpCa2Adaptive(**{
44-
"tau_m": 20.0, "cm": 0.5,
45-
"v_rest": -70.0,
46-
"v_reset": -60.0,
47-
"v_thresh": -54.0,
48-
"i_alpha": 0.1,
49-
"tau_ca2": 50.0}))
50-
51-
# Create poisson spike source
52-
spike_source = sim.Population(N, sim.SpikeSourcePoisson(rate=2500.0))
53-
54-
sim.Projection(spike_source, cell,
55-
sim.OneToOneConnector(),
56-
sim.StaticSynapse(weight=0.1, delay=0.1),
57-
receptor_type="excitatory")
58-
59-
cell.record('spikes')
60-
# cell.record_gsyn()
61-
62-
sim.run(T)
63-
64-
spike_times = cell.spinnaker_get_data('spikes')
65-
# ca2 = cell.get_gsyn(compatible_output=True)
66-
67-
# Split into list of spike times for each neuron
68-
neuron_spikes = [spike_times[spike_times[:, 0] == n, 1] for n in range(N)]
69-
70-
# Calculate interspike intervals
71-
# and pair these with bin index of last spike time in pair
72-
# **NOTE** using first spike time in pair leads to a dip at the
73-
# end as the end of long pairs goes off the end of the simulation
74-
binned_isis = numpy.hstack([
75-
numpy.vstack(
76-
(t[1:] - t[:-1],
77-
numpy.digitize(t[1:], numpy.arange(T)) - 1))
78-
for t in neuron_spikes])
79-
80-
# Split interspike intervals into separate array for each time bin
81-
time_binned_isis = [binned_isis[0, binned_isis[1] == t] for t in range(T)]
82-
83-
# Create a dictionary of non-empty time bins to mean interspike interval
84-
mean_isis = {t: numpy.average(i)
85-
for (t, i) in enumerate(time_binned_isis) if len(i) > 0}
86-
87-
# Calculate the coefficient of variance for each time bin
88-
isi_cv = {t: math.sqrt(
89-
numpy.sum(numpy.power(time_binned_isis[t] - mean_isi, 2)) /
90-
float(len(time_binned_isis[t]))) / mean_isi
91-
for (t, mean_isi) in mean_isis.items()}
92-
93-
# Take average CA2 level across all neurons
94-
# average_ca2 = numpy.average(numpy.reshape(ca2[:,2], (N, int(T / dt))),
95-
# axis=0)
96-
97-
# Plot
98-
fig, axes = pylab.subplots(2, sharex=True)
99-
100-
axes[0].scatter(mean_isis.keys(),
101-
[1000.0 / i for i in mean_isis.values()], s=2)
102-
axes[0].set_ylabel("Firing rate/Hz")
103-
104-
# axes[1].scatter(numpy.arange(0.0, T, dt), average_ca2, s=2)
105-
# axes[1].set_ylabel("CA2/mA")
106-
# axes[1].set_ylim((0.0, numpy.amax(average_ca2) * 1.25))
107-
108-
axes[1].scatter(isi_cv.keys(), isi_cv.values(), s=2)
109-
axes[1].set_ylabel("Coefficient of ISI variance")
110-
111-
axes[1].set_xlim((0.0, T))
112-
axes[1].set_xlabel("Time/ms")
113-
114-
sim.end()
115-
pylab.show()
28+
def run_script(*, split: bool = False) -> None:
29+
"""
30+
Runs the example script
31+
32+
:param split: If True will split the Populations that receive data
33+
into synapse and neuron cores.
34+
This requires more cores but allows more spikes to be received.
35+
"""
36+
# Timestep (ms)
37+
# **NOTE** the 2.5Khz input frequency is not going to work particularly well
38+
# at 1ms
39+
dt = 0.1
40+
41+
# Number of neurons - used to gather enough ISIs to get a smooth estimate
42+
N = 300
43+
44+
# Time (ms) to simulate for
45+
T = 250
46+
47+
# Setup simulator
48+
sim.setup(timestep=dt, min_delay=1.0)
49+
50+
if split:
51+
sim.extra_models.IFCurrExpCa2Adaptive.set_model_n_synapse_cores(1)
52+
53+
# Create population of neurons
54+
cell = sim.Population(N, sim.extra_models.IFCurrExpCa2Adaptive(**{
55+
"tau_m": 20.0, "cm": 0.5,
56+
"v_rest": -70.0,
57+
"v_reset": -60.0,
58+
"v_thresh": -54.0,
59+
"i_alpha": 0.1,
60+
"tau_ca2": 50.0}))
61+
62+
# Create poisson spike source
63+
spike_source = sim.Population(N, sim.SpikeSourcePoisson(rate=2500.0))
64+
65+
sim.Projection(spike_source, cell,
66+
sim.OneToOneConnector(),
67+
sim.StaticSynapse(weight=0.1, delay=0.1),
68+
receptor_type="excitatory")
69+
70+
cell.record('spikes')
71+
# cell.record_gsyn()
72+
73+
sim.run(T)
74+
75+
spike_times = cell.spinnaker_get_data('spikes')
76+
# ca2 = cell.get_gsyn(compatible_output=True)
77+
78+
# Split into list of spike times for each neuron
79+
neuron_spikes = [spike_times[spike_times[:, 0] == n, 1] for n in range(N)]
80+
81+
# Calculate interspike intervals
82+
# and pair these with bin index of last spike time in pair
83+
# **NOTE** using first spike time in pair leads to a dip at the
84+
# end as the end of long pairs goes off the end of the simulation
85+
binned_isis = numpy.hstack([
86+
numpy.vstack(
87+
(t[1:] - t[:-1],
88+
numpy.digitize(t[1:], numpy.arange(T)) - 1))
89+
for t in neuron_spikes])
90+
91+
# Split interspike intervals into separate array for each time bin
92+
time_binned_isis = [binned_isis[0, binned_isis[1] == t] for t in range(T)]
93+
94+
# Create a dictionary of non-empty time bins to mean interspike interval
95+
mean_isis = {t: numpy.average(i)
96+
for (t, i) in enumerate(time_binned_isis) if len(i) > 0}
97+
98+
# Calculate the coefficient of variance for each time bin
99+
isi_cv = {t: math.sqrt(
100+
numpy.sum(numpy.power(time_binned_isis[t] - mean_isi, 2)) /
101+
float(len(time_binned_isis[t]))) / mean_isi
102+
for (t, mean_isi) in mean_isis.items()}
103+
104+
# Take average CA2 level across all neurons
105+
# average_ca2 = numpy.average(numpy.reshape(ca2[:,2], (N, int(T / dt))),
106+
# axis=0)
107+
108+
# Plot
109+
fig, axes = pylab.subplots(2, sharex=True)
110+
111+
axes[0].scatter(mean_isis.keys(),
112+
[1000.0 / i for i in mean_isis.values()], s=2)
113+
axes[0].set_ylabel("Firing rate/Hz")
114+
115+
# axes[1].scatter(numpy.arange(0.0, T, dt), average_ca2, s=2)
116+
# axes[1].set_ylabel("CA2/mA")
117+
# axes[1].set_ylim((0.0, numpy.amax(average_ca2) * 1.25))
118+
119+
axes[1].scatter(isi_cv.keys(), isi_cv.values(), s=2)
120+
axes[1].set_ylabel("Coefficient of ISI variance")
121+
122+
axes[1].set_xlim((0.0, T))
123+
axes[1].set_xlabel("Time/ms")
124+
125+
sim.end()
126+
pylab.show()
127+
128+
129+
# combined binaries [IF_curr_exp_ca2_adaptive.aplx]
130+
# split binaries [IF_curr_exp_ca2_adaptive_neuron.aplx]
131+
132+
133+
if __name__ == "__main__":
134+
run_script()

examples/extra_models_examples/IF_curr_exp_sEMD.py

Lines changed: 105 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -23,84 +23,108 @@
2323
import matplotlib.pyplot as plt
2424
from pyNN.utility.plotting import Figure, Panel
2525

26-
# variables
27-
weights = 1
28-
spike_time_facilitation = 4
29-
spike_time_trigger = 20
30-
31-
# set up simulation
32-
simulation_timestep = 1 # ms
33-
simulation_runtime = 100 # ms
34-
p.setup(timestep=simulation_timestep)
35-
36-
# neuron parameters
37-
cell_params_semd = {'cm': 0.25,
38-
'i_offset': 0, # offset current
39-
'tau_m': 10, # membrane potential time constant
40-
'tau_refrac': 1, # refractory period time constant
41-
'tau_syn_E': 20, # excitatory current time constant
42-
'tau_syn_I': 20, # inhibitory current time constant
43-
'v_reset': -85, # reset potential
44-
'v_rest': -60, # resting potential
45-
'v_thresh': -50, # spiking threshold
46-
'scaling_factor': 100.0 # scaling factor for 2nd response
47-
}
48-
49-
# neuron populations
50-
# (population size, neuron type, cell parameters, label)
51-
sEMD = p.Population(1, p.extra_models.IF_curr_exp_sEMD,
52-
cell_params_semd, label="sEMD")
53-
input_facilitation = p.Population(1, p.SpikeSourceArray,
54-
{'spike_times': [[spike_time_facilitation]]},
55-
label="input_facilitation")
56-
input_trigger = p.Population(1, p.SpikeSourceArray,
57-
{'spike_times': [[spike_time_trigger]]},
58-
label="input_trigger")
59-
60-
sEMD.initialize(v=-60.0)
61-
62-
# projections
63-
p.Projection(input_facilitation, sEMD, p.OneToOneConnector(),
64-
p.StaticSynapse(weight=weights, delay=1),
65-
receptor_type='excitatory')
66-
p.Projection(input_trigger, sEMD, p.OneToOneConnector(),
67-
p.StaticSynapse(weight=weights, delay=1),
68-
receptor_type='excitatory2')
69-
70-
# records
71-
sEMD.record(['spikes', 'v', 'gsyn_exc', 'gsyn_inh'])
72-
73-
# run simulation
74-
p.run(simulation_runtime)
75-
76-
# receive data from neurons
77-
spikes = sEMD.get_data(['spikes'])
78-
v = sEMD.get_data(['v'])
79-
current_exc = sEMD.get_data(['gsyn_exc'])
80-
current_inh = sEMD.get_data(['gsyn_inh'])
81-
82-
# plots
83-
Figure(
84-
# raster plot of the neuron spike times
85-
Panel(spikes.segments[0].spiketrains,
86-
yticks=True, markersize=4, xlim=(0, simulation_runtime)),
87-
# membrane potential
88-
Panel(v.segments[0].filter(name='v')[0],
89-
ylabel="Membrane potential (mV)",
90-
data_labels=[sEMD.label], yticks=True, xlim=(0, simulation_runtime)),
91-
# excitatory current
92-
Panel(current_exc.segments[0].filter(name='gsyn_exc')[0],
93-
ylabel="gsyn excitatory (mV)",
94-
data_labels=[sEMD.label], yticks=True, xlim=(0, simulation_runtime)),
95-
# inhibitory current
96-
Panel(current_inh.segments[0].filter(name='gsyn_inh')[0],
97-
xlabel="Time (ms)", xticks=True,
98-
ylabel="gsyn inhibitory (mV)",
99-
data_labels=[sEMD.label], yticks=True, xlim=(0, simulation_runtime)),
100-
title="SEMD example",
101-
annotations=f"Simulated with {p.name()}"
102-
)
103-
plt.show()
104-
105-
# end
106-
p.end()
26+
def run_script(*, split: bool = False) -> None:
27+
"""
28+
Runs the example script
29+
30+
:param split: If True will split the Populations that receive data
31+
into synapse and neuron cores.
32+
This requires more cores but allows more spikes to be received.
33+
"""
34+
# variables
35+
weights = 1
36+
spike_time_facilitation = 4
37+
spike_time_trigger = 20
38+
39+
# set up simulation
40+
simulation_timestep = 1 # ms
41+
simulation_runtime = 100 # ms
42+
p.setup(timestep=simulation_timestep)
43+
44+
if split:
45+
p.extra_models.IF_curr_exp_sEMD.set_model_n_synapse_cores(1)
46+
47+
# neuron parameters
48+
cell_params_semd = {
49+
'cm': 0.25,
50+
'i_offset': 0, # offset current
51+
'tau_m': 10, # membrane potential time constant
52+
'tau_refrac': 1, # refractory period time constant
53+
'tau_syn_E': 20, # excitatory current time constant
54+
'tau_syn_I': 20, # inhibitory current time constant
55+
'v_reset': -85, # reset potential
56+
'v_rest': -60, # resting potential
57+
'v_thresh': -50, # spiking threshold
58+
'scaling_factor': 100.0 # scaling factor for 2nd response
59+
}
60+
61+
# neuron populations
62+
# (population size, neuron type, cell parameters, label)
63+
sEMD = p.Population(1, p.extra_models.IF_curr_exp_sEMD,
64+
cell_params_semd, label="sEMD")
65+
input_facilitation = p.Population(
66+
1, p.SpikeSourceArray, {'spike_times': [[spike_time_facilitation]]},
67+
label="input_facilitation")
68+
input_trigger = p.Population(
69+
1, p.SpikeSourceArray, {'spike_times': [[spike_time_trigger]]},
70+
label="input_trigger")
71+
72+
sEMD.initialize(v=-60.0)
73+
74+
# projections
75+
p.Projection(input_facilitation, sEMD, p.OneToOneConnector(),
76+
p.StaticSynapse(weight=weights, delay=1),
77+
receptor_type='excitatory')
78+
p.Projection(input_trigger, sEMD, p.OneToOneConnector(),
79+
p.StaticSynapse(weight=weights, delay=1),
80+
receptor_type='excitatory2')
81+
82+
# records
83+
sEMD.record(['spikes', 'v', 'gsyn_exc', 'gsyn_inh'])
84+
85+
# run simulation
86+
p.run(simulation_runtime)
87+
88+
# receive data from neurons
89+
spikes = sEMD.get_data(['spikes'])
90+
v = sEMD.get_data(['v'])
91+
current_exc = sEMD.get_data(['gsyn_exc'])
92+
current_inh = sEMD.get_data(['gsyn_inh'])
93+
94+
# plots
95+
Figure(
96+
# raster plot of the neuron spike times
97+
Panel(spikes.segments[0].spiketrains,
98+
yticks=True, markersize=4, xlim=(0, simulation_runtime)),
99+
# membrane potential
100+
Panel(v.segments[0].filter(name='v')[0],
101+
ylabel="Membrane potential (mV)",
102+
data_labels=[sEMD.label],
103+
yticks=True, xlim=(0, simulation_runtime)),
104+
# excitatory current
105+
Panel(current_exc.segments[0].filter(name='gsyn_exc')[0],
106+
ylabel="gsyn excitatory (mV)",
107+
data_labels=[sEMD.label],
108+
yticks=True, xlim=(0, simulation_runtime)),
109+
# inhibitory current
110+
Panel(current_inh.segments[0].filter(name='gsyn_inh')[0],
111+
xlabel="Time (ms)", xticks=True,
112+
ylabel="gsyn inhibitory (mV)",
113+
data_labels=[sEMD.label],
114+
yticks=True, xlim=(0, simulation_runtime)),
115+
title="SEMD example",
116+
annotations=f"Simulated with {p.name()}"
117+
)
118+
plt.show()
119+
120+
# end
121+
p.end()
122+
123+
124+
# combined binaries [IF_curr_exp_sEMD.aplx]
125+
# split binaries [IF_curr_exp_sEMD_neuron.aplx]
126+
127+
128+
if __name__ == "__main__":
129+
run_script()
130+

0 commit comments

Comments
 (0)