Skip to content

Handle None-type for 'sex' in raw subject info before writing EDF - #11375

Merged
larsoner merged 16 commits into
mne-tools:mainfrom
henneysq:main
Dec 19, 2022
Merged

larsoner merged 16 commits into
mne-tools:mainfrom
henneysq:main

Conversation

@henneysq

Copy link
Copy Markdown
Contributor

No reference issue.

This changes fixes an issue where, when 'sex' is not specified in the raw.info["subject_info"], the export to .edf-raises a RunTimeError.
The error occurs in mne.export._edf._try_to_set_value (https://github.com/mne-tools/mne-python/blob/main/mne/export/_edf.py#L14) because a None is return at sex = subj_info.get('sex') in mne.export._edf._export_raw (https://github.com/mne-tools/mne-python/blob/main/mne/export/_edf.py#L193) if the sex key does not exist.
The solution checks if sex is None, and, if so, changes it to 0.

@henneysq
henneysq requested a review from sappelhoff as a code owner December 16, 2022 15:43
@welcome

welcome Bot commented Dec 16, 2022

Copy link
Copy Markdown

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

@agramfort

Copy link
Copy Markdown
Member

@sappelhoff can you have a look? thx

Comment thread mne/export/_edf.py Outdated
Comment on lines +205 to +211
if sex is None:
warning_message = "'sex' not specified in " + \
"raw.info.subject_info. Encoding as n/a: 2 " + \
"for EDFwriter."
warn(message=warning_message)
sex = 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than this it would probably be better to do below some if val is not None. I think this could live in _try_to_set_value.

This assumes EDF allows you to write a subset of the patient info...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDFwriter defines a standard self.__plus_gender = 2 at instantiation, so they actually handle subsets just fine. A if val is not None guard clause should result in the same as my implementation.

I actually avoided implementing it in _try_to_set_value because I thought it was not a general case, but looking through the functions in EDFwriter I see several numeric comparisons, so the issue could arise with other parts of the info.

Let me know what you think, and i'll push a change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can live with either solution. If we go with this one, though, we shouldn't warn -- if the field is missing from the Raw instance then I think it's okay to just have it be n/a when written

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have a look at the change. I have generalized it to the cases of name, sex, and handedness for now. If a similar issue arises in the future for other fields, we could generalize it to more/all fields in the EDF header and move to _try_to_set_value.

@larsoner

Copy link
Copy Markdown
Member

Works for me! Just two remaining quick things I think:

  1. We want to document each bugfix -- can you add an entry to doc/changes/latest.inc with the :newcontrib: directive and your name to doc/changes/names.inc?

  2. We try to add regression tests that fail on main but pass on each PR. Can you just add a tiny test to:

    info['subject_info'] = dict(first_name='mne', last_name='python',
    birthday=(1992, 1, 20), sex=1, hand=3)
    raw = RawArray(data, info)

    ? For example something like this should fail on main but pass here:

    del info['subject_info']['sex']
    raw_2 = RawArray(data, info)
    raw_2.save(...)
    

    you could also read_raw_edf to check info['subect_info']['sex'] after this operation if you want, but the most critical thing is the .save step I think.

    You can check that your test works for example with pytest mne/export -k _edf

FYI we plan to release tomorrow so if there's any way you can get these in today then they'll make it into 1.3, otherwise your PR will probably go into 1.4 and be backported

@larsoner

Copy link
Copy Markdown
Member

... actually let me know in the next few hours if you can do this. If not, then I can push a quick commit to get it in. It would be good to get it into this release!

@larsoner larsoner added this to the 1.3 milestone Dec 19, 2022
@henneysq

Copy link
Copy Markdown
Contributor Author

I'm working on it now. I'll give you an update in a bit.

Comment thread doc/changes/latest.inc Resolved
@henneysq

Copy link
Copy Markdown
Contributor Author

@larsoner I added a test. It passes for this PR but not for main.

I don't know if you have preferences for the placement of it.

Comment thread mne/export/_edf.py Resolved

@larsoner larsoner left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking for merge when green, thanks on advance @henneysq !

@larsoner
larsoner enabled auto-merge (squash) December 19, 2022 15:03
@henneysq

Copy link
Copy Markdown
Contributor Author

You're welcome! I'm looking forward to contributing more in the future!
And thank you guys for maintaining this amazing project.

Comment thread mne/export/tests/test_export.py Resolved
@larsoner

Copy link
Copy Markdown
Member

The conda numba import error should be fixed by #11377, so you can ignore it @henneysq

@henneysq

Copy link
Copy Markdown
Contributor Author

Am I supposed to do anything else? I can see it's scheduled for review, but will the merge complete automatically?

@larsoner

Copy link
Copy Markdown
Member

I'm going to iterate in #11377 until it merges, then push a commit here that should get CIs green, at which point it should auto-merge. So you shouldn't need to do anything!

@larsoner
larsoner merged commit d133a30 into mne-tools:main Dec 19, 2022
@welcome

welcome Bot commented Dec 19, 2022

Copy link
Copy Markdown

🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants