From e889de02242b1161acefbc53acb19cb2d5384d99 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Wed, 5 Aug 2026 08:52:52 -0700 Subject: [PATCH] Reject a zero sampling_period in instantaneous_rate A sampling_period of exactly zero passed the 'sampling_period.magnitude < 0' guard and reached the bin-count computation, where dividing the recording duration by zero produced infinity and int() raised OverflowError: cannot convert float infinity to integer. The docstring already documents the intended contract, it lists a sampling_period that is 'not larger than zero' under Raises, so only the guard was wrong. Widen it to '<= 0' and raise the informative ValueError instead. The Raises section now lists that case under ValueError, which is the class the function actually raises for a negative sampling period. --- elephant/statistics.py | 7 +++---- elephant/test/test_statistics.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/elephant/statistics.py b/elephant/statistics.py index bb456ac02..519c4c2fb 100644 --- a/elephant/statistics.py +++ b/elephant/statistics.py @@ -760,14 +760,13 @@ def instantaneous_rate(spiketrains, sampling_period, kernel='auto', TypeError * If `spiketrain` is not an instance of :class:`neo.core.SpikeTrain`. * If `sampling_period` is not a `pq.Quantity`. - * If `sampling_period` is not larger than zero. * If `kernel` is neither instance of :mod:`elephant.kernels` nor string 'auto'. * If `cutoff` is neither `float` nor `int`. * If `t_start` and `t_stop` are neither None nor a `pq.Quantity`. * If `trim` is not `bool`. ValueError - * If `sampling_period` is smaller than zero. + * If `sampling_period` is not larger than zero. * If `kernel` is 'auto' and the function was unable to calculate optimal kernel width for instantaneous rate from input data. * If `kernel` length is larger than binned spiketrain length @@ -987,9 +986,9 @@ def optimal_kernel(st): raise TypeError(f"The 'sampling_period' must be a time Quantity." f"Found: {type(sampling_period)}") - if sampling_period.magnitude < 0: + if sampling_period.magnitude <= 0: raise ValueError(f"The 'sampling_period' ({sampling_period}) " - f"must be non-negative.") + f"must be larger than zero.") if not (isinstance(kernel, kernels.Kernel) or kernel == 'auto'): raise TypeError(f"'kernel' must be instance of class " diff --git a/elephant/test/test_statistics.py b/elephant/test/test_statistics.py index fde6d54e8..21942cdd5 100644 --- a/elephant/test/test_statistics.py +++ b/elephant/test/test_statistics.py @@ -565,6 +565,16 @@ def test_instantaneous_rate_errors(self): ValueError, statistics.instantaneous_rate, spiketrains=self.spike_train, kernel=self.kernel, sampling_period=-0.01 * pq.ms) + self.assertRaisesRegex( # sampling period is == 0 + ValueError, r"must be larger than zero", + statistics.instantaneous_rate, + spiketrains=self.spike_train, kernel=self.kernel, + sampling_period=0 * pq.ms) + self.assertRaisesRegex( # sampling period is == 0, list input + ValueError, r"must be larger than zero", + statistics.instantaneous_rate, + spiketrains=[self.spike_train, self.spike_train], + kernel=self.kernel, sampling_period=0 * pq.s) self.assertRaises( # no kernel or kernel='auto' TypeError, statistics.instantaneous_rate, spiketrains=self.spike_train, sampling_period=0.01 * pq.ms,