diff --git a/docs/core_concepts/target_awareness.md b/docs/core_concepts/target_awareness.md index 9b393e0..d8f9461 100644 --- a/docs/core_concepts/target_awareness.md +++ b/docs/core_concepts/target_awareness.md @@ -41,7 +41,8 @@ ignore `y`, so they also fit inside a multi-output pipeline. Explicit spline 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]] 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