From e16f02dd13c1213fefe3a791bf12e9e7cceeeec4 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:51:54 +0100 Subject: [PATCH 1/2] fix(splines): do not report explicit knot placement as target-aware Explicit knot_locations take priority over target-aware placement, so no selector reads y. With target_aware=True the B/M/I splines still emitted a LeakageWarning and reported uses_target_ / spec.uses_target as True, because target use was derived from target_aware alone. BaseSplineTransformer.is_supervised now returns True only for target-aware placement without knot_locations; the leakage warning, uses_target_ and the representation spec follow it. Fixes #185 --- docs/core_concepts/target_awareness.md | 3 ++- pretab/expansion/spline/base.py | 13 +++++++++++- tests/core/test_supervised_contract.py | 28 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index 9b393e0..c60e4a1 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -34,7 +34,8 @@ multi-output `y` of shape `(n_samples, k)` with `k > 1` raises `IncompatiblePara `ple` and for an `optional` method with `target_aware=True`; a column vector of shape `(n_samples, 1)` counts as a single target. With `target_aware=False` the `optional` methods ignore `y`, so they also fit inside a multi-output pipeline. Explicit spline -`knot_locations` also ignore `y`, because no target-aware selector runs. +`knot_locations` also ignore `y`, because no target-aware selector runs: such a fit reports +`uses_target_ == False` and emits no `LeakageWarning`, even with `target_aware=True`. ## The fitted-usage flag diff --git a/pretab/expansion/spline/base.py b/pretab/expansion/spline/base.py index 73424cc..321e192 100644 --- a/pretab/expansion/spline/base.py +++ b/pretab/expansion/spline/base.py @@ -69,7 +69,8 @@ class BaseSplineTransformer(BasePreTabTransformer): knot_locations : ndarray or None, default=None Explicit internal knot locations applied to every feature. Takes priority over ``target_aware`` placement and the automatic strategy, and overrides - ``output_dim`` and the adaptive bounds. Repeated knots are merged; knots + ``output_dim`` and the adaptive bounds. ``y`` is then not used, even with + ``target_aware=True``, so the fit is not reported as target-aware. Repeated knots are merged; knots on or outside a feature's fitted range are dropped for that feature with a :class:`~pretab.exceptions.DataWarning`, without adding replacements. A feature with ``K`` retained unique interior knots gets @@ -210,6 +211,16 @@ def __init__( _selector_spline_type: ClassVar[Literal["bspline", "mspline", "ispline"]] = "bspline" + @property + def is_supervised(self) -> bool: + """Whether this transformer consumes ``y`` given its configuration. + + Explicit ``knot_locations`` take priority over target-aware placement, so + no selector reads ``y`` then: the fit emits no leakage warning and + ``uses_target_`` (and the representation spec) report ``False``. + """ + return bool(self.target_aware) and self.knot_locations is None + def _basis_to_knots(self, n_basis: int) -> int: """Convert a basis-function count to the number of internal knots.""" return basis_to_knots(n_basis, self.degree) diff --git a/tests/core/test_supervised_contract.py b/tests/core/test_supervised_contract.py index 32ea18d..03f73ef 100644 --- a/tests/core/test_supervised_contract.py +++ b/tests/core/test_supervised_contract.py @@ -17,6 +17,8 @@ from pretab.core.supervised import in_controlled_context, warn_target_leakage from pretab.transformers import ( BSplineTransformer, + ISplineTransformer, + MSplineTransformer, PLETransformer, RBFExpansionTransformer, ) @@ -127,3 +129,29 @@ def test_warn_helper_ignores_unsupervised(data): with warnings.catch_warnings(): warnings.simplefilter("error", LeakageWarning) warn_target_leakage(est, y) + + +@pytest.mark.parametrize("cls", [BSplineTransformer, MSplineTransformer, ISplineTransformer]) +def test_explicit_knot_locations_are_not_target_aware(cls, data): + """Explicit knots bypass the target-aware selector, so y is never read; the fit + still warned about leakage and reported uses_target_=True.""" + X, y = data + knots = np.quantile(X[:, 0], [0.25, 0.5, 0.75]) + transformer = cls(knot_locations=knots, target_aware=True, placement_strategy="cart") + with warnings.catch_warnings(): + warnings.simplefilter("error", LeakageWarning) + transformer.fit(X, y) + + assert transformer.is_supervised is False + assert transformer.uses_target_ is False + assert transformer.get_representation_spec().uses_target is False + unrelated = cls(knot_locations=knots, target_aware=True, placement_strategy="cart").fit(X, -y) + np.testing.assert_array_equal(transformer.transform(X), unrelated.transform(X)) + + +def test_target_aware_spline_without_knots_stays_supervised(data): + X, y = data + transformer = BSplineTransformer(target_aware=True, placement_strategy="cart") + with pytest.warns(LeakageWarning): + transformer.fit(X, y) + assert transformer.uses_target_ is True From 210e79b3d8dded981892ab557d97c1033d5bd0a0 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Sun, 29 Nov 2026 14:52:57 +0100 Subject: [PATCH 2/2] docs: describe explicit-knot target usage in the fitted-usage section Move the sentence out of the paragraph on ignored targets, which #176 also extends, so the two branches merge without a conflict. Refs #185 --- docs/core_concepts/target_awareness.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index c60e4a1..d8f9461 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -34,15 +34,15 @@ multi-output `y` of shape `(n_samples, k)` with `k > 1` raises `IncompatiblePara `ple` and for an `optional` method with `target_aware=True`; a column vector of shape `(n_samples, 1)` counts as a single target. With `target_aware=False` the `optional` methods ignore `y`, so they also fit inside a multi-output pipeline. Explicit spline -`knot_locations` also ignore `y`, because no target-aware selector runs: such a fit reports -`uses_target_ == False` and emits no `LeakageWarning`, even with `target_aware=True`. +`knot_locations` also ignore `y`, because no target-aware selector runs. ## The fitted-usage flag After fitting, a transformer reports whether it actually consumed the target through `uses_target_`. This is the ground truth for an individual fit, and it flows into the [`RepresentationSpec`](outputs_and_inspection.md) so a serialized representation records -whether it was supervised. +whether it was supervised. A B/M/I spline with explicit `knot_locations` reports `False` and +emits no `LeakageWarning`, even with `target_aware=True`, because no selector reads `y`. ```python X = [[0.0], [1.0], [2.0]]