raphaelvallat / yasa

YASA (Yet Another Spindle Algorithm): a Python package to analyze polysomnographic sleep recordings.
https://raphaelvallat.com/yasa/
BSD 3-Clause "New" or "Revised" License
428 stars 115 forks source link

Handle flat data in spindles detect #85

Closed raphaelvallat closed 2 years ago

raphaelvallat commented 2 years ago

If the EEG data is flat (at any point) for at least 300 ms, the yasa.spindles_detect function will throw a ZeroDivisionError. This is because of the call to yasa.moving_transform:

https://github.com/raphaelvallat/yasa/blob/a281d556749b3a968f9858b4db9f803186dfabcd/yasa/detection.py#L750-L759

which in turn calls the lower-level numba function yasa.numba._corr:

https://github.com/raphaelvallat/yasa/blob/a281d556749b3a968f9858b4db9f803186dfabcd/yasa/numba.py#L16-L30

An easy fix for this is to add the following lines to the _corr function:

if r_den == 0:
    return np.nan

We then need to make sure that the resulting NaN are correctly handled here:

https://github.com/raphaelvallat/yasa/blob/a281d556749b3a968f9858b4db9f803186dfabcd/yasa/detection.py#L779-L782