From dcd551e453e5d8b3b2d5226a60b09537fafdd4c7 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Fri, 16 Oct 2026 14:25:08 +0200 Subject: [PATCH] fix(splines): expand a missing value to a NaN row in the B/M/I and thin-plate bases The B-, M- and I-spline transformers turned a NaN input into a finite all-zero basis row: np.nan_to_num in each design matrix, there to clear the NaN that BSpline(extrapolate=False) returns outside the knot span, also zeroed the rows of genuinely missing inputs. For the I-spline that row is exactly the encoding of the training minimum, so "missing" and "minimum" became indistinguishable; for the B/M-spline it is impossible data. ThinPlateSplineTransformer fitted on two features had the same defect: its kernel np.where(r > 0, r**2 * log(r), 0.0) sent a NaN distance to the zero branch. This was reachable standalone and from the Preprocessor with missing_policy="propagate" or numerical_imputation=None, while every other spline family and feature map propagates NaN. The shared B/M/I transform now evaluates only the observed rows and leaves the rows of missing values NaN in that feature's basis block (an optional bias column stays 1). The two-feature thin-plate kernel selects its zero branch by r == 0, so a NaN distance stays NaN. Finite rows are unchanged bit for bit under every out_of_range policy, and a missing value does not trigger the "warn" / "error" policies. The Preprocessor numerical_imputation docstring and the missing-values guide no longer claim that every numerical method raises on (or merely tolerates) a missing value. Fixes #87 --- docs/core_concepts/missing_values.md | 4 +- pretab/expansion/spline/base.py | 18 ++++++- .../spline/multivariate/thin_plate.py | 4 +- pretab/preprocessor.py | 6 ++- .../spline/test_spline_expansions.py | 49 ++++++++++++++++++- .../spline/test_thinplate_transformer.py | 17 +++++++ tests/integration/test_edge_case_contract.py | 5 +- tests/integration/test_missing_policy.py | 15 ++++++ 8 files changed, 108 insertions(+), 10 deletions(-) diff --git a/docs/core_concepts/missing_values.md b/docs/core_concepts/missing_values.md index 4729edb..8618151 100644 --- a/docs/core_concepts/missing_values.md +++ b/docs/core_concepts/missing_values.md @@ -23,8 +23,8 @@ pre = Preprocessor( ```{note} Disabling imputation lets `NaN` reach the transformer directly. Scalers, splines, and the -feature maps (`rbf`, `relu`, `sigmoid`, `tanh`) tolerate it, so an affected row's output is -itself undefined; finite-only methods (PLE, numeric binning, periodic encoding, Fourier +feature maps (`rbf`, `relu`, `sigmoid`, `tanh`) propagate it: the affected row's output columns +for that feature are `NaN` (a spline's optional bias column stays `1`); finite-only methods (PLE, numeric binning, periodic encoding, Fourier features, `rff`, `nystroem`) raise a typed error instead, since expanding an undefined value has no meaning for them. `add_missing_indicator=True` still works with imputation disabled: it routes through the same `__missing` branch `missing_policy="separate_state"` uses below. diff --git a/pretab/expansion/spline/base.py b/pretab/expansion/spline/base.py index ab365bf..acd416b 100644 --- a/pretab/expansion/spline/base.py +++ b/pretab/expansion/spline/base.py @@ -127,6 +127,10 @@ class BaseSplineTransformer(BasePreTabTransformer): takes precedence over the automatic ``output_dim`` strategy. Multi-column input is expanded column by column and stacked horizontally. + Missing values are ignored for knot placement; at transform time a missing value + expands to a row of NaN in its feature's basis block (an optional bias column + stays 1), as in the other spline families. + Examples -------- >>> import numpy as np @@ -323,7 +327,12 @@ def transform(self, X): for i in range(X.shape[1]): knots = self.knots_[i] xi = resolve_out_of_range(X[:, i], knots[0], knots[-1], self._resolved_policy(), estimator=self) - design = self._design_matrix(xi, knots) + # A missing value has no position on the basis: only the observed rows + # are evaluated and the missing ones stay NaN (the "propagate" contract + # of the other spline families) instead of becoming a finite basis row. + observed = ~np.isnan(xi) + design = np.full((len(xi), self.n_basis_[i]), np.nan) + design[observed] = self._design_matrix(xi[observed], knots) if self.include_bias: design = np.hstack([np.ones((design.shape[0], 1)), design]) transformed.append(design) @@ -335,7 +344,12 @@ def fit_transform(self, X, y=None): return self.fit(X, y).transform(X) def _design_matrix(self, x: np.ndarray, knots: np.ndarray) -> np.ndarray: - """Return the basis matrix for a single feature (without the bias column).""" + """Return the basis matrix for a single feature (without the bias column). + + ``x`` holds only the observed (non-missing) values, already resolved + against the out-of-range policy; :meth:`transform` fills the rows of + missing values with NaN. + """ raise NotImplementedError def _feature_suffix(self) -> str: diff --git a/pretab/expansion/spline/multivariate/thin_plate.py b/pretab/expansion/spline/multivariate/thin_plate.py index a9aaa84..ed7db65 100644 --- a/pretab/expansion/spline/multivariate/thin_plate.py +++ b/pretab/expansion/spline/multivariate/thin_plate.py @@ -125,7 +125,9 @@ def _tps_kernel(r, d): if d == 1: return r**3 if d == 2: - return np.where(r > 0, r**2 * np.log(np.where(r > 0, r, 1.0)), 0.0) + # r == 0 (not r > 0) selects the zero branch, so a missing + # coordinate keeps its NaN distance instead of becoming 0. + return np.where(r == 0, 0.0, r**2 * np.log(np.where(r > 0, r, 1.0))) # d >= 3: biharmonic (linear) radial kernel. return r diff --git a/pretab/preprocessor.py b/pretab/preprocessor.py index a8193b8..424e26e 100644 --- a/pretab/preprocessor.py +++ b/pretab/preprocessor.py @@ -209,8 +209,10 @@ class Preprocessor(TransformerMixin, BaseEstimator): numerical_imputation : str or None, default="median" Strategy for the ``SimpleImputer`` that runs *before* every numerical method. Accepts any ``sklearn`` strategy (``"median"``, ``"mean"``, ``"most_frequent"``, ``"constant"``). - ``None`` disables imputation, so NaNs reach the numerical transformers unchanged and the - finite-input methods (all numerical methods, including PLE) raise on missing values. + ``None`` disables imputation, so NaNs reach the numerical transformers unchanged: the + scalers, splines and ``rbf`` / ``relu`` / ``sigmoid`` / ``tanh`` feature maps propagate a + missing value as NaN in that feature's output, while the finite-input methods + (``"ple"``, ``"custombin"``, ``"fourier"``, ``"polynomial"``) raise. categorical_imputation : str or None, default="most_frequent" Strategy for the ``SimpleImputer`` that runs *before* every categorical method. ``None`` disables imputation for categorical columns. diff --git a/tests/expansion/spline/test_spline_expansions.py b/tests/expansion/spline/test_spline_expansions.py index a55d9ca..c28b553 100644 --- a/tests/expansion/spline/test_spline_expansions.py +++ b/tests/expansion/spline/test_spline_expansions.py @@ -1,3 +1,5 @@ +import warnings + import numpy as np import pytest @@ -108,12 +110,57 @@ def test_mspline_non_negative(data): def test_mspline_handles_nan(): + """A missing value is left out of knot placement and expands to a NaN row.""" X = np.linspace(0, 1, 50).reshape(-1, 1) X[5] = np.nan transformer = MSplineTransformer(output_dim=6) Xt = transformer.fit_transform(X) assert Xt.shape == (50, 6) - assert np.isfinite(Xt).all() + assert np.isnan(Xt[5]).all() + assert np.isfinite(np.delete(Xt, 5, axis=0)).all() + + +# --- missing values expand to NaN basis rows ------------------------------------------- + + +@pytest.mark.parametrize("cls", [BSplineTransformer, MSplineTransformer, ISplineTransformer]) +@pytest.mark.parametrize("include_bias", [False, True]) +@pytest.mark.parametrize("out_of_range", ["clip", "extrapolate", "warn", "error"]) +def test_bmi_spline_missing_value_expands_to_a_nan_row(cls, include_bias, out_of_range): + """A missing value became a finite all-zero basis row: impossible data for the + B/M-spline and, for the I-spline, the exact encoding of the training minimum.""" + X = np.linspace(0.0, 10.0, 50).reshape(-1, 1) + transformer = cls(output_dim=5, include_bias=include_bias, policy={"out_of_range": out_of_range}).fit(X) + observed = np.array([[0.0], [2.5], [10.0]]) + if out_of_range in ("clip", "extrapolate"): + observed = np.vstack([observed, [[-3.0], [14.0]]]) + expected = transformer.transform(observed) + + probe = np.insert(observed, [0, 2], np.nan, axis=0) + with warnings.catch_warnings(): + # A missing value is not out of range: it must not trigger "warn" / "error". + warnings.simplefilter("error") + out = transformer.transform(probe) + + missing = np.isnan(probe[:, 0]) + basis = slice(1, None) if include_bias else slice(None) + assert np.isnan(out[missing, basis]).all() + if include_bias: + np.testing.assert_array_equal(out[:, 0], 1.0) + np.testing.assert_array_equal(out[~missing], expected) + + +@pytest.mark.parametrize("cls", [BSplineTransformer, MSplineTransformer, ISplineTransformer]) +def test_bmi_spline_missing_value_only_affects_its_own_feature_block(cls): + rng = np.random.default_rng(0) + X = rng.uniform(0.0, 10.0, size=(60, 2)) + X[4, 0] = np.nan + X[9, 1] = np.nan + transformer = cls(output_dim=6).fit(X) + out = transformer.transform(X) + np.testing.assert_array_equal(np.isnan(out[:, :6]).any(axis=1), np.isnan(X[:, 0])) + np.testing.assert_array_equal(np.isnan(out[:, 6:]).any(axis=1), np.isnan(X[:, 1])) + assert np.isnan(out[4, :6]).all() and np.isnan(out[9, 6:]).all() def test_ispline_monotonic_increasing(): diff --git a/tests/expansion/spline/test_thinplate_transformer.py b/tests/expansion/spline/test_thinplate_transformer.py index 5d4c9d8..4b94b30 100644 --- a/tests/expansion/spline/test_thinplate_transformer.py +++ b/tests/expansion/spline/test_thinplate_transformer.py @@ -124,3 +124,20 @@ def test_tprs_transform_requires_fit(): transformer.transform(np.random.rand(5, 1)) with pytest.raises(NotFittedError): transformer.get_penalty_matrix() + + +@pytest.mark.parametrize("n_features", [1, 2, 3]) +def test_tprs_expands_a_missing_value_to_a_nan_row(n_features): + """The two-feature kernel used to map a NaN distance to 0, turning a missing + coordinate into an all-zero row; every input dimension now gives NaN, as the + other spline families do.""" + rng = np.random.default_rng(0) + X = rng.uniform(size=(80, n_features)) + transformer = ThinPlateSplineTransformer(n_components=5, random_state=0).fit(X) + probe = X[:3].copy() + probe[1, 0] = np.nan + + out = transformer.transform(probe) + + assert np.isnan(out[1]).all() + np.testing.assert_allclose(out[[0, 2]], transformer.transform(X[[0, 2]]), rtol=1e-12, atol=1e-12) diff --git a/tests/integration/test_edge_case_contract.py b/tests/integration/test_edge_case_contract.py index 97e8b07..1cedf8b 100644 --- a/tests/integration/test_edge_case_contract.py +++ b/tests/integration/test_edge_case_contract.py @@ -84,9 +84,10 @@ def _factory(name): ALL_FAMILIES = CONSTANT_RAISES + CONSTANT_GRACEFUL # Families that let missing values pass through the basis (NaN in -> NaN row out). -# The B/M/I splines instead clip a missing value to the fitted boundary, so they -# are intentionally excluded here. NAN_PROPAGATING = [ + "BSpline", + "MSpline", + "ISpline", "RBF", "ReLU", "Sigmoid", diff --git a/tests/integration/test_missing_policy.py b/tests/integration/test_missing_policy.py index c7ec70e..bcf481a 100644 --- a/tests/integration/test_missing_policy.py +++ b/tests/integration/test_missing_policy.py @@ -98,6 +98,21 @@ def test_propagate_lets_nan_through(frame_with_nan, y): assert np.isnan(out).any() +@pytest.mark.parametrize("method", ["bspline", "mspline", "ispline"]) +def test_propagate_expands_a_missing_value_to_a_nan_basis_row(method): + """The B/M/I splines encoded a missing value as a finite all-zero row, which for + the I-spline is exactly the row of the smallest observed value.""" + rng = np.random.default_rng(0) + frame = pd.DataFrame({"x": rng.uniform(0, 10, 300)}) + frame.loc[0, "x"] = np.nan + pre = Preprocessor( + numerical_method=method, missing_policy="propagate", target_aware=False, placement_strategy="uniform" + ).fit(frame, rng.normal(size=300)) + out = np.asarray(pre.transform(frame, return_array=True)) + assert np.isnan(out[0]).all() + assert np.isfinite(out[1:]).all() + + # --- impute --------------------------------------------------------------------