Skip to content

Restore PCA residual when applying AMICA with pcakeep < n_rank (or n_channel) #322

Closed

Description

@sunyuhongwr

Why this matters

This is particularly important when using pcakeep for dimensionality reduction. For example, suppose:
n_channels = 64
pcakeep = 20
The AMICA model represents only the 20-dimensional retained PCA subspace. However, the original MEG/EEG data still contains the remaining 44-dimensional PCA residual. Those 44 dimensions are not ICA components and should not be interpreted as components to remove. They are simply the part of the original signal that was outside the AMICA fitting subspace. But they could represent meaningful cognitive component.
Therefore, excluding an ICA component should conceptually perform:

Original data
↓
Preserve PCA residual
+
Remove selected ICA contribution
↓
Full-rank cleaned data

rather than:

Original data
↓
Project into PCA-20 space
↓
Remove ICA components
↓
Reconstruct only PCA-20 space
↓
Return rank-20 data

1. Store the retained PCA information during fitting

When dimensionality reduction is used (pcakeep < n_channels), pAMICA should store the information describing the retained PCA subspace as part of the fitted AMICA object.

For example, the fitted object could retain:

self.pca_basis_

with shape:

(n_channels, pcakeep)

where the columns span the PCA subspace retained by AMICA.

Alternatively, an equivalent PCA projection operator could be stored.

The important point is that this information should be part of the fitted model state, rather than recomputed from the input data during apply().

The full residual time series should not be stored, because this could require a large amount of memory. Only the information defining the retained PCA subspace needs to be stored.

For example:

Fit:
Original data
    |
    v
PCA decomposition
    |
    +-------------------+
    |                   |
    v                   v
Retained PCA space      PCA residual
    |                   |
    v                   |
   AMICA                 |
    |                   |
    +-------------------+
            |
            v
       Store PCA basis
       in fitted object

2. Modify AMICAICA.apply()

The apply() method could then expose an option similar to MNE's reconstruction behavior:

def apply(
    self,
    inst,
    *args,
    model_idx: int = 0,
    restore_pca_residual: bool = True,
    **kwargs,
):
    ...

When restore_pca_residual=False, the current behavior would be preserved.

When restore_pca_residual=True, the method would:

  1. Retrieve the retained PCA basis stored during fitting.
  2. Compute the PCA residual from the input data passed to apply().
  3. Apply the existing MNE ICA reconstruction to remove the selected ICA components.
  4. Add the unchanged PCA residual back to the reconstructed data.
  5. Return the full-rank sensor-space data.

Importantly, the residual should be calculated from the data passed to apply() rather than from data stored during fitting. This allows apply() to work with different time ranges and different supported MNE input objects without storing the entire training-data residual.

3. PCA residual calculation

If V is the retained PCA basis stored by the fitted model, the residual can be calculated as:

X_residual = (I - V V^T) X_c

where X_c is the centered and appropriately scaled input data.

The final reconstruction is then:

X_clean = mean + X_ica_clean + X_residual

The PCA residual is independent of ICA component rejection and should therefore remain unchanged regardless of which AMICA components are excluded.

4. Preserve the current behavior when no dimensionality reduction is used

When:

pcakeep == n_channels

there is no discarded PCA subspace, so no residual needs to be restored.

In this case:

X_residual = 0

and AMICAICA.apply() should behave exactly as it does currently.

5. Scaling must remain consistent

The stored PCA information and residual calculation must be consistent with the scaling used during fitting.

pAMICA currently applies channel-type scaling/whitening before the AMICA fit:

X_scaled = X / pre_whitener

Therefore, the PCA residual should be calculated in this same scaled space and converted back to the original sensor units before being added to the MNE ICA reconstruction.

This is important because otherwise the AMICA reconstruction and PCA residual would not be represented in the same units.

6. Suggested model state

Conceptually, the fitted object would contain something like:

self.pre_whitener_
self.pca_basis_
self.mean_
...

