-
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
Add standardized measurement error (SME) #12707
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
822a9d0
Add SME computation
cbrnr bd45131
Remove whitespace
cbrnr 7d5f936
Add SME to ignored words
cbrnr 98606c3
Add changelog entry
cbrnr fad910d
Fix docstring Notes section
cbrnr 86a06cf
Fix changelog entry
cbrnr 0bba061
Move to mne.stats.erp module
cbrnr fb2306d
Fix docstring
cbrnr 9cb5bad
Add to API docs summary
cbrnr 99ee15b
Add examples
cbrnr 67051ab
Fix rST
cbrnr c69f3cd
Fix docstring some more
cbrnr 638b7d7
Fix changelog entry
cbrnr 98b02fc
Create separate mne.stats.erp namespace
cbrnr 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 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 @@ | ||
Add :func:`~mne.stats.erp.compute_sme` to compute the analytical standardized measurement error (SME) as a data quality measure for ERP studies, by `Clemens Brunner`_. |
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 |
---|---|---|
|
@@ -38,3 +38,4 @@ pres | |
aas | ||
vor | ||
connec | ||
sme |
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,80 @@ | ||
import numpy as np | ||
|
||
from mne.utils import _validate_type | ||
|
||
|
||
def compute_sme(epochs, start=None, stop=None): | ||
"""Compute standardized measurement error (SME). | ||
|
||
The standardized measurement error :footcite:`LuckEtAl2021` can be used as a | ||
universal measure of data quality in ERP studies. | ||
|
||
Parameters | ||
---------- | ||
epochs : mne.Epochs | ||
The epochs containing the data for which to compute the SME. | ||
start : int | float | None | ||
Start time (in s) of the time window used for SME computation. If ``None``, use | ||
the start of the epoch. | ||
stop : int | float | None | ||
Stop time (in s) of the time window used for SME computation. If ``None``, use | ||
the end of the epoch. | ||
|
||
Returns | ||
------- | ||
sme : array, shape (n_channels,) | ||
SME in given time window for each channel. | ||
|
||
Notes | ||
----- | ||
Currently, only the mean value in the given time window is supported, meaning that | ||
the resulting SME is only valid in studies which quantify the amplitude of an ERP | ||
component as the mean within the time window (as opposed to e.g. the peak, which | ||
would require bootstrapping). | ||
|
||
References | ||
---------- | ||
.. footbibliography:: | ||
|
||
Examples | ||
-------- | ||
Given an :class:`~mne.Epochs` object, the SME for the entire epoch duration can be | ||
computed as follows: | ||
|
||
>>> compute_sme(epochs) # doctest: +SKIP | ||
|
||
However, the SME is best used to estimate the precision of a specific ERP measure, | ||
specifically the mean amplitude of an ERP component in a time window of interest. | ||
For example, the SME for the mean amplitude of the P3 component in the 300-500 ms | ||
time window could be computed as follows: | ||
|
||
>>> compute_sme(epochs, start=0.3, stop=0.5) # doctest: +SKIP | ||
|
||
Usually, it will be more informative to compute the SME for specific conditions | ||
separately. This can be done by selecting the epochs of interest as follows: | ||
|
||
>>> compute_sme(epochs["oddball"], 0.3, 0.5) # doctest: +SKIP | ||
|
||
Note that the SME will be reported for each channel separately. If you are only | ||
interested in a single channel (or a subset of channels), select the channels | ||
before computing the SME: | ||
|
||
>>> compute_sme(epochs.pick("Pz"), 0.3, 0.5) # doctest: +SKIP | ||
|
||
Selecting both conditions and channels is also possible: | ||
|
||
>>> compute_sme(epochs["oddball"].pick("Pz"), 0.3, 0.5) # doctest: +SKIP | ||
|
||
In any case, the output will be a NumPy array with the SME value for each channel. | ||
""" | ||
_validate_type(start, ("numeric", None), "start", "int or float") | ||
_validate_type(stop, ("numeric", None), "stop", "int or float") | ||
start = epochs.tmin if start is None else start | ||
stop = epochs.tmax if stop is None else stop | ||
if start < epochs.tmin: | ||
raise ValueError("start is out of bounds.") | ||
if stop > epochs.tmax: | ||
raise ValueError("stop is out of bounds.") | ||
|
||
data = epochs.get_data(tmin=start, tmax=stop) | ||
return data.mean(axis=2).std(axis=0) / np.sqrt(data.shape[0]) |
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,27 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from mne import Epochs, read_events | ||
from mne.io import read_raw_fif | ||
from mne.stats.erp import compute_sme | ||
|
||
base_dir = Path(__file__).parents[2] / "io" / "tests" / "data" | ||
raw = read_raw_fif(base_dir / "test_raw.fif") | ||
events = read_events(base_dir / "test-eve.fif") | ||
|
||
|
||
def test_compute_sme(): | ||
"""Test SME computation.""" | ||
epochs = Epochs(raw, events) | ||
sme = compute_sme(epochs, start=0, stop=0.1) | ||
assert sme.shape == (376,) | ||
|
||
with pytest.raises(TypeError, match="int or float"): | ||
compute_sme(epochs, "0", 0.1) | ||
with pytest.raises(TypeError, match="int or float"): | ||
compute_sme(epochs, 0, "0.1") | ||
with pytest.raises(ValueError, match="out of bounds"): | ||
compute_sme(epochs, -1.2, 0.3) | ||
with pytest.raises(ValueError, match="out of bounds"): | ||
compute_sme(epochs, -0.1, 0.8) |
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.
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.
... or something like this would work. Locally you can iteratively do
make html-noplot
(will take ~10 min the first time then should be quick on follow-up runs). I can push a commit if you want