Raise the intended error for a zero sampling_period in instantaneous_rate - #710
adityasingh2400 wants to merge 1 commit into
Conversation
|
The The sphinx build dies executing a notebook that downloads sample data, this time with Counting across the four PRs, that fetch has now failed five different ways in roughly two hours: certificate verification, 403, connection reset, timeout, and now another timeout. @CozySocksAlways mentioned on #708 they were going to look into the docs side, so flagging it here for the same reason. |
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.
e889de0 to
7c30bc6
Compare
|
Re-pushed the same change as 7c30bc6 so CI runs on the current pipeline. master has not moved since this branch was created, so there was nothing to rebase and the diff is unchanged. Locally on Python 3.12, numpy 2.5.3, neo 0.14.5: |
instantaneous_rateaccepts asampling_periodof exactly zero and then dies deep inside the bin count computation with an error that says nothing about the argument that caused it.On current master:
The documented contract is already correct, only the validation is wrong. The
Raisessection ofinstantaneous_ratesays:"not larger than zero" includes zero, but the guard only rejects negative values, so zero slips through, the duration divided by zero is infinity, and
int()raisesOverflowError. This widens the guard to<= 0and reports the intended message, so the same script now ends withValueError: The 'sampling_period' (0.0 ms) must be larger than zero.The
Raisessection is also made self consistent. It listed the zero case underTypeErrorand a separate "smaller than zero" case underValueError, while the function has always raisedValueErrorhere. The single bullet now sits underValueErrorand reads "not larger than zero", which is both the documented contract and what the code does.test_instantaneous_rate_errorsgains two cases, a single spike train and a list of spike trains, both withsampling_period=0, asserting theValueErrormessage. Revertingstatistics.pyto master makes it fail with theOverflowErrorabove. After, 101 passed, 1 skipped and 33 subtests passed.pycodestylereports no new findings on either changed file.Unrelated but noticed in the same module while reading:
cv2,lvandlvrall documentwith_nanasDefault: Truewhile their signatures usewith_nan=False. Happy to send that as a separate docs only change if it is wanted.Disclosure: this change was prepared with AI assistance. I have reviewed and tested it.