yasa icon indicating copy to clipboard operation
yasa copied to clipboard

Speed improvements in spindles detection

Open raphaelvallat opened this issue 1 year ago • 1 comments

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.

  1. 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

  1. 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 avatar Jul 08 '22 23:07 raphaelvallat

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:

  • The sampling frequency
  • The step size in yasa.moving_transform: increasing to 200 ms or 300 ms (instead of 100 ms) significantly reduces computation time, at the cost of a lower temporal resolution.

raphaelvallat avatar Jul 20 '22 22:07 raphaelvallat