Skip to content

Commit

Permalink
Fix STDP synapse bug (nest-simulator #2443) (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
clinssen authored Sep 14, 2023
1 parent 13d08a4 commit 7ee2455
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -708,12 +708,12 @@ void
// - its access counter indicates it has been read out by all connected
// STDP synapses, and
// - there is another, later spike, that is strictly more than
// (max_delay_ + eps) away from the new spike (at t_sp_ms)
// (min_global_delay + max_delay_ + eps) away from the new spike (at t_sp_ms)
while ( history_.size() > 1 )
{
const double next_t_sp = history_[ 1 ].t_;
if ( history_.front().access_counter_ >= n_incoming_ * num_transferred_variables
and t_sp_ms - next_t_sp > max_delay_ + nest::kernel().connection_manager.get_stdp_eps() )
and t_sp_ms - next_t_sp > max_delay_ + nest::Time::delay_steps_to_ms(nest::kernel().connection_manager.get_min_delay()) + nest::kernel().connection_manager.get_stdp_eps() )
{
history_.pop_front();
}
Expand Down
67 changes: 36 additions & 31 deletions tests/nest_tests/stdp_synapse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
# You should have received a copy of the GNU General Public License
# along with NEST. If not, see <http://www.gnu.org/licenses/>.

from typing import Sequence

import numpy as np
import os
import unittest
import pytest

import nest

Expand All @@ -41,15 +43,17 @@
sim_ref = True


class NestSTDPSynapseTest(unittest.TestCase):
class TestNestSTDPSynapse:

neuron_model_name = "iaf_psc_exp_nestml__with_stdp_nestml"
ref_neuron_model_name = "iaf_psc_exp_nestml_non_jit"

synapse_model_name = "stdp_nestml__with_iaf_psc_exp_nestml"
ref_synapse_model_name = "stdp_synapse"

def setUp(self):
@pytest.fixture(autouse=True,
scope="module")
def generate_model_code(self):
"""Generate the model code"""

jit_codegen_opts = {"neuron_synapse_pairs": [{"neuron": "iaf_psc_exp",
Expand Down Expand Up @@ -87,36 +91,38 @@ def setUp(self):
suffix="_nestml_non_jit",
codegen_opts=non_jit_codegen_opts)

def test_nest_stdp_synapse(self):
fname_snip = ""

pre_spike_times = [1., 11., 21.] # [ms]
post_spike_times = [6., 16., 26.] # [ms]

post_spike_times = np.sort(np.unique(1 + np.round(10 * np.sort(np.abs(np.random.randn(10)))))) # [ms]
pre_spike_times = np.sort(np.unique(1 + np.round(10 * np.sort(np.abs(np.random.randn(10)))))) # [ms]

post_spike_times = np.sort(np.unique(1 + np.round(100 * np.sort(np.abs(np.random.randn(100)))))) # [ms]
pre_spike_times = np.sort(np.unique(1 + np.round(100 * np.sort(np.abs(np.random.randn(100)))))) # [ms]

pre_spike_times = np.array([2., 4., 7., 8., 12., 13., 19., 23., 24., 28., 29., 30., 33., 34.,
35., 36., 38., 40., 42., 46., 51., 53., 54., 55., 56., 59., 63., 64.,
65., 66., 68., 72., 73., 76., 79., 80., 83., 84., 86., 87., 90., 95.,
99., 100., 103., 104., 105., 111., 112., 126., 131., 133., 134., 139., 147., 150.,
152., 155., 172., 175., 176., 181., 196., 197., 199., 202., 213., 215., 217., 265.])
post_spike_times = np.array([4., 5., 6., 7., 10., 11., 12., 16., 17., 18., 19., 20., 22., 23.,
25., 27., 29., 30., 31., 32., 34., 36., 37., 38., 39., 42., 44., 46.,
48., 49., 50., 54., 56., 57., 59., 60., 61., 62., 67., 74., 76., 79.,
80., 81., 83., 88., 93., 94., 97., 99., 100., 105., 111., 113., 114., 115.,
116., 119., 123., 130., 132., 134., 135., 145., 152., 155., 158., 166., 172., 174.,
188., 194., 202., 245., 249., 289., 454.])
# load the generated modules into NEST
nest.Install("nestml_jit_module")
nest.Install("nestml_non_jit_module")

@pytest.mark.parametrize("delay", [1., 1.5])
@pytest.mark.parametrize("resolution", [.1, .5, 1.])
@pytest.mark.parametrize("pre_spike_times,post_spike_times", [
([1., 11., 21.],
[6., 16., 26.]),
(np.sort(np.unique(1 + np.round(100 * np.sort(np.abs(np.random.randn(100)))))),
np.sort(np.unique(1 + np.round(100 * np.sort(np.abs(np.random.randn(100))))))),
(np.array([2., 4., 7., 8., 12., 13., 19., 23., 24., 28., 29., 30., 33., 34.,
35., 36., 38., 40., 42., 46., 51., 53., 54., 55., 56., 59., 63., 64.,
65., 66., 68., 72., 73., 76., 79., 80., 83., 84., 86., 87., 90., 95.,
99., 100., 103., 104., 105., 111., 112., 126., 131., 133., 134., 139., 147., 150.,
152., 155., 172., 175., 176., 181., 196., 197., 199., 202., 213., 215., 217., 265.]),
np.array([4., 5., 6., 7., 10., 11., 12., 16., 17., 18., 19., 20., 22., 23.,
25., 27., 29., 30., 31., 32., 34., 36., 37., 38., 39., 42., 44., 46.,
48., 49., 50., 54., 56., 57., 59., 60., 61., 62., 67., 74., 76., 79.,
80., 81., 83., 88., 93., 94., 97., 99., 100., 105., 111., 113., 114., 115.,
116., 119., 123., 130., 132., 134., 135., 145., 152., 155., 158., 166., 172., 174.,
188., 194., 202., 245., 249., 289., 454.])),
(np.array([1, 5, 6, 7, 9, 11, 12, 13, 14.5, 16.1]),
np.array([2, 3, 4, 8, 9, 10, 12, 13.2, 15.1, 16.4]))
])
def test_nest_stdp_synapse(self, pre_spike_times: Sequence[float], post_spike_times: Sequence[float], resolution: float, delay: float, fname_snip: str = ""):
self.run_synapse_test(neuron_model_name=self.neuron_model_name,
ref_neuron_model_name=self.ref_neuron_model_name,
synapse_model_name=self.synapse_model_name,
ref_synapse_model_name=self.ref_synapse_model_name,
resolution=.5, # [ms]
delay=1.5, # [ms]
resolution=resolution, # [ms]
delay=delay, # [ms]
pre_spike_times=pre_spike_times,
post_spike_times=post_spike_times,
fname_snip=fname_snip)
Expand All @@ -143,8 +149,6 @@ def run_synapse_test(self, neuron_model_name,

nest.set_verbosity("M_ALL")
nest.ResetKernel()
nest.Install("nestml_jit_module")
nest.Install("nestml_non_jit_module")

print("Pre spike times: " + str(pre_spike_times))
print("Post spike times: " + str(post_spike_times))
Expand All @@ -166,7 +170,8 @@ def run_synapse_test(self, neuron_model_name,

# create spike_generators with these times
pre_sg = nest.Create("spike_generator",
params={"spike_times": pre_spike_times})
params={"spike_times": pre_spike_times,
"allow_offgrid_times": True})
post_sg = nest.Create("spike_generator",
params={"spike_times": post_spike_times,
"allow_offgrid_times": True})
Expand Down

0 comments on commit 7ee2455

Please sign in to comment.