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

Allow for numpy arrays in hazard select #719

Merged
merged 1 commit into from
May 11, 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
6 changes: 3 additions & 3 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,15 +1359,15 @@ def select(self, event_names=None, event_id=None, date=None, orig=None,
return None

# filter events hist/synthetic
if isinstance(orig, bool):
if orig is not None:
sel_ev &= (self.orig.astype(bool) == orig)
if not np.any(sel_ev):
LOGGER.info('No hazard with %s original events.', str(orig))
return None

# filter events based on name
sel_ev = np.argwhere(sel_ev).reshape(-1)
if isinstance(event_names, list):
if event_names is not None:
filtered_events = [self.event_name[i] for i in sel_ev]
try:
new_sel = [filtered_events.index(n) for n in event_names]
Expand All @@ -1378,7 +1378,7 @@ def select(self, event_names=None, event_id=None, date=None, orig=None,
sel_ev = sel_ev[new_sel]

# filter events based on id
if isinstance(event_id, list):
if event_id is not None:
# preserves order of event_id
sel_ev = np.array([
np.argwhere(self.event_id == n)[0,0]
Expand Down
24 changes: 24 additions & 0 deletions climada/hazard/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,30 @@ def test_select_event_id(self):
self.assertIsInstance(sel_haz.intensity, sparse.csr_matrix)
self.assertIsInstance(sel_haz.fraction, sparse.csr_matrix)

def test_select_event_id(self):
"""Test select historical events."""
haz = dummy_hazard()
sel_haz = haz.select(event_id=np.array([4, 1]))

self.assertTrue(np.array_equal(sel_haz.centroids.coord, haz.centroids.coord))
self.assertEqual(sel_haz.tag, haz.tag)
self.assertEqual(sel_haz.units, haz.units)
self.assertTrue(np.array_equal(sel_haz.event_id, np.array([4, 1])))
self.assertTrue(np.array_equal(sel_haz.date, np.array([4, 1])))
self.assertTrue(np.array_equal(sel_haz.orig, np.array([True, True])))
self.assertTrue(np.array_equal(sel_haz.frequency, np.array([0.2, 0.1])))
self.assertEqual(sel_haz.frequency_unit, haz.frequency_unit)
self.assertTrue(np.array_equal(sel_haz.fraction.toarray(),
np.array([[0.3, 0.2, 0.0],
[0.02, 0.03, 0.04]])))
self.assertTrue(np.array_equal(sel_haz.intensity.toarray(),
np.array([[5.3, 0.2, 0.0],
[0.2, 0.3, 0.4]])))
self.assertEqual(sel_haz.event_name, ['ev4', 'ev1'])
self.assertIsInstance(sel_haz, Hazard)
self.assertIsInstance(sel_haz.intensity, sparse.csr_matrix)
self.assertIsInstance(sel_haz.fraction, sparse.csr_matrix)

def test_select_orig_pass(self):
"""Test select historical events."""
haz = dummy_hazard()
Expand Down