Repository navigation
Description
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:
- Retrieve the retained PCA basis stored during fitting.
- Compute the PCA residual from the input data passed to
apply(). - Apply the existing MNE ICA reconstruction to remove the selected ICA components.
- Add the unchanged PCA residual back to the reconstructed data.
- 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_channelsthere 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
[-]Restore PCA residual when applying AMICA with `pcakeep < n_channels`[/-][+]Restore PCA residual when applying AMICA with `pcakeep < n_rank`[/+][-]Restore PCA residual when applying AMICA with `pcakeep < n_rank`[/-][+]Restore PCA residual when applying AMICA with `pcakeep < n_rank` (or n_channel)[/+]Restore the PCA residual in AMICAICA.apply (#325)
neuromechanist commentedon Sep 22, 2026
Thanks for the detailed write-up. This is fixed on
devin #325 (squash commit 42a59c2), and it follows your proposal closely, using MNE's own reconstruction mechanism rather than a new keyword.What changed.
AMICAICA.fitnow computes the full orthonormal PCA basis of the fit once, in the pre-whitened (channel-type scaled) space, and stores it aspca_components_(n_channels x n_channels) andpca_explained_variance_. The firstn_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_icaexports that full basis. MNE'sICA.applyalready keeps rows pastn_components_as residual PCA components (n_pca_components=Nonekeeps all of them), soapplynow restores the residual by default: excluding a component removes that component's back-projection and nothing else, in the original units (thepre_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 floatn_pca_componentsselects 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
Sis zero pastnumeigsin the defaultdo_approx_spherepath, 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 indocs/guides/amica-differences.md, and theAMICAICAdocstrings 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-15get_sourcesvsamica_.transform: 2.3e-15pre_whitener_):apply()1.3e-15 / 1.2e-15, and exclusion correct in original unitspcadb, multi-model (n_models=2),Epochs,ica.save/read_icaand float32 fits are covered as wellSince 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 currentdevand confirm thatapply()without exclusions reproduces your raw data and that removing artifact components now leaves the full-rank remainder intact. Related work continues in epic #324.