You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
removeTrend produces different values in PyPREP than MATLAB PREP #55
Splitting this off from #49 to keep things organized. Basically, the removeTrend function (which essentially just runs a highpass filter to remove frequencies below 1 Hz) produces different values in PyPREP's implementation (by default, a wrapper around mne.filter.filter_data) than in MATLAB PREP's implementation (by default, a wrapper around EEGLAB's pop_eegfiltnew).
For comparison I loaded the same .set file into both EEGLAB and MNE, applied removeTrend to both, then re-saved the .set from EEGLAB so I could load it into MNE and compare. The PSD plots for both look identical to me, but the numeric values themselves via raw._data look quite different:
Comparisons of MATLAB and PyPREP values (click to unhide)
First sample of first 10 channels in MATLAB PREP before trend removal:
Since the plots look the same the differences might not practically matter all that much, but if we want to be able to do formal (or informal) 1:1 comparisons of PyPREP results to MatPREP, having identical results from this function would be helpful, especially since this always happens before any of the NoisyChannels operations.
Interesting, IMHO if there is "not much of a difference / impact", then I think we should try to follow the matprep "style". That is, adjust MNE's filter to be in line with EEGLAB's filter ...
Unless it's too much of an effort ... or too complicated / non-robust - then I think we should stay with what we have.
@sappelhoff@yjmantilla Just did a sanity check now that the quantile PR is written, and it looks like the the median channel correlations from find_bads_by_correlation still differ slightly depending on whether MATLAB's removeTrend or PyPREP's removeTrend is used:
Median maximum correlations for each channel (MATLAB removeTrend):
(Note that these match MATLAB's internal values for this step exactly)
As you can see, the values are only off by ~0.001 but it still causes difficulties for MATLAB v. PyPREP comparisons, especially if we want to eventually do them directly in CI.
so are you suggesting that there is - apart from the quantile "bug" - another bug? Or is this rather numerical differences between some low level programming language functions?
I'm suggesting that there's some minor difference in the settings or implementation of MNE's FIR filter vs EEGLAB's which leads to slightly different results. The main thing I've seen to suggest this is that the verbose input for both suggests that MNE's filter has a slightly different filter length than EEGLAB's.
Just as a quick note on this before I forget: the 50 Hz lowpass filter that happens as part of the HF noise detector in NoisyChannels produces identical results to the one in MATLAB PREP. The main difference I can see from the detrend (other than the filter type) is that the 50 Hz lowpass is done in PyPREP with a custom pyprep.utils.filter_design function.
@sappelhoff I'm having some trouble making sense of filter_design's docstring, but based on the code do you think it could be used to do the 1 Hz highpass detrend? If so, do you know what syntax I'd need to use for that? If it matches EEGLAB with the lowpass, maybe it'll match it with the highpass as well.
(I think the filter "kernel" are the coefficientes , but Im not sure)
As you can see, the values are only off by ~0.001 but it still causes difficulties for MATLAB v. PyPREP comparisons, especially if we want to eventually do them directly in CI.
Using np.isclose with a 0.001 tolerance wouldn't help here?
The main difference I can see from the detrend (other than the filter type) is that the 50 Hz lowpass is done in PyPREP with a custom pyprep.utils.filter_design function.
yes, I saw that as well - but I have never used this myself. That part of the code was written by @AamnaLawrence and @Nick3151 as far as I remember.
@a-hurst Hi, sorry for not making the docstring clear. Actually the filter_design function was not written by me... but as far as I know the function simply designs an FIR filter by giving the frequency responses(amplitude) at a few given frequencies and returns the kernel(which shoule be the coefficients in time domain)
This is a funcion which we think could skip the intrinsic python implementation of a filter and may generate outputs consistent with Matlab's version.
@sappelhoff@yjmantilla Figured this out! Basically, EEGLAB's filtering math calculates the number of filter samples slightly differently than MNE: giving a length of 847 instead of MNE's 845. Unfortunately it's not as simple as passing the MATLAB number to mne.filter.filter_data's filter_length argument, since for whatever reason (seems deliberate?) that just creates a filter of length 845 and pads both ends with zeros to get 847.
Anyway, if you calculate the filter order and cutoff frequencies using MATLAB's logic and pass those to SciPy's firwin (what MNE uses internally anyway), it generates an identical filter to MatPREP that produces identical results when applied to the EEG data! Putting together a PR now.
With this fix in place, comparing the full re-reference results between MatPREP and PyPREP will be much easier.
Reacted by Stefan Appelhoff and Yorguin José Mantilla Ramos
Activity
yjmantilla commentedon Apr 3, 2021
Yeap, if the plot looks the same I think this is not much of an issue. See #21 , there I took a long rambling about the numerical comparisons.
I actually did some local translations of pop_eegfiltnew, Im not sure it is really worthwhile but i can share them to you if you would like that.
sappelhoff commentedon Apr 4, 2021
Interesting, IMHO if there is "not much of a difference / impact", then I think we should try to follow the matprep "style". That is, adjust MNE's filter to be in line with EEGLAB's filter ...
Unless it's too much of an effort ... or too complicated / non-robust - then I think we should stay with what we have.
a-hurst commentedon Apr 8, 2021
@sappelhoff @yjmantilla Just did a sanity check now that the quantile PR is written, and it looks like the the median channel correlations from
find_bads_by_correlationstill differ slightly depending on whether MATLAB's removeTrend or PyPREP's removeTrend is used:Median maximum correlations for each channel (MATLAB removeTrend):
(Note that these match MATLAB's internal values for this step exactly)Median maximum correlations for each channel (PyPREP's removeTrend):
As you can see, the values are only off by ~0.001 but it still causes difficulties for MATLAB v. PyPREP comparisons, especially if we want to eventually do them directly in CI.
sappelhoff commentedon Apr 8, 2021
so are you suggesting that there is - apart from the quantile "bug" - another bug? Or is this rather numerical differences between some low level programming language functions?
a-hurst commentedon Apr 8, 2021
I'm suggesting that there's some minor difference in the settings or implementation of MNE's FIR filter vs EEGLAB's which leads to slightly different results. The main thing I've seen to suggest this is that the verbose input for both suggests that MNE's filter has a slightly different filter length than EEGLAB's.
a-hurst commentedon Apr 12, 2021
Just as a quick note on this before I forget: the 50 Hz lowpass filter that happens as part of the HF noise detector in NoisyChannels produces identical results to the one in MATLAB PREP. The main difference I can see from the detrend (other than the filter type) is that the 50 Hz lowpass is done in PyPREP with a custom
pyprep.utils.filter_designfunction.@sappelhoff I'm having some trouble making sense of
filter_design's docstring, but based on the code do you think it could be used to do the 1 Hz highpass detrend? If so, do you know what syntax I'd need to use for that? If it matches EEGLAB with the lowpass, maybe it'll match it with the highpass as well.yjmantilla commentedon Apr 12, 2021
Theoretically one could do spectral inversion I think.
From: https://mpastell.com/2010/01/18/fir-with-scipy/
(I think the filter "kernel" are the coefficientes , but Im not sure)
Using np.isclose with a 0.001 tolerance wouldn't help here?
sappelhoff commentedon Apr 12, 2021
yes, I saw that as well - but I have never used this myself. That part of the code was written by @AamnaLawrence and @Nick3151 as far as I remember.
Nick3151 commentedon Apr 12, 2021
@a-hurst Hi, sorry for not making the docstring clear. Actually the filter_design function was not written by me... but as far as I know the function simply designs an FIR filter by giving the frequency responses(amplitude) at a few given frequencies and returns the kernel(which shoule be the coefficients in time domain)
This is a funcion which we think could skip the intrinsic python implementation of a filter and may generate outputs consistent with Matlab's version.
Hope it helps, good luck!
a-hurst commentedon Apr 26, 2021
@sappelhoff @yjmantilla Figured this out! Basically, EEGLAB's filtering math calculates the number of filter samples slightly differently than MNE: giving a length of 847 instead of MNE's 845. Unfortunately it's not as simple as passing the MATLAB number to
mne.filter.filter_data'sfilter_lengthargument, since for whatever reason (seems deliberate?) that just creates a filter of length 845 and pads both ends with zeros to get 847.Anyway, if you calculate the filter order and cutoff frequencies using MATLAB's logic and pass those to SciPy's
firwin(what MNE uses internally anyway), it generates an identical filter to MatPREP that produces identical results when applied to the EEG data! Putting together a PR now.With this fix in place, comparing the full re-reference results between MatPREP and PyPREP will be much easier.