{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n\n# Head model and forward computation\n\nThe aim of this tutorial is to be a getting started for forward computation.\n\nFor more extensive details and presentation of the general concepts for forward\nmodeling, see `ch_forward`.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The MNE-Python contributors.\n# License: BSD-3-Clause\n# Copyright the MNE-Python contributors." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import mne\nfrom mne.datasets import sample\n\ndata_path = sample.data_path()\n\n# the raw file containing the channel location + types\nsample_dir = data_path / \"MEG\" / \"sample\"\nraw_fname = sample_dir / \"sample_audvis_raw.fif\"\n# The paths to Freesurfer reconstructions\nsubjects_dir = data_path / \"subjects\"\nsubject = \"sample\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing the forward operator\n\nTo compute a forward operator we need:\n\n - a ``-trans.fif`` file that contains the coregistration info.\n - a source space\n - the :term:`BEM` surfaces\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute and visualize BEM surfaces\n\nThe :term:`BEM` surfaces are the triangulations of the interfaces between\ndifferent tissues needed for forward computation. These surfaces are for\nexample the inner skull surface, the outer skull surface and the outer skin\nsurface, a.k.a. scalp surface.\n\nComputing the BEM surfaces requires FreeSurfer and makes use of\nthe command-line tools `mne watershed_bem` or `mne flash_bem`, or\nthe related functions :func:`mne.bem.make_watershed_bem` or\n:func:`mne.bem.make_flash_bem`.\n\nHere we'll assume it's already computed. It takes a few minutes per subject.\n\nFor EEG we use 3 layers (inner skull, outer skull, and skin) while for\nMEG 1 layer (inner skull) is enough.\n\nLet's look at these surfaces. The function :func:`mne.viz.plot_bem`\nassumes that you have the ``bem`` folder of your subject's FreeSurfer\nreconstruction, containing the necessary surface files. Here we use a smaller\nthan default subset of ``slices`` for speed.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot_bem_kwargs = dict(\n subject=subject,\n subjects_dir=subjects_dir,\n brain_surfaces=\"white\",\n orientation=\"coronal\",\n slices=[50, 100, 150, 200],\n)\n\nmne.viz.plot_bem(**plot_bem_kwargs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing the coregistration\n\nThe coregistration is the operation that allows to position the head and the\nsensors in a common coordinate system. In the MNE software the transformation\nto align the head and the sensors in stored in a so-called **trans file**.\nIt is a FIF file that ends with ``-trans.fif``. It can be obtained with\n:func:`mne.gui.coregistration` (or its convenient command line\nequivalent `mne coreg`), or mrilab if you're using a Neuromag\nsystem.\n\nHere we assume the coregistration is done, so we just visually check the\nalignment with the following code. See `creating-trans` for instructions\non creating the ``-trans.fif`` file interactively.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# The transformation file obtained by coregistration\ntrans = sample_dir / \"sample_audvis_raw-trans.fif\"\n\ninfo = mne.io.read_info(raw_fname)\n# Here we look at the dense head, which isn't used for BEM computations but\n# is useful for coregistration.\nmne.viz.plot_alignment(\n info,\n trans,\n subject=subject,\n dig=True,\n meg=[\"helmet\", \"sensors\"],\n subjects_dir=subjects_dir,\n surfaces=\"head-dense\",\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n## Compute Source Space\n\nThe source space defines the position and orientation of the candidate source\nlocations. There are two types of source spaces:\n\n- **surface-based** source space when the candidates are confined to a\n surface.\n\n- **volumetric or discrete** source space when the candidates are discrete,\n arbitrarily located source points bounded by the surface.\n\n**Surface-based** source space is computed using\n:func:`mne.setup_source_space`, while **volumetric** source space is computed\nusing :func:`mne.setup_volume_source_space`.\n\nWe will now compute a surface-based source space with an ``'oct4'``\nresolution. See `setting_up_source_space` for details on source space\ndefinition and spacing parameter.\n\n
``'oct4'`` is used here just for speed, for real analyses the recommended\n spacing is ``'oct6'``.
Some sources may appear to be outside the BEM inner skull contour.\n This is because the ``slices`` are decimated for plotting here.\n Each slice in the figure actually represents several MRI slices,\n but only the MRI voxels and BEM boundaries for a single (midpoint\n of the given slice range) slice are shown, whereas the source space\n points plotted on that midpoint slice consist of all points\n for which that slice (out of all slices shown) was the closest.
Forward computation can remove vertices that are too close to (or outside)\n the inner skull surface. For example, here we have gone from 516 to 474\n vertices in use. For many functions, such as\n :func:`mne.compute_source_morph`, it is important to pass ``fwd['src']``\n or ``inv['src']`` so that this removal is adequately accounted for.