EtienneCmb / visbrain

A multi-purpose GPU-accelerated open-source suite for brain data visualization
http://visbrain.org
Other
241 stars 65 forks source link

kcdetect fails with direct use #57

Closed tom-beer closed 4 years ago

tom-beer commented 4 years ago

We are trying to detect K-Complexes using the function kcdetect, without using the GUI. We encounter errors using raw signals form the SHHS dataset. The signals are 125Hz single channel EEGs, with shape (1,15000) (2 minute epochs). This is how we are trying to run kcdetect:

kcs = kcdetect(data, sf, proba_thr=0.6, amp_thr=1., hypno=None, nrem_only=False, tmin=80, tmax=240, kc_min_amp=.1, kc_max_amp=.7, fmin=.5, fmax=4., delta_thr=.75, smoothing_s=20, spindles_thresh=2., range_spin_sec=20, min_distance_ms=500.)

We get the following error: image

Does this code support running the functions directly on raw signals? If not, what are the preprocessing steps required? If this is not the intended use, please point us to our mistakes. Your help would be much appreciated

EtienneCmb commented 4 years ago

Hi @tom-beer ,

The function should also works outside of the GUI. Try to run it on a vector version of your data, with a shape of (15000,). For example, use np.squeeze(data) or data.squeeze() before giving it as an input.

tom-beer commented 4 years ago

Thanks for the quick reply! Unfortunately vector format yields the same error. We'll try to characterize the issues more finely, to our understanding:

  1. In the following for loop (detection.py lines 118-122): for s in idx_start: d = s - idx_zc_soft soft_beg = d[d > 0].min() soft_end = np.abs(d[d < 0]).min() idx_kc = np.append(idx_kc, np.arange(s - soft_beg, s + soft_end)) When idx_zc_soft[0] and idx_start have the same values, d[d > 0] is an empty array and yields an error when applying .min().

  2. In detection.py line 125: idx_spin = spindlesdetect(data, sf, spindles_thresh, hypno, False)[0] When spindlesdetect finds no spindles it returns an empty array and so accessing index 0 fails.

We can deal with these issues by naive fixes, but we don't know if that would preserve the desired algorithmic behavior. What would you suggest?

EtienneCmb commented 4 years ago

Did you try to run the detections through the GUI?

To me this is probably a formatting issue of either the data, the hypnogram or may be other input parameters. I would suggest to open the GUI, put a breakpoint in the kcdetect functions and checkout the inputs.

I don't see any reason why it can't work out of the GUI, we just have to figure out the input formats. Same for all of the detections

raphaelvallat commented 4 years ago

Hi @tom-beer,

I'd suggest you try changing some of the threshold parameters of this function and see if the error persists. Also, when possible make sure that you're using a hypnogram and set _nremonly=True, as this will significantly reduce the risk of bugs and invalid detection.

Finally, if you're working with raw data I would strongly recommend that you check the YASA package which is intended as a command-line-only, newer version of the detection implemented in Visbrain. Specificallly, you may want to look the yasa.sw_detect() function, and restrain the detection to stage N2 sleep in order to detected specifically K-complexes.

If you want to keep using Visbrain's detection with the same parameters, then yes, you're right, we'll need to add a few safety checks & warnings to ensure that we're not dealing with empty arrays.

Hope this helps, Raphael

tom-beer commented 4 years ago

Thanks @EtienneCmb and @raphaelvallat for the valuable advice. We have moved to use yasa.sw_detect for the KC detection as advised.