Skip to content

Epic: port MIR and PMI separation-quality metrics from postAmicaUtility/pre_ICA_cleaning #133

Closed
5 of 5 issues completed

Description

@neuromechanist

Background

Reviewer feedback on the JOSS paper: Scott Makeig strongly recommended adding
Mutual Information Reduction (MIR, Palmer's separation-quality metric from
Delorme et al. 2012) as a built-in pyAMICA option, applicable at decomposition
end and/or at specified waypoints. Investigating where MIR and the related PMI
metric actually live turned up two distinct upstream sources with different
licenses -- important since pyAMICA is BSD-3-Clause:

  • MIR: getMIR.m in
    bigdelys/pre_ICA_cleaning
    (Arnaud Delorme's repo) -- Apache License 2.0 (permissive, attribution
    required). Seyed Yahya Shirazi already adapted this into a mir() nested
    function in sccn/NEMAR-pipeline/eeg_nemar_dataqual.m (2023), with a sphering
    step added ("sphereing is needed to make sure that the MIR is only related to
    ICA"). That adapted version has a latent bug worth not reproducing: mir()'s
    signature is (data, linT) but its body references eig(W) and /N, neither
    of which are parameters -- it only runs today because MATLAB nested functions
    share the parent function's workspace and happen to have same-named variables
    in scope. The Python port should use explicit parameters (linT, N = data.shape[1]) throughout, not replicate the implicit-scope dependency.

  • PMI (Pairwise Mutual Information, a related but distinct metric -- NOT the
    same as MIR): lives in
    sccn/postAmicaUtility
    (minfojp.m/get_mi.m/arrminf2.m/pop_modPMI.m), a MATLAB/EEGLAB plugin,
    last touched 2023, GPL-2.0-or-later per file headers (no repo LICENSE file).
    There is no MIR implementation anywhere in postAmicaUtility (grepped for
    "mutual information reduction" and \bMIR\b, zero matches) -- the earlier
    assumption that MIR lived there was wrong.

Licensing approach (decided)

  • MIR: Apache-2.0 permits a direct port; carry an attribution notice to
    bigdelys/pre_ICA_cleaning in the module docstring per the Apache-2.0
    attribution requirement.
  • PMI: because the upstream is GPL-2.0-or-later and copyleft, this will be a
    clean-room reimplementation from the algorithm description (binned
    marginal/joint entropy with a Miller-Madow-style bias correction -- a standard
    statistical technique, not copyrightable expression), citing Palmer, Ozgur
    Balkan, Arnaud Delorme, and Makoto Miyakoshi in the docstring and paper.
    No MATLAB source is copied. This keeps pyAMICA entirely BSD-3-Clause.

Scope

Phase 1 -- MIR core metric: mir(unmixing, data) -> (mir_nats, variance),
ported from the Apache-2.0 source (permitted), using pyAMICA's existing
sphere + unmixing pipeline, with the scope bug fixed and real tests against the
bundled real sample EEG (NO MOCK data, per repo policy) checking internal
consistency against a literal re-derivation of the entropy terms. DONE (#134,
PR #153).

Phase 2 -- PMI core metric: pairwise mutual-information matrix between
components (clean-room reimplementation of the histogram/entropy/bias-correction
scheme) plus a greedy block-diagonal component reordering, for multi-model
AMICA fits. DONE (#135, PR #154).

Phase 3 -- Posterior-probability (LLt) output parity with Fortran: neither
pyAMICA backend wrote the per-timepoint, per-model log-likelihood file
Fortran produces every fit (LLt), and the existing reader
(loadmodout()) had a separate, confirmed reshape bug (order='C' instead
of order='F'). Discovered while planning the original Phase 3
(visualizations) -- the multi-model posterior-probability timeline needs
this real data, so it was promoted to its own blocking phase (#155) rather
than folded quietly into the viz phase. DONE (#155, PR #156).

Review of #156 additionally caught a third, unrelated parity divergence that
neither of the two known bugs would have surfaced: under do_reject, both
backends wrote real log-likelihoods for rejected samples where Fortran writes
exactly 0.0 (amica15.f90:2216). Those zeros are load-bearing rather than
cosmetic -- Fortran's load_rej reconstructs the rejection mask from them
(sum(modloglik(:,i)) == 0.0 as the sentinel, amica15.f90:887-896) -- so a
Fortran load_rej run reading a pyAMICA LLt would have silently concluded
nothing was rejected. Fixed and guarded on both backends. The NumPy forward
pass is now shared with the training path rather than duplicated (verified
bit-exact against real-data LL trajectories), and the torch backend no longer
pins the sphered dataset for the model's lifetime. Follow-up #157 tracks the
cheaper stash-logV-during-the-E-step design.

Phase 4 (was Phase 3) -- Visualization: PMI heatmap (component-reordered),
scalp topography + generalized-Gaussian mixture PDF overlay (pyAMICA already
exposes alpha/mu/sbeta/rho, a natural fit for this), and a
multi-model posterior-probability timeline built on Phase 3's LLt data.
matplotlib-based, a fresh design rather than a port of the MATLAB/EEGLAB GUI
plumbing (inputgui, figure callbacks, etc. don't translate 1:1 and aren't
worth porting).

Phase 5 (was Phase 4) -- API wiring: expose model.mir(X) / model.pmi()
on AMICA / AMICATorchNG, computable at fit-end and optionally at
specified iteration waypoints (per Scott's request).

Out of scope for this epic

A full port of postAmicaUtility's GUI (pop_modPMI.m's EEGLAB dialog,
ozeegplot.m, ERP-conditioning tools, model-selection utilities) -- only the
metrics and the visualizations that directly serve separation-quality assessment
are in scope.

Sub-issues

Sub-issues

5 of 5 Issues completed
Loading 5 sub-issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions