-
Notifications
You must be signed in to change notification settings - Fork 196
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
Alternative implementation of causal filter in filter.py #3133
Closed
JuanPimientoCaicedo
wants to merge
17
commits into
SpikeInterface:main
from
JuanPimientoCaicedo:main
Closed
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f5cbc4d
Alternative implementation of causal filter in filter.py
JuanPimientoCaicedo e595458
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 500410f
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo e74f5c2
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo 1a7149d
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo c6e3cbc
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo 377808a
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo ef402be
Hard code causal mode in CausalFilter
JuanPimientoCaicedo 892b7b6
change self._kwargs causal_mode = true
JuanPimientoCaicedo bd4909f
Apply suggestions from code review
alejoe91 25d25fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7c5956e
change causal mode for direction forward-backward
JuanPimientoCaicedo b5095ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3f1024b
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo 1d03d5e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3efa5f5
Update src/spikeinterface/preprocessing/filter.py
JuanPimientoCaicedo 8c10bcc
[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 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 | ||
---|---|---|---|---|
|
@@ -27,7 +27,8 @@ class FilterRecording(BasePreprocessor): | |||
Generic filter class based on: | ||||
|
||||
* scipy.signal.iirfilter | ||||
* scipy.signal.filtfilt or scipy.signal.sosfilt | ||||
* scipy.signal.filtfilt or scipy.signal.sosfiltfilt when direction = "forward-backward" | ||||
* scipy.signal.lfilt or scipy.signal.sosfilt | ||||
|
||||
BandpassFilterRecording is built on top of it. | ||||
|
||||
|
@@ -56,6 +57,10 @@ class FilterRecording(BasePreprocessor): | |||
- numerator/denominator : ("ba") | ||||
ftype : str, default: "butter" | ||||
Filter type for `scipy.signal.iirfilter` e.g. "butter", "cheby1". | ||||
direction : "forward" | "backward" | "forward-backward", default: "forward-backward" | ||||
Direction of filtering: | ||||
- forward and backward filter in just one direction, creating phase shifts in the signal. | ||||
- forward-backward filters in both directions, a zero-phase filtering. | ||||
|
||||
Returns | ||||
------- | ||||
|
@@ -77,6 +82,7 @@ def __init__( | |||
add_reflect_padding=False, | ||||
coeff=None, | ||||
dtype=None, | ||||
direction="forward-backward", | ||||
): | ||||
import scipy.signal | ||||
|
||||
|
@@ -108,7 +114,13 @@ def __init__( | |||
for parent_segment in recording._recording_segments: | ||||
self.add_recording_segment( | ||||
FilterRecordingSegment( | ||||
parent_segment, filter_coeff, filter_mode, margin, dtype, add_reflect_padding=add_reflect_padding | ||||
parent_segment, | ||||
filter_coeff, | ||||
filter_mode, | ||||
margin, | ||||
dtype, | ||||
add_reflect_padding=add_reflect_padding, | ||||
direction=direction, | ||||
) | ||||
) | ||||
|
||||
|
@@ -123,14 +135,25 @@ def __init__( | |||
margin_ms=margin_ms, | ||||
add_reflect_padding=add_reflect_padding, | ||||
dtype=dtype.str, | ||||
direction=direction, | ||||
) | ||||
|
||||
|
||||
class FilterRecordingSegment(BasePreprocessorSegment): | ||||
def __init__(self, parent_recording_segment, coeff, filter_mode, margin, dtype, add_reflect_padding=False): | ||||
def __init__( | ||||
self, | ||||
parent_recording_segment, | ||||
coeff, | ||||
filter_mode, | ||||
margin, | ||||
dtype, | ||||
add_reflect_padding=False, | ||||
direction="forward-backward", | ||||
): | ||||
BasePreprocessorSegment.__init__(self, parent_recording_segment) | ||||
self.coeff = coeff | ||||
self.filter_mode = filter_mode | ||||
self.direction = direction | ||||
self.margin = margin | ||||
self.add_reflect_padding = add_reflect_padding | ||||
self.dtype = dtype | ||||
|
@@ -152,11 +175,24 @@ def get_traces(self, start_frame, end_frame, channel_indices): | |||
|
||||
import scipy.signal | ||||
|
||||
if self.filter_mode == "sos": | ||||
filtered_traces = scipy.signal.sosfiltfilt(self.coeff, traces_chunk, axis=0) | ||||
elif self.filter_mode == "ba": | ||||
b, a = self.coeff | ||||
filtered_traces = scipy.signal.filtfilt(b, a, traces_chunk, axis=0) | ||||
if self.direction == "forward-backward": | ||||
if self.filter_mode == "sos": | ||||
filtered_traces = scipy.signal.sosfiltfilt(self.coeff, traces_chunk, axis=0) | ||||
elif self.filter_mode == "ba": | ||||
b, a = self.coeff | ||||
filtered_traces = scipy.signal.filtfilt(b, a, traces_chunk, axis=0) | ||||
else: | ||||
if self.direction == "backward": | ||||
traces_chunk = np.flip(traces_chunk, axis=0) | ||||
|
||||
if self.filter_mode == "sos": | ||||
filtered_traces = scipy.signal.sosfilt(self.coeff, traces_chunk, axis=0) | ||||
elif self.filter_mode == "ba": | ||||
b, a = self.coeff | ||||
filtered_traces = scipy.signal.lfilt(b, a, traces_chunk, axis=0) | ||||
|
||||
if self.direction == "backward": | ||||
filtered_traces = np.flip(filtered_traces, axis=0) | ||||
|
||||
if right_margin > 0: | ||||
filtered_traces = filtered_traces[left_margin:-right_margin, :] | ||||
|
@@ -291,14 +327,75 @@ def __init__(self, recording, freq=3000, q=30, margin_ms=5.0, dtype=None): | |||
self._kwargs = dict(recording=recording, freq=freq, q=q, margin_ms=margin_ms, dtype=dtype.str) | ||||
|
||||
|
||||
class CausalFilter(FilterRecording): | ||||
""" | ||||
Performs causal filtering using: | ||||
* scipy.signal.lfilt or scipy.signal.sosfilt | ||||
|
||||
Parameters | ||||
---------- | ||||
recording : Recording | ||||
The recording extractor to be re-referenced | ||||
band : float or list, default: [300.0, 6000.0] | ||||
If float, cutoff frequency in Hz for "highpass" filter type | ||||
If list. band (low, high) in Hz for "bandpass" filter type | ||||
margin_ms : float | ||||
Margin in ms on border to avoid border effect | ||||
dtype : dtype or None | ||||
The dtype of the returned traces. If None, the dtype of the parent recording is used | ||||
direction : "forward" | "backward", default: "forward" | ||||
when causal_mode = True, defines the direction of the filtering | ||||
|
||||
Returns | ||||
------- | ||||
filter_recording : CausalFilterRecording | ||||
The causal-filtered recording extractor object | ||||
|
||||
{} | ||||
|
||||
""" | ||||
|
||||
name = "causal_filter" | ||||
|
||||
def __init__( | ||||
self, | ||||
recording, | ||||
band=[300.0, 6000.0], | ||||
margin_ms=5.0, | ||||
dtype=None, | ||||
direction="forward", | ||||
**filter_kwargs, | ||||
): | ||||
FilterRecording.__init__( | ||||
self, | ||||
recording, | ||||
band=band, | ||||
margin_ms=margin_ms, | ||||
dtype=dtype, | ||||
direction=direction, | ||||
**filter_kwargs, | ||||
) | ||||
dtype = fix_dtype(recording, dtype) | ||||
self._kwargs = dict( | ||||
recording=recording, | ||||
band=band, | ||||
margin_ms=margin_ms, | ||||
dtype=dtype.str, | ||||
direction=direction, | ||||
) | ||||
self._kwargs.update(filter_kwargs) | ||||
JuanPimientoCaicedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
|
||||
# functions for API | ||||
filter = define_function_from_class(source_class=FilterRecording, name="filter") | ||||
bandpass_filter = define_function_from_class(source_class=BandpassFilterRecording, name="bandpass_filter") | ||||
notch_filter = define_function_from_class(source_class=NotchFilterRecording, name="notch_filter") | ||||
highpass_filter = define_function_from_class(source_class=HighpassFilterRecording, name="highpass_filter") | ||||
causal_filter = define_function_from_class(source_class=CausalFilter, name="causal_filter") | ||||
JuanPimientoCaicedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
bandpass_filter.__doc__ = bandpass_filter.__doc__.format(_common_filter_docs) | ||||
highpass_filter.__doc__ = highpass_filter.__doc__.format(_common_filter_docs) | ||||
alejoe91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
causal_filter.__doc__ = causal_filter.__doc__.format(_common_filter_docs) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We don't need this anymore since we redefined the docstring |
||||
|
||||
|
||||
def fix_dtype(recording, dtype): | ||||
|
Oops, something went wrong.
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.
Why having this class if the FilterRecording already handle it with specific options ?
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.
I guess it is for the same reason highpass_filter and bandpass_filter exist. Although it is true that in causal filtering, the users might need to provide some extra parameters compared with these other subclasses...
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.
I agree with @samuelgarcia we don't need the class in this case, since the
CasualFilter
class is really just instantiating theFilterRecording
class (thefix_dtype
andkwargs
will be handled by theFilterRecording
class).My suggestion is to:
causal_filter
function (see my comment)