mne-tools / mne-nirs

Process Near-Infrared Spectroscopy Data in MNE
https://mne.tools/mne-nirs/
BSD 3-Clause "New" or "Revised" License
84 stars 35 forks source link

subject ID stored in `first_name` field, but should be `his_id` field for MNE-BIDS compatibility #544

Closed aaronjnewman closed 7 months ago

aaronjnewman commented 7 months ago

Describe the bug

I have SNIRF files collected using NIRx Aurora that I import with mne.io.read_raw_snirf() and then write to BIDS with MNE-BIDS' write_raw_bids() function.

I then import my raw BIDS data in another script to do preprocessing. When I go to write the preprocessed data with write_raw_snirf I get the error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[9], line 75
     73 print(preproc.info['bads'])
     74 # save data
---> 75 write_raw_snirf(preproc, op.join(out_path, subject + '_' + session + '_' + 'task-' + task + '_' + run + '_preproc' + data_extn))

File [~/mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:65](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:65), in write_raw_snirf(raw, fname, add_montage)
     [61](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:61) nirs = f.create_group('[/nirs](https://file+.vscode-resource.vscode-cdn.net/nirs)')
     [62](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:62) f.create_dataset('formatVersion',
     [63](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:63)                  data=_str_encode(SPEC_FORMAT_VERSION))
---> [65](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:65) _add_metadata_tags(raw, nirs)
     [66](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:66) _add_single_data_block(raw, nirs)
     [67](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:67) _add_probe_info(raw, nirs, add_montage=add_montage)

File [~/mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:103](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:103), in _add_metadata_tags(raw, nirs)
     [99](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:99) metadata_tags.create_dataset('MeasurementTime',
    [100](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:100)                              data=_str_encode(timestr))
    [102](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:102) # Store demographic info
--> [103](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:103) subject_id = raw.info['subject_info']['first_name']
    [104](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:104) metadata_tags.create_dataset('SubjectID', data=_str_encode(subject_id))
    [106](mambaforge/envs/ncil/lib/python3.11/site-packages/mne_nirs/io/snirf/_snirf.py:106) # Store the units of measurement

KeyError: 'first_name'

I can fix the error by modifying the _snirf.py file to look for his_id rather than first_name. However, I'm not sure if that is a robust fix for the codebase. It does strike me as odd that the first_name field is assumed to store the subject ID. If changing first_name to his_id is acceptable, I can do a PR for that. But I worry that this might mess up other functions.

larsoner commented 7 months ago

If changing first_name to his_id is acceptable, I can do a PR for that. But I worry that this might mess up other functions

I suspect first_name was never the correct place for this. If it's a subject actual name (usually identifiable) there should be first_name and last_name generally. If it's an identifier (usually anonymized) it should be in his_id.