chpi channels not recognized by mne_bids.write_raw_bids

Hi,

I am working on converting some meg data to BIDS format using mne-bids based on this tutorial.

When I execute the following code snippet I get the error below. Basically the ‘chpi’ channel type in the raw data is not recognized by write_raw_bids.

raw = mne.io.read_raw_fif(raw_fname)
raw.info[‘line_freq’] = 50 # specify power line frequency as required by BIDS
events = mne.find_events(raw)
events = events[np.isin(events[:,2], list(event_id.values())), :]
bids_path = BIDSPath(subject=‘01’, task=‘wordreport’, run=‘01’, root=output_path)
write_raw_bids(raw, bids_path, events_data=events, event_id=event_id, overwrite=True)


KeyError Traceback (most recent call last)
/imaging/davis/users/ma09/Projects/AVSpeechMEG/code/analysis/mne-bids_data_conversion.ipynb Cell 3’ in <cell line: 11>()
10 bids_path = BIDSPath(subject=‘01’, task=‘wordreport’, run=‘01’, root=output_path)
—> 11 write_raw_bids(raw, bids_path, events_data=events, event_id=event_id, overwrite=True)

File :12, in write_raw_bids(raw, bids_path, events_data, event_id, anonymize, format, symlink, empty_room, allow_preload, montage, acpc_aligned, overwrite, verbose)

File ~/.local/lib/python3.10/site-packages/mne_bids/write.py:1668, in write_raw_bids(failed resolving arguments)
1663 make_dataset_description(bids_path.root, name=" ", overwrite=False)
1665 _sidecar_json(raw, task=bids_path.task, manufacturer=manufacturer,
1666 fname=sidecar_path.fpath, datatype=bids_path.datatype,
1667 emptyroom_fname=associated_er_path, overwrite=overwrite)
→ 1668 _channels_tsv(raw, channels_path.fpath, overwrite)
1670 # create parent directories if needed
1671 _mkdir_p(os.path.dirname(data_path))

File ~/.local/lib/python3.10/site-packages/mne_bids/write.py:120, in _channels_tsv(raw, fname, overwrite)
118 if _channel_type in get_specific:
119 _channel_type = coil_type(raw.info, idx, _channel_type)
→ 120 ch_type.append(map_chs[_channel_type])
121 description.append(map_desc[_channel_type])
122 low_cutoff, high_cutoff = (raw.info[‘highpass’], raw.info[‘lowpass’])

KeyError: ‘chpi’

System details:

  • MNE version: 1.1.1
  • operating system: Ubuntu 20.04.3 LTS (Focal Fossa) 64-bit
  • MEG system: Elekta

One solution which works is to remove the chpi channels, but I suspect that these channels are necessary for maxfiltering (which we intend to do with the mne-bids-pipeline), so ideally we would like to keep them.

Could anyone suggest a solution please?

Many thanks,
Máté

I cannot replicate with our test file using CHPI

import os.path as op
from tempfile import tempdir
import numpy as np
import mne
from mne.datasets import testing
from mne_bids import write_raw_bids, BIDSPath

data_path = testing.data_path(download=False)
chpi_fif_fname = op.join(data_path, 'SSS', 'test_move_anon_raw.fif')
output_path = "/tmp/chpi-debug"
raw = mne.io.read_raw_fif(chpi_fif_fname, allow_maxshield=True)
raw.info["line_freq"] = 50 # specify power line frequency as required by BIDS
bids_path = BIDSPath(subject="01", task="wordreport", run="01", root=output_path)
write_raw_bids(raw, bids_path, overwrite=True)

does this code work for you? If so can you share your problematic file with us?

thanks
Alex

Hi Alex,

Thanks for the response. Your code does work for me without an issue. In the meantime, I noticed that the file I tried to convert has already gone through maxfilter. I tried it on the original raw file and the code worked flawlessly. I’m happy to share the file with you if you are still curious, but otherwise this issue is solved.

Many thanks,
Máté

can you maxfilter the file I used for my test and tell me if you can replicate the problem?

Alex

I get the same error if I maxfilter your test file before conversion.

Máté

When I try I have no problem

import os.path as op
from tempfile import tempdir
import numpy as np
import mne
from mne.datasets import testing
from mne_bids import write_raw_bids, BIDSPath

data_path = testing.data_path(download=False)
chpi_fif_fname = op.join(data_path, 'SSS', 'test_move_anon_raw.fif')
output_path = "/tmp/chpi-debug"
raw = mne.io.read_raw_fif(chpi_fif_fname, allow_maxshield=True)
raw.info["line_freq"] = 50 # specify power line frequency as required by BIDS
raw = mne.preprocessing.maxwell_filter(raw)
bids_path = BIDSPath(subject="01", task="wordreport", run="01", root=output_path,
                     extension=".fif", datatype="meg")
write_raw_bids(raw, bids_path, overwrite=True, allow_preload=True, format="FIF")

Alex

Sorry I forgot to mention that I used this matlab script for maxfiltering, not mne.

Máté

hard to help in this case. It’s likely this matlab code screws up channel types or something in the file
measurement info.

Alex