braindecode.preprocessing.FilterData#

class braindecode.preprocessing.FilterData(sfreq, l_freq, h_freq, picks=None, filter_length='auto', l_trans_bandwidth='auto', h_trans_bandwidth='auto', n_jobs=None, method='fir', iir_params=None, copy=True, phase='zero', fir_window='hamming', fir_design='firwin', pad='reflect_limited', *, verbose=None)[source]#

Braindecode preprocessor wrapper for filter_data().

Filter a subset of channels.

Parameters:
  • data (ndarray, shape (..., n_times)) – The data to filter.

  • sfreq (float) – The sample frequency in Hz.

  • l_freq (float | None) – For FIR filters, the lower pass-band edge; for IIR filters, the lower cutoff frequency. If None the data are only low-passed.

  • h_freq (float | None) – For FIR filters, the upper pass-band edge; for IIR filters, the upper cutoff frequency. If None the data are only high-passed.

  • picks (list | slice | None) – Channels to include. Slices and lists of integers will be interpreted as channel indices. None (default) will pick all channels. Note that channels in info['bads'] will be included if their indices are explicitly provided. Currently this is only supported for 2D (n_channels, n_times) and 3D (n_epochs, n_channels, n_times) arrays.

  • filter_length (str | int) –

    Length of the FIR filter to use (if applicable):

    • ’auto’ (default): The filter length is chosen based on the size of the transition regions (6.6 times the reciprocal of the shortest transition band for fir_window=’hamming’ and fir_design=”firwin2”, and half that for “firwin”).

    • str: A human-readable time in units of “s” or “ms” (e.g., “10s” or “5500ms”) will be converted to that number of samples if phase="zero", or the shortest power-of-two length at least that duration for phase="zero-double".

    • int: Specified length in samples. For fir_design=”firwin”, this should not be used.

  • l_trans_bandwidth (float | str) –

    Width of the transition band at the low cut-off frequency in Hz (high pass or cutoff 1 in bandpass). Can be “auto” (default) to use a multiple of l_freq:

    min(max(l_freq * 0.25, 2), l_freq)
    

    Only used for method='fir'.

  • h_trans_bandwidth (float | str) –

    Width of the transition band at the high cut-off frequency in Hz (low pass or cutoff 2 in bandpass). Can be “auto” (default in 0.14) to use a multiple of h_freq:

    min(max(h_freq * 0.25, 2.), info['sfreq'] / 2. - h_freq)
    

    Only used for method='fir'.

  • n_jobs (int | str) – Number of jobs to run in parallel. Can be 'cuda' if cupy is installed properly and method='fir'.

  • method (str) – 'fir' will use overlap-add FIR filtering, 'iir' will use IIR forward-backward filtering (via filtfilt()).

  • iir_params (dict | None) – Dictionary of parameters to use for IIR filtering. If iir_params=None and method="iir", 4th order Butterworth will be used. For more information, see mne.filter.construct_iir_filter().

  • copy (bool) – If True, a copy of x, filtered, is returned. Otherwise, it operates on x in place.

  • phase (str) –

    Phase of the filter. When method='fir', symmetric linear-phase FIR filters are constructed with the following behaviors when method="fir":

    "zero" (default)

    The delay of this filter is compensated for, making it non-causal.

    "minimum"

    A minimum-phase filter will be constructed by decomposing the zero-phase filter into a minimum-phase and all-pass systems, and then retaining only the minimum-phase system (of the same length as the original zero-phase filter) via scipy.signal.minimum_phase().

    "zero-double"

    This is a legacy option for compatibility with MNE <= 0.13. The filter is applied twice, once forward, and once backward (also making it non-causal).

    "minimum-half"

    This is a legacy option for compatibility with MNE <= 1.6. A minimum-phase filter will be reconstructed from the zero-phase filter with half the length of the original filter.

    When method='iir', phase='zero' (default) or equivalently 'zero-double' constructs and applies IIR filter twice, once forward, and once backward (making it non-causal) using filtfilt(); phase='forward' will apply the filter once in the forward (causal) direction using lfilter().

    Added in version 0.13.

    Changed in version 1.7: The behavior for phase="minimum" was fixed to use a filter of the requested length and improved suppression.

  • fir_window (str) –

    The window to use in FIR design, can be “hamming” (default), “hann” (default in 0.13), or “blackman”.

    Added in version 0.15.

  • fir_design (str) –

    Can be “firwin” (default) to use scipy.signal.firwin(), or “firwin2” to use scipy.signal.firwin2(). “firwin” uses a time-domain design technique that generally gives improved attenuation using fewer samples than “firwin2”.

    Added in version 0.15.

  • pad (str) –

    The type of padding to use. Supports all numpy.pad() mode options. Can also be "reflect_limited", which pads with a reflected version of each vector mirrored on the first and last values of the vector, followed by zeros. Only used for method='fir'. The default is 'reflect_limited'.

    Added in version 0.15.

  • verbose (bool | str | int | None) – Control verbosity of the logging output. If None, use the default verbosity level. See the logging documentation and mne.verbose() for details. Should only be passed as a keyword argument.

