Skip to content

fix(validation): check task consistency for a polars Series target - #182

Open
ChrisW09 wants to merge 1 commit into
mainfrom
fix/181-polars-target-validation
Open

ChrisW09 wants to merge 1 commit into
mainfrom
fix/181-polars-target-validation

Conversation

@ChrisW09

Copy link
Copy Markdown
Collaborator

Fixes #181.

With a polars Series y, the task_validation diagnostic was skipped in Preprocessor, CrossFittedTransformer and RepresentationSearchCV. "warn" reported nothing, and "raise" let scikit-learn's own error surface instead of the documented InvalidParamError.

Root cause

_validate_task_target (pretab/core/validation.py) returned early unless y was a NumPy array, pandas object, list or tuple.

Fix

  • New private _is_polars_series(y): detects a polars Series without importing polars, like the existing is_polars_frame.
  • _validate_task_target inspects a polars Series through Series.to_numpy(). The check itself is unchanged.
  • The caller's original target object is still the one passed on to fitting.

Behaviour changes

  • With a polars Series target, task_validation="raise" raises InvalidParamError and "warn" emits ConfigWarning for the same mismatches as with NumPy or pandas targets.
  • Consistent targets and targets with nulls stay quiet, as before. task_validation="off" (the default) is unaffected.

Documentation

  • _validate_task_target docstring.
  • docs/core_concepts/resolution_and_placement.md (task_validation): which target containers the check reads, and that it skips targets it cannot classify, such as ones with missing values.

Tests

In tests/core/test_validation.py:

  • test_task_target_inspects_a_polars_series (continuous and text targets, "warn" and "raise"): fails on main, passes here (skipped without polars).
  • test_task_target_leaves_consistent_or_missing_polars_targets_quiet (binary, bool, null): guard that passes on both.
  • test_preprocessor_task_validation_applies_to_a_polars_target (Preprocessor and CrossFittedTransformer): fails on main, passes here.

Reproduction

The script from #181 on this branch:

Preprocessor(task='classification')            numpy y : InvalidParamError: Preprocessor.task = 'classification' is invalid.
Preprocessor(task='classification')            polars y: InvalidParamError: Preprocessor.task = 'classification' is invalid.
CrossFittedTransformer(task='classification')  numpy y : InvalidParamError: CrossFittedTransformer.task = 'classification' is invalid.
CrossFittedTransformer(task='classification')  polars y: InvalidParamError: CrossFittedTransformer.task = 'classification' is invalid.
RepresentationSearchCV(Ridge) on text labels   numpy y : InvalidParamError: RepresentationSearchCV.task = 'regression' is invalid.
RepresentationSearchCV(Ridge) on text labels   polars y: InvalidParamError: RepresentationSearchCV.task = 'regression' is invalid.

Checks

  • just test: 4684 passed, 59 skipped, 7 xfailed; coverage 95.71% (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; no polars, so the polars tests skip): 4493 passed, 181 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 change stays inside pretab.core.validation, and polars stays an optional import. No import direction changes.

Merge notes

_validate_task_target inspected only NumPy, pandas, list and tuple
targets, so with a polars Series y the task_validation diagnostic was
skipped in Preprocessor, CrossFittedTransformer and
RepresentationSearchCV: "warn" reported nothing and "raise" let
scikit-learn's own error surface instead of InvalidParamError.

A polars Series, detected without importing polars, is now inspected
through its NumPy values.

Fixes #181

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.

task_validation is silently skipped when y is a polars Series

1 participant