diff --git a/pysteps/blending/steps.py b/pysteps/blending/steps.py index 6c0d8372d..76d3c693e 100644 --- a/pysteps/blending/steps.py +++ b/pysteps/blending/steps.py @@ -154,7 +154,9 @@ class StepsBlendingConfig: conditional: bool, optional If set to True, compute the statistics of the precipitation field conditionally by excluding pixels where the values are below the threshold - precip_thr. + precip_thr. This setting does not affect the noise standard deviation + adjustment (noise_stddev_adj='auto'), which always uses conditional + statistics (see #579). probmatching_method: {'cdf','mean',None}, optional Method for matching the statistics of the forecast field with those of the most recently observed one. 'cdf'=map the forecast CDF to the observed @@ -1456,6 +1458,11 @@ def __initialize_noise(self): starttime = time.time() precip_forecast_min = np.min(self.__state.precip_noise_input) + + # Conditonal = False leads to issue with blending. See #579 + # Conditional=True is hardcoded currently and bypasses the user provided values + # will always require user to provide a threshold as it is needed to compute the precipitation mask + self.__params.noise_std_coeffs = noise.utils.compute_noise_stddev_adjs( self.__state.precip_noise_input[-1, :, :], self.__params.precip_threshold, @@ -1465,7 +1472,7 @@ def __initialize_noise(self): self.__params.perturbation_generator, self.__params.noise_generator, 20, - conditional=self.__config.conditional, + conditional=True, num_workers=self.__config.num_workers, seed=self.__config.seed, ) @@ -3621,7 +3628,9 @@ def forecast( conditional: bool, optional If set to True, compute the statistics of the precipitation field conditionally by excluding pixels where the values are below the threshold - precip_thr. + precip_thr. This setting does not affect the noise standard deviation + adjustment (noise_stddev_adj='auto'), which always uses conditional + statistics (see #579). probmatching_method: {'cdf','mean',None}, optional Method for matching the statistics of the forecast field with those of the most recently observed one. 'cdf'=map the forecast CDF to the observed diff --git a/pysteps/noise/utils.py b/pysteps/noise/utils.py index aaae82f9b..e5c658308 100644 --- a/pysteps/noise/utils.py +++ b/pysteps/noise/utils.py @@ -44,6 +44,8 @@ def compute_noise_stddev_adjs( The calculations are done for n realizations of the noise field, and the correction factors are calculated from the average values of the standard deviations. + If an adjustment factor is undefined (e.g. single wet pixel), it is set to 1.0, + i.e. no adjustment. Parameters ---------- @@ -131,5 +133,6 @@ def worker(k): # for each cascade level, compare the standard deviations between the # observed field and the masked noise field, which gives the correction - # factors - return decomp_R["stds"] / np.mean(np.vstack(N_stds), axis=0) + # factors. Undefined factors (e.g. 0/0 for a single-pixel mask) are set to 1. + adjs = decomp_R["stds"] / np.mean(np.vstack(N_stds), axis=0) + return np.where(np.isfinite(adjs), adjs, 1.0) diff --git a/pysteps/nowcasts/steps.py b/pysteps/nowcasts/steps.py index aa3238471..9b5669d16 100644 --- a/pysteps/nowcasts/steps.py +++ b/pysteps/nowcasts/steps.py @@ -94,7 +94,9 @@ class StepsNowcasterConfig: conditional: bool, optional If set to True, compute the statistics of the precipitation field conditionally by excluding pixels where the values are below the - threshold precip_thr. + threshold precip_thr. This setting does not affect the noise standard + deviation adjustment (noise_stddev_adj='auto'), which always uses + conditional statistics (see #579). mask_method: {'obs','sprog','incremental',None}, optional The method to use for masking no precipitation areas in the forecast field. The masked pixels are set to the minimum value of the observations. @@ -773,7 +775,10 @@ def __apply_noise_and_ar_model(self): if self.__config.measure_time: starttime = time.time() - # Compute noise adjustment coefficients + # Compute noise adjustment coefficients. + # conditional=True is hardcoded and bypasses the user-provided value: + # with conditional=False the coefficients explode when only a small + # fraction of the domain is rainy. See #579. self.__params.noise_std_coefficients = ( noise.utils.compute_noise_stddev_adjs( self.__state.precip[-1, :, :], @@ -784,7 +789,7 @@ def __apply_noise_and_ar_model(self): self.__params.perturbation_generator, self.__params.noise_generator, 20, - conditional=self.__config.conditional, + conditional=True, num_workers=self.__config.num_workers, seed=self.__config.seed, ) @@ -1370,7 +1375,9 @@ def forecast( conditional: bool, optional If set to True, compute the statistics of the precipitation field conditionally by excluding pixels where the values are below the - threshold precip_thr. + threshold precip_thr. This setting does not affect the noise standard + deviation adjustment (noise_stddev_adj='auto'), which always uses + conditional statistics (see #579). mask_method: {'obs','sprog','incremental',None}, optional The method to use for masking no precipitation areas in the forecast field. The masked pixels are set to the minimum value of the observations. diff --git a/pysteps/tests/test_blending_steps.py b/pysteps/tests/test_blending_steps.py index 940568748..46981c8b7 100644 --- a/pysteps/tests/test_blending_steps.py +++ b/pysteps/tests/test_blending_steps.py @@ -12,73 +12,77 @@ # fmt:off steps_arg_values = [ - (1, 3, 4, 8, 'steps', None, None, False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 8,'steps', "obs", None, False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 8,'steps', "incremental", None, False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 8,'steps', None, "mean", False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 8,'steps', None, "mean", False, "spn", True, 4, False, False, 0, True, None, None, None), - (1, 3, 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, [1, 2, 3], 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None), + (1, 3, 4, 8, 'steps', None, None, False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 8,'steps', "obs", None, False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 8,'steps', "incremental", None, False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 8,'steps', None, "mean", False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 8,'steps', None, "mean", False, "spn", True, 4, False, False, 0, True, None, None, None, False), + (1, 3, 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, [1, 2, 3], 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None, False), # Regression test for a gap in the timesteps list (e.g. [1, 3] skips 2): this used to raise # "TypeError: 'NoneType' object is not iterable" because the bin for the skipped step never # got a value written to final_blended_forecast_all_members_one_timestep. - (1, [1, 3], 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 8,'steps', "incremental", "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", True, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", False, 4, False, False, 0, False, None, None, None), - (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", False, 4, False, False, 0, True, None, None, None), - (1, 3, 4, 9,'steps', "incremental", "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None), - (2, 3, 10, 8,'steps', "incremental", "cdf", False, "spn", True, 10, False, False, 0, False, None, None, None), - (5, 3, 5, 8,'steps', "incremental", "cdf", False, "spn", True, 5, False, False, 0, False, None, None, None), - (1, 10, 1, 8,'steps', "incremental", "cdf", False, "spn", True, 1, False, False, 0, False, None, None, None), - (2, 3, 2, 8,'steps', "incremental", "cdf", True, "spn", True, 2, False, False, 0, False, None, None, None), - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 0, False, None, None, None), - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 0, False, "bps", None, None), + (1, [1, 3], 4, 8,'steps', None, "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 8,'steps', "incremental", "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", True, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", False, 4, False, False, 0, False, None, None, None, False), + (1, 3, 4, 6,'steps', "incremental", "cdf", False, "bps", False, 4, False, False, 0, True, None, None, None, False), + (1, 3, 4, 9,'steps', "incremental", "cdf", False, "spn", True, 4, False, False, 0, False, None, None, None, False), + (2, 3, 10, 8,'steps', "incremental", "cdf", False, "spn", True, 10, False, False, 0, False, None, None, None, False), + (5, 3, 5, 8,'steps', "incremental", "cdf", False, "spn", True, 5, False, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'steps', "incremental", "cdf", False, "spn", True, 1, False, False, 0, False, None, None, None, False), + (2, 3, 2, 8,'steps', "incremental", "cdf", True, "spn", True, 2, False, False, 0, False, None, None, None, False), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 0, False, None, None, None, False), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 0, False, "bps", None, None, False), # Test the case where the radar image contains no rain. - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, True, False, 0, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 0, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 0, True, None, None, None), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, True, False, 0, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 0, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 0, True, None, None, None, False), # Test the case where the NWP fields contain no rain. - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, True, 0, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, False, True, 0, True, None, None, None), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, True, 0, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, False, True, 0, True, None, None, None, False), # Test the case where both the radar image and the NWP fields contain no rain. - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, True, True, 0, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, True, 0, False, None, None, None), - (5, 3, 5, 6,'steps', "obs", "mean", True, "spn", True, 5, True, True, 0, False, None, None, None), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, True, True, 0, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, True, 0, False, None, None, None, False), + (5, 3, 5, 6,'steps', "obs", "mean", True, "spn", True, 5, True, True, 0, False, None, None, None, False), # Test cases where we apply timestep_start_full_nwp_weight - (1, 10, 2, 6,'steps', "incremental", "cdf", False, "bps", False, 2, False, False, 0, True, None, None, 5), - (1, 10, 2, 6,'steps', "incremental", "cdf", False, "spn", False, 2, False, False, 0, False, None, None, 5), + (1, 10, 2, 6,'steps', "incremental", "cdf", False, "bps", False, 2, False, False, 0, True, None, None, 5, False), + (1, 10, 2, 6,'steps', "incremental", "cdf", False, "spn", False, 2, False, False, 0, False, None, None, 5, False), # Test for smooth radar mask - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 80, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, False, False, 80, False, None, None, None), - (5, 3, 5, 6,'steps', "obs", "mean", False, "spn", False, 5, False, False, 80, False, None, None, None), - (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, True, 80, False, None, None, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 80, True, None, None, None), - (5, 3, 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None), - (5, [1, 2, 3], 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None), - (5, [1, 3], 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, False, 80, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, False, False, 80, False, None, None, None, False), + (5, 3, 5, 6,'steps', "obs", "mean", False, "spn", False, 5, False, False, 80, False, None, None, None, False), + (1, 3, 6, 8,'steps', None, None, False, "spn", True, 6, False, True, 80, False, None, None, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "spn", False, 5, True, False, 80, True, None, None, None, False), + (5, 3, 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None, False), + (5, [1, 2, 3], 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None, False), + (5, [1, 3], 5, 6,'steps', "obs", "mean", False, "spn", False, 5, True, True, 80, False, None, None, None, False), # Test the usage of a max_mask_rim in the mask_kwargs - (1, 3, 6, 8,'steps', None, None, False, "bps", True, 6, False, False, 80, False, None, 40, None), - (5, 3, 5, 6,'steps', "obs", "mean", False, "bps", False, 5, False, False, 80, False, None, 40, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 25, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 40, None), - (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 60, None), + (1, 3, 6, 8,'steps', None, None, False, "bps", True, 6, False, False, 80, False, None, 40, None, False), + (5, 3, 5, 6,'steps', "obs", "mean", False, "bps", False, 5, False, False, 80, False, None, 40, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 25, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 40, None, False), + (5, 3, 5, 6,'steps', "incremental", "cdf", False, "bps", False, 5, False, False, 80, False, None, 60, None, False), #Test the externally provided nowcast - (1, 10, 1, 8,'external_nowcast_det', None, None, False, "spn", True, 1, False, False, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, False, False, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "spn", True, 1, False, False, 80, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, True, False, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "spn", True, 1, False, True, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, True, True, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", "cdf", False, "spn", True, 1, False, False, 0, True, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", "obs", False, "bps", True, 1, False, False, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, False, False, 0, False, None, None, 5), - (5, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None), - (5, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None), - (1, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None), - (1, 10, 1, 8,'external_nowcast_ens', "incremental", "cdf", False, "bps", True, 5, False, False, 0, False, None, None, None), - (5, 10, 1, 8,'external_nowcast_ens', "incremental", "obs", False, "spn", True, 5, False, False, 0, False, None, None, None), - (1, 10, 5, 8,'external_nowcast_ens', "incremental", "cdf", False, "bps", True, 5, False, False, 0, False, None, None, 5) + (1, 10, 1, 8,'external_nowcast_det', None, None, False, "spn", True, 1, False, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, False, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "spn", True, 1, False, False, 80, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, True, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "spn", True, 1, False, True, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, True, True, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", "cdf", False, "spn", True, 1, False, False, 0, True, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", "obs", False, "bps", True, 1, False, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_det', "incremental", None, False, "bps", True, 1, False, False, 0, False, None, None, 5, False), + (5, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None, False), + (5, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None, False), + (1, 10, 5, 8,'external_nowcast_ens', "incremental", None, False, "spn", True, 5, False, False, 0, False, None, None, None, False), + (1, 10, 1, 8,'external_nowcast_ens', "incremental", "cdf", False, "bps", True, 5, False, False, 0, False, None, None, None, False), + (5, 10, 1, 8,'external_nowcast_ens', "incremental", "obs", False, "spn", True, 5, False, False, 0, False, None, None, None, False), + (1, 10, 5, 8,'external_nowcast_ens', "incremental", "cdf", False, "bps", True, 5, False, False, 0, False, None, None, 5, False), + # Regression test for #579: with conditional=False and a low fraction of rainy pixels, + # the noise adjustment coefficients exploded and so did the forecast values. + (1, 3, 4, 8,'steps', None, None, False, "spn", True, 4, False, False, 0, False, None, None, None, True), + (1, 3, 4, 8,'steps', "incremental", None, False, "bps", True, 4, False, False, 0, False, None, None, None, True) ] # fmt:on @@ -91,8 +95,12 @@ def run_and_assert_forecast( n_timesteps, converter, metadata, + max_precip=None, ): - """Run a blended nowcast and assert the output has the expected shape.""" + """Run a blended nowcast and assert the output has the expected shape. + + If max_precip is given, also assert that the forecast (in mm/h) stays below it. + """ precip_forecast_dataset = blending.steps.forecast(radar_dataset, **forecast_kwargs) precip_var_forecast = precip_forecast_dataset.attrs["precip_var"] precip_forecast = precip_forecast_dataset[precip_var_forecast].values @@ -119,6 +127,10 @@ def run_and_assert_forecast( assert precip_forecast.shape[1] == n_timesteps, ( "Wrong amount of output time steps in converted forecast output" ) + if max_precip is not None: + assert np.nanmax(precip_forecast) < max_precip, ( + "Forecast precipitation values exploded (see #579)" + ) steps_arg_names = ( @@ -140,6 +152,7 @@ def run_and_assert_forecast( "vel_pert_method", "max_mask_rim", "timestep_start_full_nwp_weight", + "low_rain_fraction", ) @@ -163,6 +176,7 @@ def test_steps_blending( vel_pert_method, max_mask_rim, timestep_start_full_nwp_weight, + low_rain_fraction, ): pytest.importorskip("cv2") @@ -225,6 +239,11 @@ def test_steps_blending( radar_precip[2, 30:155, 40 + 1 * 3] = 0.5 radar_precip[2, 30:155, 41 + 1 * 3] = 0.1 + if low_rain_fraction: + # Keep only a thin strip of the rain band: ~0.15% of the domain is rainy + radar_precip[:, :100, :] = 0.0 + radar_precip[:, 105:, :] = 0.0 + precip_nowcast = np.zeros((n_ens_members, last_timestep + 1, 200, 200)) if nowcasting_method == "external_nowcast_ens": @@ -415,7 +434,7 @@ def test_steps_blending( smooth_radar_mask_range=smooth_radar_mask_range, callback=None, return_output=True, - seed=None, + seed=42, num_workers=1, fft_method="numpy", domain="spatial", @@ -440,6 +459,7 @@ def test_steps_blending( n_timesteps, converter, metadata, + max_precip=1000.0 if low_rain_fraction else None, ) diff --git a/pysteps/tests/test_nowcasts_steps.py b/pysteps/tests/test_nowcasts_steps.py index 8f8bb454a..5f67792ad 100644 --- a/pysteps/tests/test_nowcasts_steps.py +++ b/pysteps/tests/test_nowcasts_steps.py @@ -249,6 +249,11 @@ def run_and_assert_forecast( assert precip_forecast.shape[1] == n_timesteps, ( "Wrong amount of output time steps in converted forecast output" ) + assert np.nanmax(precip_forecast) < 1000.0, ( + f"Forecast precipitation values exploded: max value = {np.nanmax(precip_forecast)}" + ) + + return precip_forecast @pytest.mark.parametrize(steps_arg_names, steps_arg_values) @@ -385,7 +390,7 @@ def test_steps_nowcast( mask_method=mask_method, callback=None, return_output=True, - seed=None, + seed=42, num_workers=1, fft_method="numpy", domain=domain,