Separation metrics (pamica.metrics)¶
Quality metrics for a decomposition, as free functions over plain arrays. They are backend-agnostic and independent of any fitted model object, so they work on sources from any source.
mir— Mutual Information Reduction, in nats: how much mutual information a linear unmixing removes from the data. Higher is a better separation. It needs the full raw-data-to-sources transform (the unmixing composed with the sphering matrix), which must be square and invertible, since the estimate includes a log-Jacobian term.pairwise_mi— the symmetric mutual-information matrix between sources. Its diagonal is each source's own entropy, not a mutual information.block_diagonal_order— a permutation that clusters mutually dependent components near the diagonal, for reading structure out of apairwise_mimatrix.
Most callers should prefer the model accessors, which compose the transform for
you and refuse an unusable fit: AMICA.mir and
AMICA.pmi. Use these free functions when you have sources or an
unmixing from somewhere else. To plot a pairwise_mi matrix, see
plot_pmi_heatmap, which applies block_diagonal_order itself and
masks the entropy diagonal.
Provenance¶
mir is a direct port of getMIR.m from
bigdelys/pre_ICA_cleaning
(Apache-2.0); the licence is vendored in THIRD_PARTY_NOTICES.md. It agrees
with the original to 1.7e-15 relative on the bundled sample data.
pairwise_mi and block_diagonal_order are a clean-room reimplementation. The
comparable MATLAB code (minfojp.m in postAmicaUtility) is GPL-2.0-or-later and
pamica is BSD-3-Clause, so that source was never read; the implementation works
from the published description in Delorme et al. (2012), "Independent EEG
sources are dipolar", PLoS ONE. It agrees with that reference at r=0.9887 on
identical signals.
pamica.metrics.mir
¶
Mutual Information Reduction (MIR) ICA separation-quality metric.
MIR measures how much a linear unmixing transform reduces the total mutual information among channels: it is the (signed) difference between the sum of per-channel differential entropies before and after unmixing, corrected by the log-Jacobian of the transform. A well-separated decomposition drives the unmixed channels toward independence, so MIR should be large and positive. The identity transform (or any scalar multiple of it) gives MIR exactly zero, since the bin-partition estimator below is scale-invariant; a generic non-identity rotation of dependent, non-Gaussian data need not (that directional sensitivity is the entire mechanism ICA exploits).
Ported from getMIR.m / its nested getent4 in
bigdelys/pre_ICA_cleaning
<https://github.com/bigdelys/pre_ICA_cleaning/blob/b6034f03889a6a418968ee119123f3df55251957/getMIR.m>_
(Apache License 2.0, full text reproduced in THIRD_PARTY_NOTICES.md per
that license's Section 4(a)). This is a permitted derivative under
Apache-2.0; the port keeps the original's binned-entropy estimator (including
its bin-count-1 correction and eigenvalue-based log-Jacobian) rather than
substituting a different entropy estimator.
A 2023 MATLAB adaptation of getMIR (the nested mir() function in
sccn/NEMAR-pipeline's eeg_nemar_dataqual.m, currently dead/commented-out
code there) added an explicit sphering step ahead of this computation, but its
mir(data, linT) references eig(W) and /N, neither of which are its
own parameters nor ever assigned anywhere in the enclosing function -- it would
raise an undefined-variable error if uncommented, which is why it's dead code.
It also depends on a robust_sphering_matrix routine that itself calls
GPL-licensed helpers (block_geometric_median, hlp_memfree), out of
scope for this BSD-3-Clause project. This port instead takes an explicit
unmixing matrix: callers that want the sphere-then-unmixing composition
(e.g. wiring MIR to a fitted AMICA model) compose it themselves before calling
mir.
MIR follows the separation-quality-metric approach introduced by Delorme,
Palmer, Onton, Oostenveld, and Makeig (2012), "Independent EEG sources are
dipolar", PLoS ONE (this repo's paper.bib key delorme2012independent).
mir(unmixing, data, nbins=None)
¶
Mutual Information Reduction of unmixing applied to data, in nats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
unmixing
|
ndarray
|
Square (n, n) linear transform applied to |
required |
data
|
ndarray
|
(n, N) data matrix |
required |
nbins
|
int
|
Histogram bin count for the marginal-entropy estimator; see
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mir_nats |
float
|
Mutual information (in nats) removed by |
variance |
float
|
Variance of the MIR estimate. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in pamica/metrics/mir.py
pamica.metrics.pairwise_mi(sources, nbins=None)
¶
Symmetric (n, n) pairwise mutual-information matrix, in nats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
ndarray
|
(n_components, n_samples) signal matrix (e.g. ICA source activations or raw channels). |
required |
nbins
|
int
|
Histogram bin count per axis of the joint 2-D histogram. Defaults to
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mi_matrix |
ndarray
|
(n, n) symmetric matrix; |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in pamica/metrics/pmi.py
pamica.metrics.block_diagonal_order(mi_matrix)
¶
Greedy nearest-neighbor-chain permutation clustering high-MI pairs near the diagonal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mi_matrix
|
ndarray
|
(n, n) symmetric mutual-information matrix, e.g. from |
required |
Returns:
| Name | Type | Description |
|---|---|---|
order |
ndarray
|
Length- |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |