Skip to content

Commit 826e78e

Browse files
authored
Merge pull request #139 from xillig/fix/lifecycle-stale-after-fit
fix(preprocessor): clear the stale mark after a successful fit
2 parents 0bba697 + a773522 commit 826e78e

3 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎docs/core_concepts/reproducibility.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ else:
186186
Freezing is useful when a representation is validated and about to ship. To make a fresh,
187187
unfrozen copy, use `clone_unfitted()`. To retrain, `refit(X, y)` returns a **new** fitted
188188
object and leaves the original untouched, and `mark_stale(reason)` records why an existing one
189-
should no longer be used.
189+
should no longer be used. The mark is advisory and describes the current fitted state: fitting
190+
the same (unfrozen) object again with `fit` replaces that state, so a successful `fit` clears
191+
the mark and the state returns to `FITTED`.
190192

191193
```{warning}
192194
Attempts to refit a frozen preprocessor, change its parameters, or change its

‎pretab/preprocessor.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,7 @@ def fit(self, X, y=None, embeddings=None):
740740
self._log_internal_decisions()
741741

742742
self._fit_config_ = fit_summary
743+
self.__dict__.pop("_stale_reason", None)
743744
return self
744745

745746
def transform(self, X, embeddings=None, return_array=None):
@@ -1741,7 +1742,9 @@ def mark_stale(self, reason: str) -> "Preprocessor":
17411742
17421743
Records ``reason`` and flips :attr:`lifecycle_state_` to ``STALE`` (unless
17431744
already ``FROZEN``). Purely advisory: it does not alter the fitted state.
1744-
Returns ``self`` for chaining.
1745+
A later successful :meth:`fit` of this object replaces that state, so it
1746+
clears the mark and the state returns to ``FITTED``; a ``fit`` that raises
1747+
leaves the mark in place. Returns ``self`` for chaining.
17451748
"""
17461749
check_is_fitted(self)
17471750
self._stale_reason = reason

‎tests/integration/test_frozen_lifecycle.py‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from sklearn import config_context
1313
from sklearn.pipeline import Pipeline
1414

15-
from pretab import FrozenRepresentationError, Preprocessor, RepresentationConfig
15+
from pretab import FrozenRepresentationError, OutputBudgetError, Preprocessor, RepresentationConfig
1616

1717

1818
@pytest.fixture
@@ -205,6 +205,53 @@ def test_mark_stale(frame, target):
205205
assert p.stale_reason_ == "input schema drifted"
206206

207207

208+
def test_fit_clears_the_stale_mark(frame, target):
209+
"""A successful fit replaces the state the mark described; it used to stay STALE."""
210+
p = _make().fit(frame, target)
211+
p.mark_stale("drift")
212+
p.fit(frame.assign(a=frame["a"] * 2), target)
213+
assert p.lifecycle_state_ == "FITTED"
214+
assert p.stale_reason_ is None
215+
216+
217+
def test_fit_transform_clears_the_stale_mark(frame, target):
218+
p = _make().fit(frame, target)
219+
p.mark_stale("drift")
220+
p.fit_transform(frame, target)
221+
assert p.lifecycle_state_ == "FITTED"
222+
assert p.stale_reason_ is None
223+
224+
225+
def test_failed_fit_keeps_the_stale_mark(frame, target):
226+
p = _make().fit(frame, target)
227+
p.mark_stale("drift")
228+
p.set_params(max_output_features=1)
229+
with pytest.raises(OutputBudgetError):
230+
p.fit(frame, target)
231+
assert p.stale_reason_ == "drift"
232+
233+
234+
def test_fit_does_not_add_stale_state_to_the_spec(frame, target):
235+
"""Clearing the mark removes the attribute, so specs of never-stale fits are unchanged."""
236+
never_stale = _make().fit(frame, target)
237+
assert "_stale_reason" not in never_stale.to_spec()["state"]
238+
239+
refitted = _make().fit(frame, target)
240+
refitted.mark_stale("drift")
241+
refitted.fit(frame, target)
242+
assert refitted.to_spec()["state"] == never_stale.to_spec()["state"]
243+
assert refitted.fingerprint_ == never_stale.fingerprint_
244+
245+
246+
def test_stale_mark_survives_a_spec_round_trip_until_refit(frame, target):
247+
p = _make().fit(frame, target)
248+
p.mark_stale("drift")
249+
restored = Preprocessor.from_spec(p.to_spec())
250+
assert restored.lifecycle_state_ == "STALE"
251+
restored.fit(frame, target)
252+
assert restored.lifecycle_state_ == "FITTED"
253+
254+
208255
def test_frozen_takes_precedence_over_stale(frame, target):
209256
p = _make().fit(frame, target)
210257
p.mark_stale("drift")

0 commit comments

Comments
 (0)