Skip to content

Commit b85fbf3

Browse files
committed
Use video instead jshtml for eddy animation
1 parent 827fda3 commit b85fbf3

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

examples/08_tracking_manipulation/pet_track_anim_matplotlib_animation.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@
55
Run in a terminal this script, which allow to watch eddy evolution
66
77
"""
8+
import re
9+
810
import py_eddy_tracker_sample
911
from matplotlib.animation import FuncAnimation
1012
from numpy import arange
1113

1214
from py_eddy_tracker.appli.gui import Anim
1315
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
1416

17+
# sphinx_gallery_thumbnail_path = '_static/no_image.png'
18+
19+
20+
# %%
21+
class VideoAnimation(FuncAnimation):
22+
def _repr_html_(self, *args, **kwargs):
23+
"""To get video in html and have a player"""
24+
content = self.to_html5_video()
25+
return re.sub(
26+
r'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content
27+
)
28+
29+
def save(self, *args, **kwargs):
30+
if args[0].endswith("gif"):
31+
# In this case gif is use to create thumbnail which are not use but consume same time than video
32+
# So we create an empty file, to save time
33+
with open(args[0], "w") as _:
34+
pass
35+
return
36+
return super().save(*args, **kwargs)
37+
38+
1539
# %%
1640
# Load experimental atlas, and we select one eddy
1741
a = TrackEddiesObservations.load_file(
@@ -21,14 +45,12 @@
2145

2246
# %%
2347
# Run animation
24-
a = Anim(eddy, intern=True, figsize=(8, 3.5), cmap="magma_r", nb_step=6)
48+
a = Anim(eddy, intern=True, figsize=(8, 3.5), cmap="magma_r", nb_step=5, dpi=50)
2549
a.txt.set_position((17, 34.6))
2650
a.ax.set_xlim(16.5, 23)
2751
a.ax.set_ylim(34.5, 37)
2852

2953
# arguments to get full animation
30-
# kwargs = dict(frames=arange(*a.period), interval=50)
31-
# arguments to reduce compute cost for doucmentation, we display only every 10 days
32-
kwargs = dict(frames=arange(*a.period)[200:800:10], save_count=60, interval=200)
54+
kwargs = dict(frames=arange(*a.period)[300:800], interval=90)
3355

34-
ani = FuncAnimation(a.fig, a.func_animation, **kwargs)
56+
ani = VideoAnimation(a.fig, a.func_animation, **kwargs)

notebooks/python_module/08_tracking_manipulation/pet_track_anim_matplotlib_animation.ipynb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"import py_eddy_tracker_sample\nfrom matplotlib.animation import FuncAnimation\nfrom numpy import arange\n\nfrom py_eddy_tracker.appli.gui import Anim\nfrom py_eddy_tracker.observations.tracking import TrackEddiesObservations"
29+
"import re\n\nimport py_eddy_tracker_sample\nfrom matplotlib.animation import FuncAnimation\nfrom numpy import arange\n\nfrom py_eddy_tracker.appli.gui import Anim\nfrom py_eddy_tracker.observations.tracking import TrackEddiesObservations\n# sphinx_gallery_thumbnail_path = '_static/no_image.png'"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": null,
35+
"metadata": {
36+
"collapsed": false
37+
},
38+
"outputs": [],
39+
"source": [
40+
"class VideoAnimation(FuncAnimation):\n def _repr_html_(self, *args, **kwargs):\n \"\"\"To get video in html and have a player\"\"\"\n content = self.to_html5_video()\n return re.sub(\n r'width=\"[0-9]*\"\\sheight=\"[0-9]*\"', 'width=\"100%\" height=\"100%\"', content\n )\n\n def save(self, *args, **kwargs):\n if args[0].endswith(\"gif\"):\n # In this case gif is use to create thumbnail which are not use but consume same time than video\n # So we create an empty file, to save time\n with open(args[0], \"w\") as _:\n pass\n return\n return super().save(*args, **kwargs)"
3041
]
3142
},
3243
{
@@ -62,7 +73,7 @@
6273
},
6374
"outputs": [],
6475
"source": [
65-
"a = Anim(eddy, intern=True, figsize=(8, 3.5), cmap=\"magma_r\", nb_step=6)\na.txt.set_position((17, 34.6))\na.ax.set_xlim(16.5, 23)\na.ax.set_ylim(34.5, 37)\n\n# arguments to get full animation\n# kwargs = dict(frames=arange(*a.period), interval=50)\n# arguments to reduce compute cost for doucmentation, we display only every 10 days\nkwargs = dict(frames=arange(*a.period)[200:800:10], save_count=60, interval=200)\n\nani = FuncAnimation(a.fig, a.func_animation, **kwargs)"
76+
"a = Anim(eddy, intern=True, figsize=(8, 3.5), cmap=\"magma_r\", nb_step=5, dpi=50)\na.txt.set_position((17, 34.6))\na.ax.set_xlim(16.5, 23)\na.ax.set_ylim(34.5, 37)\n\n# arguments to get full animation\n# arguments to reduce compute cost for doucmentation, we display only every 10 days\nkwargs = dict(frames=arange(*a.period)[300:800], interval=90)\n\nani = VideoAnimation(a.fig, a.func_animation, **kwargs)"
6677
]
6778
}
6879
],

0 commit comments

Comments
 (0)