def test_visualization_motionfields_quiver(source, axis, step, quiver_kwargs, map_kwargs, upscale, pass_geodata): if source is not None: fields, geodata = get_precipitation_fields(0, 2, False, True, upscale, source) if not pass_geodata: geodata = None ax = plot_precip_field(fields[-1], geodata=geodata) oflow_method = motion.get_method("LK") UV = oflow_method(fields) else: shape = (100, 100) geodata = None ax = None u = np.ones(shape[1]) * shape[0] v = np.arange(0, shape[0]) U, V = np.meshgrid(u, v) UV = np.concatenate([U[None, :], V[None, :]]) UV_orig = UV.copy() __ = quiver(UV, ax, geodata, axis, step, quiver_kwargs, map_kwargs=map_kwargs) # Check that quiver does not modify the input data assert np.array_equal(UV, UV_orig)
def test_visualization_motionfields_quiver( source, map, drawlonlatlines, lw, axis, step, quiver_kwargs, upscale, ): if map == "cartopy": pytest.importorskip("cartopy") elif map == "basemap": pytest.importorskip("basemap") if source is not None: fields, geodata = get_precipitation_fields(0, 2, False, True, upscale, source) ax = plot_precip_field( fields[-1], map=map, geodata=geodata, ) oflow_method = motion.get_method("LK") UV = oflow_method(fields) else: shape = (100, 100) geodata = None ax = None u = np.ones(shape[1]) * shape[0] v = np.arange(0, shape[0]) U, V = np.meshgrid(u, v) UV = np.concatenate([U[None, :], V[None, :]]) __ = quiver(UV, ax, map, geodata, drawlonlatlines, lw, axis, step, quiver_kwargs)
def plot_optflow_method_convergence(input_precip, optflow_method_name, motion_type): """ Test the convergence to the actual solution of the optical flow method used. Parameters ---------- input_precip: numpy array (lat, lon) Input precipitation field. optflow_method_name: str Optical flow method name motion_type : str The supported motion fields are: - linear_x: (u=2, v=0) - linear_y: (u=0, v=2) - rotor: rotor field """ if optflow_method_name.lower() != "darts": num_times = 2 else: num_times = 9 ideal_motion, precip_obs = create_observations(input_precip, motion_type, num_times=num_times) oflow_method = motion.get_method(optflow_method_name) computed_motion = oflow_method(precip_obs, verbose=False) precip_obs, _ = stp.utils.dB_transform(precip_obs, inverse=True) precip_data = precip_obs.max(axis=0) precip_data.data[precip_data.mask] = 0 precip_mask = ((uniform_filter(precip_data, size=20) > 0.1) & ~precip_obs.mask.any(axis=0)) cmap = get_cmap('jet') cmap.set_under('grey', alpha=0.25) cmap.set_over('none') # Compare retrieved motion field with the ideal one plt.figure(figsize=(9, 4)) plt.subplot(1, 2, 1) ax = plot_precip_field(precip_obs[0], title="Reference motion") quiver(ideal_motion, step=25, ax=ax) plt.subplot(1, 2, 2) ax = plot_precip_field(precip_obs[0], title="Retrieved motion") quiver(computed_motion, step=25, ax=ax) # To evaluate the accuracy of the computed_motion vectors, we will use # a relative RMSE measure. # Relative MSE = < (expected_motion - computed_motion)^2 > / <expected_motion^2 > # Relative RMSE = sqrt(Relative MSE) mse = ((ideal_motion - computed_motion)[:, precip_mask] ** 2).mean() rel_mse = mse / (ideal_motion[:, precip_mask] ** 2).mean() plt.suptitle(f"{optflow_method_name} " f"Relative RMSE: {np.sqrt(rel_mse) * 100:.2f}%") plt.show()
# Estimate the motion field with Lucas-Kanade oflow_method = motion.get_method("LK") V = oflow_method(R[-3:, :, :]) # Extrapolate the last radar observation extrapolate = nowcasts.get_method("extrapolation") R[~np.isfinite(R)] = metadata["zerovalue"] R_f = extrapolate(R[-1, :, :], V, n_leadtimes) # Back-transform to rain rate R_f = transformation.dB_transform(R_f, threshold=-10.0, inverse=True)[0] # Plot the motion field plot_precip_field(R_, geodata=metadata) quiver(V, geodata=metadata, step=50) ############################################################################### # Verify with FSS # --------------- # # The fractions skill score (FSS) provides an intuitive assessment of the # dependency of skill on spatial scale and intensity, which makes it an ideal # skill score for high-resolution precipitation forecasts. # Find observations in the data archive fns = io.archive.find_by_date( date, root_path, path_fmt, fn_pattern,
################################################################################ # Lucas-Kanade (LK) # ----------------- # # The Lucas-Kanade optical flow method implemented in pysteps is a local # tracking approach that relies on the OpenCV package. # Local features are tracked in a sequence of two or more radar images. The # scheme includes a final interpolation step in order to produce a smooth # field of motion vectors. oflow_method = motion.get_method("LK") V1 = oflow_method(R[-3:, :, :]) # Plot the motion field on top of the reference frame plot_precip_field(R_, geodata=metadata, title="LK") quiver(V1, geodata=metadata, step=25) plt.show() ################################################################################ # Variational echo tracking (VET) # ------------------------------- # # This module implements the VET algorithm presented # by Laroche and Zawadzki (1995) and used in the McGill Algorithm for # Prediction by Lagrangian Extrapolation (MAPLE) described in # Germann and Zawadzki (2002). # The approach essentially consists of a global optimization routine that seeks # at minimizing a cost function between the displaced and the reference image. oflow_method = motion.get_method("VET") V2 = oflow_method(R[-3:, :, :])