Skip to content

numpy_impl: do_reject outlier rejection is non-functional (data_mask never initialized/consumed) #123

Closed

Description

@neuromechanist

Found while fixing ty diagnostics (#122). `AMICA_NumPy._reject_outliers()`
(`pyAMICA/numpy_impl/core.py`) references `self.data_mask` at
`self.data_mask[outliers] = False`, but `data_mask` is never initialized
anywhere in `init`/`_initialize_parameters` -- `ty` correctly flags this
as `Object of type Self has no attribute data_mask`. If a user sets
`do_reject=True` with `maxrej > 0` and the iteration reaches the reject
schedule (`_optimize`'s do_reject block), this raises `AttributeError` at
runtime.

Separately, even once `data_mask` exists, nothing reads it:
`_get_updates_and_likelihood`/`_get_block_updates` iterate over
`self.data` directly with no reference to `data_mask` at all, unlike the
torch backend's `good_idx` mechanism (`AMICATorchNG`, which actually
restricts `X_use = X_t[:, self.good_idx]` each iteration). So the rejection
mask, even if computed without crashing, currently has zero effect on the
fit -- the feature is a no-op at best, a crash at worst.

No test exercises `do_reject` on the NumPy backend (only the torch backend's
`test_ng_backend.py`/`test_ng_pdf_families.py` do), which is presumably why
this has gone unnoticed.

Scope

Port the torch backend's `good_idx`-style filtering to the NumPy backend:
initialize a good-sample index/mask in `_initialize_parameters`, restrict the
per-iteration data to it in `_get_updates_and_likelihood`, and shrink it in
`_reject_outliers` (mirroring `AMICATorchNG._reject_outliers`). Add a
NumPy-backend test analogous to the torch `do_reject` tests.

Not fixed here: #122 only added a minimal `data_mask` initialization to stop
the immediate crash risk; it does not make the feature functional.

Activity

  1. added
    bug
    on Jul 13, 2026
  2. added 2 commits that reference this issue on Jul 13, 2026
  3. neuromechanist commented on Jul 14, 2026

    @neuromechanist
    MemberAuthor

    Correction to the root-cause scope (surfaced by the PR #124 type-safety review):

    The crash is worse than this issue's title/body describe. It is not gated on do_reject reaching the reject schedule -- _update_parameters divides by self.num_good_samples on every iteration once do_reject is set (numpy_impl/core.py, the if self.do_reject: branch), but num_good_samples is only ever assigned inside _reject_outliers (which fires later, on rejstart/rejint). So iteration 0 raises AttributeError immediately, independent of maxrej/rejstart/rejint. There are therefore two wired-up defects, not one:

    1. num_good_samples used before it is ever assigned (immediate crash).
    2. data_mask is computed by _reject_outliers but never consumed by the E-step to restrict the fit data (silent no-op even if (1) were fixed).

    Interim state after PR #124: fit() now refuses do_reject=True on the NumPy backend with a clear NotImplementedError pointing here (rather than crashing cryptically), _reject_outliers is kept as dormant, self-contained scaffolding, and there is a regression test (test_do_reject_refused_on_numpy_backend). The real fix here is to port the PyTorch backend's good_idx-style filtering (E-step accumulates only over data_mask, and num_good_samples initialized to num_samples), then remove the gate and add a real-data parity test.

  4. added a commit that references this issue on Jul 14, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions