Skip to content

Commit

Permalink
refact: Removing apply_zero_precipitation_rate()
Browse files Browse the repository at this point in the history
Removing apply_zero_precipitation_rate() as requested by @grantbuster
  • Loading branch information
castelao committed Jul 22, 2024
1 parent 9b391f5 commit 0a78b31
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 97 deletions.
4 changes: 1 addition & 3 deletions sup3r/bias/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

from .bias_calc import (LinearCorrection, MonthlyLinearCorrection,
MonthlyScalarCorrection, SkillAssessment)
from .bias_transforms import (apply_zero_precipitation_rate,
global_linear_bc, local_linear_bc,
from .bias_transforms import (global_linear_bc, local_linear_bc,
local_qdm_bc, local_presrat_bc,
monthly_local_linear_bc)
from .qdm import PresRat, QuantileDeltaMappingCorrection

__all__ = [
"apply_zero_precipitation_rate",
"global_linear_bc",
"local_linear_bc",
"local_qdm_bc",
Expand Down
55 changes: 0 additions & 55 deletions sup3r/bias/bias_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,61 +603,6 @@ def local_qdm_bc(data: np.ndarray,
return output


def apply_zero_precipitation_rate(arr: np.ndarray, rate):
"""Enforce the zero precipitation rate
Replace lowest values by zero to satisfy the given rate of zero
precipitation.
Parameters
----------
arr : np.ndarray
An array of values to be analyzed. Usually precipitation. The expected
dimensions here are (space, space, time).
rate : np.ndarray
Rate of zero, or negligible, days of precipitation. The two first
dimensions are expected to align the the (space, space) from arr, but
due to a limitation on how the parameters are saved in HDF files, it
is assumed to have a third dummy dimension.
Returns
-------
corrected : np.array
A copy of given array that satisfies the rate of zero precipitation
days, i.e. the lowest values of precipitation are changed to zero
to satisfy that rate.
Note
----
Whenever small values are frequent, and the dataset is truncated, such as
the outputs from numerical models, there is a chance that the precise rate
will not be respected.
Examples
--------
>>> data = np.array([5, 0.1, np.nan, 0.2, 1])
>>> apply_zero_precipitation_rate(data, 0.30)
array([5. , 0. , nan, 0.2, 1. ])
"""
assert arr.ndim == 3
# Currently, due to the way how these parameters are saved in an HDF,
# there are issues mixing a 2D array, which would be the ideal shape for
# rate. So for now, let's just use a 3D rate with a useless dimension.
assert rate.ndim >= 2
assert arr.shape[:2] == rate.shape[:2]

r = np.atleast_1d(rate).flatten()
tmp = arr.reshape(*r.shape, -1)

n_valid = np.isfinite(tmp).astype('i').sum(axis=1)
n = (r * n_valid).round().astype('i')
s = np.sort(tmp, axis=1)
criterion = n[:, np.newaxis] > np.arange(s.shape[1])
threshold = np.nanmax(np.where(criterion, s, np.nan), axis=1)
tmp = np.where(tmp <= threshold[:, np.newaxis], 0, tmp)

return tmp.reshape(arr.shape)


def get_spatial_bc_presrat(lat_lon: np.array,
base_dset: str,
feature_name: str,
Expand Down
39 changes: 0 additions & 39 deletions tests/bias/test_presrat_bias_correction.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
from sup3r.models import Sup3rGan
from sup3r.pipeline.forward_pass import ForwardPass, ForwardPassStrategy
from sup3r.bias import (
apply_zero_precipitation_rate,
local_qdm_bc,
local_presrat_bc,
PresRat,
Expand Down Expand Up @@ -484,44 +483,6 @@ def test_zero_precipitation_rate_nan():
assert r1 == r2


# ==== Test apply zero rate ====


def test_apply_zero_precipitation_rate():
"""Reinforce the zero precipitation rate, standard run"""
data = np.array([[[5, 0.1, 3, 0.2, 1]]])
out = apply_zero_precipitation_rate(data, np.array([[[0.25]]]))

assert np.allclose([5.0, 0.0, 3, 0.2, 1.0], out, equal_nan=True)


def test_apply_zero_precipitation_rate_nan():
"""Validate with NaN in the input"""
data = np.array([[[5, 0.1, np.nan, 0.2, 1]]])
out = apply_zero_precipitation_rate(data, np.array([[[0.25]]]))

assert np.allclose([5.0, 0.0, np.nan, 0.2, 1.0], out, equal_nan=True)


def test_apply_zero_precipitation_rate_2D():
"""Validate a 2D input"""
data = np.array(
[
[
[5, 0.1, np.nan, 0.2, 1],
[5, 0.1, 3, 0.2, 1],
]
]
)
out = apply_zero_precipitation_rate(data, np.array([[[0.25], [0.41]]]))

assert np.allclose(
[[5.0, 0.0, np.nan, 0.2, 1.0], [5.0, 0.0, 3, 0.0, 1.0]],
out,
equal_nan=True,
)


# ==== PresRat parameters estimate ====


Expand Down

0 comments on commit 0a78b31

Please sign in to comment.