Skip to content

Conversation

@samuelgarcia
Copy link
Member

@samuelgarcia samuelgarcia commented Jan 6, 2026

@JoeZiminski @oliche

This refactor the artifact detection and blanking

  • with a main multi method function : detect_artifact_periods()
  • return periods with the new base_period_dtype instead list of list
  • SilencedPeriodsRecording also use this new dtype
  • DetectArtifactAndSilentPeriodsRecording (replace SilencedArtifactsRecording) do detect and silence at the same
    time to be used in preprocess dict (@chrishalcrow this is for you)
  • refactor the method of Pierre based on envleope
  • incorporate the method of @oliche and @JoeZiminski to detect saturation

Could also be done in this PR:

  • add a margin_ms option in SilentPeriodsRecording to ewxtend the zeroing with a mragin : do we need this ?

To be done in 2 futures PR:

  • handle margin and taper on border of SilentPeriod (more tricky that it appear when period is at border...)
  • rewrite RemoveArtifactsRecording based on SilencedPeriodsRecording and the new dtype

@alejoe91 alejoe91 added the preprocessing Related to preprocessing module label Jan 6, 2026
@alejoe91 alejoe91 changed the title Refactor aftifacts detection. Refactor artifacts detection Jan 7, 2026
@yger
Copy link
Collaborator

yger commented Jan 12, 2026

@samuelgarcia do not forget, in this PR, to change DetectThresholdCrossing(recording, ...) to DetectThresholdCrossing(envelope, ...) in the detec_period_artefacts_by_envelope

@samuelgarcia
Copy link
Member Author

Hi @oliche @JoeZiminski
I copy paste and clean a bit your PR #4301 into this one.
Have a look and tell if this is OK.

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.

@samuelgarcia samuelgarcia changed the title Refactor artifacts detection Refactor artifacts detection + add sturation detection Jan 21, 2026
@samuelgarcia samuelgarcia marked this pull request as ready for review January 21, 2026 13:21
@samuelgarcia
Copy link
Member Author

@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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add detect_artifact_method detect_artifact_kwargs

Comment on lines +36 to +37
* 'saturation' using detect_artifact_periods_by_envelope()
* 'envelope' using detect_saturation_periods()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 'saturation' using detect_artifact_periods_by_envelope()
* 'envelope' using detect_saturation_periods()
* 'saturation' using detect_saturation_periods()
* 'envelope' using detect_artifact_periods_by_envelope()

Comment on lines +6 to +7
# from spikeinterface.core.core_tools import define_function_handling_dict_from_class
# from spikeinterface.preprocessing.silence_periods import SilencedPeriodsRecording
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# from spikeinterface.core.core_tools import define_function_handling_dict_from_class
# from spikeinterface.preprocessing.silence_periods import SilencedPeriodsRecording

Comment on lines +172 to +175
proportion :
mute_window_samples :
job_kwargs :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oups

@chrishalcrow
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's how saturation works, at least for int16. Maybe we can make a symmetric arguments?

@JoeZiminski
Copy link
Collaborator

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.

@samuelgarcia
Copy link
Member Author

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?

Yes, the last pair should be merged.
Let us check. the _collapse_events() is done for this purpose.
@JoeZiminski can you look at this ?

@samuelgarcia
Copy link
Member Author

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 of course. push anything you want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preprocessing Related to preprocessing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants