Skip to content

The order of raw1.info["bads"] should not matter when concatenating with raw0.info["bads"] #11501

@moritz-gerster

Description

@moritz-gerster

Description of the problem

I have a list of raws and combine them using raw_concat = concatenate_raws(raws). However, I got the following error:

Exception has occurred: ValueError       (note: full exception trace is shown but execution is paused at: _run_module_as_main)
raw[1].info[bads] must match:
['LFP_R_4_STN_BS_RSTN', 'LFP_R_4_STN_BS'] != ['LFP_R_4_STN_BS', 'LFP_R_4_STN_BS_RSTN']
  File "/Users/moritzgerster/Library/CloudStorage/Dropbox/Code/BIDS_Neumann_ECoG_LFP/scripts/utils.py", line 482, in _combine_raws
    raw_concat = concatenate_raws(raws)
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "<decorator-gen-238>", line 12, in concatenate_raws
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/site-packages/mne/io/base.py", line 2542, in concatenate_raws
    _ensure_infos_match(info1=raws[0].info, info2=raw.info,
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/site-packages/mne/io/meas_info.py", line 2884, in _ensure_infos_match
    raise ValueError(f'{name}.info[\'nchan\'] must match')
ValueError: raws[1].info['nchan'] must match

During handling of the above exception, another exception occurred:

  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/site-packages/mne/io/base.py", line 2495, in _check_raw_compatibility
    raise ValueError(
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/site-packages/mne/io/base.py", line 1641, in append
    _check_raw_compatibility(all_raws)
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/site-packages/mne/io/base.py", line 2551, in concatenate_raws
    raws[0].append(raws[1:], preload)
  File "<decorator-gen-238>", line 12, in concatenate_raws
  File "/Users/moritzgerster/Library/CloudStorage/Dropbox/Code/BIDS_Neumann_ECoG_LFP/scripts/utils.py", line 492, in _combine_raws
    raw_concat = concatenate_raws(raws)
                 ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moritzgerster/Library/CloudStorage/Dropbox/Code/BIDS_Neumann_ECoG_LFP/scripts/_02_fooof_slopes.py", line 59, in get_fitted_spectra
    raw_concat = _combine_raws(bids_paths, save_path=bids_path)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moritzgerster/Library/CloudStorage/Dropbox/Code/BIDS_Neumann_ECoG_LFP/scripts/_02_fooof_slopes.py", line 36, in psd_fooof_to_dataframe
    dics = get_fitted_spectra(paths_to_sub_in_cond)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moritzgerster/Library/CloudStorage/Dropbox/Code/BIDS_Neumann_ECoG_LFP/scripts/main.py", line 28, in <module>
    psd_fooof_to_dataframe()
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/Users/moritzgerster/anaconda3/envs/bids_neu/lib/python3.11/runpy.py", line 198, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: raw[1].info[bads] must match:
['LFP_R_4_STN_BS_RSTN', 'LFP_R_4_STN_BS'] != ['LFP_R_4_STN_BS', 'LFP_R_4_STN_BS_RSTN']

As you can see the bad channels
['LFP_R_4_STN_BS_RSTN', 'LFP_R_4_STN_BS'] != ['LFP_R_4_STN_BS', 'LFP_R_4_STN_BS_RSTN']
are identical in both cases, only their order is wrong.

mne-python/mne/io/base.py

Lines 2506 to 2513 in 603a96d

for key in ('nchan', 'bads', 'sfreq'):
a, b = raw[ri].info[key], raw[0].info[key]
if a != b:
raise ValueError(
f'raw[{ri}].info[{key}] must match:\n'
f'{repr(a)} != {repr(b)}')
if not set(raw[ri].info['ch_names']) == set(raw[0].info['ch_names']):
raise ValueError('raw[%d][\'info\'][\'ch_names\'] must match' % ri)

The above lines of code should be replaced with:

        for key in ('nchan', 'sfreq'):
            a, b = raw[ri].info[key], raw[0].info[key]
            if a != b:
                raise ValueError(
                    f'raw[{ri}].info[{key}] must match:\n'
                    f'{repr(a)} != {repr(b)}')
        if not set(raw[ri].info['bads']) == set(raw[0].info['bads']):
            raise ValueError('raw[%d][\'info\'][\'bads\'] must match' % ri)
        if not set(raw[ri].info['ch_names']) == set(raw[0].info['ch_names']):
            raise ValueError('raw[%d][\'info\'][\'ch_names\'] must match' % ri)

Steps to reproduce

import mne
from mne import create_info
from mne.io import RawArray
from numpy.random import default_rng

def create_toy_data(n_channels=3, sfreq=250, seed=None):
    rng = default_rng(seed)
    data = rng.standard_normal(size=(n_channels, 50 * sfreq)) * 5e-6
    info = create_info(n_channels, sfreq, "eeg")
    return RawArray(data, info)

raw0 = create_toy_data()
raw1 = create_toy_data()

raw0.info["bads"] = ["0", "1"]
raw1.info["bads"] = ["1", "0"]

raw_concat = mne.concatenate_raws([raw0, raw1])

Link to data

No response

Expected results

Concatenation should work.

Actual results

ValueError.

Additional information

Platform:         macOS-13.2.1-arm64-arm-64bit
Python:           3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ]
Executable:       /Users/moritzgerster/anaconda3/envs/bids_neu/bin/python
CPU:              arm: 10 cores
Memory:           16.0 GB

mne:              1.3.0
numpy:            1.23.4 {OpenBLAS 0.3.21 with 10 threads}
scipy:            1.9.3
matplotlib:       3.6.2 {backend=MacOSX}

sklearn:          1.1.3
numba:            Not found
nibabel:          5.0.1
nilearn:          Not found
dipy:             Not found
openmeeg:         Not found
cupy:             Not found
pandas:           1.5.1
pyvista:          Not found
pyvistaqt:        Not found
ipyvtklink:       Not found
vtk:              Not found
qtpy:             Not found
ipympl:           Not found
pyqtgraph:        Not found
pooch:            v1.6.0

mne_bids:         0.12
mne_nirs:         Not found
mne_features:     Not found
mne_qt_browser:   Not found
mne_connectivity: Not found
mne_icalabel:     Not found

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions