MNE-Python compatibility (AMICAICA)¶
AMICAICA fits AMICA directly from an MNE-Python
Raw/Epochs and hands the result back through the standard MNE ICA surface.
It is additive: the scikit-learn-style AMICA interface and the
byte-identical EEGLAB output are unchanged; this is a
second entry point for MNE users, not a replacement.
MNE is an optional dependency, so import pamica never requires it. Install the
extra and import the wrapper explicitly:
import mne
from pamica.mne_compat import AMICAICA
raw = mne.io.read_raw_eeglab("subject.set", preload=True)
ica = AMICAICA(n_mix=3, random_state=42).fit(raw, picks="eeg", max_iter=100)
sources = ica.get_sources(raw) # an mne.io.RawArray of component activations
maps = ica.get_components() # scalp maps, shape (n_channels, n_components)
ica.plot_components() # native mne.viz topographies
# Reconstruct with some components removed:
clean = ica.apply(raw.copy(), exclude=[0, 3])
fit accepts a Raw or Epochs (epochs are concatenated along time, as MNE's
own ICA does), any MNE picks selector, and forwards remaining keywords
(max_iter, lrate, do_newton, ...) to AMICA.fit. It rejects
non-finite input and PCA reduction (pcakeep/pcadb, which leaves the sphere
rank-deficient so the full-rank export would be invalid), and a degenerate
(diverged) fit is refused by the consumer methods rather than emitting NaNs.
Interoperating with mne.preprocessing.ICA¶
to_mne_ica() returns a fully-populated
mne.preprocessing.ICA,
so the entire MNE ICA ecosystem (plotting, find_bads_eog/_ecg, exclusion
workflows) works on an AMICA decomposition:
The wrapper's get_sources, apply, get_components, plot_components and
plot_sources delegate to this object, so they reproduce AMICA.transform
exactly: MNE
computes sources as unmixing_matrix_ @ pca_components_ @ (X - pca_mean_), and
the export maps pamica's mean, symmetric-ZCA sphere and unmixing into those
matrices (writing the sphere as V diag(1/√e) Vᵀ with V orthonormal so MNE's
scalp maps come out in channel space). The equivalence
to_mne_ica().get_sources(raw) == AMICA.transform(X) is pinned by the test
suite on real sample EEG.
Multi-model fits¶
AMICA can learn a mixture of ICA models (n_models > 1). MNE's ICA represents
only one unmixing matrix, so each model is exported as its own single-model
mne.preprocessing.ICA, and the per-sample model dominance (which model best
explains each timepoint) is exposed directly, since MNE has no concept for it:
ica = AMICAICA(n_models=2, random_state=42).fit(raw, max_iter=100)
# Per-model: model_idx selects the model on every consumer method.
sources_m1 = ica.get_sources(raw, model_idx=1)
ica.plot_components(model_idx=1)
model1 = ica.to_mne_ica(model_idx=1) # a standard ICA for model 1
# Model dominance over time (P(model | sample), columns sum to 1):
prob = ica.get_model_probability(raw) # (n_models, n_samples)
ica.plot_model_probability(raw) # per-model probability + best-model LL
get_model_probability/plot_model_probability build on the public
AMICA.model_loglik/model_probability accessors, which score arbitrary data
through the fitted sphere and mean. Each per-model export folds that model's
data-space center into pca_mean_, so to_mne_ica(model_idx=h).get_sources(raw)
reproduces AMICA.transform(X, model_idx=h) (with X the picked channel array)
for every model, not just the first.
Inspecting pamica-specific metadata¶
An mne.preprocessing.ICA has no field for AMICA's adaptive source densities or
component sharing, so rather than drop them, the wrapper exposes them directly:
from pamica.mne_compat import AMICAICA, PDFTYPE_NAMES
ica = AMICAICA(n_models=2, random_state=42).fit(raw, max_iter=100)
families = ica.get_pdftype(model_idx=0) # (n_components,) codes 0-4
names = [PDFTYPE_NAMES[c] for c in families] # e.g. "generalized_gaussian"
rho = ica.get_rho(model_idx=0) # (n_mix, n_components) GG shape
shared = ica.shared_components() # [(model, comp), ...] groups
get_pdftype returns each component's density family (0 generalized Gaussian,
1 super-Gaussian cosh, 2 Gaussian, 3 logistic, 4 sub-Gaussian cosh; they differ
per component only under the adaptive switcher pdftype=1). get_rho is the
generalized-Gaussian shape (meaningful for pdftype=0). shared_components
lists components merged across models by share_comps (empty otherwise). The
same three accessors exist on the scikit-learn-style AMICA.
Separation-quality metrics¶
The MIR/PMI metrics are available directly on an MNE object, so MNE-side users get the same separation-quality numbers as EEGLAB-side users:
mir_nats, variance = ica.mir(raw, model_idx=0) # mutual information reduced
mi_matrix = ica.pmi(raw, model_idx=0) # pairwise MI between sources
Both extract the fitted channels from the Raw/Epochs and delegate to
AMICA.mir/pmi, reproducing the array API exactly.
pamica.mne_compat.AMICAICA
¶
Fit AMICA from MNE objects and interoperate with mne.preprocessing.ICA.
The wrapper fits pamica's natural-gradient AMICA backend on the data of an
MNE :class:~mne.io.Raw or :class:~mne.Epochs and lets MNE consume the
result: :meth:get_sources, :meth:apply, :meth:get_components,
:meth:plot_components and :meth:plot_sources all delegate to a real
:class:mne.preprocessing.ICA built by :meth:to_mne_ica.
For a multi-model fit (n_models > 1) each model is exported as its own
single-model MNE ICA (to_mne_ica(model_idx=...) / the model_idx
argument on the consumer methods), and the per-sample model dominance --
which MNE's ICA cannot represent -- is exposed directly by
:meth:get_model_probability / :meth:plot_model_probability.
Separation-quality metrics (issue #133) are available directly on an MNE
object: :meth:mir (Mutual Information Reduction) and :meth:pmi (pairwise
mutual information between sources). The pamica-specific fitted metadata MNE
cannot hold -- source-density family, GG shape, component sharing -- is
inspectable via :meth:get_pdftype / :meth:get_rho / :meth:shared_components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_models
|
int
|
Number of ICA models to learn (AMICA |
1
|
n_mix
|
int
|
Number of mixture components per source (AMICA |
3
|
random_state
|
int or None
|
Seed for the AMICA fit (passed through as the backend |
None
|
device
|
str or device
|
Torch device for the fit ( |
None
|
verbose
|
bool
|
Whether the underlying :class: |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
amica_ |
AMICA
|
The fitted pamica model (holds all |
info_ |
Info
|
The picked measurement info the fit was run on (channel subset only). |
ch_names_ |
list of str
|
Names of the fitted channels, in order. |
n_components_ |
int
|
Number of ICA components (equals the number of fitted channels; AMICA
keeps |
converged_ |
bool
|
Whether the last fit ended usable (not degenerate). A degenerate fit is kept for inspection but refused by the consumer methods (issue #50). |
stop_reason_ |
str or None
|
Why the backend fit stopped (e.g. |
Notes
Model h's AMICA transform is S = W_fort @ (sphere @ (X - mean) - c_h),
where c_h is that model's data-space center (identically zero for a
single model, since the c update is gated to n_models > 1). MNE
computes sources as S = unmixing_matrix_ @ pca_components_ @ (X - pca_mean_)
(with a unit pre-whitener). Writing the symmetric-ZCA sphere as
V @ diag(1/sqrt(e)) @ V.T with V orthonormal, the exported ICA for
model h uses pca_components_ = V.T,
unmixing_matrix_ = W_fort @ sphere @ V and
pca_mean_ = mean + inv(sphere) @ c_h (which reduces to mean when
c_h is zero). Keeping pca_components_ orthonormal is what makes MNE's
get_components (scalp maps inv(sphere) @ inv(W_fort)) come out right,
since MNE assumes orthonormal PCA rows. The mapping is pinned by a round-trip
test (to_mne_ica(model_idx=h).get_sources(raw) equals
amica_.transform(X, model_idx=h)).
PCA reduction (pcakeep/pcadb) is unsupported: it leaves the sphere
rank-deficient, so the export (which assumes a full-rank whitening) would be
numerically invalid, and :meth:fit rejects it. Rank-deficient data (for
example average-referenced EEG) is the same hazard reached through data
conditioning rather than a keyword; such a fit typically diverges and is
refused as degenerate.
Source code in pamica/mne_compat/core.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | |
fit(inst, picks=None, start=None, stop=None, **fit_kwargs)
¶
Fit AMICA to the data of an MNE Raw or Epochs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inst
|
BaseRaw | BaseEpochs
|
The data to decompose. |
required |
picks
|
str | list | slice | None
|
Channels to fit, in any form MNE accepts (e.g. |
None
|
start
|
int | None
|
Sample range for |
None
|
stop
|
int | None
|
Sample range for |
None
|
**fit_kwargs
|
Forwarded to :meth: |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
AMICAICA
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in pamica/mne_compat/core.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
to_mne_ica(model_idx=0)
¶
Build (and cache) a fully-populated :class:mne.preprocessing.ICA.
The returned object is a genuine MNE ICA: get_sources, apply,
get_components, save and the mne.viz plotters operate on it
natively. See the class :class:Notes <AMICAICA> for the
mean/sphere/unmixing to pca_mean_/pca_components_/
unmixing_matrix_ mapping.
For a multi-model fit each model is exported as its own single-model
MNE ICA (MNE has no multi-model concept); pass model_idx to pick
one. The per-model exports are cached and returned by reference:
mutating one (for example setting .exclude) persists across
subsequent :meth:apply/:meth:get_sources calls for that model until
the next :meth:fit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_idx
|
int
|
Which AMICA model to export ( |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
ica |
ICA
|
|
Source code in pamica/mne_compat/core.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
get_sources(inst, *args, model_idx=0, **kwargs)
¶
Sources for inst from model model_idx (see ICA.get_sources).
model_idx is keyword-only so positional arguments pass straight
through to MNE's ICA.get_sources (e.g. add_channels,
start/stop).
Source code in pamica/mne_compat/core.py
apply(inst, *args, model_idx=0, **kwargs)
¶
Remove selected components of model model_idx and back-project.
model_idx is keyword-only so positional arguments pass straight
through to MNE's ICA.apply. Pass exclude=[...] (or set it on the
exported ICA) to drop components; with no exclusions this reconstructs
the input.
Source code in pamica/mne_compat/core.py
get_components(*, model_idx=0)
¶
plot_components(*args, model_idx=0, **kwargs)
¶
Plot model model_idx component topographies (see ICA.plot_components).
plot_sources(inst, *args, model_idx=0, **kwargs)
¶
Plot model model_idx component time courses (see ICA.plot_sources).
get_model_probability(inst)
¶
Per-sample posterior probability of each model on inst (dominance).
Returns P(model | sample) as (n_models, n_samples) via
:meth:AMICA.model_probability on inst's data (Epochs are
concatenated along time). Each column sums to 1; all ones for a single
model. MNE's own ICA has no multi-model concept, so this is exposed
here rather than through the exported per-model ICA objects.
Source code in pamica/mne_compat/core.py
plot_model_probability(inst, *, srate=None, **kwargs)
¶
Plot per-model probability + best-model log-likelihood over inst.
Delegates to :func:pamica.viz.plot_model_probability with the live
per-model log-likelihood (:meth:AMICA.model_loglik) on inst's
data. srate defaults to the fitted recording's sampling rate, so the
x-axis is in seconds; extra keywords (smooth_sec, window_sec,
axes) pass through.
Source code in pamica/mne_compat/core.py
mir(inst, *, model_idx=0, nbins=None)
¶
Mutual Information Reduction of model model_idx on inst.
How much mutual information the fitted unmixing removes from the data,
in nats (issue #133). Delegates to :meth:AMICA.mir on inst's
fitted-channel data; MIR is shift-invariant, so mean/c centering is
irrelevant.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inst
|
BaseRaw | BaseEpochs
|
Data to score ( |
required |
model_idx
|
int
|
Which model's unmixing to use. |
0
|
nbins
|
int
|
Histogram bin count; see :func: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mir_nats |
float
|
|
variance |
float
|
|
Source code in pamica/mne_compat/core.py
pmi(inst, *, model_idx=0, nbins=None)
¶
Pairwise Mutual Information between model model_idx's sources on inst.
The residual pairwise dependence between fitted sources, in nats
(issue #133). Delegates to :meth:AMICA.pmi on inst's fitted-channel
data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inst
|
BaseRaw | BaseEpochs
|
Data to score ( |
required |
model_idx
|
int
|
Which model's sources to use. |
0
|
nbins
|
int
|
Histogram bin count; see :func: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mi_matrix |
np.ndarray of shape (n_components, n_components)
|
Symmetric; the diagonal is each source's own entropy. |
Source code in pamica/mne_compat/core.py
get_pdftype(*, model_idx=0)
¶
Per-component source-density family code for model model_idx.
One integer per ICA component (0-4); map to names with
:data:pamica.mne_compat.PDFTYPE_NAMES. All components share one family
unless the adaptive switcher (pdftype=1) moved them (issue #26).
Source code in pamica/mne_compat/core.py
get_rho(*, model_idx=0)
¶
Generalized-Gaussian shape rho for model model_idx.
Shape (n_mix, n_components); rho == 2 is Gaussian-shaped,
rho == 1 Laplacian, rho < 1 heavier-tailed. Meaningful only for
the generalized-Gaussian family (pdftype=0).
Source code in pamica/mne_compat/core.py
shared_components()
¶
Components shared across models by share_comps (issue #60).
One group of (model_idx, component_idx) pairs per shared column;
empty when nothing is shared (always so for a single model or a default
multi-model fit with share_comps off).