Repository navigation
Phase 2: PMI core metric and component reordering - #154
neuromechanist merged 6 commits into
Conversation
Review summary (4 parallel Sonnet reviewers: code, tests, comments, silent-failures)code-reviewer: no issues. Confirmed clean-room compliance explicitly (docstring accurately discloses the provenance, code reads as genuinely original NumPy -- not a mechanical MATLAB translation, no GPL content anywhere in the diff), and hand-traced the Miller-Madow correction, joint/marginal histogram consistency, and greedy chain algorithm as correct. Also confirmed the silent-failure-hunter (2 critical + 1 high, all confirmed by executing the code):
pr-test-analyzer (no critical gaps, but independently found the same comment-analyzer (no critical issues): verified the Tests: 28/28 (up from 19), full suite 214 passed / 9 skipped / 6 deselected / 1 xfailed (pre-existing) -- no regressions. ruff/ty clean. |
79f5758
into
feature/issue-133-epic-mir-pmi-metrics
Summary
Adds a clean-room implementation of Pairwise Mutual Information (PMI) between AMICA components, plus a greedy block-diagonal reordering, as
pyAMICA.metrics.pairwise_mi(sources, nbins=None) -> np.ndarrayandpyAMICA.metrics.block_diagonal_order(mi_matrix) -> np.ndarray.This is deliberately NOT a port. The historical MATLAB reference (
minfojp.m/get_mi.m/arrminf2.minsccn/postAmicaUtility) is GPL-2.0-or-later, incompatible with a direct port into this BSD-3-Clause project. Per the epic's licensing decision, this implementation was derived solely from the algorithm description in issue #135 (binned marginal/joint entropy with a Miller-Madow bias correction; a greedy block-diagonal reordering) and standard statistics/algorithms -- the GPL source was never fetched or read at any point in this implementation.pairwise_micomputes each pair's joint 2D histogram and derives both marginals by summing that same histogram (rather than independently rebinning), keeping the entropy terms self-consistent; entropy uses a proper Miller-Madow correction ((m_occupied - 1)/(2N)), distinct from MIR's fixed(nbins-1)/(2N)term.block_diagonal_orderis a standard greedy nearest-neighbor chain seriation, matching the issue's own "greedy" framing.Also includes a small phase-1 follow-up: extracted the shared input-validation logic (non-finite check, constant-channel check, nbins resolution) from
mir.pyinto a new privatepyAMICA/metrics/_common.py, reused by both modules -- a pure refactor, the 11 existing MIR tests pass unchanged. Also added thedelorme2012independentcitation (already in this repo'spaper.bib) tomir.py's docstring, which the epic's own background section ties MIR to but the phase 1 docstring omitted.Closes #135
Part of epic #133
Test plan
pyAMICA/tests/test_metrics_pmi.py(new, 8 tests): symmetry, diagonal-matches-self-entropy, the real scientific claim that ICA sources have lower pairwise MI than raw channels (real fittedpyresults/W.npy/sphere.npyvs raw real channels), valid-permutation and adjacent-weight-improvement checks for the reordering, and the same non-finite/constant-channel error-path tests as phase 1.uv run pytest pyAMICA/tests/test_metrics_mir.py pyAMICA/tests/test_metrics_pmi.py -v: 19 passed (11 MIR unchanged + 8 new PMI).uv run pytest pyAMICA/tests/ -q -m "not slow": 205 passed, 9 skipped, 6 deselected, 1 xfailed (pre-existing) -- no regressions.uv run ruff check/uv run ruff format/uv run ty check: all clean.