def test_update_tree_thresholds_exception(): """Test that updating tree thresholds raises an error if the input thresholds are defined with an associated period, but no target_period is provided.""" tree = wxcode_decision_tree(accumulation=True) expected = "The decision tree contains thresholds defined" with pytest.raises(ValueError, match=expected): update_tree_thresholds(tree, target_period=None)
def test_update_tree_thresholds(accumulation, target_period, expected_value, expected_unit): """Test that updating tree thresholds returns iris AuxCoords with the expected value and units. Includes a test that the threshold value is scaled if it is defined with an associated period that differs from a user supplied target period.""" tree = wxcode_decision_tree(accumulation=accumulation) tree = update_tree_thresholds(tree, target_period=target_period) (result, ) = tree["heavy_precipitation"]["diagnostic_thresholds"] assert isinstance(result, iris.coords.AuxCoord) assert result.points[0] == expected_value assert result.units == expected_unit
def __init__( self, wxtree: dict, model_id_attr: Optional[str] = None, record_run_attr: Optional[str] = None, target_period: Optional[int] = None, ) -> None: """ Define a decision tree for determining weather symbols based upon the input diagnostics. Use this decision tree to allocate a weather symbol to each point. Args: wxtree: Weather symbols decision tree definition, provided as a dictionary. model_id_attr: Name of attribute recording source models that should be inherited by the output cube. The source models are expected as a space-separated string. record_run_attr: Name of attribute used to record models and cycles used in constructing the weather symbols. target_period: The period in seconds that the weather symbol being produced should represent. This should correspond with any period diagnostics, e.g. precipitation accumulation, being used as input. This is used to scale any threshold values that are defined with an associated period in the decision tree. It will only be used if the decision tree provided has threshold values defined with an associated period. float_tolerance defines the tolerance when matching thresholds to allow for the difficulty of float comparisons. float_abs_tolerance defines the tolerance for when the threshold is zero. It has to be sufficiently small that a valid rainfall rate or snowfall rate could not trigger it. """ self.model_id_attr = model_id_attr self.record_run_attr = record_run_attr self.start_node = list(wxtree.keys())[0] self.target_period = target_period self.queries = update_tree_thresholds(wxtree, target_period) self.float_tolerance = 0.01 self.float_abs_tolerance = 1e-12 # flag to indicate whether to expect "threshold" as a coordinate name # (defaults to False, checked on reading input cubes) self.coord_named_threshold = False
def test_interrogate_decision_tree(): """Test that the function returns the right strings.""" expected = ( "\u26C5 probability_of_low_and_medium_type_cloud_area_fraction_above_threshold (1): 0.1875, 0.8125\n" # noqa: E501 "\u26C5 probability_of_low_type_cloud_area_fraction_above_threshold (1): 0.85\n" "\u26C5 probability_of_lwe_graupel_and_hail_fall_rate_in_vicinity_above_threshold (mm hr-1): 0.0\n" # noqa: E501 "\u26C5 probability_of_lwe_precipitation_rate_above_threshold (mm hr-1): 0.03, 1.0\n" # noqa: E501 "\u26C5 probability_of_lwe_precipitation_rate_in_vicinity_above_threshold (mm hr-1): 0.1, 1.0\n" # noqa: E501 "\u26C5 probability_of_lwe_precipitation_rate_max_above_threshold (mm hr-1): 1.0\n" # noqa: E501 "\u26C5 probability_of_lwe_sleetfall_rate_above_threshold (mm hr-1): 0.03, 1.0\n" "\u26C5 probability_of_lwe_snowfall_rate_above_threshold (mm hr-1): 0.03, 1.0\n" "\u26C5 probability_of_number_of_lightning_flashes_per_unit_area_in_vicinity_above_threshold (m-2): 0.0\n" # noqa: E501 "\u26C5 probability_of_rainfall_rate_above_threshold (mm hr-1): 0.03, 1.0\n" "\u26C5 probability_of_shower_condition_above_threshold (1): 1.0\n" "\u26C5 probability_of_visibility_in_air_below_threshold (m): 1000.0, 5000.0\n" ) tree = update_tree_thresholds(wxcode_decision_tree(), None) result = interrogate_decision_tree(tree) assert result == expected