Minor Priority: Issue with converting MNE Raw into BIDS with mne_bids - Age/Birthday Info of Subject

Dear All,

I found a minor issue in the Patient/Subject Information raw.info[‘subject_info’] which is potentially new after some recent changes (adding weight and height).
I used some of my old code parts to convert eeg data to bids using mne_bids (which worked perfectly with the previous versions).

As before I added the age of the participants using the birthday key for the subject_info dict before converting/writing the object into bids:
raw.info[‘subject_info’][‘birthday’] = (year, month, day) in integer (int)

Unfortunately, it was not recognized after the conversion (no info (n/a) in the participants.tsv file).

I looked into some documentation on the subject_info and found this interesting format specification in the dictionary documentation :sweat_smile: - could it be a copy-paste mistake which causes the issue?

subj_birth_day 404 julian - “Birthday of the subject”

Additional Info

  • MNE version: ‘1.1.1’
  • mne_bids version: ‘0.11.1’
  • operating system: Windows 10

Many thanks for looking into it :hugs:
Best,
Katharina

Hello @kathlin42, I believe the date must be a datetime.datetime object, could you try with that? (I cannot check myself right now!)

Best wishes,
Richard

Hi Richard,

Thanks for the first idea.
I tested it with datetime.datetime object but it is still not working properly. It is correctly added in the subject_info dict but gets lost after the conversion to bids (tsv file and raw object after read_raw_bids). Of course, the issue could also be in mne_bids since they also changed some things in the last months…

Best
Katharina

I cannot reproduce this…

# %%
from pathlib import Path
import pandas as pd
import mne
import mne_bids

sample_dir = mne.datasets.sample.data_path()
sample_fname = sample_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'

raw = mne.io.read_raw_fif(sample_fname)
subject_info = {
    'birthday': (1980, 12, 30)  # YYYY, MM, DD
}
raw.info['subject_info'] = subject_info

bids_root = Path('/tmp/bids-test')
bp = mne_bids.BIDSPath(
    subject='sample', task='av', suffix='meg', extension='.fif',
    datatype='meg', root=bids_root
)
mne_bids.write_raw_bids(raw, bids_path=bp, overwrite=True)

# %%
participants_tsv_path = bids_root / 'participants.tsv'
participants_tsv = pd.read_csv(participants_tsv_path, sep='\t')
participants_tsv

produces:

The only problem I noticed is that we don’t support the birthday to be a datetime object, so I passed a tuple instead …

Can you share the code you’re using?

Richard

cc @sappelhoff

Hi Richard,

Of course:
Maybe it is an EEG specific issue? BrainVision format issue? I also get a warning for the Coordinate frame but I considered this independent from the subject_info…

CODE

# Import necessary Packages
import os
import mne_bids
import mne
import pickle
# Project Specifications
task = 'errormonitoring'
datatype = 'eeg'
suffix = 'eeg'
extension = '.vhdr'
data_format= 'BrainVision'

mne_events_id = {'Fixation': 0,
                 'Good': 100,
                 'Error': 200,
                 'Response_Cue': 2,
                 'Key_Press': 3,
                 'ITI': 4}

# Data Paths
bids_root = 'raw_data' # Path where to save BIDS files
source_root = 'source_data' # Path where to copy 'example_subj_raw.fif' and 'mne_events.pickle'

# Read Data
raw = mne.io.read_raw(os.path.join(source_root, 'example_subj_raw.fif'))

with open(os.path.join(source_root, 'mne_events.pickle'), 'rb') as file:
    # A new file will be created
    mne_events = pickle.load(file)

# Add Information for BIDS Conversion
raw.info['line_freq'] = 50
raw.info['subject_info'] = {}
raw.info['subject_info']['first_name'] = 'sub-001'
raw.info['subject_info']['his_id'] = 'sub-001'
raw.info['subject_info']['birthday'] = (1996, 10, 1)
raw.info['subject_info']['sex'] = 1
raw.info['subject_info']['hand'] = 1
montage = mne.channels.make_standard_montage('standard_1020')
print(montage)
raw.set_montage(montage)
# Write Run Task Data to BIDS
bids_path = mne_bids.BIDSPath(subject='001', task='errormonitoring', run = 1,root=bids_root, datatype='eeg', suffix='eeg',
                     extension='.vhdr')
mne_bids.write_raw_bids(raw = raw, bids_path=bids_path, events=mne_events, event_id=mne_events_id,
                        allow_preload=True, overwrite=True, format='BrainVision', montage = montage)

# Test Result
raw_bids = mne_bids.read_raw_bids(bids_path, extra_params=None, verbose=None)
print(raw_bids.info['subject_info'])

If it helps, I can also share the raw fif file and mne_events in pickle format…

Thanks a lot for your help!

If you could share the input file(s), that would be helpful!

Btw you shouldn’t use pickle for long-term storage, it will cause problems sooner or later.

You should be able to download the files via the owncloud Link: Fraunhofer ownCloud
I didn’t find an option to upload the file types directly in discourse :wink:
If there are any problems I can try another way to share them.

The problem was that the measurement date was not set, so even though the birthday was present, the participant’s age could not be calculated.

MNE-BIDS should probably warn or even raise an exception in a case like this!

The following code works for me:

import datetime
import mne_bids
import mne

bids_root = '/tmp/bids-mwe'

raw = mne.io.read_raw('example_subj_raw.fif')
raw.info['subject_info'] = {}
raw.info['subject_info']['birthday'] = (1996, 10, 1)

# meas_date must be set so participant age can be calculated
raw.set_meas_date(
    datetime.datetime(2017, 1, 30, tzinfo=datetime.timezone.utc)
)

bids_path = mne_bids.BIDSPath(
    subject='001',
    task='errormonitoring',
    root=bids_root,
    datatype='eeg',
    suffix='eeg',
    extension='.vhdr'
)
mne_bids.write_raw_bids(raw=raw, bids_path=bids_path, overwrite=True)
2 Likes

Thanks a lot!!! Now it worked - I’m sorry that it had such a trivial reason!