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
Rank estimated from data saved to disk is incorrect if interpolated channels are present #11523
Hi, I've noticed that If some channels have been interpolated and the data is saved to disk and then reloaded the estimated rank is not correct. Maybe it has to do with numerical precision when saving the data as in #7727?
Maybe it has to do with numerical precision when saving the data as in #7727?
Yes I think so. I'm not sure there is much we can do about it unfortunately. My solution/workaround would be to use mne.compute_rank on the Epochs (or raw) directly, then pass this rank to compute_covariance in both cases
compute_rank would be a natural place but it's not very discoverable. compute_covariance Notes is where it comes up a lot. It also occurs with EEG+ICA preprocessing, though -- and maybe more often there? -- so an ICA tutorial would be another good place.
It also might make sense to have a dedicated tutorial to rank issues, since it happens in multiple places, including maxwell filtering (esp. with head movement compensation and lots of head movement).
I'm seeing that it has a considerable impact on source modelling results so I think that making it evident in the docs is important.
Here are the results from the code pasted below:
original:
reloaded:
Non sure if it might make sense or be viable, but maybe keeping track of the channels that have been interpolated in info and then re-interpolating them on the fly inside compute_rank or compute_covariance to get a better estimate would be a solution.
Code:
importmneimportos.pathasopfrommne.datasetsimportsamplefrommne.minimum_normimportmake_inverse_operator, apply_inversedata_path=sample.data_path()
raw_fname=data_path/'MEG'/'sample'/'sample_audvis_filt-0-40_raw.fif'raw=mne.io.read_raw_fif(raw_fname) # already has an average referenceevents=mne.find_events(raw, stim_channel='STI 014')
event_id=dict(aud_l=1) # event trigger and conditionstmin=-0.2# start of each epoch (200ms before the trigger)tmax=0.5# end of each epoch (500ms after the trigger)raw.info['bads'] = ['MEG 2443', 'EEG 053']
baseline= (None, 0) # means from the first instant to t = 0reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)
epochs=mne.Epochs(raw, events, event_id, tmin, tmax, proj=True,
baseline=baseline, reject=reject)
epochs.load_data()
epochs.pick_types(eeg=True)
epochs.info['bads'] = ['EEG 057', 'EEG 058', 'EEG 059', 'EEG 060']
epochs.interpolate_bads()
fname_save='test_sm-epo.fif'epochs.save(fname_save, overwrite=True)
epochs_reload=mne.read_epochs(fname_save)
fname_fwd=data_path/'MEG'/'sample'/'sample_audvis-eeg-oct-6-fwd.fif'fwd=mne.read_forward_solution(fname_fwd)
method="dSPM"snr=3.lambda2=1./snr**2stcs= {'original': [], 'reload': []}
forepo, kindinzip([epochs, epochs_reload], ['original', 'reload']):
noise_cov=mne.compute_covariance(epo, tmax=0., method='shrunk', verbose=True)
fig_cov, fig_spectra=mne.viz.plot_cov(noise_cov, raw.info)
evoked=epo.average()
inverse_operator=make_inverse_operator(evoked.info, fwd, noise_cov, loose=0.2, depth=0.8)
stc, residual=apply_inverse(evoked, inverse_operator, lambda2,
method=method, pick_ori=None,
return_residual=True, verbose=True)
stc.plot(subjects_dir=op.join(data_path, 'subjects'), initial_time=0.1)
stcs[kind] =stc
compute_rank would be a natural place but it's not very discoverable. compute_covariance Notes is where it comes up a lot. It also occurs with EEG+ICA preprocessing, though -- and maybe more often there? -- so an ICA tutorial would be another good place.
@larsoner are all these cases ones where the issue only happens on save-load round-trips? If so, I can add a reusable note to the docdict and include it in the notes section of Epochs.save and ICA.save (does it also make sense for Raw.save and Evoked.save? folks can interpolate those too)
It also might make sense to have a dedicated tutorial to rank issues, since it happens in multiple places, including maxwell filtering (esp. with head movement compensation and lots of head movement).
This might still be a good idea, I'll make a new issue about that.
@larsoner are all these cases ones where the issue only happens on save-load round-trips? If so, I can add a reusable note to the docdict and include it in the notes section of Epochs.save and ICA.save (does it also make sense for Raw.save and Evoked.save? folks can interpolate those too)
I think usually -- Maxwell filtering is an exception but we have #4676 to track that more or less. Raw and Evoked would also be good places.
Activity
larsoner commentedon Mar 2, 2023
Yes I think so. I'm not sure there is much we can do about it unfortunately. My solution/workaround would be to use
mne.compute_rankon the Epochs (or raw) directly, then pass thisranktocompute_covariancein both casesdrammock commentedon Mar 3, 2023
@larsoner any thoughts on where in the docs we could add guidance about this?
larsoner commentedon Mar 3, 2023
compute_rankwould be a natural place but it's not very discoverable.compute_covarianceNotes is where it comes up a lot. It also occurs with EEG+ICA preprocessing, though -- and maybe more often there? -- so an ICA tutorial would be another good place.It also might make sense to have a dedicated tutorial to rank issues, since it happens in multiple places, including maxwell filtering (esp. with head movement compensation and lots of head movement).
ezemikulan commentedon Mar 6, 2023
I'm seeing that it has a considerable impact on source modelling results so I think that making it evident in the docs is important.
Here are the results from the code pasted below:
original:

reloaded:

Non sure if it might make sense or be viable, but maybe keeping track of the channels that have been interpolated in info and then re-interpolating them on the fly inside
compute_rankorcompute_covarianceto get a better estimate would be a solution.Code:
agramfort commentedon Mar 7, 2023
@ezemikulan use
epo.save(fname_save, fmt="double", overwrite=True)it is twice bigger on disk but it fixes your problem.
ezemikulan commentedon Mar 7, 2023
Great, thanks
drammock commentedon Mar 7, 2023
@larsoner are all these cases ones where the issue only happens on save-load round-trips? If so, I can add a reusable note to the docdict and include it in the notes section of
Epochs.saveandICA.save(does it also make sense forRaw.saveandEvoked.save? folks can interpolate those too)This might still be a good idea, I'll make a new issue about that.
larsoner commentedon Mar 7, 2023
I think usually -- Maxwell filtering is an exception but we have #4676 to track that more or less. Raw and Evoked would also be good places.