Skip to content

Commit

Permalink
adapt number of significant digits to 3 for rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinGebhart committed Feb 14, 2025
1 parent b39c65e commit 0457286
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 2 additions & 0 deletions climada/engine/impact.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ def local_exceedance_impact(
value_threshold=min_impact,
method=method,
y_asymptotic=0.0,
n_sig_dig=3,
)
for i_centroid in nonzero_centroids
]
Expand Down Expand Up @@ -711,6 +712,7 @@ def local_return_period(
value_threshold=min_impact,
method=method,
y_asymptotic=np.nan,
n_sig_dig=3,
)
for i_centroid in nonzero_centroids
]
Expand Down
6 changes: 2 additions & 4 deletions climada/hazard/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ def local_exceedance_intensity(
min_intensity=None,
log_frequency=True,
log_intensity=True,
n_sig_dig=2,
):
"""Compute local exceedance intensity for given return periods. The default method
is fitting the ordered intensitites per centroid to the corresponding cummulated
Expand Down Expand Up @@ -574,7 +573,7 @@ def local_exceedance_intensity(
value_threshold=min_intensity,
method=method,
y_asymptotic=0.0,
n_sig_dig=n_sig_dig,
n_sig_dig=3,
)
for i_centroid in nonzero_centroids
]
Expand Down Expand Up @@ -622,7 +621,6 @@ def local_return_period(
min_intensity=None,
log_frequency=True,
log_intensity=True,
n_sig_dig=2,
):
"""Compute local return periods for given hazard intensities. The default method
is fitting the ordered intensitites per centroid to the corresponding cummulated
Expand Down Expand Up @@ -702,7 +700,7 @@ def local_return_period(
value_threshold=min_intensity,
method=method,
y_asymptotic=np.nan,
n_sig_dig=n_sig_dig,
n_sig_dig=3,
)
for i_centroid in nonzero_centroids
]
Expand Down
6 changes: 5 additions & 1 deletion climada/util/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def preprocess_and_interpolate_ev(
Has no effect if method is "interpolate". Else, provides return value and if
for test x values larger than given x values, if size < 2 or if method is set
to "extrapolate_constant" or "stepfunction". Defaults to np.nan.
n_sig_dig : int, optional
number of significant digits to group the values (in order to avoid bad extrapolation behaviour). Defaults to 3.
Returns
-------
Expand All @@ -96,7 +98,7 @@ def preprocess_and_interpolate_ev(
frequency = frequency[sorted_idxs]

# group similar values together
frequency, values = group_frequency(frequency, values, n_sig_dig=n_sig_dig)
frequency, values = group_frequency(frequency, values, n_sig_dig)

# transform frequencies to cummulative frequencies
frequency = np.cumsum(frequency[::-1])[::-1]
Expand Down Expand Up @@ -368,6 +370,8 @@ def group_frequency(frequency, value, n_sig_dig):
start_indices = np.insert(start_indices, value_unique.size, frequency.size)
frequency = np.add.reduceat(frequency, start_indices[:-1])
return frequency, value_unique
elif not all(sorted(value) == value):
raise ValueError("Value array must be sorted in ascending order!")

return frequency, value

Expand Down
11 changes: 7 additions & 4 deletions climada/util/test/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,16 @@ def test_stepfunction_ev_small_input(self):
def test_frequency_group(self):
"""Test frequency grouping method"""
frequency = np.ones(6)
intensity = np.array([1.00001, 0.999, 1.0, 2.0, 3.0, 3])
intensity = np.array([1.00001, 0.9998, 1.0, 2.0, 3.0, 3])
np.testing.assert_allclose(
u_interp.group_frequency(frequency, intensity), ([3, 1, 2], [1, 2, 3])
u_interp.group_frequency(frequency, intensity, n_sig_dig=3),
([3, 1, 2], [1, 2, 3]),
)
np.testing.assert_allclose(
u_interp.group_frequency([], [], n_sig_dig=3), ([], [])
)
np.testing.assert_allclose(u_interp.group_frequency([], []), ([], []))
with self.assertRaises(ValueError):
u_interp.group_frequency(frequency, intensity[::-1])
u_interp.group_frequency(frequency, intensity[::-1], n_sig_dig=3)

def test_round_to_sig_digits(self):
array = [0.00111, 999.0, 55.5, 0.0, -1.001, -1.08]
Expand Down

0 comments on commit 0457286

Please sign in to comment.