Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Datatype handling when in write_raw_bids #774

Merged
merged 15 commits into from Apr 20, 2021
Merged

[MRG] Datatype handling when in write_raw_bids #774

merged 15 commits into from Apr 20, 2021

Conversation

richardkoehler
Copy link
Contributor

@richardkoehler richardkoehler commented Apr 16, 2021

PR Description

closes #752

The user must now specify the datatype when calling write_raw_bids. Not automatic datatype inferring is done anymore in write_raw_bids. However, it is checked if the specified datatype is present in the dataset.

Merge checklist

Maintainer, please confirm the following before merging:

  • All comments resolved
  • This is not your own PR
  • All CIs are happy
  • PR title starts with [MRG]
  • whats_new.rst is updated
  • PR description includes phrase "closes <#issue-number>"

The user must now specify the datatype when calling write_raw_bids. Not automatic datatype inferring is done anymore in write_raw_bids. However, it is checked if the specified datatype is present in the dataset.
data type specifciation by user is only required if multiple data types are present in data
Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

just a few suggestions.

thx @richardkoehler

if 'eeg' in raw:
datatypes.append('eeg')
if len(datatypes) == 0:
raise ValueError('No MEG, EEG or iEEG channels found in data.'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
raise ValueError('No MEG, EEG or iEEG channels found in data.'
raise ValueError('No MEG, EEG or iEEG channels found in data. '

mne_bids/utils.py Outdated Show resolved Hide resolved
mne_bids/utils.py Outdated Show resolved Hide resolved
if datatype in raw:
datatype_matches = True
elif datatype == 'meg':
if datatype not in raw:
Copy link
Member

Choose a reason for hiding this comment

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

idem

Comment on lines 1188 to 1192
if bids_path.datatype is None:
datatype = _handle_datatype(raw)
else:
datatype = bids_path.datatype
_check_datatype(raw, datatype)
Copy link
Member

Choose a reason for hiding this comment

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

I would do this in the _handle_datatype function

datatype = _handle_datatype(raw, bids_path.datatype)

then you call _check_datatype in _handle_datatype and your return directly the 2nd parameter if valid.

mne_bids/utils.py Outdated Show resolved Hide resolved
@richardkoehler
Copy link
Contributor Author

@agramfort Thanks for the helpful suggestions! Tests are currently failing because write_raw_bids() now makes no more automatic assumptions about the datatype if there are multiple ephys data types (meg, eeg or ieeg) in our Raw object and because it actually checks if the bids_path.datatype is present in the Raw object. I can start fixing the tests, but as I already wrote in #752, I think the current PR might "break" write_raw_bids() for a lot of existing code out there (as demonstrated by the failing tests). So I think we need to make sure that everyone agrees with the new behavior of write_raw_bids:

  1. If bids_path.datatype is not set, _handle_datatype will try to infer the datatype:
    a) If there is a single data type (of meg, eeg, ieeg) in the Raw object, this type will be picked
    b) If there are multiple types (of meg, eeg, ieeg) in the Raw object, an Error will be thrown, asking the user to specify the data type
    c) If no data type (of meg, eeg, ieeg) is found in the Raw object, an Error will be thrown, asking the user to specify the channel types and/or bids_path.datatype
  2. If bids_path.datatype is set, _handle_datatype will call _check_datatype and verify that the specified data type is actually present in the Raw object. If it is not, an Error will be thrown.

Alternatively, we could "trust" the user to know what he is doing, and simply issue a warning message for case 2. There might be some cases where a user might want to write the data without specifying the channel types, even though I can't currently think of any. Is it really the job of MNE-BIDS to enforce these kind of things?
@jasmainak

@agramfort
Copy link
Member

agramfort commented Apr 19, 2021 via email

@richardkoehler
Copy link
Contributor Author

@agramfort Yes, this actually fixed many of the tests. I implemented this way of handling datatype=None and fixed the remaining failing tests. In the case of datatype='eeg' with additional 'meg' channels in the data we are now trusting the user to know what he is doing, and NOT enforcing the datatype 'meg'. Therefore, the user can decide to write out combined MEG-EEG recordings in an EEG data format.

@richardkoehler
Copy link
Contributor Author

@agramfort Actually, it won't be possible as I get the following Error:

E           UserWarning: Encountered unsupported non-voltage units: T/m, T
E           Note that the BrainVision format specification supports only µV.

So apparently it doesn't make any sense to save MEG-EEG data as EEG data because brainvision doesn't support MEG units. Sorry, I'm not very familiar with MEG, I wasn't aware of this.

mne_bids/utils.py Outdated Show resolved Hide resolved
mne_bids/utils.py Outdated Show resolved Hide resolved
mne_bids/utils.py Outdated Show resolved Hide resolved
Comment on lines 159 to 171
datatype = None
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'is currently not supported.'):
_check_datatype(raw_eeg, datatype)
datatype = 'anat'
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'is currently not supported.'):
_check_datatype(raw_eeg, datatype)
datatype = 'eeg'
_check_datatype(raw_eeg, datatype)
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'was not found in the raw object.'):
_check_datatype(raw_meg, datatype)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
datatype = None
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'is currently not supported.'):
_check_datatype(raw_eeg, datatype)
datatype = 'anat'
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'is currently not supported.'):
_check_datatype(raw_eeg, datatype)
datatype = 'eeg'
_check_datatype(raw_eeg, datatype)
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'was not found in the raw object.'):
_check_datatype(raw_meg, datatype)
for datatype, raw in [(None, raw_eeg, ('eeg', raw_meg), ('anat', raw_eeg)]:
with pytest.raises(ValueError, match=f'The specified datatype {datatype} '
'is currently not supported.'):
_check_datatype(raw, datatype)

can you compress with something like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did, hope it is fine the way I have solved it now :)

richardkoehler and others added 2 commits April 19, 2021 18:13
Co-authored-by: Alexandre Gramfort <alexandre.gramfort@m4x.org>
@codecov-commenter
Copy link

codecov-commenter commented Apr 19, 2021

Codecov Report

Merging #774 (503046f) into main (bc488fc) will increase coverage by 0.06%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #774      +/-   ##
==========================================
+ Coverage   94.01%   94.07%   +0.06%     
==========================================
  Files          23       23              
  Lines        2822     2851      +29     
==========================================
+ Hits         2653     2682      +29     
  Misses        169      169              
Impacted Files Coverage Δ
mne_bids/utils.py 96.51% <100.00%> (+0.58%) ⬆️
mne_bids/write.py 97.31% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bc488fc...503046f. Read the comment docs.

Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

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

@agramfort agramfort merged commit 154f44b into mne-tools:main Apr 20, 2021
@agramfort
Copy link
Member

thx a lot @richardkoehler !

@richardkoehler richardkoehler changed the title Datatype handling when in write_raw_bids [MRG] Datatype handling when in write_raw_bids Apr 20, 2021
@richardkoehler
Copy link
Contributor Author

@agramfort Thanks for the merge, happy to contribute :) Stupid question because this is my first contribute to mne-bids: The Merge checklist at the top is not for myself to complete right? Because it says "This is not your own PR". I haven't updated whats_new.rst or anything, but of course I would be happy to do so, if required

@richardkoehler richardkoehler deleted the write_EEG_iEEG branch April 20, 2021 06:58
@agramfort
Copy link
Member

agramfort commented Apr 20, 2021 via email

@hoechenberger
Copy link
Member

Thanks @richardkoehler!

@hoechenberger
Copy link
Member

Oh noes, we forgot to add a changelog entry!! @richardkoehler care to add one?

@richardkoehler
Copy link
Contributor Author

@hoechenberger Yes, I did so in #779 as I couldn't reopen this PR.

hoechenberger pushed a commit that referenced this pull request Apr 20, 2021
@christinerogers christinerogers mentioned this pull request Jun 22, 2021
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.

Write out combined EEG and iEEG recordings
4 participants