Skip to content

Commit

Permalink
merge develop into branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliane Kobler committed Oct 23, 2024
2 parents 77113f1 + 242f1f3 commit d080fef
Show file tree
Hide file tree
Showing 39 changed files with 1,599 additions and 2,805 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ jobs:
needs: build-and-test
with:
core_branch: ${{ github.ref }}
petals_branch: develop
petals_branch: feature/exposures_crs
permissions:
checks: write
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,26 @@ Code freeze date: YYYY-MM-DD
### Added

- `climada.util.interpolation` module for inter- and extrapolation util functions used in local exceedance intensity and return period functions [#930](https://github.com/CLIMADA-project/climada_python/pull/930)
- `climada.exposures.exposures.Exposures.geometry` property
- `climada.exposures.exposures.Exposures.latitude` property
- `climada.exposures.exposures.Exposures.longitude` property
- `climada.exposures.exposures.Exposures.value` property
- `climada.exposures.exposures.Exposures.region_id` property
- `climada.exposures.exposures.Exposures.category_id` property
- `climada.exposures.exposures.Exposures.cover` property
- `climada.exposures.exposures.Exposures.hazard_impf` method
- `climada.exposures.exposures.Exposures.hazard_centroids` method

### Changed

- Improved scaling factors implemented in `climada.hazard.trop_cyclone.apply_climate_scenario_knu` to model the impact of climate changes to tropical cyclones [#734](https://github.com/CLIMADA-project/climada_python/pull/734)
- In `climada.util.plot.geo_im_from_array`, NaNs are plotted in gray while cells with no centroid are not plotted [#929](https://github.com/CLIMADA-project/climada_python/pull/929)
- Renamed `climada.util.plot.subplots_from_gdf` to `climada.util.plot.plot_from_gdf` [#929](https://github.com/CLIMADA-project/climada_python/pull/929)
- Exposures complete overhaul. Notably
- the _geometry_ column of the inherent `GeoDataFrame` is set up at initialization
- latitude and longitude column are no longer present there (the according arrays can be retrieved as properties of the Exposures object: `exp.latitude` instead of `exp.gdf.latitude.values`).
- `Exposures.gdf` has been renamed to `Exposures.data` (it still works though, as it is a property now pointing to the latter)
- the `check` method does not add a default "IMPF_" column to the GeoDataFrame anymore

### Fixed

Expand All @@ -27,6 +41,10 @@ Code freeze date: YYYY-MM-DD

### Deprecated

- `climada.entity.exposures.Exposures.meta` attribute
- `climada.entity.exposures.Exposures.set_lat_lon` method
- `climada.entity.exposures.Exposures.set_geometry_points` method

### Removed

## 5.0.0
Expand Down
2 changes: 1 addition & 1 deletion climada/engine/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __init__(
if exposure_name is None:
try:
self.exposure_name = u_coord.country_to_iso(
exposure.gdf["region_id"].unique()[0], "name"
np.unique(exposure.region_id)[0], "name"
)
except (KeyError, AttributeError):
self.exposure_name = "custom"
Expand Down
8 changes: 1 addition & 7 deletions climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ def from_eih(cls, exposures, hazard, at_event, eai_exp, aai_agg, imp_mat=None):
date=hazard.date,
frequency=hazard.frequency,
frequency_unit=hazard.frequency_unit,
coord_exp=np.stack(
[exposures.gdf["latitude"].values, exposures.gdf["longitude"].values],
axis=1,
),
coord_exp=np.stack([exposures.latitude, exposures.longitude], axis=1),
crs=exposures.crs,
unit=exposures.value_unit,
tot_value=exposures.centroids_total_value(hazard),
Expand Down Expand Up @@ -734,9 +731,6 @@ def plot_raster_eai_exposure(
cartopy.mpl.geoaxes.GeoAxesSubplot
"""
eai_exp = self._build_exp()
# we need to set geometry points because the `plot_raster` method accesses the
# exposures' `gdf.crs` property, which raises an error when geometry is not set
eai_exp.set_geometry_points()
axis = eai_exp.plot_raster(
res,
raster_res,
Expand Down
1 change: 1 addition & 0 deletions climada/engine/impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def impact(
apply_deductible_to_mat : apply deductible to impact matrix
apply_cover_to_mat : apply cover to impact matrix
"""
# TODO: consider refactoring, making use of Exposures.hazard_impf
# check for compatibility of exposures and hazard type
if all(
name not in self.exposures.gdf.columns
Expand Down
15 changes: 7 additions & 8 deletions climada/engine/test/test_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def test_from_eih_pass(self):
np.testing.assert_array_almost_equal(imp.eai_exp, fake_eai_exp)
np.testing.assert_array_almost_equal(imp.at_event, fake_at_event)
np.testing.assert_array_almost_equal(
imp.coord_exp,
np.stack([exp.gdf["latitude"].values, exp.gdf["longitude"].values], axis=1),
imp.coord_exp, np.stack([exp.latitude, exp.longitude], axis=1)
)

def test_pyproj_crs(self):
Expand Down Expand Up @@ -987,9 +986,9 @@ def test__build_exp(self):

imp = dummy_impact()
exp = imp._build_exp()
np.testing.assert_array_equal(imp.eai_exp, exp.gdf["value"])
np.testing.assert_array_equal(imp.coord_exp[:, 0], exp.gdf["latitude"])
np.testing.assert_array_equal(imp.coord_exp[:, 1], exp.gdf["longitude"])
np.testing.assert_array_equal(imp.eai_exp, exp.value)
np.testing.assert_array_equal(imp.coord_exp[:, 0], exp.latitude)
np.testing.assert_array_equal(imp.coord_exp[:, 1], exp.longitude)
self.assertTrue(u_coord.equal_crs(exp.crs, imp.crs))
self.assertEqual(exp.value_unit, imp.unit)
self.assertEqual(exp.ref_year, 0)
Expand All @@ -1000,9 +999,9 @@ def test__exp_build_event(self):
imp = dummy_impact()
event_id = imp.event_id[1]
exp = imp._build_exp_event(event_id=event_id)
np.testing.assert_array_equal(imp.imp_mat[1].todense().A1, exp.gdf["value"])
np.testing.assert_array_equal(imp.coord_exp[:, 0], exp.gdf["latitude"])
np.testing.assert_array_equal(imp.coord_exp[:, 1], exp.gdf["longitude"])
np.testing.assert_array_equal(imp.imp_mat[1].todense().A1, exp.value)
np.testing.assert_array_equal(imp.coord_exp[:, 0], exp.latitude)
np.testing.assert_array_equal(imp.coord_exp[:, 1], exp.longitude)
self.assertTrue(u_coord.equal_crs(exp.crs, imp.crs))
self.assertEqual(exp.value_unit, imp.unit)
self.assertEqual(exp.ref_year, 0)
Expand Down
92 changes: 32 additions & 60 deletions climada/engine/test/test_impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def check_impact(self, imp, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array=
"""Test properties of imapcts"""
self.assertEqual(len(haz.event_id), len(imp.at_event))
self.assertIsInstance(imp, Impact)
np.testing.assert_allclose(imp.coord_exp[:, 0], exp.gdf["latitude"])
np.testing.assert_allclose(imp.coord_exp[:, 1], exp.gdf["longitude"])
np.testing.assert_allclose(imp.coord_exp[:, 0], exp.latitude)
np.testing.assert_allclose(imp.coord_exp[:, 1], exp.longitude)
self.assertAlmostEqual(imp.aai_agg, aai_agg, 3)
np.testing.assert_allclose(imp.eai_exp, eai_exp, rtol=1e-5)
np.testing.assert_allclose(imp.at_event, at_event, rtol=1e-5)
Expand Down Expand Up @@ -244,80 +244,40 @@ def test_calc_impact_RF_pass(self):
0.00000000e00,
]
)
# fmt: off
imp_mat_array = np.array(
[
[
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
0.00000000e00,
6.41965663e04,
0.00000000e00,
2.02249434e02,
3.41245461e04,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 6.41965663e04, 0.00000000e00, 2.02249434e02,
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
3.41245461e04,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
7.73566566e07,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
7.73566566e07, 0.00000000e00, 0.00000000e00, 0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
[
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
],
]
)
# fmt: on
check_impact(self, impact, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array)

def test_empty_impact(self):
Expand Down Expand Up @@ -530,7 +490,12 @@ def test_minimal_exp_gdf(self):
def test_stitch_impact_matrix(self):
"""Check how sparse matrices from a generator are stitched together"""
icalc = ImpactCalc(
Exposures({"blank": [1, 2, 3, 4]}), ImpactFuncSet(), Hazard()
Exposures(
{"blank": [1, 2, 3, 4]},
geometry=[],
),
ImpactFuncSet(),
Hazard(),
)
icalc.hazard.event_id = np.array([1, 2, 3])
icalc._orig_exp_idx = np.array([0, 1, 2, 3])
Expand Down Expand Up @@ -564,7 +529,14 @@ def test_apply_deductible_to_mat(self):

def test_stitch_risk_metrics(self):
"""Test computing risk metrics from an impact matrix generator"""
icalc = ImpactCalc(Exposures({"blank": [1, 2, 3]}), ImpactFuncSet(), Hazard())
icalc = ImpactCalc(
Exposures(
{"blank": [1, 2, 3]},
geometry=[],
),
ImpactFuncSet(),
Hazard(),
)
icalc.hazard.event_id = np.array([1, 2])
icalc.hazard.frequency = np.array([2, 0.5])
icalc._orig_exp_idx = np.array([0, 1, 2])
Expand Down
4 changes: 3 additions & 1 deletion climada/engine/unsequa/calc_impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,9 @@ def uncertainty(

if calc_eai_exp:
exp = self.exp_input_var.evaluate()
coord_df = exp.gdf[["latitude", "longitude"]]
coord_df = pd.DataFrame(
dict(latitude=exp.latitude, longitude=exp.longitude)
)
else:
coord_df = pd.DataFrame([])

Expand Down
Loading

0 comments on commit d080fef

Please sign in to comment.