Skip to content

[ICA] runamica writes AMICA input in channel-major order instead of MATLAB column-major #435

Closed

Description

@suraj-ranganath

🤖 runamica writes data.fdt channel-contiguous, but AMICA expects frame-contiguous data.

In src/eegprep/functions/sigprocfunc/runamica.py, _write_data_file does:

data_f32 = np.asarray(data, dtype='<f4')
data_f32.tofile(path)

data has shape (chans, frames) (see runamica: chans, frames = data.shape), and ndarray.tofile always writes in C order, so the file holds all of channel 1, then all of channel 2, and so on. MATLAB's runamica15 writes the same matrix with fwrite(fid, dat, 'float'), which is column-major. That file holds every channel for frame 1, then every channel for frame 2, which is the data_dim x field_dim layout the AMICA binary reads. The readers in the same file (W, S, comp_list, ...) already use reshape(..., order='F'), so the writer is the odd one out. The docstring even claims it "matches MATLAB's fwrite(fid, dat, 'float') behavior".

If this holds, AMICA trains on scrambled samples, and every runamica/eeg_amica decomposition is wrong. The remaining tests wouldn't notice: test_eeg_amica.py::TestEegAmicaReconstruction and test_runamica.py::TestRunamicaIntegration::test_runamica_ll_decreasing only check self-consistency and that the log-likelihood improves. An earlier round-trip test only pinned the current layout and was removed in #429.

Not yet confirmed against the MATLAB runamica15.m source, which isn't in the bundled EEGLAB copy. Do that first.

Suggested fix, test first:

  1. Failing test: write a small known (chans, frames) array with _write_data_file, then read it back with np.fromfile(path, '<f4').reshape(chans, frames, order='F') and assert equality with the input.
  2. Fix: np.asfortranarray(data, dtype='<f4').ravel(order='F').tofile(path) (equivalently data_f32.T.tofile(path)).
  3. If MATLAB with AMICA is available, compare runamica weights on sample data against runamica15.

Also tracked in Beads as eegprep-a6n. Found during the test audit for #429.

Activity

  1. added
    bug
    P1
    on Oct 7, 2026
  2. suraj-ranganath commented on Oct 7, 2026

    @suraj-ranganath
    MemberAuthor

    🤖

    Research

    Confirmed against SCCN AMICA revision 2b364cd4a995c057a06409de55f0b8ada8a0f1d8: MATLAB writes columns of the channels-by-frames matrix, and the Fortran reader reads one record of data_dim floats into each frame. NumPy tofile writes C order regardless of the array's memory layout, so EEGPrep sends scrambled channel/frame values; reconstruction and improving likelihood do not establish that the intended data were fitted. No overlapping open PR or separate open issue was found.

    Relevant code

    Proposed Fix

    eeg_amica → runamica → _write_data_file → AMICA get_data preserves the channels-by-frames shape until serialization changes the ordering. Write the float32 little-endian transpose with tofile so each frame's channels are consecutive; first demonstrate failures with explicit file bytes/Fortran-order readback and the actual binary's recovered channel means, then verify the fix against MATLAB and update the user documentation.

  3. suraj-ranganath commented on Oct 7, 2026

    @suraj-ranganath
    MemberAuthor

    🤖 Fixed in draft PR #436 (3dee66d): frame-contiguous float32 serialization, three file-layout regressions, actual-binary channel-mean validation, and a documentation note to recompute affected decompositions. All 10 local AMICA tests pass; MATLAB comparison evidence shows byte-identical input, identical means/sphere/likelihood, and weights within 2.14e-14.

    CI tests passed on Linux Python 3.12/3.13, macOS Python 3.12, and Windows Python 3.12; pre-commit, Ruff/ty, and documentation checks passed. The Claude review job failed immediately after initialization with is_error:true and zero model usage, producing no review findings; this recurrence is recorded in existing Beads issue eegprep-aug. PR remains a draft for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions