Skip to content

Commit b67314a

Browse files
chahankChahan Kropfpeanutfun
authored
Reassign centroids by default (#730)
* Add parameter to force centroid reassignment and set it to True by default. * Add note to docstring * Update Changelog --------- Co-authored-by: Chahan Kropf <[email protected]> Co-authored-by: Lukas Riedel <[email protected]>
1 parent 2e3ed82 commit b67314a

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Removed:
3838
- Tests with long runtime were moved to integration tests in `climada/test` [#709](https://github.com/CLIMADA-project/climada_python/pull/709)
3939
- Use `myst-nb` for parsing Jupyter Notebooks for the documentation instead of `nbsphinx` [#712](https://github.com/CLIMADA-project/climada_python/pull/712)
4040
- Installation guide now recommends installing CLIMADA directly via `conda install` [#714](https://github.com/CLIMADA-project/climada_python/pull/714)
41-
- `Exposures.affected_total_value` now takes a hazard intensity threshold as argument. Affected values are only those for which at least one event exceeds the threshold. (previously, all exposures points with an assigned centroid were considered affected) [#702](https://github.com/CLIMADA-project/climada_python/pull/702)
41+
- `Exposures.affected_total_value` now takes a hazard intensity threshold as argument. Affected values are only those for which at least one event exceeds the threshold. (previously, all exposures points with an assigned centroid were considered affected). By default the centroids are reassigned. [#702](https://github.com/CLIMADA-project/climada_python/pull/702) [#730](https://github.com/CLIMADA-project/climada_python/pull/730)
4242
- Add option to pass region ID to `LitPop.from_shape` [#720](https://github.com/CLIMADA-project/climada_python/pull/720)
4343
- Slightly improved performance on `LitPop`-internal computations [#720](https://github.com/CLIMADA-project/climada_python/pull/720)
4444

climada/entity/exposures/base.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,12 @@ def centroids_total_value(self, hazard):
10481048
)
10491049
return np.sum(self.gdf.value.values[nz_mask])
10501050

1051-
def affected_total_value(self, hazard: Hazard, threshold_affected: float = 0):
1051+
def affected_total_value(
1052+
self,
1053+
hazard: Hazard,
1054+
threshold_affected: float = 0,
1055+
overwrite_assigned_centroids: bool = True,
1056+
):
10521057
"""
10531058
Total value of the exposures that are affected by at least
10541059
one hazard event (sum of value of all exposures points for which
@@ -1061,6 +1066,11 @@ def affected_total_value(self, hazard: Hazard, threshold_affected: float = 0):
10611066
threshold_affected : int or float
10621067
Hazard intensity threshold above which an exposures is
10631068
considere affected.
1069+
The default is 0.
1070+
overwrite_assigned_centroids : boolean
1071+
Assign centroids from the hazard to the exposures and overwrite
1072+
existing ones.
1073+
The default is True.
10641074
10651075
Returns
10661076
-------
@@ -1069,7 +1079,17 @@ def affected_total_value(self, hazard: Hazard, threshold_affected: float = 0):
10691079
a centroids is assigned and that have at least one
10701080
event intensity above threshold.
10711081
1082+
See Also
1083+
--------
1084+
Exposures.assign_centroids : method to assign centroids.
1085+
1086+
Note
1087+
----
1088+
The fraction attribute of the hazard is ignored. Thus, for hazards
1089+
with fraction defined the affected values will be overestimated.
1090+
10721091
"""
1092+
self.assign_centroids(hazard=hazard, overwrite=overwrite_assigned_centroids)
10731093
assigned_centroids = self.gdf[hazard.centr_exp_col]
10741094
nz_mask = (self.gdf.value.values > 0) & (assigned_centroids.values >= 0)
10751095
cents = np.unique(assigned_centroids[nz_mask])

climada/entity/exposures/test/test_base.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,50 @@ def test_affected_total_value(self):
190190
gdf = gpd.GeoDataFrame(
191191
{
192192
"value": [1, 2, 3, 4, 5, 6],
193-
"latitude": [1, 2, 3, 4, 5, 6],
194-
"longitude": [-1, -2, -3, -4, -5, -6],
193+
"latitude": [1, 2, 3, 4, 1, 0],
194+
"longitude": [-1, -2, -3, -4, 0, 1],
195195
"centr_" + haz_type: [0, 2, 2, 3, -1, 4],
196196
}
197197
)
198198
exp = Exposures(gdf, crs=4326)
199199
intensity = sp.sparse.csr_matrix(np.array([[0, 0, 1, 10, 2], [-1, 0, 0, 1, 2]]))
200-
cent = Centroids(lat=np.array([1, 2, 3, 4]), lon=np.array([1, 2, 3, 4]))
200+
cent = Centroids(lat=np.array([1, 2, 3, 4]), lon=np.array([-1, -2, -3, -4]))
201201
haz = Hazard(
202202
haz_type=haz_type, centroids=cent, intensity=intensity, event_id=[1, 2]
203203
)
204204

205-
tot_val = exp.affected_total_value(haz, threshold_affected=0)
205+
# do not reassign centroids
206+
tot_val = exp.affected_total_value(
207+
haz, threshold_affected=0, overwrite_assigned_centroids=False
208+
)
206209
self.assertEqual(tot_val, np.sum(exp.gdf.value[[1, 2, 3, 5]]))
207-
tot_val = exp.affected_total_value(haz, threshold_affected=3)
210+
tot_val = exp.affected_total_value(
211+
haz, threshold_affected=3, overwrite_assigned_centroids=False
212+
)
208213
self.assertEqual(tot_val, np.sum(exp.gdf.value[[3]]))
209-
tot_val = exp.affected_total_value(haz, threshold_affected=-2)
214+
tot_val = exp.affected_total_value(
215+
haz, threshold_affected=-2, overwrite_assigned_centroids=False
216+
)
210217
self.assertEqual(tot_val, np.sum(exp.gdf.value[[0, 1, 2, 3, 5]]))
211-
tot_val = exp.affected_total_value(haz, threshold_affected=11)
218+
tot_val = exp.affected_total_value(
219+
haz, threshold_affected=11, overwrite_assigned_centroids=False
220+
)
212221
self.assertEqual(tot_val, 0)
213222

223+
# reassign centroids (i.e. to [0, 1, 2, 3, -1, -1])
224+
tot_val = exp.affected_total_value(
225+
haz, threshold_affected=11, overwrite_assigned_centroids=True
226+
)
227+
self.assertEqual(tot_val, 0)
228+
tot_val = exp.affected_total_value(
229+
haz, threshold_affected=0, overwrite_assigned_centroids=False
230+
)
231+
self.assertEqual(tot_val, 7)
232+
tot_val = exp.affected_total_value(
233+
haz, threshold_affected=3, overwrite_assigned_centroids=False
234+
)
235+
self.assertEqual(tot_val, 4)
236+
214237
class TestChecker(unittest.TestCase):
215238
"""Test logs of check function"""
216239

0 commit comments

Comments
 (0)