-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deploying to gh-pages from @ 5864336 🚀
- Loading branch information
0 parents
commit 10d5609
Showing
482 changed files
with
156,596 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
pip-wheel-metadata/ | ||
share/python-wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
MANIFEST | ||
_build/ | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
doc/ | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.nox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
*.py,cover | ||
.hypothesis/ | ||
.pytest_cache/ | ||
junit-results.xml | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
db.sqlite3 | ||
db.sqlite3-journal | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# IPython | ||
profile_default/ | ||
ipython_config.py | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# pipenv | ||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
# However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
# having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
# install all needed dependencies. | ||
#Pipfile.lock | ||
|
||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
__pypackages__/ | ||
|
||
# Celery stuff | ||
celerybeat-schedule | ||
celerybeat.pid | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# Environments | ||
.env | ||
.venv | ||
env/ | ||
venv/ | ||
ENV/ | ||
env.bak/ | ||
venv.bak/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
.dmypy.json | ||
dmypy.json | ||
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
# macOS | ||
*.DS_Store | ||
|
||
# VSCode | ||
.vscode | ||
|
||
# IntelliJ | ||
.idea |
Empty file.
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,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 34d163b6fbb25ff21c3d8269485f4a40 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Binary file added
BIN
+9.82 KB
0.1/_downloads/07fcc19ba03226cd3d83d4e40ec44385/auto_examples_python.zip
Binary file not shown.
187 changes: 187 additions & 0 deletions
187
...ownloads/1c9eb06eb1731736c1c34358cab65d24/iclabel_automatic_artifact_correction_ica.ipynb
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,187 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib inline" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n\n# Repairing artifacts with ICA automatically using ICLabel Model\n\nThis tutorial covers automatically repairing signals using ICA with\nthe ICLabel model :footcite:`iclabel2019`, which originates in EEGLab.\nFor conceptual background on ICA, see `this scikit-learn tutorial\n<sphx_glr_auto_examples_decomposition_plot_ica_blind_source_separation.py>`.\nFor a basic understanding of how to use ICA to remove artifacts, see `the\ntutorial <https://mne.tools/stable/auto_tutorials/preprocessing/40_artifact_correction_ica.html>`_ in MNE-Python.\n\nWe begin as always by importing the necessary Python modules and loading some\n`example data <sample-dataset>`. Because ICA can be computationally\nintense, we'll also crop the data to 60 seconds; and to save ourselves from\nrepeatedly typing ``mne.preprocessing`` we'll directly import a few functions\nand classes from that submodule.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n\nimport mne\nfrom mne.preprocessing import ICA\n\nfrom mne_icalabel import label_components\n\nsample_data_folder = mne.datasets.sample.data_path()\nsample_data_raw_file = os.path.join(\n sample_data_folder, \"MEG\", \"sample\", \"sample_audvis_filt-0-40_raw.fif\"\n)\nraw = mne.io.read_raw_fif(sample_data_raw_file)\n\n# Here we'll crop to 60 seconds and drop gradiometer channels for speed\nraw.crop(tmax=60.0).pick_types(meg=\"mag\", eeg=True, stim=True, eog=True)\nraw.load_data()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"<div class=\"alert alert-info\"><h4>Note</h4><p>Before applying ICA (or any artifact repair strategy), be sure to observe\n the artifacts in your data to make sure you choose the right repair tool.\n Sometimes the right tool is no tool at all \u2014 if the artifacts are small\n enough you may not even need to repair them to get good analysis results.\n See `tut-artifact-overview` for guidance on detecting and\n visualizing various types of artifact.</p></div>\n\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Example: EOG and ECG artifact repair\n# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n#\n# Visualizing the artifacts\n# ~~~~~~~~~~~~~~~~~~~~~~~~~\n#\n# Let's begin by visualizing the artifacts that we want to repair. In this\n# dataset they are big enough to see easily in the raw data:\n\n# Note: for this example, we are using ICLabel which has only\n# been validated and works for EEG systems with less than 32 electrodes.\nraw = raw.pick_types(eeg=True, eog=True, ecg=True, emg=True)\n\n# pick some channels that clearly show heartbeats and blinks\nregexp = r\"(EEG 00.)\"\nartifact_picks = mne.pick_channels_regexp(raw.ch_names, regexp=regexp)\nraw.plot(order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Filtering to remove slow drifts\n\nBefore we run the ICA, an important step is filtering the data to remove\nlow-frequency drifts, which can negatively affect the quality of the ICA fit.\nThe slow drifts are problematic because they reduce the independence of the\nassumed-to-be-independent sources (e.g., during a slow upward drift, the\nneural, heartbeat, blink, and other muscular sources will all tend to have\nhigher values), making it harder for the algorithm to find an accurate\nsolution. A high-pass filter with 1 Hz cutoff frequency is recommended.\nHowever, because filtering is a linear operation, the ICA solution found from\nthe filtered signal can be applied to the unfiltered signal (see\n:footcite:`WinklerEtAl2015` for\nmore information), so we'll keep a copy of the unfiltered\n`~mne.io.Raw` object around so we can apply the ICA solution to it\nlater.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"filt_raw = raw.copy().filter(l_freq=1.0, h_freq=None)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Fitting and plotting the ICA solution\n\n.. sidebar:: Ignoring the time domain\n\n The ICA algorithms implemented in MNE-Python find patterns across\n channels, but ignore the time domain. This means you can compute ICA on\n discontinuous `~mne.Epochs` or `~mne.Evoked` objects (not\n just continuous `~mne.io.Raw` objects), or only use every Nth\n sample by passing the ``decim`` parameter to ``ICA.fit()``.\n\n .. note:: `~mne.Epochs` used for fitting ICA should not be\n baseline-corrected. Because cleaning the data via ICA may\n introduce DC offsets, we suggest to baseline correct your data\n **after** cleaning (and not before), should you require\n baseline correction.\n\nNow we're ready to set up and fit the ICA. Since we know (from observing our\nraw data) that the EOG and ECG artifacts are fairly strong, we would expect\nthose artifacts to be captured in the first few dimensions of the PCA\ndecomposition that happens before the ICA. Therefore, we probably don't need\na huge number of components to do a good job of isolating our artifacts\n(though it is usually preferable to include more components for a more\naccurate solution). As a first guess, we'll run ICA with ``n_components=15``\n(use only the first 15 PCA components to compute the ICA decomposition) \u2014 a\nvery small number given that our data has over 300 channels, but with the\nadvantage that it will run quickly and we will able to tell easily whether it\nworked or not (because we already know what the EOG / ECG artifacts should\nlook like).\n\nICA fitting is not deterministic (e.g., the components may get a sign\nflip on different runs, or may not always be returned in the same order), so\nwe'll also specify a `random seed`_ so that we get identical results each\ntime this tutorial is built by our web servers.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"ica = ICA(n_components=15, max_iter=\"auto\", random_state=97)\nica.fit(filt_raw)\nica" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Some optional parameters that we could have passed to the\n`~mne.preprocessing.ICA.fit` method include ``decim`` (to use only\nevery Nth sample in computing the ICs, which can yield a considerable\nspeed-up) and ``reject`` (for providing a rejection dictionary for maximum\nacceptable peak-to-peak amplitudes for each channel type, just like we used\nwhen creating epoched data in the `tut-overview` tutorial).\n\nNow we can examine the ICs to see what they captured.\n`~mne.preprocessing.ICA.plot_sources` will show the time series of the\nICs. Note that in our call to `~mne.preprocessing.ICA.plot_sources` we\ncan use the original, unfiltered `~mne.io.Raw` object:\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"raw.load_data()\nica.plot_sources(raw, show_scrollbars=False)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Here we can pretty clearly see that the first component (``ICA000``) captures\nthe EOG signal quite well, and the second component (``ICA001``) looks a lot\nlike `a heartbeat <qrs_>`_ (for more info on visually identifying Independent\nComponents, `this EEGLAB tutorial`_ is a good resource). We can also\nvisualize the scalp field distribution of each component using\n`~mne.preprocessing.ICA.plot_components`. These are interpolated based\non the values in the ICA mixing matrix:\n\n.. LINKS\n\n https://en.wikipedia.org/wiki/Signal_separation\n https://en.wikipedia.org/wiki/Independence_(probability_theory)\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# sphinx_gallery_thumbnail_number = 9\nica.plot_components()\n\n# blinks\nica.plot_overlay(raw, exclude=[0], picks=\"eeg\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We can also plot some diagnostics of each IC using\n`~mne.preprocessing.ICA.plot_properties`:\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"ica.plot_properties(raw, picks=[0, 1])\n\n# Selecting ICA components automatically\n# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n#\n# Now that we've explored what components need to be removed, we can\n# apply the automatic ICA component labeling algorithm, which will\n# assign a probability value for each component being one of:\n#\n# - brain\n# - muscle artifact\n# - eye blink\n# - heart beat\n# - line noise\n# - channel noise\n# - other\n#\n# The output of the ICLabel ``label_components`` function produces\n# predicted probability values for each of these classes in that order.\n#\n# To start this process, we will compute features of each ICA\n# component to be fed into our classification model. This is\n# done automatically underneath the hood. An autocorrelation,\n# power spectral density and topographic map feature is fed\n# into a 3-head neural network that has been pretrained.\n# See :footcite:`iclabel2019` for full details.\n\nic_labels = label_components(raw, ica, method=\"iclabel\")\nprint(ic_labels)\n\n# We can extract the labels of each component and exclude\n# non-brain classified components, keeping 'brain' and 'other'.\n# \"Other\" is a catch-all that for non-classifiable components.\n# We will ere on the side of caution and assume we cannot blindly remove these.\nlabels = ic_labels[\"labels\"]\nexclude_idx = [idx for idx, label in enumerate(labels) if label not in [\"brain\", \"other\"]]\nprint(f\"Excluding these ICA components: {exclude_idx}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Now that the exclusions have been set, we can reconstruct the sensor signals\nwith artifacts removed using the `~mne.preprocessing.ICA.apply` method\n(remember, we're applying the ICA solution from the *filtered* data to the\noriginal *unfiltered* signal). Plotting the original raw data alongside the\nreconstructed data shows that the heartbeat and blink artifacts are repaired.\n\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# ica.apply() changes the Raw object in-place, so let's make a copy first:\nreconst_raw = raw.copy()\nica.apply(reconst_raw, exclude=exclude_idx)\n\nraw.plot(order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False)\nreconst_raw.plot(order=artifact_picks, n_channels=len(artifact_picks), show_scrollbars=False)\ndel reconst_raw" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### References\n.. footbibliography::\n\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.8.13" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
Oops, something went wrong.