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

Catch case where new Hazard fraction is unintentionally zero #869

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Code freeze date: YYYY-MM-DD

### Fixed

- Avoid an issue where a Hazard subselection would have a fraction matrix with only zeros as entries by throwing an error [#866](https://github.com/CLIMADA-project/climada_python/pull/866)

### Deprecated

### Removed
Expand Down
12 changes: 12 additions & 0 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,18 @@ def select(self, event_names=None, event_id=None, date=None, orig=None,
dt.datetime.fromordinal(haz.date.min()).year) + 1
haz.frequency = haz.frequency * year_span_old / year_span_new

# Check if new fraction is zero everywhere
if self.fraction.nnz != 0 and haz.fraction.nnz == 0:
peanutfun marked this conversation as resolved.
Show resolved Hide resolved
peanutfun marked this conversation as resolved.
Show resolved Hide resolved
raise RuntimeError(
"Your selection created a Hazard object where the fraction matrix is "
"zero everywhere. This hazard will have zero impact everywhere. "
"We are catching this condition because of an implementation detail: "
"A fraction matrix without nonzero-valued entries will be completely "
"ignored. This is surely not what you intended. If you really want to, "
"you can circumvent this error by setting your original fraction "
"matrix to zero everywhere, but there probably is no point in doing so."
peanutfun marked this conversation as resolved.
Show resolved Hide resolved
)

haz.sanitize_event_ids()
return haz

Expand Down
13 changes: 13 additions & 0 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,19 @@ def test_select_tight_pass(self):
self.assertIsInstance(sel_haz.intensity, sparse.csr_matrix)
self.assertIsInstance(sel_haz.fraction, sparse.csr_matrix)

def test_select_new_fraction_zero(self):
"""Check if a new fraction of only zeros is handled correctly"""
hazard = dummy_hazard()
hazard.centroids.region_id = np.array([1, 1, 2])

# Select a part of the hazard where fraction is zero only
with self.assertRaisesRegex(
RuntimeError,
"Your selection created a Hazard object where the fraction matrix is zero "
"everywhere"
):
hazard.select(event_id=[3, 4], reg_id=[2])


class TestAppend(unittest.TestCase):
"""Test append method."""
Expand Down
Loading