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
424 stars 115 forks source link

Speed improvements in spindles detection #87

Open raphaelvallat opened 2 years ago

raphaelvallat commented 2 years ago

The spindles_detect function can be very slow when multiple channels are present and/or the sampling rate is high. Most of the overhead comes from the yasa.moving_transform and yasa.stft_power function, which are used to calculate the moving correlation / RMS and sigma relative power, respectively

image

There are several steps we could do to speed up the function:

1) Apply the stft_power function to all channels at once, instead of each channel separately

https://github.com/raphaelvallat/yasa/blob/b1890beef91e8f4c76194277a6444a7c0675715d/yasa/detection.py#L732-L738

We should also only calculate the STFT power if the relative power threshold is enabled. Otherwise, STFT should only be calculated for each detected spindle event.

2) Use linear instead of cubic interpolation:

https://github.com/raphaelvallat/yasa/blob/b1890beef91e8f4c76194277a6444a7c0675715d/yasa/detection.py#L746

https://github.com/raphaelvallat/yasa/blob/b1890beef91e8f4c76194277a6444a7c0675715d/yasa/others.py#L219

3) Use a longer step in moving_transform — however we might lose some temporal precision on the onset/offset of the spindle

https://github.com/raphaelvallat/yasa/blob/b1890beef91e8f4c76194277a6444a7c0675715d/yasa/detection.py#L750-L763

Let me know if you have any other ideas to speed up the function!

Raphael

raphaelvallat commented 2 years ago

Update: implementing the above solutions 1 and 2 does not make a noticeable difference in computation time. The two parameters that really impact the computation time (apart from number of channels and number of samples) are: