Skip to content

Commit a334c41

Browse files
authored
Merge pull request #831 from apdavison/issue759
Fix StepCurrentSource.set_parameters() after simulation run (issue #759)
2 parents 693a260 + 320a45f commit a334c41

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

pyNN/nest/standardmodels/electrodes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ def _check_step_times(self, times, amplitudes, resolution):
7373
raise ValueError("Step current timestamps should be monotonically increasing.")
7474
# NEST specific: subtract min_delay from times (set to 0.0, if result is negative)
7575
times = self._delay_correction(times)
76-
# find the last element <= dt (we find >dt and then go one element back)
76+
# find the last element <= biological_time + dt (we find >biological_time+dt and go one back)
7777
# this corresponds to the first timestamp that can be used by NEST for current injection
78-
ctr = np.searchsorted(times, resolution, side="right") - 1
78+
min_time = state.t_kernel + resolution
79+
ctr = np.searchsorted(times, min_time, side="right") - 1
7980
if ctr >= 0:
80-
times[ctr] = resolution
81+
times[ctr] = min_time
8182
times = times[ctr:]
8283
amplitudes = amplitudes[ctr:]
8384
# map timestamps to actual simulation time instants based on specified dt

test/system/scenarios/test_electrodes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,36 @@ def test_issue631(sim):
659659
assert (np.all(i_step.magnitude[:int(200.0 / sim_dt) - 1:] == 0))
660660

661661

662+
@run_with_simulators("nest")
663+
def test_issue759(sim):
664+
"""
665+
Test that StepCurrentSource.set_parameters() correctly handles times set at the current
666+
simulation time after a run. Previously, times at t=state.t would be sent to NEST as
667+
t - min_delay, which is in the past, causing no current injection.
668+
"""
669+
dt = 0.1
670+
sim.setup(timestep=dt, min_delay=dt)
671+
672+
cells = sim.Population(1, sim.IF_curr_exp(v_rest=-65.0, v_thresh=-55.0, tau_refrac=5.0))
673+
cells.initialize(v=-65.0)
674+
cells.record('spikes')
675+
676+
cs = sim.StepCurrentSource(times=[0.0], amplitudes=[0.0])
677+
cs.inject_into(cells)
678+
679+
sim.run(50.0) # no current, no spikes expected
680+
681+
cs.set_parameters(times=[50.0], amplitudes=[1.0]) # 1.0 nA drives V above threshold
682+
683+
sim.run(50.0) # current active, spikes expected
684+
685+
spikes = cells.get_data().segments[0].spiketrains[0]
686+
sim.end()
687+
688+
assert len(spikes) > 0, "Expected spikes in second run interval; current injection may have failed"
689+
assert all(t >= 50.0 for t in spikes.magnitude), "Spikes occurred before current injection started"
690+
691+
662692
if __name__ == '__main__':
663693
from pyNN.utility import get_simulator
664694
sim, args = get_simulator()

0 commit comments

Comments
 (0)