Skip to content

topoplot griddata_v4: pinv fallback unreachable; non-finite values misalign sensors #670

Done

Description

@neuromechanist

Found while writing parity vectors for a browser port of topoplot (nemarOrg/neuroschema#25, epic nemarOrg/nemar-cli#2324), at eegprep edf694c.

  1. Duplicate positions: when two sensors share a position, the Green's matrix gets 0*log(0) = NaN off the diagonal. np.linalg.solve returns NaN without raising, so the except LinAlgError -> pinv branch never runs and the whole map is NaN. Suggest checking for coincident positions (or non-finite G) before solve, or catching non-finite solutions and falling back to pinv as intended.
  2. Non-finite values: topoplot drops a sensor with a non-finite value from the positions but not from the values, so later values shift onto the wrong sensors. EEGLAB's topoplot.m drops both. Suggest dropping the same indices from both arrays.

The interpolation-square test (x <= intrad) & (y <= intrad) without abs matches EEGLAB's topoplot.m, so no change there.

Activity

  1. claude commented on Nov 23, 2026

    @claude
    Contributor

    🤖 Plan:

    • Research
    • Failing tests in tests/test_topoplot.py (both failed before the fix)
    • Fix griddata_v4 duplicates + topoplot value alignment
    • pre-commit OK; tests/test_topoplot.py, test_pop_topoplot.py, test_envtopo.py pass
    • PR [topoplot] Average near-coincident sensors and select channels with their values #672
    • CI: no workflow runs were triggered on the branch; a maintainer needs to approve/trigger them. Core-function change, so the EEGLAB reference workflow (eeglab-reference.yml) also needs a run before merge.

    Research

    1. griddata_v4 leaves 0*log(0)=NaN off-diagonal for coincident sensors; np.linalg.solve returns NaN silently, so the pinv branch never runs. MATLAB griddata (which topoplot.m calls with v4) averages values at duplicate points before gdatav4.
    2. topoplot removes NaN/Inf-valued channels from plotchans and subsets Th/Rd/x/y by it, but never subsets datavector/ContourVals, so datavector[intchans] indexes a different array. topoplot.m does Values = Values(plotchans). plotchans is also built via list(set(...)), which is unordered; topoplot.m uses sorted setdiff.

    Relevant code

    • src/eegprep/functions/sigprocfunc/topoplot.py#L28-L47 - Green matrix + solve/pinv
    • src/eegprep/functions/sigprocfunc/topoplot.py#L179-L194 - plotchans filtering without value subset
    • src/eegprep/functions/sigprocfunc/topoplot.py#L213-L216 - values indexed by pltchans/intchans

    Fix

    griddata_v4 merges exactly coincident positions with np.unique(..., return_inverse=True) and averages their values using np.bincount. topoplot sorts plotchans and applies datavector[plotchans] / ContourVals[plotchans] along with the position subset.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions