Skip to content

Commit

Permalink
RDPS precip dif can't be negative (#4001)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgboss authored Oct 11, 2024
1 parent 423c52a commit e396f6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
22 changes: 22 additions & 0 deletions api/app/tests/weather_models/test_precip_rdps_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ def test_difference_identity():
assert np.allclose(res, np.zeros(precip_raster.shape))


def test_negative_precip_diff_raises_value_error():
"""
Verify ValueError raised if raster subtraction contains a negative value.
"""
later_precip = TemporalPrecip(datetime.fromisoformat("2024-06-10T18:42:49"), np.zeros(1))
earlier_precip = TemporalPrecip(datetime.fromisoformat("2024-06-09T18:42:49"), np.ones(1))
with pytest.raises(ValueError):
compute_precip_difference(later_precip, earlier_precip)


def test_trivial_negative_precip_diff_returns_zero():
"""
Verify that a negative precip dif between -0.01 and 0 returns 0.
"""
early_array = np.empty(1)
early_array[0] = 0.005
later_precip = TemporalPrecip(datetime.fromisoformat("2024-06-10T18:42:49"), np.zeros(1))
earlier_precip = TemporalPrecip(datetime.fromisoformat("2024-06-09T18:42:49"), early_array)
result = compute_precip_difference(later_precip, earlier_precip)
assert result[0] == 0


@pytest.mark.parametrize(
"later_datetime,earlier_datetime",
[
Expand Down
13 changes: 12 additions & 1 deletion api/app/weather_models/precip_rdps_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ def compute_precip_difference(later_precip: TemporalPrecip, earlier_precip: Temp


def _diff(value_a: float, value_b: float):
return value_a - value_b
"""
Subtract value_a from value_b.
:param value_a: The first value
:param value_b: The second value
:raises ValueError: If difference is less than -0.01 (ie. negative precip not allowed)
:return: Return value_a minus value_b if the value is >= 0. If the difference is slightly negative (-0.01 <= value < 0), due to floating
point math for example, return 0. If the value is truly negative, raise an error.
"""
result = value_a - value_b
if result < -0.01:
raise ValueError("Precip difference cannot be negative")
return result if result > 0 else 0


vectorized_diff = vectorize(_diff)

0 comments on commit e396f6a

Please sign in to comment.