Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions doc/manual/channel_interpolation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
.. _channel_interpolation:

.. contents:: Contents
:local:
:depth: 2

Repairing bad channels
######################

Spherical spline interpolation (EEG)
====================================

In short, data repair using spherical spline interpolation [1]_ consists of the following steps:

* Project the good and bad electrodes onto a unit sphere
* Compute a mapping matrix that maps :math:`N` good channels to :math:`M` bad channels
* Use this mapping matrix to compute interpolated data in the bad channels

Spherical splines assume that the potential :math:`V(\boldsymbol{r_i})` at any point :math:`\boldsymbol{r_i}` on the surface of the sphere can be represented by:

.. math:: V(\boldsymbol{r_i}) = c_0 + \sum_{j=1}^{N}c_{i}g_{m}(cos(\boldsymbol{r_i}, \boldsymbol{r_{j}}))
:label: model

where the :math:`C = (c_{1}, ..., c_{N})^{T}` are constants which must be estimated. The function :math:`g_{m}(\cdot)` of order :math:`m` is given by:

.. math:: g_{m}(x) = \frac{1}{4 \pi}\sum_{n=1}^{\infty} \frac{2n + 1}{(n(n + 1))^m}P_{n}(x)
:label: legendre

where :math:`P_{n}(x)` are `Legendre polynomials`_ of order `n`.

.. _Legendre polynomials: https://en.wikipedia.org/wiki/Legendre_polynomials

To estimate the constants :math:`C`, we must solve the following two equations simultaneously:

.. math:: G_{ss}C + T_{s}c_0 = X
:label: matrix_form

.. math:: {T_s}^{T}C = 0
:label: constraint

where :math:`G_{ss} \in R^{N \times N}` is a matrix whose entries are :math:`G_{ss}[i, j] = g_{m}(cos(\boldsymbol{r_i}, \boldsymbol{r_j}))` and :math:`X \in R^{N \times 1}` are the potentials :math:`V(\boldsymbol{r_i})` measured at the good channels. :math:`T_{s} = (1, 1, ..., 1)^T` is a column vector of dimension :math:`N`. Equation :eq:`matrix_form` is the matrix formulation of Equation :eq:`model` and equation :eq:`constraint` is like applying an average reference to the data. From equation :eq:`matrix_form` and :eq:`constraint`, we get:

.. math:: \begin{bmatrix} c_0 \\ C \end{bmatrix} = {\begin{bmatrix} {T_s}^{T} && 0 \\ T_s && G_{ss} \end{bmatrix}}^{-1} \begin{bmatrix} 0 \\ X \end{bmatrix} = C_{i}X
:label: estimate_constant

:math:`C_{i}` is the same as matrix :math:`{\begin{bmatrix} {T_s}^{T} && 0 \\ T_s && G_{ss} \end{bmatrix}}^{-1}` but with its first column deleted, therefore giving a matrix of dimension :math:`(N + 1) \times N`.

Now, to estimate the potentials :math:`\hat{X} \in R^{M \times 1}` at the bad channels, we have to do:

.. math:: \hat{X} = G_{ds}C + T_{d}c_0
:label: estimate_data

where :math:`G_{ds} \in R^{M \times N}` computes :math:`g_{m}(\boldsymbol{r_i}, \boldsymbol{r_j})` between the bad and good channels. :math:`T_{d} = (1, 1, ..., 1)^T` is a column vector of dimension :math:`M`. Plugging in equation :eq:`estimate_constant` in :eq:`estimate_data`, we get

.. math:: \hat{X} = \begin{bmatrix} T_d && G_{ds} \end{bmatrix} \begin{bmatrix} c_0 \\ C \end{bmatrix} = \underbrace{\begin{bmatrix} T_d && G_{ds} \end{bmatrix} C_{i}}_\text{mapping matrix}X


To interpolate bad channels, one can simply do:

>>> evoked.interpolate_bads(reset_bads=False)

and the bad channel will be fixed