Returns:

data (ndarray, shape (…, n_times)) – The filtered data.

See also

construct_iir_filter
create_filter
mne.io.Raw.filter
notch_filter
resample

Notes

Applies a zero-phase low-pass, high-pass, band-pass, or band-stop filter to the channels selected by picks.

l_freq and h_freq are the frequencies below which and above which, respectively, to filter out of the data. Thus the uses are:

  • l_freq < h_freq: band-pass filter

  • l_freq > h_freq: band-stop filter

  • l_freq is not None and h_freq is None: high-pass filter

  • l_freq is None and h_freq is not None: low-pass filter

Note

If n_jobs > 1, more memory is required as len(picks) * n_times additional time points need to be temporarily stored in memory.

For more information, see the tutorials Background information on filtering and Filtering and resampling data and mne.filter.create_filter().

Methods

fn(sfreq, l_freq, h_freq, picks=None, filter_length='auto', l_trans_bandwidth='auto', h_trans_bandwidth='auto', n_jobs=None, method='fir', iir_params=None, copy=True, phase='zero', fir_window='hamming', fir_design='firwin', pad='reflect_limited', *, verbose=None)[source]#

Filter a subset of channels.

Parameters:
  • data (ndarray, shape (..., n_times)) – The data to filter.

  • sfreq (float) – The sample frequency in Hz.

  • l_freq (float | None) – For FIR filters, the lower pass-band edge; for IIR filters, the lower cutoff frequency. If None the data are only low-passed.

  • h_freq (float | None) – For FIR filters, the upper pass-band edge; for IIR filters, the upper cutoff frequency. If None the data are only high-passed.

  • picks (list | slice | None) – Channels to include. Slices and lists of integers will be interpreted as channel indices. None (default) will pick all channels. Note that channels in info['bads'] will be included if their indices are explicitly provided. Currently this is only supported for 2D (n_channels, n_times) and 3D (n_epochs, n_channels, n_times) arrays.

  • filter_length (str | int) –

    Length of the FIR filter to use (if applicable):

    • ’auto’ (default): The filter length is chosen based on the size of the transition regions (6.6 times the reciprocal of the shortest transition band for fir_window=’hamming’ and fir_design=”firwin2”, and half that for “firwin”).

    • str: A human-readable time in units of “s” or “ms” (e.g., “10s” or “5500ms”) will be converted to that number of samples if phase="zero", or the shortest power-of-two length at least that duration for phase="zero-double".

    • int: Specified length in samples. For fir_design=”firwin”, this should not be used.

  • l_trans_bandwidth (float | str) –

    Width of the transition band at the low cut-off frequency in Hz (high pass or cutoff 1 in bandpass). Can be “auto” (default) to use a multiple of l_freq:

    min(max(l_freq * 0.25, 2), l_freq)
    

    Only used for method='fir'.

  • h_trans_bandwidth (float | str) –

    Width of the transition band at the high cut-off frequency in Hz (low pass or cutoff 2 in bandpass). Can be “auto” (default in 0.14) to use a multiple of h_freq:

    min(max(h_freq * 0.25, 2.), info['sfreq'] / 2. - h_freq)
    

    Only used for method='fir'.

  • n_jobs (int | str) – Number of jobs to run in parallel. Can be 'cuda' if cupy is installed properly and method='fir'.

  • method (str) – 'fir' will use overlap-add FIR filtering, 'iir' will use IIR forward-backward filtering (via filtfilt()).

  • iir_params (dict | None) – Dictionary of parameters to use for IIR filtering. If iir_params=None and method="iir", 4th order Butterworth will be used. For more information, see mne.filter.construct_iir_filter().

  • copy (bool) – If True, a copy of x, filtered, is returned. Otherwise, it operates on x in place.

  • phase (str) –

    Phase of the filter. When method='fir', symmetric linear-phase FIR filters are constructed with the following behaviors when method="fir":

    "zero" (default)

    The delay of this filter is compensated for, making it non-causal.

    "minimum"

    A minimum-phase filter will be constructed by decomposing the zero-phase filter into a minimum-phase and all-pass systems, and then retaining only the minimum-phase system (of the same length as the original zero-phase filter) via scipy.signal.minimum_phase().

    "zero-double"

    This is a legacy option for compatibility with MNE <= 0.13. The filter is applied twice, once forward, and once backward (also making it non-causal).

    "minimum-half"

    This is a legacy option for compatibility with MNE <= 1.6. A minimum-phase filter will be reconstructed from the zero-phase filter with half the length of the original filter.

    When method='iir', phase='zero' (default) or equivalently 'zero-double' constructs and applies IIR filter twice, once forward, and once backward (making it non-causal) using filtfilt(); phase='forward' will apply the filter once in the forward (causal) direction using lfilter().

    Added in version 0.13.

    Changed in version 1.7: The behavior for phase="minimum" was fixed to use a filter of the requested length and improved suppression.

  • fir_window (str) –

    The window to use in FIR design, can be “hamming” (default), “hann” (default in 0.13), or “blackman”.

    Added in version 0.15.

  • fir_design (str) –

    Can be “firwin” (default) to use scipy.signal.firwin(), or “firwin2” to use scipy.signal.firwin2(). “firwin” uses a time-domain design technique that generally gives improved attenuation using fewer samples than “firwin2”.

    Added in version 0.15.

  • pad (str) –

    The type of padding to use. Supports all numpy.pad() mode options. Can also be "reflect_limited", which pads with a reflected version of each vector mirrored on the first and last values of the vector, followed by zeros. Only used for method='fir'. The default is 'reflect_limited'.

    Added in version 0.15.

  • verbose (bool | str | int | None) – Control verbosity of the logging output. If None, use the default verbosity level. See the logging documentation and mne.verbose() for details. Should only be passed as a keyword argument.

Returns:

data – The filtered data.

Return type:

ndarray, shape (…, n_times)

See also

construct_iir_filter, create_filter, mne.io.Raw.filter, notch_filter, resample

Notes

Applies a zero-phase low-pass, high-pass, band-pass, or band-stop filter to the channels selected by picks.

l_freq and h_freq are the frequencies below which and above which, respectively, to filter out of the data. Thus the uses are:

  • l_freq < h_freq: band-pass filter

  • l_freq > h_freq: band-stop filter

  • l_freq is not None and h_freq is None: high-pass filter

  • l_freq is None and h_freq is not None: low-pass filter

Note

If n_jobs > 1, more memory is required as len(picks) * n_times additional time points need to be temporarily stored in memory.

For more information, see the tutorials Background information on filtering and Filtering and resampling data and mne.filter.create_filter().