Vectorize _apply_impacts loop over realizations - #96
Merged
Conversation
MeteorInterface._apply_impacts was calling DegreeDaysCalculator.calculate() inside a Python for-loop over realizations. The calculator itself is already fully vectorized across the 'month' dimension via xarray, so passing the entire (n_realizations, n_month) DataArray in one call produces identical results without the per-realization Python overhead. New test_degree_days_calculate_batches_realizations pins the contract that batched output and per-realization output agree to atol=1e-9. gen_impacts N=100 profile: DegreeDays.calculate cumulative was 20.5 s of a 63 s workload; after this change it's a rounding error in the profile. Wall-time savings scale linearly in n_realizations. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- black reformats an assertion block in the new test - After removing the per-realization loop, `n_realizations` from the shape unpack is now unused; use `n_months = ts_data.shape[1]` directly Lint-only, no behavior change. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
maritsandstad
approved these changes
Aug 3, 2026
maritsandstad
left a comment
Collaborator
There was a problem hiding this comment.
Fine this can probably just go in
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
MeteorInterface._apply_impactswas looping over realizations in Python:The underlying
DegreeDaysCalculator.calculateis already fully vectorized across themonthdimension via xarray, so passing the entire(n_realizations, n_month)DataArray in one call produces identical results without the per-realization Python overhead.Correctness
test_degree_days_calculate_batches_realizationspins the contract: batched output and looped-per-realization output agree toatol=1e-9.tests/unit/impacts/+tests/unit/test_meteor_interface.pystill pass.Wins
On the
gen_impactsprofiling workload at N=100 (via the sibling profiling harness):DegreeDaysCalculator.calculatecumulative was 20.5 s of a 63 s workload — the serial loop was called 100 times per variable × 2 variables = 200 times.Savings scale linearly in
n_realizations.Independence
Independent of the other perf/fix PRs in flight (#91–#95). Touches
_apply_impacts(around line 2089) — well away from any other in-flight edits tometeor_interface.py. Can land in any order.Test plan
pytest tests/unit/impacts/test_degree_days.py::test_degree_days_calculate_batches_realizationspytest tests/unit/impacts/(full impacts test suite still passes)🤖 Generated with Claude Code