Skip to content

Commit 4370351

Browse files
adapt further functions to nonzero centroids
1 parent 6b01ad7 commit 4370351

File tree

2 files changed

+71
-50
lines changed

2 files changed

+71
-50
lines changed

climada/engine/impact.py

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ def __init__(
107107
crs=DEF_CRS,
108108
eai_exp=None,
109109
at_event=None,
110-
tot_value=0.,
111-
aai_agg=0.,
110+
tot_value=0.0,
111+
aai_agg=0.0,
112112
unit="",
113113
imp_mat=None,
114114
haz_type="",
@@ -564,23 +564,32 @@ def local_exceedance_impact(
564564

565565
# calculate local exceedance impact
566566
test_frequency = 1 / np.array(return_periods)
567-
exceedance_impact = np.array(
568-
[
569-
u_interp.preprocess_and_interpolate_ev(
570-
test_frequency,
571-
None,
572-
self.frequency,
573-
self.imp_mat.getcol(i_centroid).toarray().flatten(),
574-
log_frequency=log_frequency,
575-
log_values=log_impact,
576-
value_threshold=min_impact,
577-
method=method,
578-
y_asymptotic=0.0,
579-
)
580-
for i_centroid in range(self.imp_mat.shape[1])
581-
]
567+
568+
exceedance_impact = np.full(
569+
(self.imp_mat.shape[1], test_frequency.shape[0]),
570+
np.nan if method == "interpolate" else 0.0,
582571
)
583572

573+
nonzero_centroids = np.where(self.imp_mat.getnnz(axis=0) > 0)[0]
574+
575+
if not len(nonzero_centroids) == 0:
576+
exceedance_impact[nonzero_centroids, :] = np.array(
577+
[
578+
u_interp.preprocess_and_interpolate_ev(
579+
test_frequency,
580+
None,
581+
self.frequency,
582+
self.imp_mat.getcol(i_centroid).toarray().flatten(),
583+
log_frequency=log_frequency,
584+
log_values=log_impact,
585+
value_threshold=min_impact,
586+
method=method,
587+
y_asymptotic=0.0,
588+
)
589+
for i_centroid in nonzero_centroids
590+
]
591+
)
592+
584593
# create the output GeoDataFrame
585594
gdf = gpd.GeoDataFrame(
586595
geometry=gpd.points_from_xy(self.coord_exp[:, 1], self.coord_exp[:, 0]),
@@ -683,23 +692,28 @@ def local_return_period(
683692
]:
684693
raise ValueError(f"Unknown method: {method}")
685694

695+
return_periods = np.full((self.imp_mat.shape[1], len(threshold_impact)), np.nan)
696+
697+
nonzero_centroids = np.where(self.imp_mat.getnnz(axis=0) > 0)[0]
698+
686699
# calculate local return periods
687-
return_periods = np.array(
688-
[
689-
u_interp.preprocess_and_interpolate_ev(
690-
None,
691-
np.array(threshold_impact),
692-
self.frequency,
693-
self.imp_mat.getcol(i_centroid).toarray().flatten(),
694-
log_frequency=log_frequency,
695-
log_values=log_impact,
696-
value_threshold=min_impact,
697-
method=method,
698-
y_asymptotic=np.nan,
699-
)
700-
for i_centroid in range(self.imp_mat.shape[1])
701-
]
702-
)
700+
if not len(nonzero_centroids) == 0:
701+
return_periods[nonzero_centroids, :] = np.array(
702+
[
703+
u_interp.preprocess_and_interpolate_ev(
704+
None,
705+
np.array(threshold_impact),
706+
self.frequency,
707+
self.imp_mat.getcol(i_centroid).toarray().flatten(),
708+
log_frequency=log_frequency,
709+
log_values=log_impact,
710+
value_threshold=min_impact,
711+
method=method,
712+
y_asymptotic=np.nan,
713+
)
714+
for i_centroid in nonzero_centroids
715+
]
716+
)
703717
return_periods = safe_divide(1.0, return_periods)
704718

705719
# create the output GeoDataFrame

climada/hazard/base.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -683,24 +683,31 @@ def local_return_period(
683683
]:
684684
raise ValueError(f"Unknown method: {method}")
685685

686-
# calculate local return periods
687-
return_periods = np.array(
688-
[
689-
u_interp.preprocess_and_interpolate_ev(
690-
None,
691-
np.array(threshold_intensities),
692-
self.frequency,
693-
self.intensity.getcol(i_centroid).toarray().flatten(),
694-
log_frequency=log_frequency,
695-
log_values=log_intensity,
696-
value_threshold=min_intensity,
697-
method=method,
698-
y_asymptotic=np.nan,
699-
n_sig_dig=n_sig_dig,
700-
)
701-
for i_centroid in range(self.intensity.shape[1])
702-
]
686+
return_periods = np.full(
687+
(self.intensity.shape[1], threshold_intensities.shape[0]), np.nan
703688
)
689+
690+
nonzero_centroids = np.where(self.intensity.getnnz(axis=0) > 0)[0]
691+
692+
if not len(nonzero_centroids) == 0:
693+
return_periods[nonzero_centroids, :] = np.array(
694+
[
695+
u_interp.preprocess_and_interpolate_ev(
696+
None,
697+
np.array(threshold_intensities),
698+
self.frequency,
699+
self.intensity.getcol(i_centroid).toarray().flatten(),
700+
log_frequency=log_frequency,
701+
log_values=log_intensity,
702+
value_threshold=min_intensity,
703+
method=method,
704+
y_asymptotic=np.nan,
705+
n_sig_dig=n_sig_dig,
706+
)
707+
for i_centroid in nonzero_centroids
708+
]
709+
)
710+
704711
return_periods = safe_divide(1.0, return_periods)
705712

706713
# create the output GeoDataFrame

0 commit comments

Comments
 (0)