Skip to content

ENH: Support file-like objects in mne.io.read_raw (#14514) - #14603

Open
ADITYASINGH1206 wants to merge 1 commit into
mne-tools:mainfrom
ADITYASINGH1206:enh/bytesio-raw-io
Open

ADITYASINGH1206 wants to merge 1 commit into
mne-tools:mainfrom
ADITYASINGH1206:enh/bytesio-raw-io

Conversation

@ADITYASINGH1206

Copy link
Copy Markdown

Closes #14514

BaseRaw.save already gained support for binary file-like objects in #14516, but mne.io.read_raw still failed in split_name_ext because it passed the stream directly to Path(fname).

This PR adds file-like detection to mne/io/_read_raw.py:

  • Check getattr(fname, "name", None) first so open file handles with a known extension dispatch to their existing reader.
  • For unnamed seekable binary streams such as io.BytesIO, peek at the header magic bytes (.fif, .edf, .bdf, .gdf) and restore the stream position before dispatching.
  • Raise a clear ValueError in _read_unsupported for unrecognized file-like objects instead of TypeError from Path(fname).
  • Update read_raw type annotations and docstring, and add test_read_raw_file_like covering BytesIO round-trip (raw.save -> read_raw), open file handles, EDF streams, and unsupported streams.

Testing

  • .venv/bin/pytest -p no:pytest-qt mne/io/tests/test_read_raw.py -v (15 passed, 5 skipped requiring external testing dataset)
  • ruff check and ruff format --check passed on both modified files

This branch has not been deployed

No deployments
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.

ENH: Support io.BytesIO in BaseRaw.save and mne.io.read_raw

1 participant