Repository navigation
Open
Description
Working with the somato dataset, I ran into an issue related to the fact that the measurement date I'm intending to anonymize deviates from the date in info['file_id']['secs'] such that, when I reach the desired adjustment for the measurement date, info['file_id']['secs'] goes out-of-range (even though the measurement date would be within the valid range).
MWE:
import mne
import os.path as op
from datetime import datetime
data_path = mne.datasets.somato.data_path()
raw_fname = op.join(data_path, 'sub-01', 'meg', 'sub-01_task-somato_meg.fif')
raw = mne.io.read_raw_fif(raw_fname, verbose='error')
print(f"Date in info['meas_date']: {raw.info['meas_date']}")
print(f"Date in info['file_id']: {datetime.fromtimestamp(raw.info['file_id']['secs'])}")
# Works
print('\nAnonymizing with daysback -> 10 years')
daysback = 10 * 365
raw_anon = raw.copy().anonymize(daysback=daysback)
print(f"Date in info['meas_date']: {raw_anon.info['meas_date']}")
print(f"Date in info['file_id']: {datetime.fromtimestamp(raw_anon.info['file_id']['secs'])}")
# Does not work
print('\nAnonymizing with daysback -> 90 years')
daysback = 90 * 365
raw_anon = raw.copy().anonymize(daysback=daysback)
print(f"Date in info['meas_date']: {raw_anon.info['meas_date']}")
print(f"Date in info['file_id']: {datetime.fromtimestamp(raw_anon.info['file_id']['secs'])}")Output:
Date in info['meas_date']: 2007-07-05 11:17:11.172243+00:00
Date in info['file_id']: 1970-01-01 01:00:00
Anonymizing with daysback -> 10 years
Date in info['meas_date']: 1997-07-07 11:17:11.172243+00:00
Date in info['file_id']: 1960-01-04 01:35:47
Anonymizing with daysback -> 90 years
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Untitled-2 in
21 print('\nAnonymizing with daysback -> 90 years')
22 daysback = 90 * 365
---> 23 raw_anon = raw.copy().anonymize(daysback=daysback)
24 print(f"Date in info['meas_date']: {raw_anon.info['meas_date']}")
25 print(f"Date in info['file_id']: {datetime.fromtimestamp(raw_anon.info['file_id']['secs'])}")
in anonymize(self, daysback, keep_his, verbose)
~/Development/mne-python/mne/channels/channels.py in anonymize(self, daysback, keep_his, verbose)
593 .. versionadded:: 0.13.0
594 """
--> 595 anonymize_info(self.info, daysback=daysback, keep_his=keep_his,
596 verbose=verbose)
597 self.set_meas_date(self.info['meas_date']) # unify annot update
in anonymize_info(info, daysback, keep_his, verbose)
~/Development/mne-python/mne/io/meas_info.py in anonymize_info(info, daysback, keep_his, verbose)
2245 'daysback parameter was too large.'
2246 'Underlying Error:\n')
-> 2247 _check_dates(info, prepend_error=err_mesg)
2248
2249 return info
~/Development/mne-python/mne/io/meas_info.py in _check_dates(info, prepend_error)
1454 if (value[key_2] < np.iinfo('>i4').min or
1455 value[key_2] > np.iinfo('>i4').max):
-> 1456 raise RuntimeError('%sinfo[%s][%s] must be between '
1457 '"%r" and "%r", got "%r"'
1458 % (prepend_error, key, key_2,
RuntimeError: anonymize_info generated an inconsistent info object. daysback parameter was too large.Underlying Error:
info[file_id][secs] must be between "-2147483648" and "2147483647", got "-2838237853"How shall I go about anonymizing this dataset? I want to use daysback large enough to move the measurement date before 1925, as is required by BIDS.
cc @agramfort
Metadata
Metadata
Assignees
Labels
No labels
Activity
cbrnr commentedon Aug 18, 2020
Why is
info['file_id']equal to1970-01-01 01:00:00and why does this have to be anonymized? It seems like this date has already been set to a date that definitely does not correspond to the file creation date (assuming that thefile_idkey corresponds to file creation).agramfort commentedon Aug 18, 2020
hoechenberger commentedon Aug 18, 2020
Ok, so I guess what we should do in this case is
Right? I can make a PR if we all agree.
agramfort commentedon Aug 18, 2020
hoechenberger commentedon Aug 18, 2020
But there are files like this out there in the wild.... should we not take care of them automatically in case of (re)anonymozation? Or is that what you meant: handle this inside Raw.anonymize()?
agramfort commentedon Aug 18, 2020
cbrnr commentedon Aug 18, 2020
The
file_idkey seems to be specific to FIF, so I agree that if thesecsubfield does not matchmeas_date, warn, anonymizemeas_date, and setfile_idequal to the anonymizedmeas_date.While we're at it, why does
anonymizedefault to setting the date to Jan 01 2000? Wouldn't it be more convenient to follow the BIDS recommendation to set the date to e.g. Jan 01 1920 instead?agramfort commentedon Aug 18, 2020
hoechenberger commentedon Aug 18, 2020
I agree with @agramfort , there probably should not be a default date
cbrnr commentedon Aug 18, 2020
Yes, but there is a default, which happens to be a bad one because it's not BIDS-compliant. Are you suggesting we should remove the default (in another PR)?
hoechenberger commentedon Aug 18, 2020
@cbrnr Yes I suggest we should consider removing the default
cbrnr commentedon Aug 18, 2020
OK. Some arguments for/against removing the default:
Pros:
Cons:
I don't really see the point of removing the default, I think it's more convenient to let MNE handle the anonymization - but I'd set a different default to <1925.
agramfort commentedon Aug 18, 2020
drammock commentedon Aug 18, 2020
An example would be a training study where the number of days between different recordings of the same subject was an important covariate.
hoechenberger commentedon Aug 18, 2020
2 remaining items
hoechenberger commentedon Aug 19, 2020
Looking at this again with @agramfort, there seems to be another problem involved:
Raw.set_meas_date()doesn't touch the dates ininfo['file_id']-- maybe that's on purpose? MWE:Output:
cbrnr commentedon Aug 19, 2020
I have no clue what
info['file_id']contains and the docs don't really explain it either.agramfort commentedon Aug 19, 2020
here is a fix:
don't know what happened to this file. But what bothers now with current code
is that when you anonymize the default for file_id is 1970 and for meas_date
it's 2000 so you quickly end up with data where file_id and meas_date are not
consistent. For me this should not happened. This deserves a first fix
and then I think we should make sure that when we set_meas_date the file_id
should then be adjusted.
thoughts? @alexrockhill @larsoner ?
larsoner commentedon Aug 19, 2020
It does seem incorrect that these are not consistent. Worth trying to fix it to see if it breaks some existing test -- this might tell us if we had some reason to do it this way
agramfort commentedon Aug 19, 2020
hoechenberger commentedon Sep 7, 2020
Not sure if this is exactly the same problem, but it seems related: in the following MWE,
info['file_id']['secs']doesn't survive an I/O roundtrip.Output:
alexrockhill commentedon Sep 7, 2020
I think @bloyl worked on this part if I'm not mistaken. I can take a look but I was only using the functionality as an end-user with
mne-bidshoechenberger commentedon Sep 7, 2020
I ran into this when trying to ensure that all date-related information in the FIFF files is properly anonymized when converting to BIDS and before sharing the datasets. So I'm in a similar situation: only ever got in touch with this stuff because I was using MNE-BIDS. :)
agramfort commentedon Sep 7, 2020
I think meas_id is meant to be saved but the file_id is supposed to be unique to a file. That's what I remember from conversations. Maybe @larsoner knows more.
hoechenberger commentedon Sep 8, 2020
@agramfort
@larsoner earlier said,
So I believe they should always stay in sync
hoechenberger commentedon Sep 8, 2020
I mean I wouldn't care, really, if I weren't concerned we might be accidentally leaking (non-anonymized) measurement info via
file_id. Is this concern justified?larsoner commentedon Sep 8, 2020
Thinking about it again, it's likely that file_id is supposed to be unique for the given file / generated at write time. Maybe we should just comment about this in the code?
agramfort commentedon Sep 8, 2020
bloyl commentedon Sep 8, 2020
git grep -n FIFF_FILE_IDsuggests that file id is set here during write:
mne-python/mne/io/write.py
Lines 292 to 304 in 9225419
however we don't seem to call
start_filewith the_idargument. So the default (None) is use. which leads toDATE_NONEbeing used for mne-python file_ids.see:
mne-python/mne/io/write.py
Lines 265 to 267 in 9225419
mne-python/mne/io/write.py
Lines 458 to 464 in 9225419
Fundamentally, the problem is the lack of a published FIFF standard (at least that I know of). The docstring below seems to be the best we have:
mne-python/mne/io/meas_info.py
Line 195 in 9225419
agramfort commentedon Sep 8, 2020