Skip to content

Commit

Permalink
Bugfix: make STEPS (blending) nowcast reproducable when the seed argu…
Browse files Browse the repository at this point in the history
…ment is given (pySTEPS#346)
  • Loading branch information
mpvginde committed Feb 9, 2024
1 parent 324419e commit ba3db09
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion pysteps/blending/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def forecast(
)

# 2. Initialize the noise method
np.random.seed(seed)
pp, generate_noise, noise_std_coeffs = _init_noise(
precip,
precip_thr,
Expand All @@ -526,6 +527,7 @@ def forecast(
noise_stddev_adj,
measure_time,
num_workers,
seed
)

# 3. Perform the cascade decomposition for the input precip fields and
Expand Down Expand Up @@ -1662,6 +1664,7 @@ def _init_noise(
noise_stddev_adj,
measure_time,
num_workers,
seed
):
"""Initialize the noise method."""
if noise_method is None:
Expand Down Expand Up @@ -1690,6 +1693,7 @@ def _init_noise(
20,
conditional=True,
num_workers=num_workers,
seed=seed
)

if measure_time:
Expand Down Expand Up @@ -1944,7 +1948,6 @@ def _init_random_generators(
if noise_method is not None:
randgen_prec = []
randgen_motion = []
np.random.seed(seed)
for j in range(n_ens_members):
rs = np.random.RandomState(seed)
randgen_prec.append(rs)
Expand Down
6 changes: 3 additions & 3 deletions pysteps/noise/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ def compute_noise_stddev_adjs(
N_stds = []

randstates = []
seed = None

for k in range(num_iter):
randstates.append(np.random.RandomState(seed=seed))
seed = np.random.randint(0, high=1e9)

for k in range(num_iter):

def worker():
# generate Gaussian white noise field, filter it using the chosen
# method, multiply it with the standard deviation of the observed
# field and apply the precipitation mask
N = noise_generator(noise_filter, randstate=randstates[k], seed=seed)
N = noise_generator(noise_filter, randstate=randstates[k])
N = N / np.std(N) * sigma + mu
N[~MASK] = R_thr_2

Expand Down
3 changes: 2 additions & 1 deletion pysteps/nowcasts/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ def f(precip, i):
precip[i, ~np.isfinite(precip[i, :])] = np.nanmin(precip[i, :])

if noise_method is not None:
np.random.seed(seed)
# get methods for perturbations
init_noise, generate_noise = noise.get_method(noise_method)

Expand All @@ -466,6 +467,7 @@ def f(precip, i):
20,
conditional=True,
num_workers=num_workers,
seed=seed
)

if measure_time:
Expand Down Expand Up @@ -543,7 +545,6 @@ def f(precip, i):
if noise_method is not None:
randgen_prec = []
randgen_motion = []
np.random.seed(seed)
for _ in range(n_ens_members):
rs = np.random.RandomState(seed)
randgen_prec.append(rs)
Expand Down

0 comments on commit ba3db09

Please sign in to comment.