Open jasmainak opened 1 year ago
Hi everyone,
I think the issue stems from the _firwin_design
function, specifically these lines:
transition = (prev_freq - this_freq) / 2.0
this_N = int(round(_length_factors[window] / transition))
this_N += 1 - this_N % 2 # make it odd
if this_N > N:
raise ValueError(
f"The requested filter length {N} is too short for the requested "
f"{transition * sfreq / 2.0:0.2f} Hz transition band, which "
f"requires {this_N} samples"
)
prev_freq
and this_freq
are normalized by the Nyquist frequency, as seen in _construct_fir_filter()
in filter.py
at line 485:
# normalize frequencies
freq = np.array(freq) / (sfreq / 2.0)
The line transition = (prev_freq - this_freq) / 2.0
seems unusual. However, this_N
is still calculated correctly. This is because in the equation transition width = 3.3/N
(where 3.3 is for the Hamming window), the transition
is actually normalized by the sampling frequency and not the Nyquist frequency [refer to the calculation at the end of page 182]. When the transition width is normalized with the Nyquist frequency, the equation should be transition width = 6.6/N
[reference].
Therefore, prev_freq - this_freq
is normalized by the Nyquist frequency, but the equation needs normalization with the sampling frequency, which is why we divide by 2.
We can modify the code as follows:
transition = prev_freq - this_freq # transition width normalized to Nyquist frequency
transition_norm_fs = transition / 2
this_N = int(round(_length_factors[window] / transition_norm_fs))
this_N += 1 - this_N % 2 # make it odd
if this_N > N:
raise ValueError(
f"The requested filter length {N} is too short for the requested "
f"{transition * sfreq / 2.0:0.2f} Hz transition band, which "
f"requires {this_N} samples"
)
or
transition = (prev_freq - this_freq) / 2.0 # normalization from Nyquist freq. to sampling freq.
this_N = int(round(_length_factors[window] / transition))
this_N += 1 - this_N % 2 # make it odd
if this_N > N:
raise ValueError(
f"The requested filter length {N} is too short for the requested "
f"{transition * sfreq:0.2f} Hz transition band, which "
f"requires {this_N} samples"
)
@sena-neuro that sounsd like it's probably correct (and the second one looks better), would you be up for making a PR to fix it?
Sure!
Description of the problem
When I try to fiddle with
filter_length
MNE gives an incorrect error messageSteps to reproduce
Link to data
No response
Expected results
It should say that the the transition bandwidth is 10 Hz or 5 Hz (if only considering upper/lower) but it reports 2.5 Hz.
Actual results
Additional information
Platform: Linux-3.10.0-1160.76.1.el7.x86_64-x86_64-with-glibc2.10 Python: 3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 19:08:05) [GCC 7.5.0] Executable: /autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/bin/python3.8 CPU: x86_64: 64 cores Memory: 125.4 GB
Exception ignored on calling ctypes callback function: <function _ThreadpoolInfo._find_modules_with_dl_iterate_phdr..match_module_callback at 0x7fd4f91fc820>
Traceback (most recent call last):
File "/autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/lib/python3.8/site-packages/threadpoolctl.py", line 400, in match_module_callback
self._make_module_from_path(filepath)
File "/autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/lib/python3.8/site-packages/threadpoolctl.py", line 515, in _make_module_from_path
module = module_class(filepath, prefix, user_api, internal_api)
File "/autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/lib/python3.8/site-packages/threadpoolctl.py", line 606, in init
self.version = self.get_version()
File "/autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/lib/python3.8/site-packages/threadpoolctl.py", line 646, in get_version
config = get_config().split()
AttributeError: 'NoneType' object has no attribute 'split'
mne: 1.4.dev0
numpy: 1.23.0 {OpenBLAS 0.3.12 with 1 thread}
scipy: 1.5.3
matplotlib: 3.3.3 {backend=Qt5Agg}
sklearn: 0.23.2 numba: Not found nibabel: 3.2.1 nilearn: 0.7.0 dipy: 1.3.0 openmeeg: Not found cupy: Not found pandas: 1.1.5 pyvista: 0.36.1 {OpenGL 4.5.0 NVIDIA 455.45.01 via Quadro P5000/PCIe/SSE2} pyvistaqt: 0.9.0 ipyvtklink: Not found vtk: 9.0.1 qtpy: 2.0.1 {PyQt5=5.12.9} ipympl: Not found /autofs/space/meghnn_001/users/mjas/anaconda3/envs/mne/lib/python3.8/site-packages/pyqtgraph/colors/palette.py:1: RuntimeWarning: PyQtGraph supports Qt version >= 5.15, but 5.12.9 detected. from ..Qt import QtGui pyqtgraph: 0.13.1 pooch: v1.5.2
mne_bids: Not found mne_nirs: Not found mne_features: Not found mne_qt_browser: 0.4.0 mne_connectivity: Not found mne_icalabel: Not found