Repository navigation
fix(lscp): keep n_bins and random_state as given, resolve them in fit - #778
choksi2212 wants to merge 1 commit into
Conversation
LSCP.fit wrote two derived values back onto its constructor parameters: check_random_state(self.random_state) replaced the seed with a live generator (lscp.py:157) and the n_bins > n_clf clamp overwrote n_bins (lscp.py:379-383). After one fit, get_params() no longer described the estimator the user built and clone() produced a different configuration. Mirror the Sampling/KPCA fix (yzhao062#753): the parameters are kept as given and the resolved values become fitted attributes. random_state_ holds the generator used for feature subspace selection; n_bins_ holds the effective bin count after the clamp, which also moves the reduction warning into fit so it fires once per fit instead of once per test instance (it previously warned per _get_competent_detectors call). RandomState instances and None keep their advancing-generator behavior. Closes yzhao062#754 Signed-off-by: choksi2212 <choksi2212@users.noreply.github.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Hi, gentle nudge on this one — it has been sitting green (12/12 checks) for about five days now. Quick summary of the change: Happy to rebase or reshape anything if you would like it done differently. |
Closes #754
What happened
LSCP.fitwrote two derived values back onto its constructor parameters:self.random_state = check_random_state(self.random_state)replaced the seed the user passed with a live generator objectn_bins > n_clf, the clamp overwroteself.n_binswithn_clfAfter one
fit,get_params()no longer described the estimator the user built, soclone()produced a different configuration:The fix
Mirrors the Sampling/KPCA fix from #753: constructor parameters are kept as given, and the resolved values become fitted attributes:
random_state_holds the generator used for feature subspace selection in_get_local_regionn_bins_holds the effective bin count after the clampn_binsreduction warning moves intofit, so it fires once per fit instead of once per test instance (it previously warned inside_get_competent_detectors, which runs per instance: a 60-row predict emitted 60 identical warnings)RandomStateinstances andNonekeep their advancing-generator behavior, matching the Keep random_state as given in Sampling and KPCA #753 contractThe clamp semantics themselves are unchanged: a clamped fit scores identically to before, because
np.histogram(scores, bins=n_clf)is the same call the old inline clamp made.Tests
pyod/test/test_lscp_contract.py: 10 tests over two classes.Negative control (proven both directions): with
lscp.pyreverted to pristine development, 8 of the 10 fail with the exact issue symptoms (get_paramsleaks the generator,n_binsreads back as 2,cloneinherits the advanced generator, no fitted attributes); the other 2 are behavior-equivalence tests that pass on both (warning dedup + scores unchanged). All 10 pass on the fix.Full suite: 1250 passed, 19 failed, 10 skipped locally - every failure is a missing optional dependency in my venv (torch for the deep-model and GAAL modules, suod for the SUOD tests), none related to LSCP. The pre-existing
test_lscp.py(30 tests) andtest_save_load_clone.py(LSCP clone/save/load round-trips) are fully green, andruff checkreports the same 6 pre-existing complaints on pristine development as on the fix (nothing new introduced).Notes
While tracing the call flow I noticed
_get_local_regionalso mutatesself.local_max_featuresandself.local_min_featureswhen they fall outside their valid ranges - the same parameter-overwrite defect class. It is not part of this fix (the issue scopes #754 ton_bins/random_state), but worth a follow-up issue if the maintainers agree the contract should hold there too.