fix: import scikit-learn lazily in openstef-beam r2 metric - #981
fix: import scikit-learn lazily in openstef-beam r2 metric#981Valyrian-Code wants to merge 6 commits into
Conversation
egordm
left a comment
There was a problem hiding this comment.
Not sure about this one. R2 is used as default in many openstef components. Making the import lazy would notify users only at runtime that they are missing a dependency.
I don't think making it lazy is the right approach.
It is really tempting to inline R2 though, which would remove openstef-beam's dependency on sklearn.
Per @egordm's review on OpenSTEF#981: instead of lazily importing sklearn's r2_score, compute R2 directly with numpy so openstef-beam no longer depends on scikit-learn at all. Matches scikit-learn's r2_score including sample weights and the constant-y_true convention (1.0 for a perfect fit, else 0.0). Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
|
Good call, thanks! I've inlined the R2 computation in numpy, so openstef-beam no longer depends on scikit-learn at all rather than just deferring the import to runtime. It matches scikit-learn's |
|
Updated per your suggestion, @egordm: R2 is now inlined in numpy so openstef-beam no longer depends on scikit-learn. I verified it matches |
…#805) openstef-beam imported sklearn.metrics.r2_score in metrics_deterministic but never declared scikit-learn as a dependency, so r2 failed whenever sklearn was absent. Reimplement R2 in numpy, matching scikit-learn for the weighted, constant-target, and fewer-than-two-sample (NaN) cases, and add unit tests covering them. Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
d520473 to
4431c97
Compare
Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com>
Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com>
…ror. Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com> Signed-off-by: Egor Dmitriev <egor.dmitriev@alliander.com>
egordm
left a comment
There was a problem hiding this comment.
Looks great now. I added handing of additional edge cases so we don't throw errors on data issues, and throw errors on structure / logic issues.
What
scikit-learnis an optional dependency ofopenstef-beam(it is not declared in the package's dependencies), butmetrics_deterministic.pyimportedr2_scorefromsklearn.metricsat module top. As a result, importingopenstef_beam.metrics(or anything that pulls it in) failed withModuleNotFoundErrorwhen scikit-learn was not installed. Resolves #805.Fix
Move the
from sklearn.metrics import r2_scoreimport inside ther2()function, its only use site. Importing the module no longer requires scikit-learn; only callingr2()does.Tests
test_r2_score_is_imported_lazily: assertsr2_scoreis not bound at module level (locks the lazy import).test_r2_still_computes_with_sklearn_available: confirmsr2()still computes correctly.ty check,ruff check,ruff format --check, the module doctests, and the full beam metrics suite all pass.