.. image:: ../../_images/sphx_glr_plot_interpolate_bad_channels_002.png
:align: center
:height: 300 px

.. topic:: Examples:

* :ref:`sphx_glr_auto_examples_preprocessing_plot_interpolate_bad_channels.py`

References
==========
.. [1] Perrin, F., Pernier, J., Bertrand, O. and Echallier, JF. (1989). Spherical splines for scalp potential and current density mapping. Electroencephalography Clinical Neurophysiology, Feb; 72(2):184-7.
1 change: 1 addition & 0 deletions doc/manual/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Dealing with artifacts and noise sources in data.
preprocessing/ica
preprocessing/maxwell
preprocessing/ssp
channel_interpolation

Source localization
-------------------
Expand Down
2 changes: 2 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ BUG

- Time-cropping functions (e.g., :func:`mne.Epochs.crop`, :func:`mne.Evoked.crop`, :func:`mne.io.Raw.crop`, :func:`mne.SourceEstimate.crop`) made consistent with behavior of ``tmin`` and ``tmax`` of :class:`mne.Epochs`, where nearest sample is kept. For example, for MGH data acquired with ``sfreq=600.614990234``, constructing ``Epochs(..., tmin=-1, tmax=1)`` has bounds ``+/-1.00064103``, and now ``epochs.crop(-1, 1)`` will also have these bounds (previously they would have been ``+/-0.99897607``). This also has minor effects on functions that use cropping under the hood, such as :func:`mne.compute_covariance` and :func:`mne.connectivity.spectral_connectivity`.

- Fix EEG spherical spline interpolation code to account for average reference by `Mainak Jas`_ (`#2758 <https://github.com/mne-tools/mne-python/pull/2758>`_)

API
~~~

Expand Down
32 changes: 8 additions & 24 deletions mne/channels/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,6 @@ def _calc_g(cosang, stiffness=4, num_lterms=50):
return legval(cosang, [0] + factors)


def _calc_h(cosang, stiffness=4, num_lterms=50):
"""Calculate spherical spline h function between points on a sphere.

Parameters
----------
cosang : array-like of float, shape(n_channels, n_channels)
cosine of angles between pairs of points on a spherical surface. This
is equivalent to the dot product of unit vectors.
stiffness : float
stiffness of the spline. Also referred to as `m`.
num_lterms : int
number of Legendre terms to evaluate.
H : np.ndrarray of float, shape(n_channels, n_channels)
The H matrix.
"""
factors = [(2 * n + 1) /
(n ** (stiffness - 1) * (n + 1) ** (stiffness - 1) * 4 * np.pi)
for n in range(1, num_lterms + 1)]
return legval(cosang, [0] + factors)


def _make_interpolation_matrix(pos_from, pos_to, alpha=1e-5):
"""Compute interpolation matrix based on spherical splines

Expand Down Expand Up @@ -95,13 +74,18 @@ def _make_interpolation_matrix(pos_from, pos_to, alpha=1e-5):
cosang_from = pos_from.dot(pos_from.T)
cosang_to_from = pos_to.dot(pos_from.T)
G_from = _calc_g(cosang_from)
G_to_from, H_to_from = (f(cosang_to_from) for f in (_calc_g, _calc_h))
G_to_from = _calc_g(cosang_to_from)

if alpha is not None:
G_from.flat[::len(G_from) + 1] += alpha

C_inv = linalg.pinv(G_from)
interpolation = G_to_from.dot(C_inv)
n_channels = G_from.shape[0] # G_from should be square matrix
C = np.r_[np.c_[G_from, np.ones((n_channels, 1))],
np.c_[np.ones((1, n_channels)), 0]]
C_inv = linalg.pinv(C)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a change although valid.

you should document it as a BUG fix...

@dengemann ok for you?

maybe @wmvanvliet has an opinion too ...


interpolation = np.c_[G_to_from,
np.ones((G_to_from.shape[0], 1))].dot(C_inv[:, :-1])
return interpolation


Expand Down