Repository navigation
Closed
Description
Description of the problem
When anonymizing a raw object with func anonymize(), the subject info and meas_date are still accessible through raw._raw_extras attribute as shown in the below example.
Regarding subject info, it would be easier in my opinion to remove field "subject_info" from both raw.info and *raw._raw_extras (or simply empty unecessary fields, keeping "his_id" only for instance) as it is not expected to be able to retrieve the information afterwards by definition.
Steps to reproduce
import mne
data_path = mne.datasets.testing.data_path(download=False)
fpath = data_path / "EDF" / "test_edf_stim_resamp.edf"
raw = mne.io.read_raw_edf(fpath, verbose=False)
print("Subject info before anonymization")
print("\t", raw.info["subject_info"])
print("\t", raw.info["meas_date"])
print("\t", raw._raw_extras[0]["subject_info"])
print("\t", raw._raw_extras[0]["meas_date"])
raw.anonymize()
print("\t", "\nSubject info after anonymization")
print("\t", raw.info["subject_info"])
print("\t", raw.info["meas_date"])
print("\t", raw._raw_extras[0]["subject_info"])
print("\t", raw._raw_extras[0]["meas_date"])
print(raw._raw_extras[0]["subject_info"])Expected results
Subject info before anonymization
{'his_id': 'X', 'sex': 1, 'last_name': 'X', 'birthday': (1967, 10, 9)}
2016-02-14 10:33:35+00:00
{'id': 'X', 'sex': 'M', 'birthday': datetime.datetime(1967, 10, 9, 0, 0), 'name': 'X'}
2016-02-14 10:33:35+00:00
Subject info after anonymization
{'his_id': '0', 'sex': 0, 'last_name': 'mne_anonymize', 'birthday': (1951, 8, 25)}
2000-01-01 00:00:00+00:00
{'id': '0', 'sex': 'unknown', 'birthday': datetime.datetime(1951, 8, 25, 0, 0), 'name': 'mne_anonymize'}
2000-01-01 00:00:00+00:00
or alternatively
Subject info before anonymization
{'his_id': 'X', 'sex': 1, 'last_name': 'X', 'birthday': (1967, 10, 9)}
2016-02-14 10:33:35+00:00
{'id': 'X', 'sex': 'M', 'birthday': datetime.datetime(1967, 10, 9, 0, 0), 'name': 'X'}
2016-02-14 10:33:35+00:00
Subject info after anonymization
{'his_id': 'X'}
2000-01-01 00:00:00+00:00
{}
2000-01-01 00:00:00+00:00
Actual results
Subject info before anonymization
{'his_id': 'X', 'sex': 1, 'last_name': 'X', 'birthday': (1967, 10, 9)}
2016-02-14 10:33:35+00:00
{'id': 'X', 'sex': 'M', 'birthday': datetime.datetime(1967, 10, 9, 0, 0), 'name': 'X'}
2016-02-14 10:33:35+00:00
Subject info after anonymization
{'his_id': '0', 'sex': 0, 'last_name': 'mne_anonymize', 'birthday': (1951, 8, 25)}
2000-01-01 00:00:00+00:00
{'id': 'X', 'sex': 'M', 'birthday': datetime.datetime(1967, 10, 9, 0, 0), 'name': 'X'}
2016-02-14 10:33:35+00:00
Additional information
The above code uses the latest release (mne==1.5.1).
Activity
larsoner commentedon Sep 7, 2023
_raw_extrasis private so in principle users shouldn't mess with it. As soon as youexportorraw.save-- which is how you'd share the anonymized file -- it should be fully anonymized. So I'm not sure this is something we really need to fix for users in practice, though I agree it's ugly to have some stuff in memory that is not anonymized.I suspect it will be a pain to fix this because it means that all raw readers now have to implement some way of cleaning/anonymizing
_raw_extras, since in general that variable is allowed to hold anything that is helpful for reading the given data format, so there are no standards really for what it contains (so it's not as easy as just removing some standard dict entries for examples).paulroujansky commentedon Sep 7, 2023
Indeed, I was actually concerned about some compliance issues one might have by anonymizing in-memory in his own app for instance, and therefore potentially exposing the unanomyzed data to malicious users, but it's definitely an edge case and would not be recommended anyhow.
larsoner commentedon Sep 7, 2023
If it is an important use case for you, then that's at least one confirmed use :)
Based on your activity, I'm guessing you're working with (just) EDF for now, is that right? 😄 If so, maybe the thing to do is open a PR to keep any non-anonymized keys out of
_raw_extrasentirely before/independent of.anonymize(), i.e., in theread_raw_edf_raw_extrassetting process. In theory, non-anonymized info shouldn't be necessary to facilitate reading the data (which is the purpose of_raw_extras). A PR for this would be welcome -- I consider less "stuff" hanging around in_raw_extrasa win independent of the anonymization consideration (to some extent anyway)!As part of that PR, we could document somewhere -- probably
mne/io/base.pywhere we talk about what subclasses ofBaseRawshould do (like implement_read_segment_file) -- that it's best practice to avoid any non-anonymized data in_raw_extras.Then in follow-up PRs we could proceed piecemeal to inspect and change other readers as others have time/effort/motivation. Or if you're feeling bold you can tackle them all at once -- reviewing won't be much harder since changing each reader will be nicely isolated to its own folder in the source tree.
paulroujansky commentedon Sep 7, 2023
Haha indeed, well spotted! 😉 But I'd be happy to extend the changes to other formats if they apply of course!
Great! I'd be happy to contribute obviously 😃