diff --git a/deeptab/models/base.py b/deeptab/models/base.py index 62afb7aa..35627990 100644 --- a/deeptab/models/base.py +++ b/deeptab/models/base.py @@ -126,7 +126,11 @@ def _validate_fit_inputs(X, y, regression: bool, family: str | None = None, X_va """Validate training and paired validation inputs before estimator mutation. Targets must be finite and valid for the task. Explicit validation features - must match training width and, for named DataFrames, ordered columns. + must match training width and, when the training frame carries string column + names, the same ordered columns. Unnamed validation input (an array or a + frame with positional labels) is rejected for a named training frame here, + before any fitted state is replaced, rather than failing inside the fitted + preprocessor once the model has been rebuilt. """ from deeptab.core.sklearn_compat import ensure_dataframe @@ -152,10 +156,12 @@ def _validate_fit_inputs(X, y, regression: bool, family: str | None = None, X_va ) if validation_features.shape[1] != features.shape[1]: raise column_count_error(features.shape[1], validation_features.shape[1]) - if hasattr(X, "columns") and hasattr(X_val, "columns") and list(X.columns) != list(X_val.columns): + training_columns = list(features.columns) + validation_columns = list(validation_features.columns) + if all(isinstance(name, str) for name in training_columns) and validation_columns != training_columns: raise column_name_error( - missing=[str(name) for name in X.columns if name not in X_val.columns], - extra=[str(name) for name in X_val.columns if name not in X.columns], + missing=[name for name in training_columns if name not in validation_columns], + extra=[str(name) for name in validation_columns if name not in training_columns], ) if not regression or family == "categorical": classes = np.unique(targets) diff --git a/docs/core_concepts/sklearn_api.md b/docs/core_concepts/sklearn_api.md index 917e1a48..5b636f52 100644 --- a/docs/core_concepts/sklearn_api.md +++ b/docs/core_concepts/sklearn_api.md @@ -109,7 +109,12 @@ point regressors accept one numeric target column. Distributional models retain their task-specific target dimensions, including Dirichlet component columns. When supplying an explicit validation set, provide both `X_val` and `y_val`. Feature widths, ordered DataFrame columns, and target widths must match training -data; classifier validation labels must occur in the training classes. +data; classifier validation labels must occur in the training classes. When `X` +is a DataFrame with string column names, `X_val` must carry the same names in +the same order: an unnamed array, a list of rows, or a frame with positional +labels is rejected with a `ColumnNameError` before any fitted state changes. +Training on an array and validating on a named frame remains possible, because +array columns are used by position. Preflight input rejection leaves a previously fitted estimator unchanged, including classes, preprocessing, weights, and predictions. A failure during diff --git a/tests/integration/test_estimator_input_validation.py b/tests/integration/test_estimator_input_validation.py index c3f66bf9..16046ba7 100644 --- a/tests/integration/test_estimator_input_validation.py +++ b/tests/integration/test_estimator_input_validation.py @@ -120,6 +120,36 @@ def test_all_missing_column_fits_and_predicts(self): preds = model.predict(X) assert preds.shape == (40,) + @pytest.mark.parametrize("estimator_name", ["MLPClassifier", "MLPRegressor", "MLPLSS"]) + def test_unnamed_validation_features_for_named_frame_are_rejected_before_building(self, estimator_name): + """An array X_val for a DataFrame X used to pass preflight and crash inside the + fitted preprocessor's column transformer after the model had been rebuilt.""" + import deeptab.models as models + from deeptab.core.exceptions import ColumnNameError + + rng = np.random.default_rng(0) + X = pd.DataFrame(rng.standard_normal((48, 3)), columns=pd.Index(["a", "b", "c"])) + y = rng.integers(0, 2, 48) if estimator_name == "MLPClassifier" else rng.standard_normal(48) + options = {"family": "normal"} if estimator_name == "MLPLSS" else {} + + model = getattr(models, estimator_name)() + with pytest.raises(ColumnNameError, match=r"Missing : \['a', 'b', 'c'\]"): + model.fit(X.iloc[:32], y[:32], X_val=X.iloc[32:].to_numpy(), y_val=y[32:], max_epochs=1, **options) + assert model._data_module is None + assert model._task_model is None + assert not model._built + + def test_named_validation_frame_for_array_training_data_fits(self): + from deeptab.models import MLPRegressor + + rng = np.random.default_rng(0) + X = pd.DataFrame(rng.standard_normal((48, 3)), columns=pd.Index(["a", "b", "c"])) + y = rng.standard_normal(48) + + model = MLPRegressor() + model.fit(X.iloc[:32].to_numpy(), y[:32], X_val=X.iloc[32:], y_val=y[32:], max_epochs=1, batch_size=16) + assert model.predict(X.iloc[:5].to_numpy()).shape == (5,) + class TestIntegerColumnLabels: """Integer column labels that are not 0..n-1 used to be read by position for diff --git a/tests/lifecycle/test_failed_refit.py b/tests/lifecycle/test_failed_refit.py index 120c7177..e9e2d68a 100644 --- a/tests/lifecycle/test_failed_refit.py +++ b/tests/lifecycle/test_failed_refit.py @@ -286,6 +286,26 @@ def test_named_validation_schema_preserves_state(tmp_path): _assert_preserved(model, validation_features, snapshot) +@pytest.mark.parametrize("container", ["ndarray", "positional_frame", "list"]) +def test_unnamed_validation_features_preserve_state(tmp_path, container): + """An unnamed X_val for a named training frame is rejected before the fitted + model is replaced, instead of failing inside the fitted preprocessor afterwards.""" + features, labels, validation_features, validation_labels = _data() + features = pd.DataFrame(features, columns=pd.Index(["first", "second", "third"])) + validation_features = pd.DataFrame(validation_features, columns=features.columns) + model = _model(MLPClassifier, tmp_path) + model.fit(features, labels, X_val=validation_features, y_val=validation_labels, **_fit_options()) + snapshot = _snapshot(model, validation_features) + unnamed = { + "ndarray": validation_features.to_numpy(), + "positional_frame": pd.DataFrame(validation_features.to_numpy()), + "list": validation_features.to_numpy().tolist(), + }[container] + with pytest.raises(DataError, match="first"): + model.fit(features, labels, X_val=unnamed, y_val=validation_labels, **_fit_options()) + _assert_preserved(model, validation_features, snapshot) + + def test_dirichlet_refit_preserves_target_dimensions_and_selected_family(tmp_path): features, _, validation_features, _ = _data() targets = np.random.default_rng(19).dirichlet([2, 3, 4], size=len(features))