Skip to content

Commit

Permalink
Replicate percentile collapse functionality.
Browse files Browse the repository at this point in the history
This commit also addresses many of the review comments
though the expansion / rewriting of the unit tests will
be undertaken separately.

The BasicThreshold plugin is also renamed to Threshold.
  • Loading branch information
bayliffe committed Jul 31, 2023
1 parent 4fa3df7 commit 19083bf
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 77 deletions.
2 changes: 1 addition & 1 deletion doc/source/Code-Style-Guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ Plugins (classes) should be an example of a non-trivial algorithm or set
of algorithms for a particular purpose. They should be set up via the
``__init__`` method and then invoked on a particular iris Cube ``cube``
using a ``process`` method - e.g. using ``process(cube)``. See
e.g. `BasicThreshold <https://github.com/metoppv/improver/blob/master/lib/improver/threshold.py>`_
e.g. `Threshold <https://github.com/metoppv/improver/blob/master/lib/improver/threshold.py>`_
class. In some limited cases an iris ``CubeList`` may be preferable.
Avoid writing code that can do both. Class names use
`PascalCase <https://en.wikipedia.org/wiki/PascalCase>`_ whilst
Expand Down
24 changes: 12 additions & 12 deletions improver/cli/threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def process(
threshold_units: str = None,
comparison_operator=">",
fuzzy_factor: float = None,
collapse_realizations: bool = False,
collapse_coord: str = None,
vicinity: cli.comma_separated_list = None,
fill_masked: float = None,
):
Expand Down Expand Up @@ -96,9 +96,14 @@ def process(
indicating a narrower fuzzy factor region / sharper threshold.
A fuzzy factor cannot be used with a zero threshold or a
threshold_config file.
collapse_realizations (bool):
If set, the realization coordinate is collapsed to calculate an
ensemble average threshold exceedance value.
collapse_coord (str):
An optional ability to set which coordinate we want to collapse
over. The only supported options are "realization" or "percentile".
If "percentile" is requested, the percentile coordinate will be
rebadged as a realization coordinate prior to collapse. The percentile
coordinate needs to be evenly spaced around the 50th percentile
to allow successful conversion from percentiles to realizations and
subsequent collapsing over the realization coordinate.
vicinity (list of float / int):
List of distances in metres used to define the vicinities within
which to search for an occurrence. Each vicinity provided will
Expand All @@ -115,13 +120,8 @@ def process(
ValueError: If threshold_config and threshold_values are both set
ValueError: If threshold_config is used for fuzzy thresholding
ValueError: Cannot apply land-mask cube without in-vicinity processing.
ValueError: Can only collapse over a realization coordinate or a percentile
coordinate that has been rebadged as a realization coordinate.
"""
from improver.ensemble_copula_coupling.ensemble_copula_coupling import (
RebadgePercentilesAsRealizations,
)
from improver.threshold import BasicThreshold
from improver.threshold import Threshold

if threshold_config and threshold_values:
raise ValueError(
Expand All @@ -133,13 +133,13 @@ def process(
if fill_masked is not None:
fill_masked = float(fill_masked)

return BasicThreshold(
return Threshold(
threshold_values=threshold_values,
threshold_config=threshold_config,
fuzzy_factor=fuzzy_factor,
threshold_units=threshold_units,
comparison_operator=comparison_operator,
collapse_realizations=collapse_realizations,
collapse_coord=collapse_coord,
vicinity=vicinity,
fill_masked=fill_masked,
)(cube, land_sea_mask)
6 changes: 3 additions & 3 deletions improver/precipitation_type/shower_condition_probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
create_new_diagnostic_cube,
generate_mandatory_attributes,
)
from improver.threshold import BasicThreshold
from improver.threshold import Threshold
from improver.utilities.cube_manipulation import collapse_realizations

from .utilities import make_shower_condition_cube
Expand Down Expand Up @@ -171,10 +171,10 @@ def process(self, cubes: CubeList) -> Cube:
cloud, convection = self._extract_inputs(cubes)

# Threshold cubes
cloud_thresholded = BasicThreshold(
cloud_thresholded = Threshold(
self.cloud_threshold, comparison_operator="<="
).process(cloud)
convection_thresholded = BasicThreshold(self.convection_threshold).process(
convection_thresholded = Threshold(self.convection_threshold).process(
convection
)

Expand Down
6 changes: 3 additions & 3 deletions improver/regrid/landsea.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from improver.metadata.constants.attributes import MANDATORY_ATTRIBUTE_DEFAULTS
from improver.metadata.constants.mo_attributes import MOSG_GRID_ATTRIBUTES
from improver.regrid.landsea2 import RegridWithLandSeaMask
from improver.threshold import BasicThreshold
from improver.threshold import Threshold
from improver.utilities.cube_checker import spatial_coords_match
from improver.utilities.spatial import OccurrenceWithinVicinity

Expand Down Expand Up @@ -288,9 +288,9 @@ def _get_matches(
# Identify nearby points on regridded input_land that match the
# selector_value
if selector_val > 0.5:
thresholder = BasicThreshold(0.5)
thresholder = Threshold(0.5)
else:
thresholder = BasicThreshold(0.5, comparison_operator="<=")
thresholder = Threshold(0.5, comparison_operator="<=")
in_vicinity = self.vicinity(thresholder(self.input_land))

# Identify those points sourced from the opposite mask that are
Expand Down
Loading

0 comments on commit 19083bf

Please sign in to comment.