Repository: mne-tools/mne-python

Issue:
combine ICA example into ICA tutorial
merge `examples/preprocessing/plot_run_ica.py` into `tutorials/preprocessing/plot_40_artifact_correction_ica.py`

probably easiest to add a new section to the end of the tutorial, saying something like "By the way, we've been doing ICA on raw, but FYI you can do it on Epochs too...".  Try to re-use whatever objects have already been created by the earlier steps of the tutorial, to minimize run time.


Discussion:
@neurogima 
yep I take this one

Resolving patch:
diff --git a/examples/preprocessing/plot_run_ica.py b/examples/preprocessing/plot_run_ica.py
deleted file mode 100644
--- a/examples/preprocessing/plot_run_ica.py
+++ /dev/null
@@ -1,64 +0,0 @@
-"""
-================================
-Compute ICA components on epochs
-================================
-
-ICA is fit to MEG raw data.
-We assume that the non-stationary EOG artifacts have already been removed.
-The sources matching the ECG are automatically found and displayed.
-
-.. note:: This example does quite a bit of processing, so even on a
-          fast machine it can take about a minute to complete.
-"""
-# Authors: Denis Engemann <denis.engemann@gmail.com>
-#
-# License: BSD (3-clause)
-
-import mne
-from mne.preprocessing import ICA, create_ecg_epochs
-from mne.datasets import sample
-
-print(__doc__)
-
-###############################################################################
-# Read and preprocess the data. Preprocessing consists of:
-#
-# - MEG channel selection
-# - 1-30 Hz band-pass filter
-# - epoching -0.2 to 0.5 seconds with respect to events
-# - rejection based on peak-to-peak amplitude
-
-data_path = sample.data_path()
-raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
-
-raw = mne.io.read_raw_fif(raw_fname)
-raw.pick_types(meg=True, eeg=False, exclude='bads', stim=True).load_data()
-raw.filter(1, 30, fir_design='firwin')
-
-# peak-to-peak amplitude rejection parameters
-reject = dict(grad=4000e-13, mag=4e-12)
-# longer + more epochs for more artifact exposure
-events = mne.find_events(raw, stim_channel='STI 014')
-epochs = mne.Epochs(raw, events, event_id=None, tmin=-0.2, tmax=0.5,
-                    reject=reject)
-
-###############################################################################
-# Fit ICA model using the FastICA algorithm, detect and plot components
-# explaining ECG artifacts.
-
-ica = ICA(n_components=0.95, method='fastica', max_iter='auto',
-          random_state=0)
-ica.fit(epochs)
-
-ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5)
-ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, threshold='auto')
-
-ica.plot_components(ecg_inds)
-
-###############################################################################
-# Plot properties of ECG components:
-ica.plot_properties(epochs, picks=ecg_inds)
-
-###############################################################################
-# Plot the estimated source of detected ECG related components:
-ica.plot_sources(raw, picks=ecg_inds)
diff --git a/tutorials/preprocessing/plot_40_artifact_correction_ica.py b/tutorials/preprocessing/plot_40_artifact_correction_ica.py
--- a/tutorials/preprocessing/plot_40_artifact_correction_ica.py
+++ b/tutorials/preprocessing/plot_40_artifact_correction_ica.py
@@ -437,7 +437,7 @@
 # before accepting them.
 
 # clean up memory before moving on
-del raw, filt_raw, ica, new_ica
+del raw, ica, new_ica
 
 ###############################################################################
 # Selecting ICA components using template matching
@@ -585,7 +585,56 @@
 # .. _`qrs`: https://en.wikipedia.org/wiki/QRS_complex
 # .. _`this EEGLAB tutorial`: https://labeling.ucsd.edu/tutorial/labels
 
+
+###############################################################################
+# Compute ICA components on epochs
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#
+# ICA is now fit to epoched MEG data instead of the raw data.
+# We assume that the non-stationary EOG artifacts have already been removed.
+# The sources matching the ECG are automatically found and displayed.
+#
+# .. note::
+#     This example is computationally intensive, so it might take a few minutes
+#     to complete.
+#
+# Read and preprocess the data. Preprocessing consists of:
+#
+# - MEG channel selection
+# - 1-30 Hz band-pass filter
+# - epoching -0.2 to 0.5 seconds with respect to events
+# - rejection based on peak-to-peak amplitude
+
+filt_raw.pick_types(meg=True, eeg=False, exclude='bads', stim=True).load_data()
+filt_raw.filter(1, 30, fir_design='firwin')
+
+# peak-to-peak amplitude rejection parameters
+reject = dict(grad=4000e-13, mag=4e-12)
+# create longer and more epochs for more artifact exposure
+events = mne.find_events(filt_raw, stim_channel='STI 014')
+epochs = mne.Epochs(filt_raw, events, event_id=None, tmin=-0.2, tmax=0.5,
+                    reject=reject)
+
+###############################################################################
+# Fit ICA model using the FastICA algorithm, detect and plot components
+# explaining ECG artifacts.
+
+ica = ICA(n_components=15, method='fastica', max_iter="auto").fit(epochs)
+
+ecg_epochs = create_ecg_epochs(filt_raw, tmin=-.5, tmax=.5)
+ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, threshold='auto')
+
+ica.plot_components(ecg_inds)
+
+###############################################################################
+# Plot the properties of the ECG components:
+ica.plot_properties(epochs, picks=ecg_inds)
+
+###############################################################################
+# Plot the estimated sources of detected ECG related components:
+ica.plot_sources(filt_raw, picks=ecg_inds)
+
 ###############################################################################
 # References
-# ----------
+# ^^^^^^^^^^
 # .. footbibliography::


Test patch: