Skip to content

clean_channels: raise on degenerate channel locations instead of returning a meaningless result #539

Closed

Description

@neuromechanist

With all channels at one point, or on a line, clean_channels raises nothing and removes a large part of the channels.

import numpy as np
from eegprep import clean_channels, eeg_checkset, eeg_emptyset
LOCS = {"Fp1": (-32, 77, 19), "Fp2": (32, 77, 19), "F3": (-52, 35, 57), "F4": (52, 35, 57), "C3": (-69, -29, 69),
        "C4": (69, -29, 69), "P3": (-54, -92, 44), "P4": (54, -92, 44), "O1": (-31, -116, -7), "O2": (31, -116, -7),
        "F7": (-72, 32, 5), "F8": (72, 32, 5), "T3": (-83, -21, -4), "T4": (83, -21, -4), "T5": (-73, -74, -8),
        "T6": (73, -74, -8), "Fz": (0, 37, 84), "Cz": (0, -34, 105), "Pz": (0, -98, 66)}
names = list(LOCS); xyz = np.array([LOCS[n] for n in names], dtype=float)
rng = np.random.default_rng(0); srate, t = 250.0, 250 * 120
src = rng.standard_normal((5, t)); pos = rng.standard_normal((5, 3)); pos = 80 * pos / np.linalg.norm(pos, axis=1, keepdims=True)
data = np.exp(-np.linalg.norm(xyz[:, None, :] - pos[None], axis=2) / 60.0) @ src + 0.2 * rng.standard_normal((len(names), t))
def run(positions):
    eeg = eeg_emptyset()
    eeg.update(data=data.copy(), nbchan=len(names), pnts=t, trials=1, srate=srate, xmin=0.0, xmax=(t - 1) / srate,
               chanlocs=[{"labels": n, "X": float(p[0]), "Y": float(p[1]), "Z": float(p[2])} for n, p in zip(names, positions)])
    return len(names) - int(clean_channels(eeg_checkset(eeg))["nbchan"])
print("true geometry:", run(xyz))                                                # 0
print("collinear:", run([(10 + 5 * i, 0, 0) for i in range(len(names))]))       # 6
print("identical:", run([(10, 20, 30)] * len(names)))                            # 6

Expected: a ValueError (or at least a warning) when the unit-normalised locations do not span three dimensions
(numerical rank below 3), as the existing errors for missing locations and for more than 75 percent removed channels do.

Checked on eegprep 0.5.0.

Activity

  1. neuromechanist commented on Nov 6, 2026

    @neuromechanist
    MemberAuthor

    One more case for the same request: positions that are nearly, but not exactly, degenerate.

    A rank test with NumPy's default tolerance (the largest singular value times the larger dimension times machine epsilon) accepts a montage that is flat to a picometer, and clean_channels still returns a degraded answer for it.
    With the 19 positions and the data of the first post (eegprep 0.5.0), the true geometry removes 0 channels.
    Putting every position on the plane Z = 0 and adding a normal jitter of 1e-9 mm, 1e-6 mm, 1e-3 mm or 0.1 mm to the three coordinates leaves np.linalg.matrix_rank of the unit position vectors at 3, and clean_channels removes 2 of the 19 channels every time.

    So a check that raises on degenerate geometry has to define its own tolerance (for example a floor on the ratio of the smallest to the largest singular value of the unit vectors), because the default of matrix_rank is far below anything a real head has.

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