-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: muscle artifact detection (#7407)
* muscle artifact detection * docstrings * docstrings * Alex suggestions Co-Authored-By: Alexandre Gramfort <[email protected]> * example * example * for eeg * skip_by_annotation * new func added * docstring * cross ref * cross ref2 * Alex suggestions Co-Authored-By: Alexandre Gramfort <[email protected]> * new parameters * docstring * API name change, zscore * test update * sqrt nchans * handle nans zscore * example note * Apply suggestions from code review drammock Co-Authored-By: Daniel McCloy <[email protected]> * revision changes * docstring * docstring * ch_type param * logger * docstrings * ch type * test no meeg data * test no meeg data * fix docstring Co-authored-by: Alexandre Gramfort <[email protected]> Co-authored-by: Daniel McCloy <[email protected]>
- Loading branch information
1 parent
ab50b23
commit e3cc4b1
Showing
8 changed files
with
232 additions
and
4 deletions.
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
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 @@ | ||
""" | ||
=========================== | ||
Annotate muscle artifacts | ||
=========================== | ||
Muscle contractions produce high frequency activity that can mask brain signal | ||
of interest. Muscle artifacts can be produced when clenching the jaw, | ||
swallowing, or twitching a cranial muscle. Muscle artifacts are most | ||
noticeable in the range of 110-140 Hz. | ||
This example uses :func:`~mne.preprocessing.annotate_muscle_zscore` to annotate | ||
segments where muscle activity is likely present. This is done by band-pass | ||
filtering the data in the 110-140 Hz range. Then, the envelope is taken using | ||
the hilbert analytical signal to only consider the absolute amplitude and not | ||
the phase of the high frequency signal. The envelope is z-scored and summed | ||
across channels and divided by the square root of the number of channels. | ||
Because muscle artifacts last several hundred milliseconds, a low-pass filter | ||
is applied on the averaged z-scores at 4 Hz, to remove transient peaks. | ||
Segments above a set threshold are annotated as ``BAD_muscle``. In addition, | ||
the ``min_length_good`` parameter determines the cutoff for whether short | ||
spans of "good data" in between muscle artifacts are included in the | ||
surrounding "BAD" annotation. | ||
""" | ||
# Authors: Adonay Nunes <[email protected]> | ||
# Luke Bloy <[email protected]> | ||
# License: BSD (3-clause) | ||
|
||
import os.path as op | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
from mne.datasets.brainstorm import bst_auditory | ||
from mne.io import read_raw_ctf | ||
from mne.preprocessing import annotate_muscle_zscore | ||
|
||
|
||
# Load data | ||
data_path = bst_auditory.data_path() | ||
raw_fname = op.join(data_path, 'MEG', 'bst_auditory', 'S01_AEF_20131218_01.ds') | ||
|
||
raw = read_raw_ctf(raw_fname, preload=False) | ||
|
||
raw.crop(130, 160).load_data() # just use a fraction of data for speed here | ||
raw.resample(300, npad="auto") | ||
|
||
############################################################################### | ||
# Notch filter the data: | ||
# | ||
# .. note:: | ||
# If line noise is present, you should perform notch-filtering *before* | ||
# detecting muscle artifacts. See :ref:`tut-section-line-noise` for an | ||
# example. | ||
|
||
raw.notch_filter([50, 100]) | ||
|
||
############################################################################### | ||
|
||
# The threshold is data dependent, check the optimal threshold by plotting | ||
# ``scores_muscle``. | ||
threshold_muscle = 5 # z-score | ||
# Choose one channel type, if there are axial gradiometers and magnetometers, | ||
# select magnetometers as they are more sensitive to muscle activity. | ||
annot_muscle, scores_muscle = annotate_muscle_zscore( | ||
raw, ch_type="mag", threshold=threshold_muscle, min_length_good=0.2, | ||
filter_freq=[110, 140]) | ||
|
||
############################################################################### | ||
# Plot muscle z-scores across recording | ||
# -------------------------------------------------------------------------- | ||
|
||
fig, ax = plt.subplots() | ||
ax.plot(raw.times, scores_muscle) | ||
ax.axhline(y=threshold_muscle, color='r') | ||
ax.set(xlabel='time, (s)', ylabel='zscore', title='Muscle activity') | ||
############################################################################### | ||
# View the annotations | ||
# -------------------------------------------------------------------------- | ||
order = np.arange(144, 164) | ||
raw.set_annotations(annot_muscle) | ||
raw.plot(start=5, duration=20, order=order) |
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
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