-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 33 additions & 0 deletions
33
src/spikeinterface/sortingcomponents/tests/test_waveforms/test_hanning_filter.py
This file contains 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,33 @@ | ||
import pytest | ||
|
||
|
||
from spikeinterface.sortingcomponents.waveforms.hanning_filter import HanningFilter | ||
|
||
from spikeinterface.core.node_pipeline import ( | ||
PeakRetriever, | ||
ExtractDenseWaveforms, | ||
run_node_pipeline, | ||
) | ||
|
||
|
||
def test_hanning_filter(generated_recording, detected_peaks, chunk_executor_kwargs): | ||
recording = generated_recording | ||
peaks = detected_peaks | ||
|
||
# Parameters | ||
ms_before = 1.0 | ||
ms_after = 1.0 | ||
|
||
# Node initialization | ||
peak_retriever = PeakRetriever(recording, peaks) | ||
|
||
extract_waveforms = ExtractDenseWaveforms( | ||
recording=recording, parents=[peak_retriever], ms_before=ms_before, ms_after=ms_after, return_output=True | ||
) | ||
|
||
hanning_filter = HanningFilter(recording=recording, parents=[peak_retriever, extract_waveforms]) | ||
pipeline_nodes = [peak_retriever, extract_waveforms, hanning_filter] | ||
|
||
# Extract projected waveforms and compare | ||
waveforms, denoised_waveforms = run_node_pipeline(recording, nodes=pipeline_nodes, job_kwargs=chunk_executor_kwargs) | ||
assert waveforms.shape == denoised_waveforms.shape |
47 changes: 47 additions & 0 deletions
47
src/spikeinterface/sortingcomponents/waveforms/hanning_filter.py
This file contains 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,47 @@ | ||
from __future__ import annotations | ||
|
||
|
||
from typing import List, Optional | ||
import numpy as np | ||
from spikeinterface.core import BaseRecording | ||
from spikeinterface.core.node_pipeline import PipelineNode, WaveformsNode, find_parent_of_type | ||
|
||
|
||
class HanningFilter(WaveformsNode): | ||
""" | ||
Hanning Filtering to remove border effects while extracting waveforms | ||
Parameters | ||
---------- | ||
recording: BaseRecording | ||
The recording extractor object | ||
return_output: bool, default: True | ||
Whether to return output from this node | ||
parents: list of PipelineNodes, default: None | ||
The parent nodes of this node | ||
""" | ||
|
||
def __init__( | ||
self, | ||
recording: BaseRecording, | ||
return_output: bool = True, | ||
parents: Optional[List[PipelineNode]] = None, | ||
): | ||
waveform_extractor = find_parent_of_type(parents, WaveformsNode) | ||
if waveform_extractor is None: | ||
raise TypeError(f"HanningFilter should have a single {WaveformsNode.__name__} in its parents") | ||
|
||
super().__init__( | ||
recording, | ||
waveform_extractor.ms_before, | ||
waveform_extractor.ms_after, | ||
return_output=return_output, | ||
parents=parents, | ||
) | ||
|
||
self.hanning = np.hanning(self.nbefore + self.nafter)[:, None] | ||
self._kwargs.update(dict()) | ||
|
||
def compute(self, traces, peaks, waveforms): | ||
denoised_waveforms = waveforms * self.hanning | ||
return denoised_waveforms |