{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Compare the different ICA algorithms in MNE\n\nDifferent ICA algorithms are fit to raw MEG data, and the corresponding maps\nare displayed.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Pierre Ablin \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from time import time\n\nimport mne\nfrom mne.datasets import sample\nfrom mne.preprocessing import ICA\n\nprint(__doc__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read and preprocess the data. Preprocessing consists of:\n\n- MEG channel selection\n- 1-30 Hz band-pass filter\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "data_path = sample.data_path()\nmeg_path = data_path / \"MEG\" / \"sample\"\nraw_fname = meg_path / \"sample_audvis_filt-0-40_raw.fif\"\n\nraw = mne.io.read_raw_fif(raw_fname).crop(0, 60).pick(\"meg\").load_data()\n\nreject = dict(mag=5e-12, grad=4000e-13)\nraw.filter(1, 30, fir_design=\"firwin\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function that runs ICA on the raw MEG data and plots the components\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def run_ica(method, fit_params=None):\n ica = ICA(\n n_components=20,\n method=method,\n fit_params=fit_params,\n max_iter=\"auto\",\n random_state=0,\n )\n t0 = time()\n ica.fit(raw, reject=reject)\n fit_time = time() - t0\n title = f\"ICA decomposition using {method} (took {fit_time:.1f}s)\"\n ica.plot_components(title=title)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "FastICA\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "run_ica(\"fastica\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Picard\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "run_ica(\"picard\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Infomax\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "run_ica(\"infomax\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Extended Infomax\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "run_ica(\"infomax\", fit_params=dict(extended=True))" ] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }