AMICA¶
The main scikit-learn-style interface. Wraps the natural-gradient EM backend
(AMICATorchNG).
pyAMICA.AMICA
¶
Adaptive Mixture ICA using the PyTorch natural-gradient EM backend.
This is the main interface for pyAMICA, providing a scikit-learn style
API over :class:AMICATorchNG, the natural-gradient EM implementation
that matches the Fortran reference (Newton, exact-EM mixture updates,
symmetric-ZCA sphere, Jacobian LL).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_models
|
int
|
Number of ICA models to learn |
1
|
n_mix
|
int
|
Number of mixture components per source |
3
|
device
|
str or device
|
Device to use ('cuda', 'mps', 'cpu', or None for auto). With |
None
|
verbose
|
bool
|
Whether to show progress during fitting |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
model_ |
AMICATorchNG
|
The underlying PyTorch model |
is_fitted_ |
bool
|
Whether a usable model is available. |
converged_ |
bool
|
Whether the last |
stop_reason_ |
str or None
|
Why the last |
ll_history_ |
list
|
Log-likelihood history during training (the true per-iteration trajectory; may dip below its peak on a late overshoot) |
final_ll_ |
float
|
Log-likelihood of the fitted parameters (issue #51). Use this, not
|
Examples:
>>> from pyAMICA import AMICA
>>> import numpy as np
>>>
>>> # Generate sample data
>>> X = np.random.randn(32, 10000) # 32 channels, 10000 samples
>>>
>>> # Fit AMICA model
>>> amica = AMICA(n_models=1, n_mix=3)
>>> amica.fit(X, max_iter=100)
>>>
>>> # Transform data to sources
>>> S = amica.transform(X)
>>>
>>> # Get mixing matrix
>>> A = amica.get_mixing_matrix()
Source code in pyAMICA/amica.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 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 | |
fit(X, max_iter=100, lrate=0.05, do_mean=True, do_sphere=True, do_newton=False, **kwargs)
¶
Fit AMICA model to data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input data of shape (n_channels, n_samples) |
required |
max_iter
|
int
|
Maximum number of iterations |
100
|
lrate
|
float
|
Learning rate |
0.05
|
do_mean
|
bool
|
Whether to remove mean from data |
True
|
do_sphere
|
bool
|
Whether to sphere (whiten) the data |
True
|
do_newton
|
bool
|
Whether to enable the Fortran-parity Newton preconditioner (tune
via |
False
|
**kwargs
|
Additional parameters passed to the :class: |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
self |
AMICA
|
Fitted model |
Source code in pyAMICA/amica.py
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 | |
transform(X, model_idx=0)
¶
Transform data to source space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input data of shape (n_channels, n_samples) |
required |
model_idx
|
int
|
Which model to use for transformation |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
S |
ndarray
|
Sources of shape (n_sources, n_samples) |
Source code in pyAMICA/amica.py
fit_transform(X, **fit_params)
¶
Fit model and transform data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
ndarray
|
Input data of shape (n_channels, n_samples) |
required |
**fit_params
|
Parameters passed to fit() |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
S |
ndarray
|
Sources of shape (n_sources, n_samples) |
Source code in pyAMICA/amica.py
get_mixing_matrix(model_idx=0)
¶
Get the mixing matrix A.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_idx
|
int
|
Which model's mixing matrix to return |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
ndarray
|
Mixing matrix of shape (n_channels, n_sources) |
Source code in pyAMICA/amica.py
get_unmixing_matrix(model_idx=0)
¶
Get the unmixing matrix W.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_idx
|
int
|
Which model's unmixing matrix to return |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
W |
ndarray
|
Unmixing matrix of shape (n_sources, n_channels) |
Source code in pyAMICA/amica.py
variance_order(model_idx=0, return_svar=False)
¶
Component order by EEGLAB back-projected variance (IC1 = highest).
Reports the display order EEGLAB's loadmodout15.m applies on load,
without mutating the fitted parameters. Apply it to the columns of
:meth:get_mixing_matrix (or rows of :meth:get_unmixing_matrix) to get
EEGLAB-ordered components in Python.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_idx
|
int
|
Which model's components to order. |
0
|
return_svar
|
bool
|
If True, also return the per-source variance sorted to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
order |
np.ndarray of int
|
Source indices, highest back-projected variance first. |
Source code in pyAMICA/amica.py
write_amica_output(outdir)
¶
Write the fitted model as an EEGLAB-readable AMICA output directory.
Emits the raw binary files that EEGLAB's loadmodout15.m reads (W,
S, gm, mean, c, alpha, mu, sbeta, rho,
comp_list, LL), so a pyAMICA fit drops directly into an EEGLAB
workflow (mod = loadmodout15(outdir)). loadmodout15 applies the
variance-ordering and normalization on load, so no manual re-ordering or
sign-flipping is needed. Single-model output is byte-compatible with the
Fortran reference (issue #92).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outdir
|
str
|
Destination directory (created if absent). |
required |
Source code in pyAMICA/amica.py
save(filepath)
¶
Save the fitted model to filepath via torch.save.
Persists the underlying :class:AMICATorchNG state (config + fitted
tensors) plus the wrapper's own configuration, so :meth:load can
fully reconstruct a transform-ready model. Everything written is a
tensor or plain Python primitive (see
:meth:AMICATorchNG.state_dict), so it reloads with
weights_only=True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Destination path (a |
required |
Source code in pyAMICA/amica.py
load(filepath, device=None)
classmethod
¶
Load a fitted model saved by :meth:save.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to a file written by :meth: |
required |
device
|
str or device
|
Device to place the restored model on. With |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
amica |
AMICA
|
A fitted model ready for :meth: |
Source code in pyAMICA/amica.py
from_params_file(params_file, **kwargs)
classmethod
¶
Create AMICA instance from parameter file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params_file
|
str
|
Path to JSON parameter file |
required |
**kwargs
|
Additional parameters to override |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
amica |
AMICA
|
Configured AMICA instance |