Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Impact.impact_at_reg support all zero impacts #773

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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