Skip to content

Commit

Permalink
Change default Hazard.event_name to string of event_id (#795)
Browse files Browse the repository at this point in the history
* Update Hazard base.py

Change event_name default for Hazard.from_xarray_raster to string of event id.
Change variable description in class method.

* Update test_base_xarray.py

adapted event_name in test_assert_default_values and deleted event_name tests from test_missing_dims since the time dimension does not affect event_name anymore

* change default name to string of date

* save event_name as string of date and time

* ensure list of event names is flat

* Update CHANGELOG.md

* Update CHANGELOG.md

* update changelog

* Apply suggestions from code review

Co-authored-by: Lukas Riedel <[email protected]>

* Update AUTHORS.md

---------

Co-authored-by: Chahan M. Kropf <[email protected]>
Co-authored-by: Lukas Riedel <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2023
1 parent 158b1d8 commit 05ee576
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
* Nicolas Colombi
* Leonie Villiger
* Kam Lam Yeung
* Sarah Hülsen
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Code freeze date: YYYY-MM-DD

### Fixed

- `Hazard.from_xarray_raster` now stores strings as default values for `Hazard.event_name` [#795](https://github.com/CLIMADA-project/climada_python/pull/795)
- Fix the dist_approx util function when used with method="geosphere" and log=True and points that are very close. [#792](https://github.com/CLIMADA-project/climada_python/pull/792)

### Deprecated
Expand Down
2 changes: 1 addition & 1 deletion climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ def maybe_repeat(values: np.ndarray, times: int) -> np.ndarray:
None,
np.ones(num_events),
np.array(range(num_events), dtype=int) + 1,
list(data[coords["event"]].values),
data[coords["event"]].dt.strftime("%Y-%m-%d").values.flatten().tolist(),
np.array(u_dt.datetime64_to_ordinal(data[coords["event"]].values)),
],
# The accessor for the data in the Dataset
Expand Down
9 changes: 6 additions & 3 deletions climada/hazard/test/test_base_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _assert_default_values(self, hazard):
self.assertEqual(hazard.units, "")
np.testing.assert_array_equal(hazard.event_id, [1, 2])
np.testing.assert_array_equal(
hazard.event_name, [np.datetime64(val) for val in self.time]
hazard.event_name, [x.strftime("%Y-%m-%d") for x in self.time]
)
np.testing.assert_array_equal(
hazard.date, [val.toordinal() for val in self.time]
Expand Down Expand Up @@ -343,7 +343,7 @@ def test_missing_dims(self):
hazard = Hazard.from_xarray_raster(ds, "", "")
self._assert_default_types(hazard)
np.testing.assert_array_equal(
hazard.event_name, [np.datetime64(self.time[0])]
hazard.event_name, [self.time[0].strftime("%Y-%m-%d")]
)
np.testing.assert_array_equal(hazard.date, [self.time[0].toordinal()])
np.testing.assert_array_equal(hazard.centroids.lat, [0, 0, 0, 1, 1, 1])
Expand All @@ -370,7 +370,7 @@ def test_missing_dims(self):
hazard = Hazard.from_xarray_raster(ds, "", "")
self._assert_default_types(hazard)
np.testing.assert_array_equal(
hazard.event_name, [np.datetime64("2022-01-01")]
hazard.event_name, ["2022-01-01"]
)
np.testing.assert_array_equal(
hazard.date, [dt.datetime(2022, 1, 1).toordinal()]
Expand Down Expand Up @@ -551,6 +551,9 @@ def test_2D_time(self):
np.testing.assert_array_equal(
hazard.date, [val.toordinal() for val in time.flat]
)
np.testing.assert_array_equal(
hazard.event_name, ["1999-01-01", "1999-02-01", "2000-01-01", "2000-02-01"]
)

def test_errors(self):
"""Check if expected errors are thrown"""
Expand Down

0 comments on commit 05ee576

Please sign in to comment.