mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.7k stars 1.31k forks source link

mne.preprocessing.find_bad_channels_maxwell()'s crosstalk_file kwarg cannot be pathlib.Path #7466

Closed hoechenberger closed 4 years ago

hoechenberger commented 4 years ago

Describe the bug

mne.preprocessing.find_bad_channels_maxwell() features a crosstalk_file kwarg. Unlike in many (all?) other places in MNE, this argument cannot be a pathlib.Path object, and is required to be a string.

Steps to reproduce

import pathlib
import mne

sample_data_folder = pathlib.Path(mne.datasets.sample.data_path())
sample_data_raw_file = (sample_data_folder / 'MEG' / 'sample' / 
                        'sample_audvis_raw.fif')
fine_cal_file = sample_data_folder / 'SSS' / 'sss_cal_mgh.dat'
crosstalk_file = sample_data_folder / 'SSS' / 'ct_sparse_mgh.fif'

raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False)
raw.crop(tmax=60).load_data()

mne.preprocessing.find_bad_channels_maxwell(raw, cross_talk=crosstalk_file,
                                            calibration=fine_cal_file) 

Expected results

Scanning for bad channels in 12 intervals (5.0 sec) ...
    Bad MEG channels being reconstructed: ['MEG 2443']
    Processing 204 gradiometers and 102 magnetometers
    Low-pass filtering data at 40.0 Hz
    Static bad channels: []
[done]
[]

Actual results

~/Development/mne-python/mne/preprocessing/maxwell.py in _maxwell_filter(***failed resolving arguments***)
    316     #
    317     if cross_talk is not None:
--> 318         sss_ctc = _read_ctc(cross_talk)
    319         ctc_chs = sss_ctc['proj_items_chs']
    320         meg_ch_names = [info['ch_names'][p] for p in meg_picks]

~/Development/mne-python/mne/io/proc_history.py in _read_ctc(fname)
    167     """Read cross-talk correction matrix."""
    168     if not isinstance(fname, str) or not op.isfile(fname):
--> 169         raise ValueError('fname must be a file that exists, not %s' % fname)
    170     f, tree, _ = fiff_open(fname)
    171     with f as fid:

ValueError: fname must be a file that exists, not /Users/hoechenberger/mne_data/MNE-sample-data/SSS/ct_sparse_mgh.fif

Additional information

It works as expected when converting crosstalk_file to a string before passing it to find_bad_channels_maxwell().

agramfort commented 4 years ago

PR welcome with the fix

larsoner commented 4 years ago

I'll roll it into my open PR