diff --git a/doc/whats_new.rst b/doc/whats_new.rst index 8aa1676a521..58fde222f18 100644 --- a/doc/whats_new.rst +++ b/doc/whats_new.rst @@ -19,7 +19,9 @@ Current Changelog ~~~~~~~~~ -- Add new tutorial on :ref:`sphx_glr_auto_tutorials_plot_eeg_no_mri.py` by `Alex Gramfort`_, and `Joan Massich`_ +- Add example on how to load standard montage :ref:`plot_montage` by `Joan Massich`_ + +- Add new tutorial on :ref:`plot_eeg_no_mri` by `Alex Gramfort`_, and `Joan Massich`_ - Add convenience ``fsaverage`` subject dataset fetcher / updater :func:`mne.datasets.fetch_fsaverage` by `Eric Larson`_ diff --git a/examples/visualization/plot_montage.py b/examples/visualization/plot_montage.py new file mode 100644 index 00000000000..3af9779a554 --- /dev/null +++ b/examples/visualization/plot_montage.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +""" +.. _plot_montage: + +Plotting sensor layouts of EEG Systems +====================================== + +This example illustrates how to load all the EEG system montages +shipped in MNE-python, and display it on fsaverage template. +""" # noqa: D205, D400 +# Authors: Alexandre Gramfort +# Joan Massich +# +# License: BSD Style. + +from mayavi import mlab +import os.path as op + +import mne +from mne.channels.montage import get_builtin_montages +from mne.datasets import fetch_fsaverage +from mne.viz import plot_alignment + +subjects_dir = op.dirname(fetch_fsaverage()) + +############################################################################### +# check all montages +# + +for current_montage in get_builtin_montages(): + + montage = mne.channels.read_montage(current_montage, + unit='auto', + transform=False) + + info = mne.create_info(ch_names=montage.ch_names, + sfreq=1, + ch_types='eeg', + montage=montage) + + fig = plot_alignment(info, trans=None, + subject='fsaverage', + subjects_dir=subjects_dir, + eeg=['projected'], + ) + mlab.view(135, 80) + mlab.title(montage.kind, figure=fig) diff --git a/mne/channels/montage.py b/mne/channels/montage.py index 28775b5a8b0..c0c809087f1 100644 --- a/mne/channels/montage.py +++ b/mne/channels/montage.py @@ -26,7 +26,7 @@ from ..io.open import fiff_open from ..io.constants import FIFF from ..utils import (_check_fname, warn, copy_function_doc_to_method_doc, - _clean_names) + _clean_names, _check_option) from .layout import _pol_to_cart, _cart_to_sph @@ -132,9 +132,9 @@ def read_montage(kind, ch_names=None, path=None, unit='m', transform=False): path : str | None The path of the folder containing the montage file. Defaults to the mne/channels/data/montages folder in your mne-python installation. - unit : 'm' | 'cm' | 'mm' - Unit of the input file. If not 'm' (default), coordinates will be - rescaled to 'm'. + unit : 'm' | 'cm' | 'mm' | 'auto' + Unit of the input file. When 'auto' the montage is normalized to + a sphere of radius equal to the average brain size. Defaults to 'auto'. transform : bool If True, points will be transformed to Neuromag space. The fidicuals, 'nasion', 'lpa', 'rpa' must be specified in the montage file. Useful @@ -209,6 +209,8 @@ def read_montage(kind, ch_names=None, path=None, unit='m', transform=False): .. versionadded:: 0.9.0 """ + _check_option('unit', unit, ['mm', 'cm', 'm', 'auto']) + if path is None: path = op.join(op.dirname(__file__), 'data', 'montages') if not op.isabs(kind): @@ -332,12 +334,16 @@ def read_montage(kind, ch_names=None, path=None, unit='m', transform=False): kind) selection = np.arange(len(pos)) - if unit == 'mm': + if unit == 'auto': # rescale to 0.085 + pos -= np.mean(pos, axis=0) + pos = 0.085 * (pos / np.linalg.norm(pos, axis=1).mean()) + elif unit == 'mm': pos /= 1e3 elif unit == 'cm': pos /= 1e2 - elif unit != 'm': - raise ValueError("'unit' should be either 'm', 'cm', or 'mm'.") + elif unit == 'm': # montage is supposed to be in m + pass + names_lower = [name.lower() for name in list(ch_names_)] fids = {key: pos[names_lower.index(fid_names[ii])] if fid_names[ii] in names_lower else None diff --git a/tutorials/plot_eeg_no_mri.py b/tutorials/plot_eeg_no_mri.py index 7125eedfac6..bf9dd0d6850 100644 --- a/tutorials/plot_eeg_no_mri.py +++ b/tutorials/plot_eeg_no_mri.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- """ +.. _plot_eeg_no_mri: + EEG forward operator with a template MRI ======================================== @@ -10,6 +12,8 @@ subject will be less accurate. Do not over interpret activity locations which can be off by multiple centimeters. +.. note:: :ref:`plot_montage` show all the standard montages in MNE-Python. + .. contents:: This tutorial covers: :local: :depth: 2