Repository navigation
MRG: spline interp fixes #2758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
MRG: spline interp fixes #2758
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3cfb8a4
MAINT: remove superfluous computation
jasmainak b766da9
DOC: Add explanation of interpolation code
jasmainak 0cc42b4
MAINT: name the file channel_interpolation
jasmainak 5e7c964
FIX: make interp code match 1989 paper
jasmainak 14ebd0e
More fixes
jasmainak fe1ae92
Address comments + fixes
jasmainak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ...