Skip to content

fix(validation): raise a typed error for non-finite input - #172

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/171-typed-non-finite-errors
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/171-typed-non-finite-errors

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #171.

The docs promise a typed error when NaN reaches a finite-only representation. Only NumericBinningTransformer raised one. PLE (the default numerical method), Fourier, periodic encoding, RFF and Nyström raised scikit-learn's plain ValueError("Input contains NaN."), and infinite values did the same for every representation.

Root cause

validate_2d_allow_nan (pretab/core/validation.py) and PLETransformer.fit / transform delegated the finiteness check to check_array(ensure_all_finite=...), which raises a builtin ValueError without the estimator or the column.

Fix

  • New reject_non_finite(X, *, allow_nan, estimator) in pretab/core/validation.py raises PretabDataError:

    • for infinite values, always;
    • for NaN, when allow_nan is False.

    The message names the estimator and the affected columns, by feature_names_in_ when the estimator has them, otherwise by position. For NaN it also points to imputation.

  • validate_2d_allow_nan now calls check_array(..., ensure_all_finite=False) followed by reject_non_finite. This covers every BasePreTabTransformer, including the NaN-propagating splines and feature maps for infinite values.

  • PLETransformer.fit and transform call the helper the same way.

PretabDataError subclasses ValueError, so code that catches ValueError keeps working.

Behaviour changes

  • NaN into PLE, Fourier, periodic encoding, RFF or Nyström raises PretabDataError, for example PLETransformer received missing values (NaN) in column(s) ['hour'], but it needs finite input: .... Before, it raised a plain ValueError.
  • Infinite input raises PretabDataError ... received infinite values in column(s) [...] for these representations and for the splines and feature maps.
  • NaN still passes through the NaN-propagating representations, and binning keeps its own message.

Documentation

  • validate_2d_allow_nan docstring and the new reject_non_finite docstring (NumPy format).
  • docs/core_concepts/missing_values.md: the typed error is a PretabDataError naming the columns, and infinite values raise it for every method.
  • docs/representations/choosing_a_method.md ("NaN into a finite-only method: raises a typed error") now holds as written.

Tests

In tests/integration/test_edge_case_contract.py:

  • test_missing_values_into_a_finite_only_method_raise_a_typed_error (six finite-only representations, at fit and transform): the column is named. Fails on main except for binning (a guard), passes here.
  • test_infinite_values_raise_a_typed_error_naming_the_column (every family): fails on main except for binning, passes here.
  • test_preprocessor_without_imputation_raises_a_typed_error_for_ple: fails on main, passes here.
  • The existing tests that expect a ValueError matching "NaN" for PLE and Fourier still pass.

Reproduction

The script from #171 on this branch:

NumericBinningTransformer    NaN -> pretab.exceptions.PretabDataError | PretabError: True | NumericBinningTransformer received missing values (NaN), whi
PLETransformer               NaN -> pretab.exceptions.PretabDataError | PretabError: True | PLETransformer received missing values (NaN) in column(s) ['
FourierFeatureTransformer    NaN -> pretab.exceptions.PretabDataError | PretabError: True | FourierFeatureTransformer received missing values (NaN) in c
PeriodicEncodingTransformer  NaN -> pretab.exceptions.PretabDataError | PretabError: True | PeriodicEncodingTransformer received missing values (NaN) in
Preprocessor (no imputation) NaN -> PretabDataError | PretabError: True
BSplineTransformer           inf -> PretabDataError | PretabError: True

Checks

  • just test: 4703 passed, 59 skipped, 7 xfailed; coverage 95.72% (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): 4520 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.encoding.numerical.ple imports it from core, which the architecture's dependency direction allows.

Merge notes

The docs promise a typed error when NaN reaches a finite-only method,
but only NumericBinningTransformer raised PretabDataError. PLE, Fourier,
periodic encoding, RFF and Nystroem left the check to scikit-learn's
check_array, which raises a plain ValueError("Input contains NaN.")
without the estimator or column; infinite values did the same for every
method.

A shared reject_non_finite helper in pretab.core.validation now raises
PretabDataError (a ValueError) naming the estimator and the affected
columns: always for infinite values, and for NaN when the estimator does
not allow it. validate_2d_allow_nan and PLETransformer use it.

Fixes #171

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.

Finite-only representations raise a plain ValueError for NaN/inf, although the docs promise a typed error

1 participant