{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# From raw data to dSPM on SPM Faces dataset\n\nRuns a full pipeline using MNE-Python. This example does quite a bit of processing, so\neven on a fast machine it can take several minutes to complete.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: Alexandre Gramfort \n# Denis Engemann \n#\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors.\n\nimport mne\nfrom mne import combine_evoked, io\nfrom mne.datasets import spm_face\nfrom mne.minimum_norm import apply_inverse, make_inverse_operator\nfrom mne.preprocessing import ICA, create_eog_epochs\n\nprint(__doc__)\n\ndata_path = spm_face.data_path()\nsubjects_dir = data_path / \"subjects\"\nspm_path = data_path / \"MEG\" / \"spm\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load data, filter it, and fit ICA.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "raw_fname = spm_path / \"SPM_CTF_MEG_example_faces1_3D.ds\"\nraw = io.read_raw_ctf(raw_fname, preload=True) # Take first run\n# Here to save memory and time we'll downsample heavily -- this is not\n# advised for real data as it can effectively jitter events!\nraw.resample(100)\nraw.filter(1.0, None) # high-pass\nreject = dict(mag=5e-12)\nica = ICA(n_components=0.95, max_iter=\"auto\", random_state=0)\nica.fit(raw, reject=reject)\n# compute correlation scores, get bad indices sorted by score\neog_epochs = create_eog_epochs(raw, ch_name=\"MRT31-2908\", reject=reject)\neog_inds, eog_scores = ica.find_bads_eog(eog_epochs, ch_name=\"MRT31-2908\")\nica.plot_scores(eog_scores, eog_inds) # see scores the selection is based on\nica.plot_components(eog_inds) # view topographic sensitivity of components\nica.exclude += eog_inds[:1] # we saw the 2nd ECG component looked too dipolar\nica.plot_overlay(eog_epochs.average()) # inspect artifact removal" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Epoch data and apply ICA.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "events = mne.find_events(raw, stim_channel=\"UPPT001\")\nevent_ids = {\"faces\": 1, \"scrambled\": 2}\ntmin, tmax = -0.2, 0.6\nepochs = mne.Epochs(\n raw,\n events,\n event_ids,\n tmin,\n tmax,\n picks=\"meg\",\n baseline=None,\n preload=True,\n reject=reject,\n)\ndel raw\nica.apply(epochs) # clean data, default in place\nevoked = [epochs[k].average() for k in event_ids]\ncontrast = combine_evoked(evoked, weights=[-1, 1]) # Faces - scrambled\nevoked.append(contrast)\nfor e in evoked:\n e.plot(ylim=dict(mag=[-400, 400]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Estimate noise covariance and look at the whitened evoked data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "noise_cov = mne.compute_covariance(epochs, tmax=0, method=\"shrunk\", rank=None)\nevoked[0].plot_white(noise_cov)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute forward model\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trans_fname = spm_path / \"SPM_CTF_MEG_example_faces1_3D_raw-trans.fif\"\nsrc = subjects_dir / \"spm\" / \"bem\" / \"spm-oct-6-src.fif\"\nbem = subjects_dir / \"spm\" / \"bem\" / \"spm-5120-5120-5120-bem-sol.fif\"\nforward = mne.make_forward_solution(contrast.info, trans_fname, src, bem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute inverse solution and plot\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "snr = 3.0\nlambda2 = 1.0 / snr**2\ninverse_operator = make_inverse_operator(contrast.info, forward, noise_cov)\nstc = apply_inverse(contrast, inverse_operator, lambda2, method=\"dSPM\", pick_ori=None)\nbrain = stc.plot(\n hemi=\"both\",\n subjects_dir=subjects_dir,\n initial_time=0.170,\n views=[\"ven\"],\n clim={\"kind\": \"value\", \"lims\": [3.0, 6.0, 9.0]},\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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }