.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python_module/16_network/pet_segmentation_anim.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_python_module_16_network_pet_segmentation_anim.py: Network segmentation process ============================ .. GENERATED FROM PYTHON SOURCE LINES 5-19 .. code-block:: Python # sphinx_gallery_thumbnail_number = 2 import re from matplotlib import pyplot as plt from matplotlib.animation import FuncAnimation from matplotlib.colors import ListedColormap from numpy import ones, where from py_eddy_tracker.data import get_demo_path from py_eddy_tracker.gui import GUI_AXES from py_eddy_tracker.observations.network import NetworkObservations from py_eddy_tracker.observations.tracking import TrackEddiesObservations .. GENERATED FROM PYTHON SOURCE LINES 20-50 .. code-block:: Python class VideoAnimation(FuncAnimation): def _repr_html_(self, *args, **kwargs): """To get video in html and have a player""" content = self.to_html5_video() return re.sub( r'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content ) def save(self, *args, **kwargs): if args[0].endswith("gif"): # In this case gif is used to create thumbnail which is not used but consume same time than video # So we create an empty file, to save time with open(args[0], "w") as _: pass return return super().save(*args, **kwargs) def get_obs(dataset): "Function to isolate a specific obs" return where( (dataset.lat > 33) * (dataset.lat < 34) * (dataset.lon > 22) * (dataset.lon < 23) * (dataset.time > 20630) * (dataset.time < 20650) )[0][0] .. GENERATED FROM PYTHON SOURCE LINES 51-52 Hack to pick up each step of segmentation .. GENERATED FROM PYTHON SOURCE LINES 52-66 .. code-block:: Python TRACKS = list() INDICES = list() class MyTrack(TrackEddiesObservations): @staticmethod def get_next_obs(i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs): TRACKS.append(ids["track"].copy()) INDICES.append(i_current) return TrackEddiesObservations.get_next_obs( i_current, ids, x, y, time_s, time_e, time_ref, window, **kwargs ) .. GENERATED FROM PYTHON SOURCE LINES 67-70 Load data --------- Load data where observations are put in same network but no segmentation .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: Python # Get a known network for the demonstration n = NetworkObservations.load_file(get_demo_path("network_med.nc")).network(651) # We keep only some segment n = n.relative(get_obs(n), order=2) print(len(n)) # We convert and order object like segmentation was never happen on observations e = n.astype(MyTrack) e.obs.sort(order=("track", "time"), kind="stable") .. rst-class:: sphx-glr-script-out .. code-block:: none 1580 .. GENERATED FROM PYTHON SOURCE LINES 81-84 Do segmentation --------------- Segmentation based on maximum overlap, temporal window for candidates = 5 days .. GENERATED FROM PYTHON SOURCE LINES 84-87 .. code-block:: Python matrix = e.split_network(intern=False, window=5) .. GENERATED FROM PYTHON SOURCE LINES 88-90 Anim ---- .. GENERATED FROM PYTHON SOURCE LINES 90-119 .. code-block:: Python def update(i_frame): tr = TRACKS[i_frame] mappable_tracks.set_array(tr) s = 40 * ones(tr.shape) s[tr == 0] = 4 mappable_tracks.set_sizes(s) indices_frames = INDICES[i_frame] mappable_CONTOUR.set_data( e.contour_lon_e[indices_frames], e.contour_lat_e[indices_frames] ) mappable_CONTOUR.set_color(cmap.colors[tr[indices_frames] % len(cmap.colors)]) return (mappable_tracks,) fig = plt.figure(figsize=(16, 9), dpi=60) ax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection=GUI_AXES) ax.set_title(f"{len(e)} observations to segment") ax.set_xlim(19, 29), ax.set_ylim(31, 35.5), ax.grid() vmax = TRACKS[-1].max() cmap = ListedColormap(["gray", *e.COLORS[:-1]], name="from_list", N=vmax) mappable_tracks = ax.scatter( e.lon, e.lat, c=TRACKS[0], cmap=cmap, vmin=0, vmax=vmax, s=20 ) mappable_CONTOUR = ax.plot( e.contour_lon_e[INDICES[0]], e.contour_lat_e[INDICES[0]], color=cmap.colors[0] )[0] ani = VideoAnimation(fig, update, frames=range(1, len(TRACKS), 4), interval=125) .. container:: sphx-glr-animation .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 120-122 Final Result ------------ .. GENERATED FROM PYTHON SOURCE LINES 122-126 .. code-block:: Python fig = plt.figure(figsize=(16, 9)) ax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection=GUI_AXES) ax.set_xlim(19, 29), ax.set_ylim(31, 35.5), ax.grid() _ = ax.scatter(e.lon, e.lat, c=TRACKS[-1], cmap=cmap, vmin=0, vmax=vmax, s=20) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_segmentation_anim_002.png :alt: pet segmentation anim :srcset: /python_module/16_network/images/sphx_glr_pet_segmentation_anim_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 23.851 seconds) .. _sphx_glr_download_python_module_16_network_pet_segmentation_anim.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AntSimi/py-eddy-tracker/master?urlpath=lab/tree/notebooks/python_module/16_network/pet_segmentation_anim.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pet_segmentation_anim.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pet_segmentation_anim.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pet_segmentation_anim.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_