Refactor artifacts detection + add sturation detection#4297
Refactor artifacts detection + add sturation detection#4297samuelgarcia wants to merge 10 commits intoSpikeInterface:mainfrom
Conversation
|
@samuelgarcia do not forget, in this PR, to change DetectThresholdCrossing(recording, ...) to DetectThresholdCrossing(envelope, ...) in the detec_period_artefacts_by_envelope |
|
Hi @oliche @JoeZiminski I think we do not need to scaled traces at each chunk we could only could the unscaled threhold in the init and then we could avoid the costly convertion to float32 or float64. Also I put the unit in uV to be more SI friendly. |
|
@alejoe91 @chrishalcrow ready to review on my side |
| self, recording, periods=artifacts, mode=mode, noise_levels=noise_levels, seed=seed, **noise_levels_kwargs | ||
| ) | ||
| # note self._kwargs["periods"] is done by SilencedPeriodsRecording and so the computaion is done once | ||
|
|
There was a problem hiding this comment.
add detect_artifact_method detect_artifact_kwargs
| * 'saturation' using detect_artifact_periods_by_envelope() | ||
| * 'envelope' using detect_saturation_periods() |
There was a problem hiding this comment.
| * 'saturation' using detect_artifact_periods_by_envelope() | |
| * 'envelope' using detect_saturation_periods() | |
| * 'saturation' using detect_saturation_periods() | |
| * 'envelope' using detect_artifact_periods_by_envelope() |
| # from spikeinterface.core.core_tools import define_function_handling_dict_from_class | ||
| # from spikeinterface.preprocessing.silence_periods import SilencedPeriodsRecording |
There was a problem hiding this comment.
| # from spikeinterface.core.core_tools import define_function_handling_dict_from_class | |
| # from spikeinterface.preprocessing.silence_periods import SilencedPeriodsRecording |
|
Hello, trying this out on real data with saturation. I get this output artifact_periods=array([(0, 637721, 638769), (0, 638955, 639206), (0, 923219, 923602),
(0, 923605, 923606), (0, 923607, 923835)],Are these periods inclusive? And if they are, should the last pair be merged? |
|
|
||
| def detect_saturation_periods( | ||
| recording, | ||
| saturation_threshold_uV, # 1200 uV |
There was a problem hiding this comment.
Is it assumed that saturation_threshold_uV is symmetric around 0uV? E.g. you'll get saturation at $\pm 1200$uV? Is this always the case?
There was a problem hiding this comment.
I think that's how saturation works, at least for int16. Maybe we can make a symmetric arguments?
There was a problem hiding this comment.
Makes sense to me, @oliche? (we have a plus-minus saturation and voltage derivative threshold?
|
Hey @samuelgarcia thanks for this! will check this out tomorrow. I accidentally did not push two tidy-up commits to #4301, do you mind if I push to your branch to add some changes? I think it is mostly superficial stuff like docstrings etc. |
Yes, the last pair should be merged. |
yes of course. push anything you want |
|
Hey @samuelgarcia thanks for this, I added the content of those commits I missed. I ran into some issues with the (now renamed) If possible I'd prefer to cast and scale the data to avoid such issues, then we always know we are solid whatever the user inputs. I get |
| max_ = np.max(np.r_[data_seg_1.flatten(), data_seg_2.flatten()]) | ||
| gain = max_ / 2**15 | ||
| offset = 0 | ||
| offset = 50 |
There was a problem hiding this comment.
@JoeZiminski : why did you put 50 as offset here ?
There was a problem hiding this comment.
Apologies forgot to say, just to ensure it is tested (if it was zero and was accidentally not applied in the function, the test would not flag it)
There was a problem hiding this comment.
(that was just a quick test, see update below)
NP1 is 12bits, so I think that the lsb is 5000 / 2**11 = 2.4uV |
|
Hey @samuelgarcia I didn't realize the bit depth of the ADC is not 16-bit! I see here ADC resolution is given as 10 bits. In any case, the resolution from the gains/offsets used to scale the data won't be able to represent the values used for the |
|
@JoeZiminski the sample to volts conversion is a probe dependent variable. To get a feel of the complexity of the matter, here are the Neuropixel possible configs for all probes released to date: https://docs.google.com/spreadsheets/d/1UZycmxwwaJGCJ4AknyXrHkDXWOUO24TtAnBbe1mQ9u4/edit?gid=0#gid=0 As you can see there are potentially multiple conversion factors depending on the probe vintage. It seems the SpikeInterface canon is to leave this up to the user. Our design in https://github.com/int-brain-lab/ibl-neuropixel has been to read it from the metadata, making sure we are compatible with the oldest probe versions we observed at IBL. |
|
Thanks @oliche, in this case @samuelgarcia I think we should definitely scale the data rather than the parameters, as we cannot be sure the gains / offsets used to scale the data will not destroy information when converting float-to-int e.g. |
| # 0.98 is empirically determined as the true saturating point is | ||
| # slightly lower than the documented saturation point of the probe | ||
| self.saturation_threshold_unscaled = (thresh - offsets) / gains * 0.98 | ||
| self.uV_per_sec_threshold = (uV_per_sec_threshold - offsets) / gains |
There was a problem hiding this comment.
It should be easy to transform this in uv_per_sample_threshold as:
self.uv_per_sample_threshold = (uV_per_sec_threshold * fs - offsets) / gains
| self, | ||
| recording, | ||
| saturation_threshold_uV, | ||
| uV_per_sec_threshold, |
There was a problem hiding this comment.
I think it would be better to define uV_per_ms_threshold (or uv_per_sample as gfix does). The value in per seconds is very hard to grasp!
|
Hey @samuelgarcia @alejoe91 thanks for the feedback, I'm running into the below issue calculating the derivative on See below for the test data in float and int. When taking the derivative, information is lost due to overflow.
The above is the data converted to int, each timeseries is the data from one channel. In many instances, the data after the saturation is negative in int space, resulting in overflow e.g. by definition the saturation point is at the max of the int16 range, |
|
@JoeZiminski I think the test data is not realistic. Usually the maximum int recorded on neuropixel is 512 on NP1 and 8192 on NP2 so you wouldn't have the issues above as the max diff would be 8192 * 2 (2 ** 14). However in general I think it is a poor practice to compute in Anyways for this specific saturation detection problem, you just have to fix your test data by staying in the maxint ranges above. |
|
Cheers @oliche! That's good to know for NP probes. For other providers like Nexus, run their ADC at 16 bit-depth (e.g. here), in this case it might be a problem? That being said I don't know how often these probes saturate and whether people are likely to run this function for non-NP1 probes. But yes I think the easiest solution to avoid these worries is to perform computation in float. |


@JoeZiminski @oliche
This refactor the artifact detection and blanking
detect_artifact_periods()time to be used in preprocess dict (@chrishalcrow this is for you)
Could also be done in this PR:
To be done in 2 futures PR: