Skip to content

Commit

Permalink
Make Impact.impact_at_reg support all zero impacts (#773)
Browse files Browse the repository at this point in the history
* Fix a bug where impact_at_reg would not work for all zero impacts
* Update CHANGELOG.md
  • Loading branch information
peanutfun authored Aug 21, 2023
1 parent 015fdbf commit ef410e1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Removed:
- 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.
- 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/)
- Text in `Forecast` class plots can now be adjusted [#769](https://github.com/CLIMADA-project/climada_python/issues/769)
- `Impact.impact_at_reg` now supports impact matrices where all entries are zero [#773](https://github.com/CLIMADA-project/climada_python/pull/773)

### Deprecated

Expand Down
2 changes: 1 addition & 1 deletion climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def impact_at_reg(self, agg_regions=None):
Contains the aggregated data per event.
Rows: Hazard events. Columns: Aggregation regions.
"""
if self.imp_mat.nnz == 0:
if np.prod(self.imp_mat.shape) == 0:
raise ValueError(
"The aggregated impact cannot be computed as no Impact.imp_mat was "
"stored during the impact calculation"
Expand Down
8 changes: 6 additions & 2 deletions climada/engine/test/test_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,13 @@ def test_admin0(self):

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

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

0 comments on commit ef410e1

Please sign in to comment.