Replace scipy.stats.linregress with inlined OLS in numpy#48
Merged
scottstanie merged 2 commits intoopera-adt:mainfrom Apr 24, 2026
Merged
Conversation
calculate_trend was the only use of scipy in the repo, and it only needed three of linregress's five return values (slope, intercept, r_value → r_squared). p_value and std_err were both discarded. Drops the `from scipy import stats` import in favour of a 7-line OLS that matches scipy's formula exactly (verified numerically: slope/intercept/r² agree to ~1e-20 on a synthesized 40-point time-series with injected NaNs). scipy remains a transitive dependency via xarray/matplotlib — this just cuts the direct coupling. Also fixes a drive-by E501 in an unrelated docstring that ruff only flags on full-file recheck. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
The only use of scipy in the whole repo was one
stats.linregress(x, y)call inutils.calculate_trend, and it only needed three of the five return values (slope,intercept,r_value→ squared forr_squared);p_valueandstd_errwere discarded.Replaced with 7 lines of numpy. Same formula scipy uses:
slope = Σ dx·dy / Σ dx²intercept = ȳ − slope · x̄r² = (Σ dx·dy)² / (Σ dx² · Σ dy²)Numerical parity verified: on a synthesized 40-point series with NaN injected, scipy and the new code agree to ~1e-20 on slope, intercept, and r² (below float64 round-off).
scipy is still a transitive dep via xarray/matplotlib/etc., so
pixi installwon't get smaller. This just cuts the direct import — one fewer library bowser needs to cooperate with.Drive-by
Fixed a pre-existing E501 in an unrelated docstring in the same file; ruff doesn't flag it until a full-file recheck on commit.
Test plan
ruff+ruff-formatpassmypywas skipped locally — the pre-commit config for the mypy hook is missingtypes-python-dateutilinadditional_dependencies, so it fails on every commit touchingutils.pyon currentmain. Separate cleanup item, not introduced by this PR. CI doesn't run pre-commit.🤖 Generated with Claude Code