Skip to content

Commit

Permalink
Merge pull request #515 from apdavison/issue511
Browse files Browse the repository at this point in the history
Raise an Exception if the spike times given to SpikeSourceArray are not ordered
  • Loading branch information
apdavison committed Oct 5, 2017
2 parents 2b5b4ff + 978193c commit 3e336ef
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions pyNN/brian/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import numpy
import brian
from pyNN.parameters import Sequence, simplify
from pyNN import errors
from . import simulator

mV = brian.mV
Expand Down Expand Up @@ -252,6 +253,7 @@ def __init__(self, n, equations, spike_times=None):
Note that `equations` is not used: it is simply for compatibility with
other NeuronGroup subclasses.
"""
self._check_spike_times(spike_times)
spiketimes = [(i, t) for i, seq in enumerate(spike_times) for t in seq.value]
brian.SpikeGeneratorGroup.__init__(self, n, spiketimes,
clock=simulator.state.network.clock)
Expand All @@ -267,9 +269,16 @@ def _set_spike_times(self, spike_times, mask=None):
existing_times = self._get_spike_times()
existing_times[mask] = spike_times
spike_times = existing_times
self._check_spike_times(spike_times)
values = [(i, t) for i, seq in enumerate(spike_times) for t in seq.value]
brian.SpikeGeneratorGroup.__init__(self, self.N, values, period=self.period)
spike_times = property(fget=_get_spike_times, fset=_set_spike_times)

def _check_spike_times(self, spike_times):
for seq in spike_times:
if numpy.any(seq.value[:-1] > seq.value[1:]):
raise errors.InvalidParameterValueError(
"Spike times given to SpikeSourceArray must be in increasing order")

def initialize(self):
pass
2 changes: 2 additions & 0 deletions pyNN/nest/populations.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ def _create_cells(self):
except nest.NESTError as err:
if "UnknownModelName" in err.args[0] and "cond" in err.args[0]:
raise errors.InvalidModelError("%s Have you compiled NEST with the GSL (Gnu Scientific Library)?" % err)
if "Spike times must be sorted in non-descending order" in err.args[0]:
raise errors.InvalidParameterValueError("Spike times given to SpikeSourceArray must be in increasing order")
raise # errors.InvalidModelError(err)
# create parrot neurons if necessary
if hasattr(self.celltype, "uses_parrot") and self.celltype.uses_parrot:
Expand Down
3 changes: 3 additions & 0 deletions pyNN/neuron/cells.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import logging
from math import pi
import numpy
from neuron import h, nrn, hclass

from pyNN import errors
Expand Down Expand Up @@ -572,6 +573,8 @@ def _set_spike_times(self, spike_times):
self._spike_times = h.Vector(spike_times.value)
except (RuntimeError, AttributeError):
raise errors.InvalidParameterValueError("spike_times must be an array of floats")
if numpy.any(spike_times.value[:-1] > spike_times.value[1:]):
raise errors.InvalidParameterValueError("Spike times given to SpikeSourceArray must be in increasing order")
self.play(self._spike_times)

def _get_spike_times(self):
Expand Down
14 changes: 13 additions & 1 deletion test/system/scenarios/test_cell_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
except ImportError:
have_scipy = False
import quantities as pq
from nose.tools import assert_greater, assert_less
from nose.tools import assert_greater, assert_less, assert_raises
from pyNN.errors import InvalidParameterValueError

from .registry import register

Expand Down Expand Up @@ -261,6 +262,16 @@ def test_SpikeSourcePoissonRefractory(sim, plot_figure=False):
test_SpikeSourcePoissonRefractory.__test__ = False



@register()
def issue511(sim):
"""Giving SpikeSourceArray an array of non-ordered spike times should produce an InvalidParameterValueError error"""
sim.setup()
celltype = sim.SpikeSourceArray(spike_times=[[2.4, 4.8, 6.6, 9.4], [3.5, 6.8, 9.6, 8.3]])
assert_raises(InvalidParameterValueError, sim.Population, 2, celltype)



# todo: add test of Izhikevich model


Expand All @@ -275,3 +286,4 @@ def test_SpikeSourcePoissonRefractory(sim, plot_figure=False):
test_SpikeSourcePoisson(sim, plot_figure=args.plot_figure)
test_SpikeSourceGamma(sim, plot_figure=args.plot_figure)
test_SpikeSourcePoissonRefractory(sim, plot_figure=args.plot_figure)
issue511(sim)

0 comments on commit 3e336ef

Please sign in to comment.