Skip to content

Commit ef410e1

Browse files
authored
Make Impact.impact_at_reg support all zero impacts (#773)
* Fix a bug where impact_at_reg would not work for all zero impacts * Update CHANGELOG.md
1 parent 015fdbf commit ef410e1

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Removed:
6464
- Problem with `pyproj.CRS` as `Impact` attribute, [#706](https://github.com/CLIMADA-project/climada_python/issues/706). Now CRS is always stored as `str` in WKT format.
6565
- Correctly handle assertion errors in `Centroids.values_from_vector_files` and fix the associated test [#768](https://github.com/CLIMADA-project/climada_python/pull/768/)
6666
- Text in `Forecast` class plots can now be adjusted [#769](https://github.com/CLIMADA-project/climada_python/issues/769)
67+
- `Impact.impact_at_reg` now supports impact matrices where all entries are zero [#773](https://github.com/CLIMADA-project/climada_python/pull/773)
6768

6869
### Deprecated
6970

climada/engine/impact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ def impact_at_reg(self, agg_regions=None):
434434
Contains the aggregated data per event.
435435
Rows: Hazard events. Columns: Aggregation regions.
436436
"""
437-
if self.imp_mat.nnz == 0:
437+
if np.prod(self.imp_mat.shape) == 0:
438438
raise ValueError(
439439
"The aggregated impact cannot be computed as no Impact.imp_mat was "
440440
"stored during the impact calculation"

climada/engine/test/test_impact.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,13 @@ def test_admin0(self):
562562

563563
def test_no_imp_mat(self):
564564
"""Check error if no impact matrix is stored"""
565-
# Test error when no imp_mat is stored
566-
self.imp.imp_mat = sparse.csr_matrix((0, 0))
565+
# A matrix with only zeros should work!
566+
self.imp.imp_mat = sparse.csr_matrix(np.zeros_like(self.imp.imp_mat.toarray()))
567+
at_reg = self.imp.impact_at_reg(["A", "A"])
568+
self.assertEqual(at_reg["A"].sum(), 0)
567569

570+
# An empty matrix should not work
571+
self.imp.imp_mat = sparse.csr_matrix((0, 0))
568572
with self.assertRaises(ValueError) as cm:
569573
self.imp.impact_at_reg()
570574
self.assertIn("no Impact.imp_mat was stored", str(cm.exception))

0 commit comments

Comments
 (0)