Skip to content

BUG: raw.anonymize() not fully anonymizing #11966

Closed

Description

@paulroujansky

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

  1. larsoner commented on Sep 7, 2023

    @larsoner
    Member

    _raw_extras is private so in principle users shouldn't mess with it. As soon as you export or raw.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).

  2. paulroujansky commented on Sep 7, 2023

    @paulroujansky
    ContributorAuthor

    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.

  3. larsoner commented on Sep 7, 2023

    @larsoner
    Member

    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

    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_extras entirely before/independent of .anonymize(), i.e., in the read_raw_edf _raw_extras setting 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_extras a win independent of the anonymization consideration (to some extent anyway)!

    As part of that PR, we could document somewhere -- probably mne/io/base.py where we talk about what subclasses of BaseRaw should 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.

  4. added this to the 1.6 milestone on Sep 7, 2023
  5. paulroujansky commented on Sep 7, 2023

    @paulroujansky
    ContributorAuthor

    Based on your activity, I'm guessing you're working with (just) EDF for now, is that right?

    Haha indeed, well spotted! 😉 But I'd be happy to extend the changes to other formats if they apply of course!

    A PR for this would be welcome

    Great! I'd be happy to contribute obviously 😃

  6. modified the milestones: 1.6, 1.7 on Nov 7, 2023
  7. modified the milestones: 1.7, 1.8 on Apr 9, 2024
  8. removed this from the 1.8 milestone on Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions