-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MRG+4: Epochs metadata #4414
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
Merged
Merged
MRG+4: Epochs metadata #4414
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
8c78cb5
meta epochs rough draft
choldgraf 3a26fc9
removing regression
choldgraf 1bdbc67
removing regress and adding I/O
choldgraf 65c56a4
adding tests and making changes per alex
choldgraf d0553b0
FIX: Many fixes
larsoner 3ac7b87
FIX: Fix endings
larsoner ea7f7f0
FIX: Minor fixes
larsoner ea926a6
FIX: Fix for old Pandas
larsoner fb8b11d
FIX: Tweak example
larsoner 757f0fd
FIX: Fixes for Py3k
larsoner 663dda2
FIX: Simpler records
larsoner 097f707
FIX: Fix for old Pandas
larsoner 6c1c9f7
FIX: No numexpr
larsoner ea3ce62
tutorial
choldgraf ead914c
FIX: Many fixes
larsoner c3be7cd
add ref info
jona-sassenhagen afdca11
add ref info
jona-sassenhagen 2686c02
FIX: Refs
larsoner e066cfd
jona comments
choldgraf a0a86a8
FIX: Minor tweaks
larsoner 4d401d0
docs
jona-sassenhagen 3f70019
FIX: Fix docstring
larsoner afc1b99
FIX: Fix flake
larsoner 60a4097
FIX: whats_new [ci skip]
larsoner afcbbff
FIX: Fix cmap for CircleCI
larsoner d35bee1
FIX: whats_new [ci skip]
larsoner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| """ | ||
| ================================== | ||
| Querying epochs with rich metadata | ||
| ================================== | ||
|
|
||
| Selecting a subset of epochs based on rich metadata. | ||
|
|
||
| MNE allows you to include metadata along with your :class:`mne.Epochs` objects. | ||
| This is in the form of a :class:`pandas.DataFrame` that has one row for each | ||
| event, and an arbitrary number of columns corresponding to different | ||
| features that were collected. Columns may be of type int, float, or str. | ||
|
|
||
| If an :class:`mne.Epochs` object has a metadata attribute, you can select | ||
| subsets of epochs by using pandas query syntax directly. Here we'll show | ||
| a few examples of how this looks. | ||
| """ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add authors + licence |
||
|
|
||
| # Authors: Chris Holdgraf <[email protected]> | ||
| # Jona Sassenhagen <[email protected]> | ||
| # Eric Larson <[email protected]> | ||
|
|
||
| # License: BSD (3-clause) | ||
|
|
||
| import os | ||
| import pandas as pd | ||
| import matplotlib.pyplot as plt | ||
| import mne | ||
|
|
||
| # First load some data | ||
| events = mne.read_events(os.path.join(mne.datasets.sample.data_path(), | ||
| 'MEG/sample/sample_audvis_raw-eve.fif')) | ||
| raw = mne.io.read_raw_fif(os.path.join(mne.datasets.sample.data_path(), | ||
| 'MEG/sample/sample_audvis_raw.fif')) | ||
|
|
||
| # We'll create some dummy names for each event type | ||
| event_id = {'Auditory/Left': 1, 'Auditory/Right': 2, | ||
| 'Visual/Left': 3, 'Visual/Right': 4, | ||
| 'smiley': 5, 'button': 32} | ||
| event_id_rev = {val: key for key, val in event_id.items()} | ||
|
|
||
| sides, kinds = [], [] | ||
| for ev in events: | ||
| split = event_id_rev[ev[2]].lower().split('/') | ||
| if len(split) == 2: | ||
| kind, side = split | ||
| else: | ||
| kind = split[0] | ||
| side = 'both' | ||
| kinds.append(kind) | ||
| sides.append(side) | ||
|
|
||
|
|
||
| # Here's a helper function we'll use later | ||
| def plot_query_results(query): | ||
| fig = epochs[query].average().plot(show=False) | ||
| title = fig.axes[0].get_title() | ||
| add = 'Query: {}\nNum Epochs: {}'.format(query, len(epochs[query])) | ||
| fig.axes[0].set(title='\n'.join([add, title])) | ||
| plt.show() | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # First we'll create our metadata object. This should be a | ||
| # :class:`pandas.DataFrame` with each row corresponding to an event. | ||
| # | ||
| # .. warning:: The Dataframe Index can change during MNE I/O operations, so | ||
| # do not rely on it to query your metadata. | ||
|
|
||
| metadata = {'event_time': events[:, 0] / raw.info['sfreq'], | ||
| 'trial_number': range(len(events)), | ||
| 'kind': kinds, | ||
| 'side': sides} | ||
| metadata = pd.DataFrame(metadata) | ||
| metadata.head() | ||
|
|
||
| ############################################################################### | ||
| # We can use this metadata object in the construction of an :class:`mne.Epochs` | ||
| # object. The metadata will then exist as an attribute: | ||
|
|
||
| epochs = mne.Epochs(raw, events, metadata=metadata, preload=True) | ||
| print(epochs.metadata.head()) | ||
|
|
||
| ############################################################################### | ||
| # You can select rows by passing various queries to the Epochs object. For | ||
| # example, you can select a subset of events based on the value of a column. | ||
|
|
||
| query = 'kind == "auditory"' | ||
| plot_query_results(query) | ||
|
|
||
| ############################################################################### | ||
| # If a column has numeric values, you can also use numeric-style queries: | ||
|
|
||
| query = 'trial_number < 10' | ||
| plot_query_results(query) | ||
|
|
||
| ############################################################################### | ||
| # It is possible to chain these queries together, giving you more expressive | ||
| # ways to select particular epochs: | ||
|
|
||
| query = 'trial_number < 10 and side == "left"' | ||
| plot_query_results(query) | ||
|
|
||
| ############################################################################### | ||
| # Any query that works with ``DataFrame.query`` will work for selecting epochs. | ||
|
|
||
| plot_events = ['smiley', 'button'] | ||
| query = 'kind in {}'.format(plot_events) | ||
| plot_query_results(query) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it'd be easier to do this last one in one line. |
||
This file contains hidden or 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 hidden or 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,3 @@ | ||
| """MNE visual_92_categories dataset.""" | ||
|
|
||
| from .kiloword import data_path, get_version |
This file contains hidden or 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,55 @@ | ||
| # License: BSD Style. | ||
|
|
||
| from ...utils import verbose | ||
| from ..utils import _data_path, _get_version, _version_doc | ||
|
|
||
|
|
||
| @verbose | ||
| def data_path(path=None, force_update=False, update_path=True, download=True, | ||
| verbose=None): | ||
| """ | ||
| Get path to local copy of the kiloword dataset. | ||
|
|
||
| This is the dataset from [1]_. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| path : None | str | ||
| Location of where to look for the kiloword data storing | ||
| location. If None, the environment variable or config parameter | ||
| MNE_DATASETS_KILOWORD_PATH is used. If it doesn't exist, | ||
| the "mne-python/examples" directory is used. If the | ||
| kiloword dataset is not found under the given path (e.g., | ||
| as "mne-python/examples/MNE-kiloword-data"), the data | ||
| will be automatically downloaded to the specified folder. | ||
| force_update : bool | ||
| Force update of the dataset even if a local copy exists. | ||
| update_path : bool | None | ||
| If True, set the MNE_DATASETS_KILOWORD_PATH in mne-python | ||
| config to the given path. If None, the user is prompted. | ||
| verbose : bool, str, int, or None | ||
| If not None, override default verbose level (see mne.verbose). | ||
|
|
||
| Returns | ||
| ------- | ||
| path : list of str | ||
| Local path to the given data file. This path is contained inside a list | ||
| of length one, for compatibility. | ||
|
|
||
| References | ||
| ---------- | ||
| .. [1] Dufau, S., Grainger, J., Midgley, KJ., Holcomb, PJ. A thousand | ||
| words are worth a picture: Snapshots of printed-word processing in an | ||
| event-related potential megastudy. Psychological science, 2015 | ||
| """ | ||
| return _data_path(path=path, force_update=force_update, | ||
| update_path=update_path, name='kiloword', | ||
| download=download) | ||
|
|
||
|
|
||
| def get_version(): | ||
| """Get dataset version.""" | ||
| return _get_version('kiloword') | ||
|
|
||
|
|
||
| get_version.__doc__ = _version_doc.format(name='kiloword') |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| """MNE visual_92_categories dataset.""" | ||
|
|
||
| from .visual_92_categories import (data_path, has_visual_92_categories_data, | ||
| get_version, | ||
| requires_visual_92_categories_data) | ||
| from .visual_92_categories import data_path, get_version |
This file contains hidden or 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
Oops, something went wrong.
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 you also cross refs the entry tutorial on epochs data container?