|
12 | 12 | from sklearn import config_context |
13 | 13 | from sklearn.pipeline import Pipeline |
14 | 14 |
|
15 | | -from pretab import FrozenRepresentationError, Preprocessor, RepresentationConfig |
| 15 | +from pretab import FrozenRepresentationError, OutputBudgetError, Preprocessor, RepresentationConfig |
16 | 16 |
|
17 | 17 |
|
18 | 18 | @pytest.fixture |
@@ -205,6 +205,53 @@ def test_mark_stale(frame, target): |
205 | 205 | assert p.stale_reason_ == "input schema drifted" |
206 | 206 |
|
207 | 207 |
|
| 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 | + |
208 | 255 | def test_frozen_takes_precedence_over_stale(frame, target): |
209 | 256 | p = _make().fit(frame, target) |
210 | 257 | p.mark_stale("drift") |
|
0 commit comments