|  | 
|  | 1 | +""" | 
|  | 2 | +Visvalingam algorithm | 
|  | 3 | +===================== | 
|  | 4 | +""" | 
|  | 5 | +import matplotlib.animation as animation | 
|  | 6 | +from matplotlib import pyplot as plt | 
|  | 7 | +from numba import njit | 
|  | 8 | +from numpy import array, empty | 
|  | 9 | + | 
|  | 10 | +from py_eddy_tracker import data | 
|  | 11 | +from py_eddy_tracker.generic import uniform_resample | 
|  | 12 | +from py_eddy_tracker.observations.observation import EddiesObservations | 
|  | 13 | +from py_eddy_tracker.poly import vertice_overlap, visvalingam | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +@njit(cache=True) | 
|  | 17 | +def visvalingam_polys(x, y, nb_pt): | 
|  | 18 | +    nb = x.shape[0] | 
|  | 19 | +    x_new = empty((nb, nb_pt), dtype=x.dtype) | 
|  | 20 | +    y_new = empty((nb, nb_pt), dtype=y.dtype) | 
|  | 21 | +    for i in range(nb): | 
|  | 22 | +        x_new[i], y_new[i] = visvalingam(x[i], y[i], nb_pt) | 
|  | 23 | +    return x_new, y_new | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +@njit(cache=True) | 
|  | 27 | +def uniform_resample_polys(x, y, nb_pt): | 
|  | 28 | +    nb = x.shape[0] | 
|  | 29 | +    x_new = empty((nb, nb_pt), dtype=x.dtype) | 
|  | 30 | +    y_new = empty((nb, nb_pt), dtype=y.dtype) | 
|  | 31 | +    for i in range(nb): | 
|  | 32 | +        x_new[i], y_new[i] = uniform_resample(x[i], y[i], fixed_size=nb_pt) | 
|  | 33 | +    return x_new, y_new | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +def update_line(num): | 
|  | 37 | +    nb = 50 - num - 20 | 
|  | 38 | +    x_v, y_v = visvalingam_polys(a.contour_lon_e, a.contour_lat_e, nb) | 
|  | 39 | +    for i, (x_, y_) in enumerate(zip(x_v, y_v)): | 
|  | 40 | +        lines_v[i].set_data(x_, y_) | 
|  | 41 | +    x_u, y_u = uniform_resample_polys(a.contour_lon_e, a.contour_lat_e, nb) | 
|  | 42 | +    for i, (x_, y_) in enumerate(zip(x_u, y_u)): | 
|  | 43 | +        lines_u[i].set_data(x_, y_) | 
|  | 44 | +    scores_v = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_v, y_v) * 100.0 | 
|  | 45 | +    scores_u = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_u, y_u) * 100.0 | 
|  | 46 | +    for i, (s_v, s_u) in enumerate(zip(scores_v, scores_u)): | 
|  | 47 | +        texts[i].set_text(f"Score uniform {s_u:.1f} %\nScore visvalingam {s_v:.1f} %") | 
|  | 48 | +    title.set_text(f"{nb} points by contour in place of 50") | 
|  | 49 | +    return (title, *lines_u, *lines_v, *texts) | 
|  | 50 | + | 
|  | 51 | + | 
|  | 52 | +# %% | 
|  | 53 | +# Load detection files | 
|  | 54 | +a = EddiesObservations.load_file(data.get_path("Anticyclonic_20190223.nc")) | 
|  | 55 | +a = a.extract_with_mask((abs(a.lat) < 66) * (abs(a.radius_e) > 80e3)) | 
|  | 56 | + | 
|  | 57 | +nb_pt = 10 | 
|  | 58 | +x_v, y_v = visvalingam_polys(a.contour_lon_e, a.contour_lat_e, nb_pt) | 
|  | 59 | +x_u, y_u = uniform_resample_polys(a.contour_lon_e, a.contour_lat_e, nb_pt) | 
|  | 60 | +scores_v = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_v, y_v) * 100.0 | 
|  | 61 | +scores_u = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_u, y_u) * 100.0 | 
|  | 62 | +d_6 = scores_v - scores_u | 
|  | 63 | +nb_pt = 18 | 
|  | 64 | +x_v, y_v = visvalingam_polys(a.contour_lon_e, a.contour_lat_e, nb_pt) | 
|  | 65 | +x_u, y_u = uniform_resample_polys(a.contour_lon_e, a.contour_lat_e, nb_pt) | 
|  | 66 | +scores_v = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_v, y_v) * 100.0 | 
|  | 67 | +scores_u = vertice_overlap(a.contour_lon_e, a.contour_lat_e, x_u, y_u) * 100.0 | 
|  | 68 | +d_12 = scores_v - scores_u | 
|  | 69 | +a = a.index(array((d_6.argmin(), d_6.argmax(), d_12.argmin(), d_12.argmax()))) | 
|  | 70 | + | 
|  | 71 | + | 
|  | 72 | +# %% | 
|  | 73 | +fig = plt.figure() | 
|  | 74 | +axs = [ | 
|  | 75 | +    fig.add_subplot(221), | 
|  | 76 | +    fig.add_subplot(222), | 
|  | 77 | +    fig.add_subplot(223), | 
|  | 78 | +    fig.add_subplot(224), | 
|  | 79 | +] | 
|  | 80 | +lines_u, lines_v, texts, score_text = list(), list(), list(), list() | 
|  | 81 | +for i, obs in enumerate(a): | 
|  | 82 | +    axs[i].set_aspect("equal") | 
|  | 83 | +    axs[i].grid() | 
|  | 84 | +    axs[i].set_xticklabels([]), axs[i].set_yticklabels([]) | 
|  | 85 | +    axs[i].plot( | 
|  | 86 | +        obs["contour_lon_e"], obs["contour_lat_e"], "r", lw=6, label="Original contour" | 
|  | 87 | +    ) | 
|  | 88 | +    lines_v.append(axs[i].plot([], [], color="limegreen", lw=4, label="visvalingam")[0]) | 
|  | 89 | +    lines_u.append( | 
|  | 90 | +        axs[i].plot([], [], color="black", lw=2, label="uniform resampling")[0] | 
|  | 91 | +    ) | 
|  | 92 | +    texts.append(axs[i].set_title("", fontsize=8)) | 
|  | 93 | +axs[0].legend(fontsize=8) | 
|  | 94 | +title = fig.suptitle("") | 
|  | 95 | +ani = animation.FuncAnimation(fig, update_line, 27) | 
0 commit comments