-
Notifications
You must be signed in to change notification settings - Fork 221
Remove artefacts based of an enveloppe #3715
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
Open
yger
wants to merge
26
commits into
SpikeInterface:main
Choose a base branch
from
yger:thr_crossings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d545d6a
WIP
yger c69b7be
WIP
yger c5a538c
WIP
yger b1ce726
Finishing the node
yger 816e4bc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d295851
WIP
yger c02ad97
WIP
yger 09291d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3cf2615
Renaming
yger ee74efc
WIP
yger cdd48ec
WIP
yger 0636637
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 871a760
WIP
yger 2fc771a
Imports
yger 39e4770
WIP
yger 674f3de
WIP
yger e2139f0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 31de097
Merge branch 'SpikeInterface:main' into thr_crossings
yger 3de822e
Merge branch 'main' into thr_crossings
yger d1d2097
Making the detector reproducible
yger 2f022dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2697736
Fixing bug in silence_periods
yger eaee6cf
Merge branch 'thr_crossings' of github.com:yger/spikeinterface into t…
yger 83853e5
Fix
yger 7dfdd17
Patching
yger 3dc4a95
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
from __future__ import annotations | ||
|
||
import numpy as np | ||
|
||
from spikeinterface.core.core_tools import define_function_handling_dict_from_class | ||
from spikeinterface.preprocessing.silence_periods import SilencedPeriodsRecording | ||
from spikeinterface.preprocessing.rectify import RectifyRecording | ||
from spikeinterface.preprocessing.common_reference import CommonReferenceRecording | ||
from spikeinterface.preprocessing.filter_gaussian import GaussianFilterRecording | ||
from spikeinterface.core.job_tools import split_job_kwargs, fix_job_kwargs | ||
from spikeinterface.core.recording_tools import get_noise_levels | ||
from spikeinterface.core.node_pipeline import PeakDetector, base_peak_dtype | ||
import numpy as np | ||
|
||
|
||
class DetectThresholdCrossing(PeakDetector): | ||
|
||
name = "threshold_crossings" | ||
preferred_mp_context = None | ||
|
||
def __init__( | ||
self, | ||
recording, | ||
detect_threshold=5, | ||
noise_levels=None, | ||
seed=None, | ||
random_slices_kwargs={}, | ||
): | ||
PeakDetector.__init__(self, recording, return_output=True) | ||
if noise_levels is None: | ||
random_slices_kwargs.update({"seed": seed}) | ||
noise_levels = get_noise_levels(recording, return_scaled=False, random_slices_kwargs=random_slices_kwargs) | ||
self.abs_thresholds = noise_levels * detect_threshold | ||
self._dtype = np.dtype(base_peak_dtype + [("onset", "bool")]) | ||
|
||
def get_trace_margin(self): | ||
return 0 | ||
|
||
def get_dtype(self): | ||
return self._dtype | ||
|
||
def compute(self, traces, start_frame, end_frame, segment_index, max_margin): | ||
z = np.median(traces / self.abs_thresholds, 1) | ||
threshold_mask = np.diff((z > 1) != 0, axis=0) | ||
indices = np.flatnonzero(threshold_mask) | ||
local_peaks = np.zeros(indices.size, dtype=self._dtype) | ||
local_peaks["sample_index"] = indices | ||
local_peaks["onset"][::2] = True | ||
local_peaks["onset"][1::2] = False | ||
return (local_peaks,) | ||
|
||
|
||
def detect_onsets(recording, detect_threshold=5, min_duration_ms=50, **extra_kwargs): | ||
|
||
from spikeinterface.core.node_pipeline import ( | ||
run_node_pipeline, | ||
) | ||
|
||
random_chunk_kwargs, job_kwargs = split_job_kwargs(extra_kwargs) | ||
job_kwargs = fix_job_kwargs(job_kwargs) | ||
|
||
node0 = DetectThresholdCrossing(recording, detect_threshold, **random_chunk_kwargs) | ||
|
||
peaks = run_node_pipeline( | ||
recording, | ||
[node0], | ||
job_kwargs, | ||
job_name="detect threshold crossings", | ||
) | ||
|
||
order = np.lexsort((peaks["sample_index"], peaks["segment_index"])) | ||
peaks = peaks[order] | ||
|
||
periods = [] | ||
fs = recording.sampling_frequency | ||
max_duration_samples = int(min_duration_ms * fs / 1000) | ||
num_seg = recording.get_num_segments() | ||
|
||
for seg_index in range(num_seg): | ||
sub_periods = [] | ||
mask = peaks["segment_index"] == 0 | ||
sub_peaks = peaks[mask] | ||
if len(sub_peaks) > 0: | ||
if not sub_peaks["onset"][0]: | ||
local_peaks = np.zeros(1, dtype=np.dtype(base_peak_dtype + [("onset", "bool")])) | ||
local_peaks["sample_index"] = 0 | ||
local_peaks["onset"] = True | ||
sub_peaks = np.hstack((local_peaks, sub_peaks)) | ||
if sub_peaks["onset"][-1]: | ||
local_peaks = np.zeros(1, dtype=np.dtype(base_peak_dtype + [("onset", "bool")])) | ||
local_peaks["sample_index"] = recording.get_num_samples(seg_index) | ||
local_peaks["onset"] = False | ||
sub_peaks = np.hstack((sub_peaks, local_peaks)) | ||
|
||
indices = np.flatnonzero(np.diff(sub_peaks["onset"])) | ||
for i, j in zip(indices[:-1], indices[1:]): | ||
if sub_peaks["onset"][i]: | ||
start = sub_peaks["sample_index"][i] | ||
end = sub_peaks["sample_index"][j] | ||
if end - start > max_duration_samples: | ||
sub_periods.append((start, end)) | ||
|
||
periods.append(sub_periods) | ||
|
||
return periods | ||
|
||
|
||
class SilencedArtifactsRecording(SilencedPeriodsRecording): | ||
""" | ||
Silence user-defined periods from recording extractor traces. The code will construct | ||
an enveloppe of the recording (as a low pass filtered version of the traces) and detect | ||
threshold crossings to identify the periods to silence. The periods are then silenced either | ||
on a per channel basis or across all channels by replacing the values by zeros or by | ||
adding gaussian noise with the same variance as the one in the recordings | ||
|
||
Parameters | ||
---------- | ||
recording : RecordingExtractor | ||
The recording extractor to silence putative artifacts | ||
detect_threshold : float, default: 5 | ||
The threshold to detect artifacts. The threshold is computed as `detect_threshold * noise_level` | ||
freq_max : float, default: 20 | ||
The maximum frequency for the low pass filter used | ||
min_duration_ms : float, default: 50 | ||
The minimum duration for a threshold crossing to be considered as an artefact. | ||
noise_levels : array | ||
Noise levels if already computed | ||
seed : int | None, default: None | ||
Random seed for `get_noise_levels` and `NoiseGeneratorRecording`. | ||
If none, `get_noise_levels` uses `seed=0` and `NoiseGeneratorRecording` generates a random seed using `numpy.random.default_rng`. | ||
mode : "zeros" | "noise", default: "zeros" | ||
Determines what periods are replaced by. Can be one of the following: | ||
|
||
- "zeros": Artifacts are replaced by zeros. | ||
|
||
- "noise": The periods are filled with a gaussion noise that has the | ||
same variance that the one in the recordings, on a per channel | ||
basis | ||
**random_slices_kwargs : Keyword arguments for `spikeinterface.core.get_random_data_chunk()` function | ||
|
||
Returns | ||
------- | ||
silenced_recording : SilencedArtifactsRecording | ||
The recording extractor after silencing detected artifacts | ||
""" | ||
|
||
def __init__( | ||
self, | ||
recording, | ||
detect_threshold=5, | ||
verbose=False, | ||
freq_max=5.0, | ||
min_duration_ms=50, | ||
mode="zeros", | ||
noise_levels=None, | ||
seed=None, | ||
list_periods=None, | ||
**random_slices_kwargs, | ||
): | ||
|
||
self.enveloppe = RectifyRecording(recording) | ||
self.enveloppe = GaussianFilterRecording(self.enveloppe, freq_min=None, freq_max=freq_max) | ||
self.enveloppe = CommonReferenceRecording(self.enveloppe) | ||
|
||
if list_periods is None: | ||
list_periods = detect_onsets( | ||
self.enveloppe, | ||
detect_threshold=detect_threshold, | ||
min_duration_ms=min_duration_ms, | ||
seed=seed, | ||
**random_slices_kwargs, | ||
) | ||
|
||
if verbose: | ||
for i, periods in enumerate(list_periods): | ||
total_time = np.sum([end - start for start, end in periods]) | ||
percentage = 100 * total_time / recording.get_num_samples(i) | ||
print(f"{percentage}% of segment {i} has been flagged as artifactual") | ||
|
||
if "enveloppe" in random_slices_kwargs: | ||
random_slices_kwargs.pop("enveloppe") | ||
|
||
SilencedPeriodsRecording.__init__( | ||
self, recording, list_periods, mode=mode, noise_levels=noise_levels, seed=seed, **random_slices_kwargs | ||
) | ||
|
||
self._kwargs.update( | ||
{ | ||
"detect_threshold": detect_threshold, | ||
"freq_max": freq_max, | ||
"verbose": verbose, | ||
"min_duration_ms": min_duration_ms, | ||
"enveloppe": self.enveloppe, | ||
} | ||
) | ||
|
||
|
||
# function for API | ||
silence_artifacts = define_function_handling_dict_from_class( | ||
source_class=SilencedArtifactsRecording, name="silence_artifacts" | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/spikeinterface/preprocessing/tests/test_silence_artifacts.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
import numpy as np | ||
|
||
from spikeinterface.core import generate_recording | ||
from spikeinterface.preprocessing import silence_artifacts | ||
|
||
|
||
def test_silence_artifacts(): | ||
# one segment only | ||
rec = generate_recording(durations=[10.0, 10]) | ||
new_rec = silence_artifacts(rec, detect_threshold=5, freq_max=5.0, min_duration_ms=50) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_silence_artifacts() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.