Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed improvements in spindles detection #87

Open
raphaelvallat opened this issue Jul 8, 2022 · 1 comment
Open

Speed improvements in spindles detection #87

raphaelvallat opened this issue Jul 8, 2022 · 1 comment
Assignees
Labels
enhancement 🚧 New feature or request
Projects

Comments

@raphaelvallat
Copy link
Owner

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

yasa/yasa/detection.py

Lines 732 to 738 in b1890be

# Compute the pointwise relative power using interpolated STFT
# Here we use a step of 200 ms to speed up the computation.
# Note that even if the threshold is None we still need to calculate it
# for the individual spindles parameter (RelPow).
f, t, Sxx = stft_power(
data_broad[i, :], sf, window=2, step=0.2, band=freq_broad, interp=False, norm=True
)

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:

func = interp1d(t, rel_pow, kind="cubic", bounds_error=False, fill_value=0)

f = interp1d(t, out, kind="cubic", bounds_error=False, fill_value=0, assume_sorted=True)

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

yasa/yasa/detection.py

Lines 750 to 763 in b1890be

if do_corr:
_, mcorr = moving_transform(
x=data_sigma[i, :],
y=data_broad[i, :],
sf=sf,
window=0.3,
step=0.1,
method="corr",
interp=True,
)
if do_rms:
_, mrms = moving_transform(
x=data_sigma[i, :], sf=sf, window=0.3, step=0.1, method="rms", interp=True
)

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

Raphael

@raphaelvallat raphaelvallat added the enhancement 🚧 New feature or request label Jul 8, 2022
@raphaelvallat raphaelvallat self-assigned this Jul 8, 2022
@raphaelvallat
Copy link
Owner Author

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 raphaelvallat added this to To do in Detections Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🚧 New feature or request
Projects
Development

No branches or pull requests

1 participant