From 6d07e560ace5d8723ca5b3eeaf7a8f2dbbbbb32a Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Thu, 6 Jun 2024 17:49:57 -0400 Subject: [PATCH 1/8] add single neuron NEST example --- examples/one_neuron_example.py | 44 ++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/one_neuron_example.py diff --git a/examples/one_neuron_example.py b/examples/one_neuron_example.py new file mode 100644 index 00000000..db4fd8ec --- /dev/null +++ b/examples/one_neuron_example.py @@ -0,0 +1,44 @@ +""" +A very simple example of simulating One Neuron +using NEST simulator backend with the help of PyNN. + +""" +import pyNN.nest as sim +from pyNN.utility.plotting import Figure, Panel + +sim.setup(timestep=0.1) # (ms) + +cell_params = { + "v_rest": -70.0, # (mV) + "v_reset": -70.0, # (mV) + "cm": 0.250, # (nF) + "tau_m": 10, # (ms) + "tau_refrac": 2, # (ms) + "tau_syn_E": 2, # (ms) + "tau_syn_I": 2, # (ms) + "v_thresh": -55.0, # (mV) + "i_offset": 0.376, # (nA) +} + + +cell_type = sim.IF_curr_alpha(**cell_params) +neuron = sim.Population(1, cell_type, label="Neuron 1") +neuron.record("v") + +sim.run(1000.0) + +data_v = neuron.get_data().segments[0].filter(name="v")[0] + +Figure( + Panel( + data_v[:,0], + xticks=True, + yticks=True, + xlabel="Time (in ms)", + ylabel="Membrane Potential (mV)" + ), + title="One Neuron", + annotations="Translating Single Neuron using PyNN" +).show() + + From 39f01148289ef0f783190ee64476bc4b5409eb11 Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Thu, 6 Jun 2024 21:19:54 -0400 Subject: [PATCH 2/8] add one neuron noise example --- examples/one_neuron_noise_example.py | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/one_neuron_noise_example.py diff --git a/examples/one_neuron_noise_example.py b/examples/one_neuron_noise_example.py new file mode 100644 index 00000000..388487dd --- /dev/null +++ b/examples/one_neuron_noise_example.py @@ -0,0 +1,43 @@ +import pyNN.nest as sim +from pyNN.utility.plotting import Figure, Panel +import matplotlib.pyplot as plt + +sim.setup(timestep=0.1) # (ms) + + +cell_params = { + "v_rest": -70.0, # (mV) + "v_reset": -70.0, # (mV) + "cm": 0.250, # (nF) + "tau_m": 10, # (ms) + "tau_refrac": 2, # (ms) + "tau_syn_E": 2, # (ms) + "tau_syn_I": 2, # (ms) + "v_thresh": -55.0, # (mV) + "i_offset": 0, # (nA) +} + + +cell_type = sim.IF_curr_alpha(**cell_params) + +neuron = sim.Population(1, cell_type, label="Neuron") +neuron.record(["v", "spikes"]) + +poisson_noise_generators = sim.Population(2, sim.SpikeSourcePoisson(rate=[80000, 15000])) +poisson_noise_generators.record("spikes") + +syn = sim.StaticSynapse(weight=0.0012, delay=1) + +prj = sim.Projection(poisson_noise_generators, neuron, sim.AllToAllConnector() , syn) +prj.setWeights([0.0012, 0.001]) +prj.setDelays([1, 1]) + +sim.run(1000) + +data_v = neuron.get_data().segments[0].filter(name="v")[0] +data_spikes = neuron.get_data().segments[0].spiketrains + +Figure( + Panel(data_v[:,0], xlabel="Time (in ms)", ylabel="Membrane Potential", xticks=True, yticks=True) +).show() + From ce7a8681c29397f9d8379edf5775b3d273a4383c Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Fri, 7 Jun 2024 17:09:36 -0400 Subject: [PATCH 3/8] add two neuron example --- examples/two_neuron_example.py | 53 ++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 examples/two_neuron_example.py diff --git a/examples/two_neuron_example.py b/examples/two_neuron_example.py new file mode 100644 index 00000000..1c0a0d73 --- /dev/null +++ b/examples/two_neuron_example.py @@ -0,0 +1,53 @@ +""" +Two Neuron Example translations from NEST into PyNN. +""" + +import pyNN.nest as sim +from pyNN.utility.plotting import Figure, Panel +import matplotlib.pyplot as plt + +sim.setup(timestep=0.1) # (ms) + +cell_params = { + "v_rest": -70.0, # (mV) + "v_reset": -70.0, # (mV) + "cm": 0.250, # (nF) + "tau_m": 10, # (ms) + "tau_refrac": 2, # (ms) + "tau_syn_E": 2, # (ms) + "tau_syn_I": 2, # (ms) + "v_thresh": -55.0, # (mV) + "i_offset": 0, # (nA) +} + +cell_type = sim.IF_curr_alpha(**cell_params) + +neuron1 = sim.Population(1, cell_type, label="Neuron 1") +neuron1.set(i_offset=0.376) +neuron1.record("v") + +neuron2 = sim.Population(1, cell_type, label="Neuron 2") +neuron2.record("v") + +syn = sim.StaticSynapse(weight=0.02, delay=1.0) + +projection1 = sim.Projection(neuron1, neuron2, sim.AllToAllConnector(), syn) + +sim.run(1000) + +neuron1_data_v = neuron1.get_data().segments[0].filter(name="v")[0] +neuron2_data_v = neuron2.get_data().segments[0].filter(name="v")[0] + +Figure( + Panel(neuron1_data_v[:,0], + xticks=True, + yticks=True, + xlabel="Time (in ms)", + ylabel="Membrane Potential (in mV)"), + Panel(neuron2_data_v[:,0], + xticks=True, + yticks=True, + xlabel="Time (in ms)", + ylabel="Membrane Potential (in mV)") +).show() + From 587b07f27796508fdb3080fd8ae1a11794fdc05e Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Fri, 7 Jun 2024 18:49:39 -0400 Subject: [PATCH 4/8] add balanced neuron example (TESTING) --- examples/balanced_neuron_example.py | 67 +++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/balanced_neuron_example.py diff --git a/examples/balanced_neuron_example.py b/examples/balanced_neuron_example.py new file mode 100644 index 00000000..98380463 --- /dev/null +++ b/examples/balanced_neuron_example.py @@ -0,0 +1,67 @@ +from scipy.optimize import bisect +import matplotlib.pyplot as plt +import pyNN.nest as sim +from pyNN.utility.plotting import Figure, Panel + +import pyNN as testpynn + +spec_dict = { + "simulation_time": 25000.0, # (ms) + "n_ex": 16000, # Number of excitatory neurons + "n_in": 4000, # Number of inhibitory neurons + "r_ex": 5.0, # (Hz) Excitatory Rate + "r_in": 20.5, # (Hz) Inhibitory Rate + "ex_syn": 0.045, # (nA) Excitatory Synaptic Current Amplitude + "in_syn": -0.045, # (nA) Inhibitory Synaptic Current Amplitude + "delay": 1.0, # (ms) + "low": 15.0, # (Hz) + "high": 25.0, # (Hz) + "precision": 0.01 +} + +def output_rate(spec_dict, guess): + + newsim = testpynn.nest + newsim.setup(timestep=0.1) + + cell_params = { + "v_rest": -70.0, # (mV) + "v_reset": -70.0, # (mV) + "cm": 0.250, # (nF) + "tau_m": 10, # (ms) + "tau_refrac": 2, # (ms) + "tau_syn_E": 2, # (ms) + "tau_syn_I": 2, # (ms) + "v_thresh": -55.0, # (mV) + "i_offset": 0.0, # (nA) + } + + cell_type = sim.IF_curr_alpha(**cell_params) + neuron = sim.Population(1, cell_type, label="Neuron 1") + neuron.record(["v", "spikes"]) + + print("Inhibitory rate estimate: %5.2f Hz" % guess) + + noise_rate_ex = spec_dict["n_ex"] * spec_dict["r_ex"] + noise_rate_in = spec_dict["n_in"] * spec_dict["r_in"] + in_rate = float(abs(spec_dict["n_in"] * guess)) + + poisson_noise_generators = sim.Population(2, sim.SpikeSourcePoisson(rate=[noise_rate_ex, in_rate])) + + syn = sim.StaticSynapse(delay=1) + + prj = sim.Projection(poisson_noise_generators, neuron, sim.AllToAllConnector(), syn) + prj.setWeights([spec_dict["ex_syn"], spec_dict["in_syn"]]) + + newsim.run(1000) + + data_spikes = neuron.get_data().segments[0].spiketrains[0] + n_spikes = len(data_spikes) + output_rate = (n_spikes * 1000.0) / spec_dict["simulation_time"] + + print(" -> Neuron rate: %6.2f Hz (goal: %4.2f Hz)" % (output_rate, spec_dict["r_ex"])) + return output_rate + + +in_rate = bisect(lambda guess: output_rate(spec_dict, guess) - spec_dict["r_ex"], spec_dict["low"], spec_dict["high"], xtol=spec_dict["precision"]) +print("Optimal rate for the inhibitory population: %.2f Hz" % in_rate) From 8635f8088a5e81cc8664f7b3282e58d039407a29 Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Mon, 10 Jun 2024 13:34:57 -0400 Subject: [PATCH 5/8] add NEST Example iaf_neuron_with_current_generator --- examples/iaf_neuron_with_current_generator.py | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/iaf_neuron_with_current_generator.py diff --git a/examples/iaf_neuron_with_current_generator.py b/examples/iaf_neuron_with_current_generator.py new file mode 100644 index 00000000..b7f2945e --- /dev/null +++ b/examples/iaf_neuron_with_current_generator.py @@ -0,0 +1,41 @@ +import matplotlib.pyplot as plt +from pyNN.utility.plotting import Figure, Panel +import pyNN.nest as sim +import pyNN as devpynn + +timesteps = [0.1, 0.5, 1.0] +fig_counter=0 + +for dt in timesteps: + newsim = devpynn.nest + newsim.setup(timestep=dt) + + cell_params = { + "v_rest": -70.0, # (mV) + "v_reset": -70.0, # (mV) + "cm": 0.250, # (nF) + "tau_m": 10, # (ms) + "tau_refrac": 2, # (ms) + "tau_syn_E": 2, # (ms) + "tau_syn_I": 2, # (ms) + "v_thresh": -55.0, # (mV) + "i_offset": 0.376 # (nA) + } + + cell_type = sim.IF_curr_alpha(**cell_params) + neuron = sim.Population(1, cell_type, label="Neuron 1") + neuron.record(["v", "spikes"]) + newsim.run(1000.0) + + data_v = neuron.get_data().segments[0].filter(name="v")[0] + data_spikes = neuron.get_data().segments[0].spiketrains + + fig_counter += 1 + plt.figure(fig_counter) + plt.plot(data_v[:,0].times, data_v[:, 0]) + plt.xlabel("Time (in ms)") + plt.ylabel("Membrane Potential (in mV)") + plt.savefig(f"PLOT_iaf_neuron_current_{fig_counter}") + plt.show() + + print(f"\nNumber of spikes: {len(data_spikes)}") \ No newline at end of file From 56362b0afb7e51f8501f6b0962e82e8f344f5129 Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Mon, 10 Jun 2024 13:57:31 -0400 Subject: [PATCH 6/8] fix typo for number of spikes --- examples/iaf_neuron_with_current_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/iaf_neuron_with_current_generator.py b/examples/iaf_neuron_with_current_generator.py index b7f2945e..d1721309 100644 --- a/examples/iaf_neuron_with_current_generator.py +++ b/examples/iaf_neuron_with_current_generator.py @@ -28,7 +28,7 @@ newsim.run(1000.0) data_v = neuron.get_data().segments[0].filter(name="v")[0] - data_spikes = neuron.get_data().segments[0].spiketrains + data_spikes = neuron.get_data().segments[0].spiketrains[0] fig_counter += 1 plt.figure(fig_counter) From 9b2874b9fc61ed49e488d5793c5327cd9e7d3c89 Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Tue, 18 Jun 2024 16:33:40 -0400 Subject: [PATCH 7/8] update one_neuron_example --- examples/one_neuron_example.py | 61 ++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/examples/one_neuron_example.py b/examples/one_neuron_example.py index db4fd8ec..ff5390a0 100644 --- a/examples/one_neuron_example.py +++ b/examples/one_neuron_example.py @@ -1,12 +1,31 @@ +# encoding: utf-8 """ -A very simple example of simulating One Neuron -using NEST simulator backend with the help of PyNN. +A simple native NEST example of simulating One Neuron with the help of PyNN. + +Usage: one_neuron_example.py [-h] [--plot-figure] [--debug DEBUG] simulator + +positional arguments: + simulator neuron, nest, brian or another backend simulator + +optional arguments: + -h, --help show this help message and exit + --plot-figure plot the simulation results to a file + --debug DEBUG print debugging information """ + import pyNN.nest as sim +from pyNN.utility import get_simulator, init_logging, normalized_filename from pyNN.utility.plotting import Figure, Panel -sim.setup(timestep=0.1) # (ms) +sim, options = get_simulator(("--plot-figure", "Plot the simulation results to a file.", {"action": "store_true"}), + ("--debug", "Print debugging information.")) + + +if options.debug: + init_logging(None, debug=True) + +# === Define parameters ======================================================== cell_params = { "v_rest": -70.0, # (mV) @@ -20,25 +39,39 @@ "i_offset": 0.376, # (nA) } +# === Build the network ======================================================== + +sim.setup(timestep=0.1) # (ms) cell_type = sim.IF_curr_alpha(**cell_params) neuron = sim.Population(1, cell_type, label="Neuron 1") neuron.record("v") + +# === Run simulation =========================================================== + sim.run(1000.0) data_v = neuron.get_data().segments[0].filter(name="v")[0] -Figure( - Panel( - data_v[:,0], - xticks=True, - yticks=True, - xlabel="Time (in ms)", - ylabel="Membrane Potential (mV)" - ), - title="One Neuron", - annotations="Translating Single Neuron using PyNN" -).show() +if options.plot_figure: + figure_filename = normalized_filename("Results", "one_neuron_example", "png", + options.simulator, sim.num_processes()) + Figure( + Panel( + data_v[:,0], + xticks=True, + yticks=True, + xlabel="Time (in ms)", + ylabel="Membrane Potential (mV)" + ), + title="One Neuron", + annotations="One Neuron Example using PyNN" + ).save(figure_filename) + print(figure_filename) + +# === Clean up and quit ======================================================== + +sim.end() From 949946d21578c4990265fdad9a22448395cec89d Mon Sep 17 00:00:00 2001 From: Harsh Khilawala Date: Tue, 2 Jul 2024 15:46:25 -0400 Subject: [PATCH 8/8] stale commit --- examples/balanced_neuron_example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/balanced_neuron_example.py b/examples/balanced_neuron_example.py index 98380463..1ccad09c 100644 --- a/examples/balanced_neuron_example.py +++ b/examples/balanced_neuron_example.py @@ -55,7 +55,7 @@ def output_rate(spec_dict, guess): newsim.run(1000) - data_spikes = neuron.get_data().segments[0].spiketrains[0] + data_spikes = neuron.get_data(clear=True).segments[0].spiketrains[0] n_spikes = len(data_spikes) output_rate = (n_spikes * 1000.0) / spec_dict["simulation_time"]