mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.74k stars 1.33k forks source link

Error when anonymizing via `meas_info.anonymize()` #12963

Closed berkgercek closed 1 week ago

berkgercek commented 1 week ago

Description of the problem

Attempting to anonymize a raw file from version 1.8.0 onwards results in one of two errors.

The first possible error happens for me using mne anonymize -f myfile.fif, and is during a call to mne._fiff.meas_info._add_timedelta_to_stamp:

Traceback (most recent call last):
  File "/tmp/gercek_li_venv/bin/mne", line 8, in <module>
    sys.exit(main())
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/commands/utils.py", line 109, in main
    cmd.run()
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/commands/mne_anonymize.py", line 122, in run
    mne_anonymize(fname, out_fname, keep_his, daysback, overwrite)
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/commands/mne_anonymize.py", line 47, in mne_anonymize
    raw.anonymize(daysback=daysback, keep_his=keep_his)
  File "<decorator-gen-26>", line 12, in anonymize
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/_fiff/meas_info.py", line 793, in anonymize
    anonymize_info(info, daysback=daysback, keep_his=keep_his, verbose=verbose)
  File "<decorator-gen-34>", line 12, in anonymize_info
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/_fiff/meas_info.py", line 3529, in anonymize_info
    _add_timedelta_to_stamp(hi["meas_date"], -delta_t)
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/_fiff/meas_info.py", line 3386, in _add_timedelta_to_stamp
    meas_date_stamp = _dt_to_stamp(_stamp_to_dt(meas_date_stamp) + delta_t)
  File "/tmp/gercek_li_venv/lib/python3.10/site-packages/mne/utils/numerics.py", line 1012, in _stamp_to_dt
    stamp = [int(s) for s in utc_stamp]
TypeError: 'datetime.datetime' object is not iterable

This happens because it seems the datetime.datetime object is not iterable, and my FIF file (MEGIN Triux Neo) only has a single datetime object as the helium measurement date.

The second possible error is if raw.info["meas_date"] has been set to None prior to the anonymize script being run, but the helium info meas_date is still a datetime value. I believe mne-bids-pipeline does something to this effect when it runs the pipeline step preprocessing/data_quality. This results in an error when attempting to set the measurement date to None:

helium_info['meas_date'] must be an instance of datetime, got <class 'NoneType'> instead.

I don't have the stack trace on hand for this one, but it occurs during at this line, when the code attempts to set the HeliumInfo attribute to None and None isn't an accepted value for the ValidatedDict subclass HeliumInfo.

It seems like there are two PRs that result in the issue, one which changes from a tuple of ints to a datetime.datetime object for the helium measurement date, and another that implements the HeliumInfo class from ValidatedDict and checks against a datetime.datetime type for the date

Hopefully the above helps in a quick solution!

Steps to reproduce

# Error version 1:
$ mne anonymize -f ./myfile.fif 

# Error version 2:
$ mne_bids_pipeline --steps preprocessing [relevant arguments here]

Link to data

No response

Expected results

It should be possible, given that MNE supports anonymization, to set the field in question to None. I'm less sure about why the iterable error is happening.

Actual results

See above

Additional information

I've tried this with MNE versions 1.7.1, 1.8.0, and 1.9.0-dev. Both categories of error only occur with 1.8 and 1.9.

The .fif file in question is from a MEGIN Triux Neo, I can provide an empty-room file that produces the error on request, but it's a pain to upload.

welcome[bot] commented 1 week ago

Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

larsoner commented 1 week ago

Indeed this looks like an error, I should be able to look tomorrow!

berkgercek commented 1 week ago

Thanks Eric!