.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_ecg_detection.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_ecg_detection.py: Comparing Picard and FastICA for the task of detecting ECG artifacts in MEG =========================================================================== Picard and FastICA are fitted to MEG data, from several initializations. The scores related to the detection of ECG artifacts are displayed for each run. Picard is faster, and less dependent on the initialization. .. GENERATED FROM PYTHON SOURCE LINES 10-21 .. code-block:: default # Authors: Pierre Ablin # Alexandre Gramfort # # License: BSD (3-clause) from time import time import mne from mne.preprocessing import ICA from mne.preprocessing import create_ecg_epochs from mne.datasets import sample .. GENERATED FROM PYTHON SOURCE LINES 22-23 Setup paths and prepare raw data. .. GENERATED FROM PYTHON SOURCE LINES 23-43 .. code-block:: default mne.set_log_level(verbose='CRITICAL') 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, preload=True) raw.filter(1, None, fir_design='firwin') # already lowpassed @ 40 raw.set_annotations(mne.Annotations([1], [10], 'BAD')) # raw.plot(block=True) # For the sake of example we annotate first 10 seconds of the recording as # 'BAD'. This part of data is excluded from the ICA decomposition by default. # To turn this behavior off, pass ``reject_by_annotation=False`` to # :meth:`mne.preprocessing.ICA.fit`. raw.set_annotations(mne.Annotations([0], [10], 'BAD')) picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') .. GENERATED FROM PYTHON SOURCE LINES 44-46 Define a function that fits ICA to the data, identifies bad components related to ECG, and plots the resulting scores and topographies. .. GENERATED FROM PYTHON SOURCE LINES 46-63 .. code-block:: default def fit_ica_and_plot_scores(method, random_state, tol=1e-4): ica = ICA(n_components=0.95, method=method, random_state=random_state, fit_params={'tol': tol}, max_iter=400) t0 = time() ica.fit(raw.copy(), picks=picks, decim=3, reject=dict(mag=4e-12, grad=4000e-13), verbose='warning') fit_time = time() - t0 title = ('Method : %s, init %d. Took %.2g sec.' % (method, random_state + 1, fit_time)) ecg_epochs = create_ecg_epochs(raw, tmin=-.5, tmax=.5, picks=picks) ecg_inds, scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ica.plot_scores(scores, exclude=ecg_inds, title=title) ica.plot_components(ecg_inds, colorbar=True) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Fit ICA using FastICA, with a few iterations, for different initializations : .. GENERATED FROM PYTHON SOURCE LINES 65-71 .. code-block:: default n_inits = 3 method = 'fastica' for random_state in range(n_inits): fit_ica_and_plot_scores(method, random_state) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_001.png :alt: Method : fastica, init 1. Took 62 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_001.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_002.png :alt: ICA components, ICA005 (mag), ICA000 (mag), ICA007 (mag), AU, AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_002.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_003.png :alt: Method : fastica, init 2. Took 57 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_003.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_004.png :alt: ICA components, ICA005 (mag), ICA000 (mag), AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_004.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_005.png :alt: Method : fastica, init 3. Took 54 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_005.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_006.png :alt: ICA components, ICA005 (mag), ICA000 (mag), AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_006.png :class: sphx-glr-multi-img .. rst-class:: sphx-glr-script-out .. code-block:: none /home/circleci/.local/lib/python3.8/site-packages/sklearn/decomposition/_fastica.py:120: ConvergenceWarning: FastICA did not converge. Consider increasing tolerance or the maximum number of iterations. warnings.warn( /home/circleci/.local/lib/python3.8/site-packages/sklearn/decomposition/_fastica.py:120: ConvergenceWarning: FastICA did not converge. Consider increasing tolerance or the maximum number of iterations. warnings.warn( /home/circleci/.local/lib/python3.8/site-packages/sklearn/decomposition/_fastica.py:120: ConvergenceWarning: FastICA did not converge. Consider increasing tolerance or the maximum number of iterations. warnings.warn( .. GENERATED FROM PYTHON SOURCE LINES 72-73 Do the same thing with Picard : .. GENERATED FROM PYTHON SOURCE LINES 73-79 .. code-block:: default method = 'picard' for random_state in range(n_inits): fit_ica_and_plot_scores(method, random_state) .. rst-class:: sphx-glr-horizontal * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_007.png :alt: Method : picard, init 1. Took 48 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_007.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_008.png :alt: ICA components, ICA005 (mag), ICA000 (mag), AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_008.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_009.png :alt: Method : picard, init 2. Took 41 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_009.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_010.png :alt: ICA components, ICA004 (mag), ICA000 (mag), AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_010.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_011.png :alt: Method : picard, init 3. Took 38 sec. :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_011.png :class: sphx-glr-multi-img * .. image-sg:: /auto_examples/images/sphx_glr_plot_ecg_detection_012.png :alt: ICA components, ICA004 (mag), ICA000 (mag), AU, AU :srcset: /auto_examples/images/sphx_glr_plot_ecg_detection_012.png :class: sphx-glr-multi-img .. GENERATED FROM PYTHON SOURCE LINES 80-83 The third topography found by FastICA does not really look like an ECG artifact, and is not consistent across different initializations. Picard finds the same topographies each time, and more consistently. .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 5 minutes 20.973 seconds) .. _sphx_glr_download_auto_examples_plot_ecg_detection.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ecg_detection.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ecg_detection.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_