mne_bids.read_raw_bids: KeyError: 'HeadCoilFrequency'

Hello to everyone,

first time poster so please let me know if I am making any mistake regarding your posting etiquette.

  • MNE-Python version: 0.23.0
  • MNE_bids version: 0.8
  • operating system: Windows 10 (working with JupyterNotebook)

I am working with the MEG dataset provided by Rathee et al. (2021).
Data: Link
Publication describing the data: Link

I am struggeling to read my data into a raw object using mne_bids.read_raw_bids.

dir_name = "MEG_BIDS"
bids_root = os.path.abspath(dir_name)
subject = "20" 
session = "1" 
bids_root = pathlib.Path(bids_root)
bids_path = mne_bids.BIDSPath(subject=subject,
                              session=session,
                              root=bids_root,
                              task="bcimici")
raw = mne_bids.read_raw_bids(bids_path)

First, it seems to work. I am getting the following output: (Please note that I removed part of the path with “…” for privacy reasons)

Opening raw data file C:\Users\...s\MNE\00_DataExploration\MEG_BIDS\sub-20\ses-1\meg\sub-20_ses-1_task-bcimici_meg.fif...
    Read a total of 13 projection items:
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
        generated with autossp-1.0.1 (1 x 306)  idle
    Range : 8000 ... 2018999 =      8.000 ...  2018.999 secs
Ready.
Reading events from C:\Users\...\MNE\00_DataExploration\MEG_BIDS\sub-20\ses-1\meg\sub-20_ses-1_task-bcimici_events.tsv.
Reading channel info from C:\Users\...\MNE\00_DataExploration\MEG_BIDS\sub-20\ses-1\meg\sub-20_ses-1_task-bcimici_channels.tsv.

After this, I get the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14764/2373481023.py in <module>
      6                               root=bids_root,
      7                               task="bcimici")
----> 8 raw = mne_bids.read_raw_bids(bids_path)
      9 # mne_bids.inspect_dataset(bids_path)

~\.conda\envs\mne\lib\site-packages\mne_bids\read.py in read_raw_bids(bids_path, extra_params, verbose)
    694                                            on_error='warn')
    695     if sidecar_fname is not None:
--> 696         raw = _handle_info_reading(sidecar_fname, raw, verbose=verbose)
    697 
    698     # read in associated scans filename

~\.conda\envs\mne\lib\site-packages\mne_bids\read.py in _handle_info_reading(sidecar_fname, raw, verbose)
    328                         'data for KIT files.')
    329         else:
--> 330             hpi_freqs_json = sidecar_json['HeadCoilFrequency']
    331             try:
    332                 hpi_freqs_raw, _, _ = mne.chpi.get_chpi_info(raw.info)

KeyError: 'HeadCoilFrequency'

I looked into the source code. It seems like since my data was recorded on an Elekta system, it automatically expects the sub-20_ses-1_task-bcimici_meg.json file to contain the column “HeadCoilFrequency”, which it does not (I checked manually). Checking the BIDS documentation, it only lists “HeadCoilFrequency” as a recommended value to include, but not as mandatory. Therefore, I am confused why mne_bids seems to expect this value to be always included in the .json file.

How can I solve this problem / work around it? Thanks a lot in advance.

1 Like

Thanks for this nice report @tempMEG

I think the issue is that your JSON sidecar has a value ContinuousHeadLocalization set to True.

That makes MNE-BIDS think (understandably so?) that other fields are present like HeadCoilFrequency, which is there as a:

List of frequencies (in Hz) used by the head localisation coils

(otherwise ContinuousHeadLocalization would be False, right?)

See: https://github.com/mne-tools/mne-bids/blob/2ef3516ba6ba54506901a773204f5f88adff224f/mne_bids/read.py#L305

You are right though that there is nothing in the BIDS spec that says if ContinuousHeadLocalization is True, the other fields MUST be present. So I think we have to relax MNE-BIDS here.

Would you be up to send a PR for that? Then after merging you could work with the main (unstable/dev) version of MNE-BIDS until the next release.

The only other alternative I see is to work on your BIDS data and include the information that MNE-BIDS wants.

I would like to chime in and support @sappelhoff‘s impression that this is something we should fix in MNE-BIDS, and potentoally push for an amendment of the BIDS specs in addition

Hi, thanks for your answers. I created a PR. However, it is my first PR ever so I hope everything went alright.

2 Likes