Repository navigation
perf(engine,coordinates): vectorize Impact.impact_at_reg CSR reduction and points_to_raster boxes - #1350
Conversation
…n and points_to_raster boxes
peanutfun
left a comment
There was a problem hiding this comment.
@napattan Thank you for your contribution! Please provide Python scripts to verify your claims on the speedup. Please also use the provided pull request description template (https://github.com/CLIMADA-project/climada_python/blob/main/.github/pull_request_template.md) for a smoother review. Finally, you added a test that does not verify anything (new). Please revise before we can merge
|
Hi @peanutfun, thank you for taking the time to review. My background is in landscape architecture, and I have been trying on integrating computational workflow and spatial analysis into my landscape and urban fields. Also I have used AI aided in my coding workflow and I read CLIMADA's AI policy markdown now, I take full responsibility for every line of code in this PR. I am still very new to contributing in open-source projects, I truly apologize for missing description template, issuing very unclearly, and for redundant test. I have updated the PR description above using If you checked and think this is unhelpful, please feel free to close this issue. |
|
@napattan All good, I appreciate your frankness and I'll walk you through it. Thanks for providing the benchmark code. Could you simplify it by only including the end-to-end calculations with timings? The script does not have to provide the speedup, just the run time. Then one can simply switch between branches, re-run the script, and see the timings for the actual branch code. The speedup for |
|
Thank you @peanutfun I have simplified the script in PR so it only runs the end-to-end And your mentioning about "boxing algorithm" is correct. When profiling the function step-by-step on 20,000 points (
Thank you again. |
|
@napattan Great, thanks for simplifying the script. I can confirm a speedup of around 8x for |
* 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
Changes proposed in this PR:
Impact.impact_at_reginclimada/engine/impact.pyby replacing the per-region Python list comprehension ([self.imp_mat[:, np.where(agg_regions == reg)[0]].sum(1) for reg in agg_reg_unique]) withnp.unique(agg_regions, return_inverse=True)and a single CSR sparse indicator matrix multiplication(self.imp_mat @ reg_ind).toarray().points_to_rasterinclimada/util/coordinates.py(apply_box) by replacing row-wisedf_exp.apply(lambda r: ..., axis=1)(Point(...).buffer(res / 2).envelope) withshapely.box(lon - res / 2, lat - res / 2, lon + res / 2, lat + res / 2).df_geomtest block inclimada/util/test/test_coordinates.pyand replace the duplicated list-comprehension check inclimada/engine/test/test_impact.py(TestImpactReg.test_agg_regions) with direct per-event assertions againstself.imp.at_eventandself.imp.imp_mat.toarray().Benchmark Verification Script
This PR fixes # (N/A — performance optimization)
PR Author Checklist
develop)PR Reviewer Checklist