-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[WIP] Support exporting mne objects to xarray DataArray objects #11464
Open
mmagnuski
wants to merge
14
commits into
mne-tools:main
Choose a base branch
from
mmagnuski:support_xarray
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
200c121
MAINT: add xarray to requirements
mmagnuski 8d13c3b
ENH: firt sketch of xarray utils
mmagnuski 3422d79
TST: first sketch of a test
mmagnuski 3857b6f
MAINT: should xarray be added this way to github actions?
mmagnuski a933cdf
MAINT: requires_xarray, add docs, remove support for AverageTFR for now
mmagnuski b3fd2a2
ENH: add picks etc.
mmagnuski 5380036
ENH: add .to_xarray() to Evoked
mmagnuski 273b1ed
TST: update test
mmagnuski d7cbab6
STY: remove unused import
mmagnuski ec25a36
Merge branch 'main' into support_xarray
mmagnuski ff379dc
FIX: add fill_doc, remove copy arg
mmagnuski d0536c2
TST: test picks
mmagnuski 0c5494e
FIX: picks
mmagnuski f0ac552
Merge branch 'support_xarray' of https://github.com/mmagnuski/mne-pyt…
mmagnuski File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ dependencies: | |
- eeglabio | ||
- edflib-python | ||
- pybv | ||
- xarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
import mne | ||
from mne.utils import requires_xarray | ||
|
||
|
||
@requires_xarray | ||
def test_conversion_to_xarray(): | ||
"""Test conversion of mne object to xarray DataArray.""" | ||
import xarray as xr | ||
from mne.utils.xarray import to_xarray | ||
|
||
info = mne.create_info(list('abcd'), sfreq=250) | ||
data = np.random.rand(4, 350) | ||
erp = mne.EvokedArray(data, info, tmin=-0.5) | ||
|
||
erp_x = to_xarray(erp) | ||
assert isinstance(erp_x, xr.DataArray) | ||
assert erp_x.shape == (4, 350) | ||
assert erp_x.dims == ('chan', 'time') | ||
assert erp_x.coords['chan'].data.tolist() == erp.ch_names | ||
assert (erp_x.coords['time'].data == erp.times).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import numpy as np | ||
|
||
from .. import Epochs, Evoked | ||
from ..time_frequency import AverageTFR | ||
|
||
|
||
def to_xarray(mne_inst): | ||
"""Convert MNE object instance to xarray DataArray. | ||
|
||
Parameters | ||
---------- | ||
mne_inst : Epochs | Evoked | ||
The MNE object to convert. | ||
|
||
Returns | ||
------- | ||
xarr : DataArray | ||
The xarray object. | ||
""" | ||
from xarray import DataArray | ||
from mne.utils import _validate_type | ||
|
||
_validate_type(mne_inst, (Epochs, Evoked, AverageTFR)) | ||
|
||
if isinstance(mne_inst, Epochs): | ||
data = mne_inst.get_data() | ||
dims = ('chan', 'trial', 'time') | ||
elif isinstance(mne_inst, Evoked): | ||
data = mne_inst.data | ||
dims = ('chan', 'time') | ||
else: | ||
raise ValueError('MNE instance must be Epochs or Evoked.') | ||
|
||
coords = dict(chan=mne_inst.ch_names) | ||
if 'time' in dims: | ||
coords['time'] = mne_inst.times | ||
if 'trial' in dims: | ||
coords['trial'] = np.arange(mne_inst.n_epochs) | ||
|
||
return DataArray(data, dims=dims, coords=coords) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,4 @@ mne-qt-browser | |
darkdetect | ||
qdarkstyle | ||
threadpoolctl | ||
xarray |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we call the dimension "channel"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer shorter dimension names as you use them often during various operations, but I will change the name if this would be the common preference. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we're internally consistent on questions like this. E.g., in function names sometimes we use
channel
and sometimesch
. Also when exporting to data frames, the spectrum class makes a columnfreq
(notfrequency
). So I'm not really sure what to do WRT "channel". However, I would say thattrial
is not ideal, it should beepoch
(for e.g. resting state recordings cut into sequential chunks "trial" doesn't make sense)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, yes I will definitelly change
trial
toepoch
, especially that you can have multiple epochs from the same trial (for example successively presented items to remember).