Skip to content

Vectorize Impact.impact_at_reg CSR reduction and points_to_raster boxes - #1358

Open
napattan wants to merge 1 commit into
CLIMADA-project:developfrom
napattan:fix/vectorize-impact-at-reg-points-to-raster-1357
Open

napattan wants to merge 1 commit into
CLIMADA-project:developfrom
napattan:fix/vectorize-impact-at-reg-points-to-raster-1357

Conversation

@napattan

@napattan napattan commented Nov 20, 2026 •

Copy link
Copy Markdown

Changes proposed in this PR:

  • Re-apply the performance vectorizations from perf(engine,coordinates): vectorize Impact.impact_at_reg CSR reduction and points_to_raster boxes #1350:
    • Replace the per-region Python loop in Impact.impact_at_reg (climada/engine/impact.py) with a single sparse indicator matrix multiplication (self.imp_mat @ reg_ind), preserving imp_mat.dtype and the agg_regions=None admin-0 country fallback.
    • Replace the row-wise Point(...).buffer(res / 2).envelope call in climada.util.coordinates.points_to_raster with vectorized shapely.box (and drop the unused Point import).
  • Per maintainer consensus in PR #1350 introduced an integration test failure #1357, enforce strict 1D exact-length validation on agg_regions in Impact.impact_at_reg (agg_regions.ndim == 1 and len(agg_regions) == imp_mat.shape[1], raising ValueError otherwise) to match the existing docstring contract, and document the Raises section.
  • Update the calibration integration tests in climada/test/test_util_calibrate.py (TestScipyMinimizeOptimizer.setUp and TestBayesianOptimizer.setUp) from impact.impact_at_reg(["a", "b"]) to impact.impact_at_reg(["a", "b", "c"])[["a", "b"]] to match the 3-point exposure() fixture (whose 3rd point is unassigned) without altering Input.impact_to_aligned_df alignment, MSE scaling, or OutputEvaluator plots. Audited all other impact_at_reg call sites in climada/ and doc/ and confirmed none rely on mismatched lengths.
  • Add unit tests in climada/engine/test/test_impact.py covering unsorted duplicate region labels, float32 dtype preservation, and ValueError validation for empty, shorter, longer, scalar, and 2D agg_regions.
  • Update CHANGELOG.md and AUTHORS.md.

Verification

  • Tests (MPLBACKEND=Agg, CI=true):
    pytest climada/test/test_util_calibrate.py climada/engine/test/test_impact.py climada/util/test/test_coordinates.py climada/util/calibrate/test/ climada/entity/exposures/test/test_base.py
    -> 214 passed, 1 skipped (pre-existing test_cycling_csv), 15 subtests passed, 0 failed (including 6/6 in climada/test/test_util_calibrate.py).
  • Lint & formatting:
    • pylint --rcfile=.pylintrc climada/engine/impact.py climada/util/coordinates.py: 9.34/10 (0 warnings on modified lines).
    • black --check (24.4.2) and isort --profile black --check-only (5.13.2): passed.
  • Benchmarks (vs a7849c8f baseline):
Routine Workload Baseline -> Vectorized Parity
Impact.impact_at_reg 200 x 100k CSR (5% nnz), 200 regions 511.17 ms -> 8.28 ms (61.7x) max_diff = 1.42e-14
points_to_raster box 50k points 2163.32 ms -> 34.76 ms (62.2x) Exact polygon equality (True)

This PR fixes #1357

PR Author Checklist

PR Reviewer Checklist

* re-apply CLIMADA-project#1350 vectorizations for Impact.impact_at_reg and points_to_raster
* enforce strict 1D exact-length agg_regions check matching docstring (CLIMADA-project#1357)
* update climada/test/test_util_calibrate.py for 3-point exposure fixture
@napattan
napattan force-pushed the fix/vectorize-impact-at-reg-points-to-raster-1357 branch from ee5eb87 to 2179733 Compare November 23, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant