Skip to content

Commit ceb3610

Browse files
authored
expand limits as needed in _prepare_for_brier (#240)
* expand limits as needed in _prepare_for_brier * update test function that no longer raises ValueError
1 parent 71838c0 commit ceb3610

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

src/qp/metrics/metrics.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ def _prepare_for_brier(p, truth, limits, dx=0.01):
226226
% (p.npdf, len(truth))
227227
)
228228

229-
# Values of truth that are outside the defined limits will not appear truth_array.
230-
# Consider expanding the limits or using numpy.clip to restrict truth values to the limits.
231-
if np.any(np.less(truth, limits[0])) or np.any(np.greater(truth, limits[1])):
232-
raise ValueError(f"Input truth values exceed the defined limits ({min(truth)}, {max(truth)}) ({limits[0]} {limits[1]})")
229+
# Handle values of truth that are outside the defined limit
230+
# by expanding the limits
231+
if np.any(np.less(truth, limits[0])) or np.any(np.greater(truth, limits[1])): # pragma: no cover
232+
limits=(np.min([np.min(truth),limits[0]]), np.max([np.max(truth),limits[1]]))
233+
print("Expanding limits to {limits}")
233234

234235
# Make a grid object that defines grid values and histogram bin edges using limits and dx
235236
grid = _calculate_grid_parameters(limits, dx)

tests/qp/test_metrics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,11 @@ def test_calculate_brier_truth_outside_of_limits(self):
322322
truth = 2 * (np.random.uniform(size=(10, 1)) - 0.5)
323323
truth = np.append(truth, 100)
324324
limits = [-2.0, 2]
325-
with self.assertRaises(ValueError) as context:
326-
_ = calculate_brier(self.ens_n, truth, limits)
327-
328-
error_msg = "Input truth values exceed the defined limits"
329-
self.assertTrue(error_msg in str(context.exception))
325+
# this no longer raises, but rather expands the limits
326+
# with self.assertRaises(ValueError) as context:
327+
# error_msg = "Input truth values exceed the defined limits"
328+
# self.assertTrue(error_msg in str(context.exception))
329+
_ = calculate_brier(self.ens_n, truth, limits)
330330

331331
def test_calculate_outlier_rate(self):
332332
"""Base case test. Ensure that the class wrapped

0 commit comments

Comments
 (0)