mne-tools / mne-python

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

BUG: read_raw_bids and raw.save round-trip with splits #11900

Open larsoner opened 11 months ago

larsoner commented 11 months ago
          ouch. it's really bad that `raw.save(my_bidspath)` generates filenames that can't be read by `read_raw_bids(my_bidspath)`.

Originally posted by @drammock in https://github.com/mne-tools/mne-python/issues/11870#issuecomment-1671754453

larsoner commented 11 months ago

I started looking into this a bit. In principle I think we could fix this on the MNE-BIDS side for read_raw_bids, but we'd be stuck with the same problem for read_epochs in MNE-Python -- epochs.save(bids_path) can write a path that has effectively been .update(split='01')'ed so read_epochs(bids_path) will fail.

I also thought about trying to fix the BIDSPath.fpath to do some magic, but that was a non-starter because it doesn't know whether you intend to read or write, and that matters. If you want to write you want the nonexistent split=None filename sent to the .save method (otherwise you end up with split-01_split-01_ in your filenames if they get split!), and if you want to read you want the split='01' version (because it actually exists).

So I think in the end one way around this problem is to put a fix in MNE in read_raw_fif and read_epochs directly. These could detect BIDSPath instances, and if present, check for bids_path existence and if not found and split is None in the BIDSPath, also check split='01'. It's not perfect but it would at least allow the round-trip to succeed for both raw.save+read_raw_fif+read_raw_bids and epochs.save+read_epochs.