Skip to content

Commit 351d86c

Browse files
committed
Add animation of segmentation
1 parent cd58812 commit 351d86c

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
Network segmentation process
3+
============================
4+
"""
5+
6+
import re
7+
8+
from matplotlib import pyplot as plt
9+
from matplotlib.animation import FuncAnimation
10+
from matplotlib.colors import ListedColormap
11+
from numpy import ones
12+
13+
import py_eddy_tracker.gui
14+
from py_eddy_tracker import data
15+
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
16+
17+
18+
# %%
19+
class VideoAnimation(FuncAnimation):
20+
def _repr_html_(self, *args, **kwargs):
21+
"""To get video in html and have a player"""
22+
content = self.to_html5_video()
23+
return re.sub(
24+
'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content
25+
)
26+
27+
def save(self, *args, **kwargs):
28+
if args[0].endswith("gif"):
29+
# In this case gif is use to create thumbnail which are not use but consume same time than video
30+
# So we create an empty file, to save time
31+
with open(args[0], "w") as _:
32+
pass
33+
return
34+
return super().save(*args, **kwargs)
35+
36+
37+
# %%
38+
# Overlaod of class to pick up
39+
TRACKS = list()
40+
41+
42+
class MyTrack(TrackEddiesObservations):
43+
@staticmethod
44+
def get_next_obs(i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs):
45+
TRACKS.append(ids["track"].copy())
46+
return TrackEddiesObservations.get_next_obs(
47+
i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs
48+
)
49+
50+
51+
# %%
52+
# Load data
53+
# ---------
54+
# Load data where observations are put in same network but no segmentation
55+
e = MyTrack.load_file(data.get_path("c568803.nc"))
56+
# FIXME : Must be rewrote
57+
e.lon[:] = (e.lon + 180) % 360 - 180
58+
e.contour_lon_e[:] = ((e.contour_lon_e.T - e.lon + 180) % 360 - 180 + e.lon).T
59+
e.contour_lon_s[:] = ((e.contour_lon_s.T - e.lon + 180) % 360 - 180 + e.lon).T
60+
# %%
61+
# Do segmentation
62+
# ---------------
63+
# Segmentation based on maximum overlap, temporal window for candidates = 5 days
64+
matrix = e.split_network(intern=False, window=5)
65+
66+
67+
# %%
68+
# Anim
69+
# ----
70+
def update(i_frame):
71+
tr = TRACKS[i_frame]
72+
mappable_tracks.set_array(tr)
73+
s = 80 * ones(tr.shape)
74+
s[tr == 0] = 4
75+
mappable_tracks.set_sizes(s)
76+
return (mappable_tracks,)
77+
78+
79+
fig = plt.figure(figsize=(15, 8), dpi=60)
80+
ax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection="full_axes")
81+
ax.set_title(f"{len(e)} observations to segment")
82+
ax.set_xlim(-13, 20), ax.set_ylim(-36.5, -20), ax.grid()
83+
vmax = TRACKS[-1].max()
84+
cmap = ListedColormap(["gray", *e.COLORS[:-1]], name="from_list", N=vmax)
85+
mappable_tracks = ax.scatter(
86+
e.lon, e.lat, c=TRACKS[0], cmap=cmap, vmin=0, vmax=vmax, s=20
87+
)
88+
ani = VideoAnimation(
89+
fig, update, frames=range(1, len(TRACKS), 4), interval=125, blit=True
90+
)

0 commit comments

Comments
 (0)