Skip to content

fix(placement): reject a target whose length differs from X - #176

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/175-target-length-check
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/175-target-length-check

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #175.

A target-aware fit with a y of the wrong length raised a raw NumPy IndexError (B/M/I splines) or broadcast ValueError (natural/cubic-regression splines and the feature maps). Only PLE raised a typed error. The unsupervised natural and cubic-regression splines also failed on a wrong-length y when X had a missing value, although they ignore y.

Root cause

  • Nothing checked the target length before y was aligned with X's rows, except in PLE.
  • BaseSplineTransformer.fit sliced y with a mask of X's length, and BaseLocationSelector.select broadcast a mismatched y.
  • The spline mixins' _finite_column aligned y with the finite rows even when no selector would read it.

Fix

  • New check_target_length(n_samples, y, estimator) in pretab/core/validation.py raises PretabDataError: <Estimator>: X and y must have same length. Got 100 and 60; ....
  • It is called where each family pairs X with y for target-aware placement:
    • BaseSplineTransformer.fit (B/M/I);
    • SplineBasisMixin._place_spanning_knots / _place_interior_knots (natural and cubic-regression splines);
    • BaseFunctionalExpansion.fit (RBF, ReLU, sigmoid, tanh);
    • BaseLocationSelector.select, as a safety net for direct selector use.
  • The two mixin helpers set y = None when there is no selector, so unsupervised fits ignore y as documented.

PLE keeps its existing check and message.

Behaviour changes

  • A wrong-length y raises PretabDataError for every target-aware representation. Before, it raised an IndexError or ValueError.
  • Unsupervised natural and cubic-regression splines no longer fail on a wrong-length y when X has missing values. Their output is unchanged, since y was never used.
  • Fits with a correctly sized y are unchanged. The golden regression baselines pass without regeneration.

Documentation

  • check_target_length docstring, and a NumPy-style docstring for BaseSplineTransformer.fit, which had a one-line summary only, with the new Raises entry.
  • docs/core_concepts/target_awareness.md: a y of another length raises PretabDataError for every target-aware fit.

Tests

  • tests/expansion/spline/test_spline_expansions.py::test_target_aware_fit_rejects_a_target_of_another_length (nine families × shorter and longer y): fails on main, passes here.
  • tests/expansion/spline/test_spline_expansions.py::test_unsupervised_fit_ignores_the_length_of_y (nine families): the natural and cubic-regression cases fail on main; the others are guards that pass on both.
  • tests/core/test_location_selectors.py::test_selector_rejects_a_target_of_another_length: fails on main, passes here.

Reproduction

The script from #175 on this branch:

PLETransformer                 -> PretabDataError: X and y must have same length. Got 100 and 60
BSplineTransformer             -> PretabDataError: BSplineTransformer: X and y must have same length. Got 100 and 60; target-aware 
NaturalCubicSplineTransformer  -> PretabDataError: NaturalCubicSplineTransformer: X and y must have same length. Got 100 and 60; ta
RBFExpansionTransformer        -> PretabDataError: RBFExpansionTransformer: X and y must have same length. Got 100 and 60; target-a
unsupervised CubicRegressionSplineTransformer -> fitted

Checks

  • just test: 4704 passed, 59 skipped, 7 xfailed; coverage 95.73% (gate 90%).
  • Minimum dependencies (CI job: Python 3.11, numpy 1.24.4, pandas 2.0.3, scipy 1.10.1, scikit-learn 1.6.0): 4521 passed, 173 skipped, 7 xfailed; scripts/quickstart.py passes.
  • just docs: strict build succeeds.
  • just check: all pre-commit and pre-push hooks pass (file hygiene, ruff format, ruff lint, prettier, pyright with 0 errors).
  • The helper lives in pretab.core.validation. pretab.core.selectors and the expansion modules import it from core, following the documented dependency direction.

Merge notes

Only PLETransformer checked that y has one row per sample. The B/M/I
splines failed with a raw IndexError, and the natural and cubic
regression splines and the feature maps with a NumPy broadcast error
inside the location selector. The unsupervised natural and cubic
regression splines also aligned an unused y with the finite rows and
failed when X had a missing value.

A shared check_target_length helper in pretab.core.validation raises
PretabDataError naming the estimator and both lengths. The spline base,
the spline mixins and the feature-map base call it before target-aware
placement, and the selectors as a safety net. The mixins drop y when no
selector reads it.

Fixes #175
ChrisW09 added a commit that referenced this pull request Nov 29, 2026
Move the sentence out of the paragraph on ignored targets, which #176
also extends, so the two branches merge without a conflict.

Refs #185

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Target-aware splines and feature maps crash with a raw IndexError/broadcast error for a y of the wrong length

1 participant