Skip to content

Commit 0457286

Browse files
adapt number of significant digits to 3 for rounding
1 parent b39c65e commit 0457286

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

climada/engine/impact.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ def local_exceedance_impact(
586586
value_threshold=min_impact,
587587
method=method,
588588
y_asymptotic=0.0,
589+
n_sig_dig=3,
589590
)
590591
for i_centroid in nonzero_centroids
591592
]
@@ -711,6 +712,7 @@ def local_return_period(
711712
value_threshold=min_impact,
712713
method=method,
713714
y_asymptotic=np.nan,
715+
n_sig_dig=3,
714716
)
715717
for i_centroid in nonzero_centroids
716718
]

climada/hazard/base.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ def local_exceedance_intensity(
491491
min_intensity=None,
492492
log_frequency=True,
493493
log_intensity=True,
494-
n_sig_dig=2,
495494
):
496495
"""Compute local exceedance intensity for given return periods. The default method
497496
is fitting the ordered intensitites per centroid to the corresponding cummulated
@@ -574,7 +573,7 @@ def local_exceedance_intensity(
574573
value_threshold=min_intensity,
575574
method=method,
576575
y_asymptotic=0.0,
577-
n_sig_dig=n_sig_dig,
576+
n_sig_dig=3,
578577
)
579578
for i_centroid in nonzero_centroids
580579
]
@@ -622,7 +621,6 @@ def local_return_period(
622621
min_intensity=None,
623622
log_frequency=True,
624623
log_intensity=True,
625-
n_sig_dig=2,
626624
):
627625
"""Compute local return periods for given hazard intensities. The default method
628626
is fitting the ordered intensitites per centroid to the corresponding cummulated
@@ -702,7 +700,7 @@ def local_return_period(
702700
value_threshold=min_intensity,
703701
method=method,
704702
y_asymptotic=np.nan,
705-
n_sig_dig=n_sig_dig,
703+
n_sig_dig=3,
706704
)
707705
for i_centroid in nonzero_centroids
708706
]

climada/util/interpolation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def preprocess_and_interpolate_ev(
7171
Has no effect if method is "interpolate". Else, provides return value and if
7272
for test x values larger than given x values, if size < 2 or if method is set
7373
to "extrapolate_constant" or "stepfunction". Defaults to np.nan.
74+
n_sig_dig : int, optional
75+
number of significant digits to group the values (in order to avoid bad extrapolation behaviour). Defaults to 3.
7476
7577
Returns
7678
-------
@@ -96,7 +98,7 @@ def preprocess_and_interpolate_ev(
9698
frequency = frequency[sorted_idxs]
9799

98100
# group similar values together
99-
frequency, values = group_frequency(frequency, values, n_sig_dig=n_sig_dig)
101+
frequency, values = group_frequency(frequency, values, n_sig_dig)
100102

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

372376
return frequency, value
373377

climada/util/test/test_interpolation.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,16 @@ def test_stepfunction_ev_small_input(self):
221221
def test_frequency_group(self):
222222
"""Test frequency grouping method"""
223223
frequency = np.ones(6)
224-
intensity = np.array([1.00001, 0.999, 1.0, 2.0, 3.0, 3])
224+
intensity = np.array([1.00001, 0.9998, 1.0, 2.0, 3.0, 3])
225225
np.testing.assert_allclose(
226-
u_interp.group_frequency(frequency, intensity), ([3, 1, 2], [1, 2, 3])
226+
u_interp.group_frequency(frequency, intensity, n_sig_dig=3),
227+
([3, 1, 2], [1, 2, 3]),
228+
)
229+
np.testing.assert_allclose(
230+
u_interp.group_frequency([], [], n_sig_dig=3), ([], [])
227231
)
228-
np.testing.assert_allclose(u_interp.group_frequency([], []), ([], []))
229232
with self.assertRaises(ValueError):
230-
u_interp.group_frequency(frequency, intensity[::-1])
233+
u_interp.group_frequency(frequency, intensity[::-1], n_sig_dig=3)
231234

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

0 commit comments

Comments
 (0)