where:

pre_whitener_
    -> channel scaling used during fitting

mean_
    -> centering used during fitting

pca_basis_
    -> retained PCA subspace required to reconstruct
       the discarded PCA residual

The exact attribute name and whether this information should live in AMICAICA or the AMICA backend can be determined during implementation. The main requirement is that the information survives fitting and is available to apply().

Activity

  1. changed the title [-]Restore PCA residual when applying AMICA with `pcakeep < n_channels`[/-] [+]Restore PCA residual when applying AMICA with `pcakeep < n_rank`[/+] on Sep 3, 2026
  2. changed the title [-]Restore PCA residual when applying AMICA with `pcakeep < n_rank`[/-] [+]Restore PCA residual when applying AMICA with `pcakeep < n_rank` (or n_channel)[/+] on Sep 3, 2026
  3. neuromechanist commented on Sep 22, 2026

    @neuromechanist
    Member

    Thanks for the detailed write-up. This is fixed on dev in #325 (squash commit 42a59c2), and it follows your proposal closely, using MNE's own reconstruction mechanism rather than a new keyword.

    What changed. AMICAICA.fit now computes the full orthonormal PCA basis of the fit once, in the pre-whitened (channel-type scaled) space, and stores it as pca_components_ (n_channels x n_channels) and pca_explained_variance_. The first n_components_ rows are the retained subspace, taken from the fitted sphere exactly as before. For a rank-reduced fit, the remaining rows span the sphere's null space (the discarded residual), rotated to the eigenvectors of the fit covariance restricted to that subspace, so they are ordered by variance and their variances equal the discarded eigenvalues. to_mne_ica exports that full basis. MNE's ICA.apply already keeps rows past n_components_ as residual PCA components (n_pca_components=None keeps all of them), so apply now restores the residual by default: excluding a component removes that component's back-projection and nothing else, in the original units (the pre_whitener_ scaling is handled by MNE's own restore step). Nothing is recomputed from data at apply time and no time series is stored; the fit covariance is accumulated in column blocks, so this stays cheap for long 306-channel recordings.

    Reference behavior on request. ica.apply(raw, n_pca_components=ica.n_components_) reproduces the previous rank-reduced reconstruction bit for bit. A float n_pca_components selects residual rows by cumulative explained variance, following MNE.

    Beyond the Fortran reference, documented as such. The Fortran reference has no representation of the discarded subspace (its written sphere S is zero past numeigs in the default do_approx_sphere path, amica15.f90:501-508), so any back-projection from its output reconstructs only the retained subspace. The new default therefore goes beyond the reference; ADR 0005, a new section in docs/guides/amica-differences.md, and the AMICAICA docstrings say so, together with the opt-out above. Sources, component maps and the log-likelihood are unchanged; only reconstruction differs. Full-rank fits are bit-identical to before.

    Measured on the bundled 32-channel EEG with pcakeep=20 (Frobenius-relative errors):

    • apply() with nothing excluded vs the input: 1.3e-15 (previously 8.0e-2, i.e. the dropped residual)
    • exclude=[0] vs the backend's own back-projection of that component: 7.7e-15, with a residual-subspace component of 1.5e-15
    • get_sources vs amica_.transform: 2.3e-15
    • residual variances vs the discarded covariance eigenvalues: max relative 5.7e-13
    • mixed magnetometer/gradiometer channel types (non-uniform pre_whitener_): apply() 1.3e-15 / 1.2e-15, and exclusion correct in original units
    • pcadb, multi-model (n_models=2), Epochs, ica.save/read_ica and float32 fits are covered as well

    Since your data is exactly the case this targets (Maxwell-filtered Elekta MEG with pcakeep=20), it would be very valuable if you could rerun your workflow on current dev and confirm that apply() without exclusions reproduces your raw data and that removing artifact components now leaves the full-rank remainder intact. Related work continues in epic #324.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions