fix: import scikit-learn lazily in openstef-beam r2 metric#981
Open
Valyrian-Code wants to merge 4 commits into
Open
fix: import scikit-learn lazily in openstef-beam r2 metric#981Valyrian-Code wants to merge 4 commits into
Valyrian-Code wants to merge 4 commits into
Conversation
scikit-learn is an optional dependency of openstef-beam (not declared in its dependencies), but metrics_deterministic imported r2_score at module top, so importing openstef_beam.metrics failed when scikit-learn was not installed. Move the import inside r2() (its only use site) so the metrics module imports without scikit-learn; only calling r2() needs it. Add regression tests. Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
egordm
requested changes
Jun 25, 2026
egordm
left a comment
Collaborator
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>
Contributor
Author
|
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.