Skip to content

Conversation

@mscheltienne
Copy link
Member

Closes #12840

Attempting to use paths inside of raw.filenames and epochs.filename attributes.
Seems to work except for IO 'file-likes' with FIFF. It was a surprise to me that this was even possible, so I'm having a bit of an hard time getting this to work. Ideally, a file-like should not be listed in the filenames attribute which should then be set to None. It isn't informative to display "File-Like" or the repr(...). At the same time, I don't understand how preload works with a file-like object, since it needs the file-like object in raw.filenames to load the segments when preload=False.

@mscheltienne
Copy link
Member Author

mscheltienne commented Sep 10, 2024

I'll pick this up tomorrow, let's see how much stuff breaks. I actually went further than my initial approach where now if _fiff_get_fid receives a str instead of Path, it will issue a warning. It should let me track down and remove a good portion of the existing conversion Path -> str which are left from #11473 and others. Then we can remove the warning and just cast to Path anyway for safety.

Copy link
Member

@larsoner larsoner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay @mscheltienne I finally figured it out, and added some comments along the way. Can you look then mark as ready and merge-when-green if you're also happy?

@larsoner
Copy link
Member

Failures on this and other PRs are just scipy/scipy#21623 and can be safely ignored (I think we can trust Windows pip-pre to continue being picky while the Linux pip-pre job fails in the dependencies step)

@mscheltienne mscheltienne marked this pull request as ready for review September 25, 2024 08:57
Copy link
Member Author

@mscheltienne mscheltienne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks straightforward 😆 The comments are very clear, thanks for finishing this one!

@larsoner larsoner enabled auto-merge (squash) September 25, 2024 14:07
@larsoner larsoner merged commit 10ff91a into mne-tools:main Sep 25, 2024
@mscheltienne mscheltienne deleted the paths branch September 25, 2024 14:49
@sappelhoff
Copy link
Member

We are getting an error in mne-bids that may be related to this: https://github.com/mne-tools/mne-bids/actions/runs/11081048651/job/30792323679#step:16:416

It seems to be related to the list of raws that is being created here:

raws = []
do_check_ext = not _file_like(fname)
next_fname = fname
while next_fname is not None:
raw, next_fname, buffer_size_sec = self._read_raw_file(
next_fname, allow_maxshield, preload, do_check_ext
)
do_check_ext = False
raws.append(raw)
if next_fname is not None:
if not op.exists(next_fname):
msg = (
f"Split raw file detected but next file {next_fname} "
"does not exist. Ensure all files were transferred "
"properly and that split and original files were not "
"manually renamed on disk (split files should be "
"renamed by loading and re-saving with MNE-Python to "
"preserve proper filename linkage)."
)
_on_missing(on_split_missing, msg, name="on_split_missing")
break

with the eventual error being:

copy.deepcopy(raws[0].info),
E IndexError: list index out of range

@sappelhoff
Copy link
Member

sappelhoff commented Oct 9, 2024

@mscheltienne is this expected behavior that I get an error here (on mne main)?

# %%
from pathlib import Path
import numpy as np
import mne

# create a dummy raw
sampling_freq = 1000
times = np.linspace(0, 1, sampling_freq, endpoint=False)
sine = np.sin(20 * np.pi * times)
cosine = np.cos(10 * np.pi * times)
data = np.array([sine, cosine])
info = mne.create_info(
    ch_names=["10 Hz sine", "5 Hz cosine"], ch_types=["misc"] * 2, sfreq=sampling_freq
)
raw = mne.io.RawArray(data, info)

# I want to set raw.filenames
a = Path.cwd() / "IDoNotExist.jpg"

# error: TypeError: 'tuple' object does not support item assignment
raw.filenames = (a,)

# this gives the right error: FileNotFoundError: File <...>/IDoNotExist.jpg not found.
raw.filenames = [a]

# this works correctly
#path_to_file_that_exists = ...
#raw.filenames = [path_to_file_that_exists]

# this works, no errors raised (which is ok, because I am using a private attribute to circumvent checks)
raw._filenames = (a,)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants