Skip to content

Allow concatenating BaseRaw objects #13263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/devel/13263.newfeature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
It is now possible to concatenate raw objects with :func:`mne.concatenate_raws` as long as they inherit from :class:`~mne.io.BaseRaw`, even if their specific types differ (e.g., :class:`~mne.io.Raw` and :class:`~mne.io.RawArray`), by `Clemens Brunner`_.
5 changes: 3 additions & 2 deletions mne/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3104,8 +3104,9 @@ def _write_raw_buffer(fid, buf, cals, fmt):
def _check_raw_compatibility(raw):
"""Ensure all instances of Raw have compatible parameters."""
for ri in range(1, len(raw)):
if not isinstance(raw[ri], type(raw[0])):
raise ValueError(f"raw[{ri}] type must match")
if not isinstance(raw[ri], BaseRaw):
if type(raw[ri]) is not type(raw[0]):
raise ValueError(f"raw[{ri}] type must match")
for key in ("nchan", "sfreq"):
a, b = raw[ri].info[key], raw[0].info[key]
if a != b:
Expand Down
16 changes: 16 additions & 0 deletions mne/io/fiff/tests/test_raw_fiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ def test_concatenate_raws_order():
assert np.all(ch0 == 0)


def test_concatenate_raws_different_subtypes(tmp_path):
"""Test concatenating raws with different subtypes."""
sfreq = 100.0
ch_names = ["EEG 001", "EEG 002"]
ch_types = ["eeg"] * 2
info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types=ch_types)
data = np.random.randn(len(ch_names), 1000)

raw_array = RawArray(data, info)
raw_array.save(tmp_path / "temp_raw.fif", overwrite=True)
raw_fiff = read_raw_fif(tmp_path / "temp_raw.fif", preload=True)

with pytest.warns(RuntimeWarning, match="raw files do not all have the same"):
concatenate_raws([raw_fiff, raw_array])


@testing.requires_testing_data
@pytest.mark.parametrize(
"mod",
Expand Down
Loading