From 827054ac6ad11705221eed10e60976ebfa76359e Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Wed, 21 Oct 2026 20:49:00 +0200 Subject: [PATCH 1/3] fix(models): mark the estimator unfitted when build_model() rebuilds it build_model() replaced the trained network with freshly initialized weights but kept is_fitted_ = True from an earlier fit(), so predict(), save() and InferenceModel silently used the untrained network. Reset the flag in _FitMixin._build_model and SklearnBaseLSS.build_model (only when it was set, so a fresh estimator gains no attribute) and document it in the build_model() docstrings. Fixes #541 --- deeptab/models/_mixins/fit.py | 4 ++++ deeptab/models/classifier_base.py | 4 +++- deeptab/models/lss_base.py | 8 ++++++- deeptab/models/regressor_base.py | 4 +++- tests/integration/test_fit_lifecycle.py | 30 +++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/deeptab/models/_mixins/fit.py b/deeptab/models/_mixins/fit.py index 036a0480..adde8f9c 100644 --- a/deeptab/models/_mixins/fit.py +++ b/deeptab/models/_mixins/fit.py @@ -113,6 +113,10 @@ def _build_model( mode: str | None = None, ): """Builds the model using the provided training data.""" + # The rebuilt network has untrained weights, so a previous fit() no + # longer describes this estimator until it is fitted again. + if getattr(self, "is_fitted_", False): + self.is_fitted_ = False if dataloader_kwargs is None: dataloader_kwargs = {} diff --git a/deeptab/models/classifier_base.py b/deeptab/models/classifier_base.py index 6acb089a..7e87081f 100644 --- a/deeptab/models/classifier_base.py +++ b/deeptab/models/classifier_base.py @@ -193,7 +193,9 @@ def build_model( Returns ------- self : object - The built classifier. + The built classifier. Its weights are untrained, so the estimator + counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` + completes. """ classes = np.unique(y) diff --git a/deeptab/models/lss_base.py b/deeptab/models/lss_base.py index 1b1c7365..58706d7e 100644 --- a/deeptab/models/lss_base.py +++ b/deeptab/models/lss_base.py @@ -137,8 +137,14 @@ def build_model( Returns ------- self : object - The built distributional regressor. + The built distributional regressor. Its weights are untrained, so the estimator + counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` + completes. """ + # The rebuilt network has untrained weights, so a previous fit() no + # longer describes this estimator until it is fitted again. + if getattr(self, "is_fitted_", False): + self.is_fitted_ = False if dataloader_kwargs is None: dataloader_kwargs = {} self._configure_distribution(y, family, distributional_kwargs) diff --git a/deeptab/models/regressor_base.py b/deeptab/models/regressor_base.py index b5db64e9..ca49df6e 100644 --- a/deeptab/models/regressor_base.py +++ b/deeptab/models/regressor_base.py @@ -92,7 +92,9 @@ def build_model( Returns ------- self : object - The built regressor. + The built regressor. Its weights are untrained, so the estimator + counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` + completes. """ return super()._build_model( diff --git a/tests/integration/test_fit_lifecycle.py b/tests/integration/test_fit_lifecycle.py index 94f9db93..915101a5 100644 --- a/tests/integration/test_fit_lifecycle.py +++ b/tests/integration/test_fit_lifecycle.py @@ -92,3 +92,33 @@ def test_builtin_callbacks_still_present(self): cb = _RecordingCallback() reg.fit(X, y, random_state=RANDOM_STATE, callbacks=[cb], **FIT_KWARGS) assert reg._best_model_path, "ModelCheckpoint callback must still have produced a checkpoint" + + +class TestBuildModelResetsFittedState: + """build_model() replaces the network with untrained weights, so the estimator + must not keep counting as fitted from an earlier fit().""" + + def test_regressor_is_unfitted_after_rebuilding(self): + X, y = _make_reg_data() + reg = MLPRegressor().fit(X, y, random_state=RANDOM_STATE, **FIT_KWARGS) + assert reg.is_fitted_ is True + + reg.build_model(X, y) + assert reg.is_fitted_ is False + with pytest.raises(NotFittedError): + reg.predict(X) + + reg.fit(X, y, rebuild=False, **FIT_KWARGS) + assert reg.is_fitted_ is True + + def test_lss_is_unfitted_after_rebuilding(self): + from deeptab.models import MLPLSS + + X, y = _make_reg_data() + model = MLPLSS().fit(X, y, family="normal", random_state=RANDOM_STATE, **FIT_KWARGS) + assert model.is_fitted_ is True + + model.build_model(X, y, family="normal") + assert model.is_fitted_ is False + with pytest.raises(NotFittedError): + model.predict(X) From 3ffdf2e6ca352a32ab36b20ad48db291fd15598c Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Wed, 21 Oct 2026 21:00:30 +0200 Subject: [PATCH 2/3] test: move build_model fitted-state tests into their own module Keeps them apart from the callback tests appended to test_fit_lifecycle.py by #486. Refs #541 --- tests/integration/test_build_model_state.py | 49 +++++++++++++++++++++ tests/integration/test_fit_lifecycle.py | 30 ------------- 2 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 tests/integration/test_build_model_state.py diff --git a/tests/integration/test_build_model_state.py b/tests/integration/test_build_model_state.py new file mode 100644 index 00000000..08e4ddf7 --- /dev/null +++ b/tests/integration/test_build_model_state.py @@ -0,0 +1,49 @@ +"""build_model() replaces the network with untrained weights, so the estimator +must not keep counting as fitted from an earlier fit().""" + +from __future__ import annotations + +from typing import Any + +import numpy as np +import pandas as pd +import pytest + +from deeptab.core.exceptions import NotFittedError +from deeptab.models import MLPLSS, MLPRegressor + +RANDOM_STATE = 0 + +FIT_KWARGS: dict[str, Any] = {"max_epochs": 1, "batch_size": 32} + + +def _make_reg_data(): + rng = np.random.default_rng(RANDOM_STATE) + X = rng.standard_normal((120, 4)) + y = rng.standard_normal(120) + return pd.DataFrame(X, columns=[f"f{i}" for i in range(4)]), y # type: ignore[call-overload] + + +def test_regressor_is_unfitted_after_rebuilding(): + X, y = _make_reg_data() + reg = MLPRegressor().fit(X, y, random_state=RANDOM_STATE, **FIT_KWARGS) + assert reg.is_fitted_ is True + + reg.build_model(X, y) + assert reg.is_fitted_ is False + with pytest.raises(NotFittedError): + reg.predict(X) + + reg.fit(X, y, rebuild=False, **FIT_KWARGS) + assert reg.is_fitted_ is True + + +def test_lss_is_unfitted_after_rebuilding(): + X, y = _make_reg_data() + model = MLPLSS().fit(X, y, family="normal", random_state=RANDOM_STATE, **FIT_KWARGS) + assert model.is_fitted_ is True + + model.build_model(X, y, family="normal") + assert model.is_fitted_ is False + with pytest.raises(NotFittedError): + model.predict(X) diff --git a/tests/integration/test_fit_lifecycle.py b/tests/integration/test_fit_lifecycle.py index 915101a5..94f9db93 100644 --- a/tests/integration/test_fit_lifecycle.py +++ b/tests/integration/test_fit_lifecycle.py @@ -92,33 +92,3 @@ def test_builtin_callbacks_still_present(self): cb = _RecordingCallback() reg.fit(X, y, random_state=RANDOM_STATE, callbacks=[cb], **FIT_KWARGS) assert reg._best_model_path, "ModelCheckpoint callback must still have produced a checkpoint" - - -class TestBuildModelResetsFittedState: - """build_model() replaces the network with untrained weights, so the estimator - must not keep counting as fitted from an earlier fit().""" - - def test_regressor_is_unfitted_after_rebuilding(self): - X, y = _make_reg_data() - reg = MLPRegressor().fit(X, y, random_state=RANDOM_STATE, **FIT_KWARGS) - assert reg.is_fitted_ is True - - reg.build_model(X, y) - assert reg.is_fitted_ is False - with pytest.raises(NotFittedError): - reg.predict(X) - - reg.fit(X, y, rebuild=False, **FIT_KWARGS) - assert reg.is_fitted_ is True - - def test_lss_is_unfitted_after_rebuilding(self): - from deeptab.models import MLPLSS - - X, y = _make_reg_data() - model = MLPLSS().fit(X, y, family="normal", random_state=RANDOM_STATE, **FIT_KWARGS) - assert model.is_fitted_ is True - - model.build_model(X, y, family="normal") - assert model.is_fitted_ is False - with pytest.raises(NotFittedError): - model.predict(X) From c4b0e2dbb38c37f92c3aaa52fcd87c94ad8fef86 Mon Sep 17 00:00:00 2001 From: ChrisW09 <50968720+ChrisW09@users.noreply.github.com> Date: Wed, 21 Oct 2026 23:43:56 +0200 Subject: [PATCH 3/3] fix(models): forget the previous best checkpoint and reset before a failing build build_model() now also clears _best_model_path, which a regressor's score() would otherwise reload into the rebuilt network. Classifiers reset the fitted state before replacing classes_, so a build that fails on its labels no longer leaves a fitted-looking estimator with the new classes and the old network. The docstrings no longer claim is_fitted_ exists on an estimator that was never fitted. Refs #541 --- deeptab/models/_mixins/fit.py | 9 +++++-- deeptab/models/classifier_base.py | 7 ++++-- deeptab/models/lss_base.py | 9 +++---- deeptab/models/regressor_base.py | 4 +-- tests/integration/test_build_model_state.py | 28 ++++++++++++++++----- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/deeptab/models/_mixins/fit.py b/deeptab/models/_mixins/fit.py index adde8f9c..bd399e7f 100644 --- a/deeptab/models/_mixins/fit.py +++ b/deeptab/models/_mixins/fit.py @@ -85,6 +85,12 @@ def _emit_event(self, event: str, **kwargs: Any) -> None: ... # Model construction # ------------------------------------------------------------------ + def _mark_unfitted(self) -> None: + """Forget a previous ``fit()``: its fitted flag and its best checkpoint.""" + if getattr(self, "is_fitted_", False): + self.is_fitted_ = False + self._best_model_path = None + def _build_model( self, X, @@ -115,8 +121,7 @@ def _build_model( """Builds the model using the provided training data.""" # The rebuilt network has untrained weights, so a previous fit() no # longer describes this estimator until it is fitted again. - if getattr(self, "is_fitted_", False): - self.is_fitted_ = False + self._mark_unfitted() if dataloader_kwargs is None: dataloader_kwargs = {} diff --git a/deeptab/models/classifier_base.py b/deeptab/models/classifier_base.py index 7e87081f..20f724aa 100644 --- a/deeptab/models/classifier_base.py +++ b/deeptab/models/classifier_base.py @@ -194,9 +194,12 @@ def build_model( ------- self : object The built classifier. Its weights are untrained, so the estimator - counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` - completes. + counts as unfitted (``predict()`` raises ``NotFittedError``) until + ``fit()`` completes. """ + # Forget the previous fit before classes_ changes, so that a build that + # fails below doesn't leave a fitted-looking estimator behind. + self._mark_unfitted() classes = np.unique(y) num_classes = len(classes) diff --git a/deeptab/models/lss_base.py b/deeptab/models/lss_base.py index 58706d7e..8f2bdbb5 100644 --- a/deeptab/models/lss_base.py +++ b/deeptab/models/lss_base.py @@ -137,14 +137,13 @@ def build_model( Returns ------- self : object - The built distributional regressor. Its weights are untrained, so the estimator - counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` - completes. + The built distributional regressor. Its weights are untrained, so + the estimator counts as unfitted (``predict()`` raises + ``NotFittedError``) until ``fit()`` completes. """ # The rebuilt network has untrained weights, so a previous fit() no # longer describes this estimator until it is fitted again. - if getattr(self, "is_fitted_", False): - self.is_fitted_ = False + self._mark_unfitted() if dataloader_kwargs is None: dataloader_kwargs = {} self._configure_distribution(y, family, distributional_kwargs) diff --git a/deeptab/models/regressor_base.py b/deeptab/models/regressor_base.py index ca49df6e..45239ed0 100644 --- a/deeptab/models/regressor_base.py +++ b/deeptab/models/regressor_base.py @@ -93,8 +93,8 @@ def build_model( ------- self : object The built regressor. Its weights are untrained, so the estimator - counts as unfitted (``is_fitted_`` is ``False``) until ``fit()`` - completes. + counts as unfitted (``predict()`` raises ``NotFittedError``) until + ``fit()`` completes. """ return super()._build_model( diff --git a/tests/integration/test_build_model_state.py b/tests/integration/test_build_model_state.py index 08e4ddf7..77592a60 100644 --- a/tests/integration/test_build_model_state.py +++ b/tests/integration/test_build_model_state.py @@ -10,7 +10,7 @@ import pytest from deeptab.core.exceptions import NotFittedError -from deeptab.models import MLPLSS, MLPRegressor +from deeptab.models import MLPLSS, MLPClassifier, MLPRegressor RANDOM_STATE = 0 @@ -24,26 +24,42 @@ def _make_reg_data(): return pd.DataFrame(X, columns=[f"f{i}" for i in range(4)]), y # type: ignore[call-overload] -def test_regressor_is_unfitted_after_rebuilding(): +def test_regressor_is_unfitted_after_rebuilding(tmp_path): X, y = _make_reg_data() - reg = MLPRegressor().fit(X, y, random_state=RANDOM_STATE, **FIT_KWARGS) + reg = MLPRegressor().fit(X, y, random_state=RANDOM_STATE, checkpoint_path=str(tmp_path), **FIT_KWARGS) assert reg.is_fitted_ is True + assert reg._best_model_path reg.build_model(X, y) assert reg.is_fitted_ is False + assert reg._best_model_path is None with pytest.raises(NotFittedError): reg.predict(X) - reg.fit(X, y, rebuild=False, **FIT_KWARGS) + reg.fit(X, y, rebuild=False, checkpoint_path=str(tmp_path), **FIT_KWARGS) assert reg.is_fitted_ is True -def test_lss_is_unfitted_after_rebuilding(): +def test_lss_is_unfitted_after_rebuilding(tmp_path): X, y = _make_reg_data() - model = MLPLSS().fit(X, y, family="normal", random_state=RANDOM_STATE, **FIT_KWARGS) + model = MLPLSS().fit(X, y, family="normal", random_state=RANDOM_STATE, checkpoint_path=str(tmp_path), **FIT_KWARGS) assert model.is_fitted_ is True model.build_model(X, y, family="normal") assert model.is_fitted_ is False with pytest.raises(NotFittedError): model.predict(X) + + +def test_classifier_is_unfitted_when_rebuilding_fails(tmp_path): + X, _ = _make_reg_data() + y = np.arange(len(X)) % 3 + clf = MLPClassifier().fit(X, y, random_state=RANDOM_STATE, checkpoint_path=str(tmp_path), **FIT_KWARGS) + assert clf.is_fitted_ is True + + two_classes = y % 2 + with pytest.raises(ValueError, match="not seen during fit"): + clf.build_model(X[:100], two_classes[:100], X_val=X[100:], y_val=y[100:]) + assert clf.is_fitted_ is False + with pytest.raises(NotFittedError): + clf.predict(X)