.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "python_module/16_network/pet_relative.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_relative.py: Network basic manipulation ========================== .. GENERATED FROM PYTHON SOURCE LINES 5-12 .. code-block:: Python from matplotlib import pyplot as plt from numpy import where from py_eddy_tracker import data from py_eddy_tracker.gui import GUI_AXES from py_eddy_tracker.observations.network import NetworkObservations .. GENERATED FROM PYTHON SOURCE LINES 13-16 Load data --------- Load data where observations are put in same network but no segmentation .. GENERATED FROM PYTHON SOURCE LINES 16-30 .. code-block:: Python n = NetworkObservations.load_file(data.get_demo_path("network_med.nc")).network(651) i = where( (n.lat > 33) * (n.lat < 34) * (n.lon > 22) * (n.lon < 23) * (n.time > 20630) * (n.time < 20650) )[0][0] # For event use n2 = n.relative(i, order=2) n = n.relative(i, order=4) n.numbering_segment() .. GENERATED FROM PYTHON SOURCE LINES 31-33 Timeline -------- .. GENERATED FROM PYTHON SOURCE LINES 35-39 Display timeline with events A segment generated by a splitting is marked with a star A segment merging in another is marked with an exagon .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.04, 0.92, 0.92]) _ = n.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_001.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 44-45 Display timeline without event .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. code-block:: Python fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.04, 0.92, 0.92]) _ = n.display_timeline(ax, event=False) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_002.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-53 Timeline by mean latitude ------------------------- Display timeline with the mean latitude of the segments in yaxis .. GENERATED FROM PYTHON SOURCE LINES 53-58 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.04, 0.92, 0.92]) ax.set_ylabel("Latitude") _ = n.display_timeline(ax, field="latitude") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_003.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_003.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 59-62 Timeline by mean Effective Radius --------------------------------- The factor argument is applied on the chosen field .. GENERATED FROM PYTHON SOURCE LINES 62-67 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.04, 0.92, 0.92]) ax.set_ylabel("Effective Radius (km)") _ = n.display_timeline(ax, field="radius_e", factor=1e-3) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_004.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_004.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 68-71 Timeline by latitude -------------------- Use `method="all"` to display the consecutive values of the field .. GENERATED FROM PYTHON SOURCE LINES 71-76 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.05, 0.92, 0.92]) ax.set_ylabel("Latitude") _ = n.display_timeline(ax, field="lat", method="all") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_005.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_005.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-78 You can filter the data, here with a time window of 15 days .. GENERATED FROM PYTHON SOURCE LINES 78-84 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.05, 0.92, 0.92]) n_copy = n.copy() n_copy.median_filter(15, "time", "latitude") _ = n_copy.display_timeline(ax, field="lat", method="all") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_006.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_006.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 85-90 Parameters timeline ------------------- Scatter is usefull to display the parameters' temporal evolution Effective Radius and Amplitude .. GENERATED FROM PYTHON SOURCE LINES 90-106 .. code-block:: Python kw = dict(s=25, cmap="Spectral_r", zorder=10) fig = plt.figure(figsize=(15, 12)) ax = fig.add_axes([0.04, 0.54, 0.90, 0.44]) m = n.scatter_timeline(ax, "radius_e", factor=1e-3, vmin=50, vmax=150, **kw) cb = plt.colorbar( m["scatter"], cax=fig.add_axes([0.95, 0.54, 0.01, 0.44]), orientation="vertical" ) cb.set_label("Effective radius (km)") ax = fig.add_axes([0.04, 0.04, 0.90, 0.44]) m = n.scatter_timeline(ax, "amplitude", factor=100, vmin=0, vmax=15, **kw) cb = plt.colorbar( m["scatter"], cax=fig.add_axes([0.95, 0.04, 0.01, 0.44]), orientation="vertical" ) cb.set_label("Amplitude (cm)") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_007.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_007.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 107-108 Speed .. GENERATED FROM PYTHON SOURCE LINES 108-116 .. code-block:: Python fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) m = n.scatter_timeline(ax, "speed_average", factor=100, vmin=0, vmax=40, **kw) cb = plt.colorbar( m["scatter"], cax=fig.add_axes([0.95, 0.04, 0.01, 0.92]), orientation="vertical" ) cb.set_label("Maximum speed (cm/s)") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_008.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_008.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 117-118 Speed Radius .. GENERATED FROM PYTHON SOURCE LINES 118-126 .. code-block:: Python fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) m = n.scatter_timeline(ax, "radius_s", factor=1e-3, vmin=20, vmax=100, **kw) cb = plt.colorbar( m["scatter"], cax=fig.add_axes([0.95, 0.04, 0.01, 0.92]), orientation="vertical" ) cb.set_label("Speed radius (km)") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_009.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_009.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 127-130 Remove dead branch ------------------ Remove all tiny segments with less than N obs which didn't join two segments .. GENERATED FROM PYTHON SOURCE LINES 130-141 .. code-block:: Python n_clean = n.copy() n_clean.remove_dead_end(nobs=5, ndays=10) n_clean = n_clean.remove_trash() fig = plt.figure(figsize=(15, 12)) ax = fig.add_axes([0.04, 0.54, 0.90, 0.40]) ax.set_title(f"Original network ({n.infos()})") n.display_timeline(ax) ax = fig.add_axes([0.04, 0.04, 0.90, 0.40]) ax.set_title(f"Clean network ({n_clean.infos()})") _ = n_clean.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_010.png :alt: Original network (2868 obs 39 segments), Clean network (2831 obs 26 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_010.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 142-143 For further figure we will use clean path .. GENERATED FROM PYTHON SOURCE LINES 143-145 .. code-block:: Python n = n_clean .. GENERATED FROM PYTHON SOURCE LINES 146-149 Change splitting-merging events ------------------------------- change event where seg A split to B, then A merge into B, to A split to B then B merge into A .. GENERATED FROM PYTHON SOURCE LINES 149-162 .. code-block:: Python fig = plt.figure(figsize=(15, 12)) ax = fig.add_axes([0.04, 0.54, 0.90, 0.40]) ax.set_title(f"Clean network ({n.infos()})") n.display_timeline(ax) clean_modified = n.copy() # If it's happen in less than 40 days clean_modified.correct_close_events(40) ax = fig.add_axes([0.04, 0.04, 0.90, 0.40]) ax.set_title(f"resplitted network ({clean_modified.infos()})") _ = clean_modified.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_011.png :alt: Clean network (2831 obs 26 segments), resplitted network (2831 obs 26 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_011.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 163-165 Keep only observations where water could propagate from an observation ---------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 165-182 .. code-block:: Python i_observation = 600 only_linked = n.find_link(i_observation) fig = plt.figure(figsize=(15, 12)) ax1 = fig.add_axes([0.04, 0.54, 0.90, 0.40]) ax2 = fig.add_axes([0.04, 0.04, 0.90, 0.40]) kw = dict(marker="s", s=300, color="black", zorder=200, label="observation start") for ax, dataset in zip([ax1, ax2], [n, only_linked]): dataset.display_timeline(ax, field="segment", lw=2, markersize=5, colors_mode="y") ax.scatter(n.time[i_observation], n.segment[i_observation], **kw) ax.legend() ax1.set_title(f"full example ({n.infos()})") ax2.set_title(f"only linked observations ({only_linked.infos()})") _ = ax2.set_xlim(ax1.get_xlim()), ax2.set_ylim(ax1.get_ylim()) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_012.png :alt: full example (2831 obs 26 segments), only linked observations (1430 obs 10 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_012.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 183-186 Keep close relative ------------------- When you want to investigate one particular observation and select only the closest segments .. GENERATED FROM PYTHON SOURCE LINES 186-197 .. code-block:: Python # First choose an observation in the network i = 1100 fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) n.display_timeline(ax) obs_args = n.time[i], n.segment[i] obs_kw = dict(color="black", markersize=30, marker=".") _ = ax.plot(*obs_args, **obs_kw) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_013.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_013.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 198-199 Colors show the relative order of the segment with regards to the chosen one .. GENERATED FROM PYTHON SOURCE LINES 199-209 .. code-block:: Python fig = plt.figure(figsize=(15, 6)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) m = n.scatter_timeline( ax, n.obs_relative_order(i), vmin=-1.5, vmax=6.5, cmap=plt.get_cmap("jet", 8), s=10 ) ax.plot(*obs_args, **obs_kw) cb = plt.colorbar( m["scatter"], cax=fig.add_axes([0.95, 0.04, 0.01, 0.92]), orientation="vertical" ) cb.set_label("Relative order") .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_014.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_014.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 210-211 You want to keep only the segments at the order 1 .. GENERATED FROM PYTHON SOURCE LINES 211-216 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) close_to_i1 = n.relative(i, order=1) ax.set_title(f"Close segments ({close_to_i1.infos()})") _ = close_to_i1.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_015.png :alt: Close segments (1204 obs 11 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_015.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 217-218 You want to keep the segments until order 2 .. GENERATED FROM PYTHON SOURCE LINES 218-223 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) close_to_i2 = n.relative(i, order=2) ax.set_title(f"Close segments ({close_to_i2.infos()})") _ = close_to_i2.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_016.png :alt: Close segments (1284 obs 12 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_016.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 224-225 You want to keep the segments until order 3 .. GENERATED FROM PYTHON SOURCE LINES 225-231 .. code-block:: Python fig = plt.figure(figsize=(15, 5)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88]) close_to_i3 = n.relative(i, order=3) ax.set_title(f"Close segments ({close_to_i3.infos()})") _ = close_to_i3.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_017.png :alt: Close segments (2509 obs 15 segments) :srcset: /python_module/16_network/images/sphx_glr_pet_relative_017.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 232-237 Keep relatives to an event -------------------------- When you want to investigate one particular event and select only the closest segments First choose a merging event in the network .. GENERATED FROM PYTHON SOURCE LINES 237-239 .. code-block:: Python after, before, stopped = n.merging_event(triplet=True, only_index=True) i_event = 7 .. GENERATED FROM PYTHON SOURCE LINES 240-241 then see some order of relatives .. GENERATED FROM PYTHON SOURCE LINES 241-261 .. code-block:: Python max_order = 1 fig, axs = plt.subplots( max_order + 2, 1, sharex=True, figsize=(15, 5 * (max_order + 2)) ) # Original network ax = axs[0] ax.set_title("Full network", weight="bold") n.display_timeline(axs[0], colors_mode="y") ax.grid(), ax.legend() for k in range(0, max_order + 1): ax = axs[k + 1] ax.set_title(f"Relatives order={k}", weight="bold") # Extract neighbours of event sub_network = n.find_segments_relative(after[i_event], stopped[i_event], order=k) sub_network.display_timeline(ax, colors_mode="y") ax.legend(), ax.grid() _ = ax.set_ylim(axs[0].get_ylim()) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_018.png :alt: Full network, Relatives order=0, Relatives order=1 :srcset: /python_module/16_network/images/sphx_glr_pet_relative_018.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 262-264 Display track on map -------------------- .. GENERATED FROM PYTHON SOURCE LINES 264-270 .. code-block:: Python # Get a simplified network n = n2.copy() n.remove_dead_end(nobs=50, recursive=1) n = n.remove_trash() n.numbering_segment() .. GENERATED FROM PYTHON SOURCE LINES 271-272 Only a map can be tricky to understand, with a timeline it's easier! .. GENERATED FROM PYTHON SOURCE LINES 272-280 .. code-block:: Python fig = plt.figure(figsize=(15, 8)) ax = fig.add_axes([0.04, 0.06, 0.94, 0.88], projection=GUI_AXES) n.plot(ax, color_cycle=n.COLORS) ax.set_xlim(17.5, 27.5), ax.set_ylim(31, 36), ax.grid() ax = fig.add_axes([0.08, 0.7, 0.7, 0.3]) _ = n.display_timeline(ax) .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_019.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_019.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 281-284 Get merging event ----------------- Display the position of the eddies after a merging .. GENERATED FROM PYTHON SOURCE LINES 284-298 .. code-block:: Python fig = plt.figure(figsize=(15, 8)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection=GUI_AXES) n.plot(ax, color_cycle=n.COLORS) m1, m0, m0_stop = n.merging_event(triplet=True) m1.display(ax, color="violet", lw=2, label="Eddies after merging") m0.display(ax, color="blueviolet", lw=2, label="Eddies before merging") m0_stop.display(ax, color="black", lw=2, label="Eddies stopped by merging") ax.plot(m1.lon, m1.lat, marker=".", color="purple", ls="") ax.plot(m0.lon, m0.lat, marker=".", color="blueviolet", ls="") ax.plot(m0_stop.lon, m0_stop.lat, marker=".", color="black", ls="") ax.legend() ax.set_xlim(17.5, 27.5), ax.set_ylim(31, 36), ax.grid() m1 .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_020.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_020.png :class: sphx-glr-single-img .. raw:: html
3 observations from 20245.0 to 20762.0


.. GENERATED FROM PYTHON SOURCE LINES 299-302 Get splitting event ------------------- Display the position of the eddies before a splitting .. GENERATED FROM PYTHON SOURCE LINES 302-316 .. code-block:: Python fig = plt.figure(figsize=(15, 8)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection=GUI_AXES) n.plot(ax, color_cycle=n.COLORS) s0, s1, s1_start = n.splitting_event(triplet=True) s0.display(ax, color="violet", lw=2, label="Eddies before splitting") s1.display(ax, color="blueviolet", lw=2, label="Eddies after splitting") s1_start.display(ax, color="black", lw=2, label="Eddies starting by splitting") ax.plot(s0.lon, s0.lat, marker=".", color="purple", ls="") ax.plot(s1.lon, s1.lat, marker=".", color="blueviolet", ls="") ax.plot(s1_start.lon, s1_start.lat, marker=".", color="black", ls="") ax.legend() ax.set_xlim(17.5, 27.5), ax.set_ylim(31, 36), ax.grid() s1 .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_021.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_021.png :class: sphx-glr-single-img .. raw:: html
4 observations from 20306.0 to 20908.0


.. GENERATED FROM PYTHON SOURCE LINES 317-320 Get birth event --------------- Display the starting position of non-splitted eddies .. GENERATED FROM PYTHON SOURCE LINES 320-327 .. code-block:: Python fig = plt.figure(figsize=(15, 8)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection=GUI_AXES) birth = n.birth_event() birth.display(ax) ax.set_xlim(17.5, 27.5), ax.set_ylim(31, 36), ax.grid() birth .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_022.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_022.png :class: sphx-glr-single-img .. raw:: html
2 observations from 19889.0 to 20223.0


.. GENERATED FROM PYTHON SOURCE LINES 328-331 Get death event --------------- Display the last position of non-merged eddies .. GENERATED FROM PYTHON SOURCE LINES 331-337 .. code-block:: Python fig = plt.figure(figsize=(15, 8)) ax = fig.add_axes([0.04, 0.06, 0.90, 0.88], projection=GUI_AXES) death = n.death_event() death.display(ax) ax.set_xlim(17.5, 27.5), ax.set_ylim(31, 36), ax.grid() death .. image-sg:: /python_module/16_network/images/sphx_glr_pet_relative_023.png :alt: pet relative :srcset: /python_module/16_network/images/sphx_glr_pet_relative_023.png :class: sphx-glr-single-img .. raw:: html
3 observations from 20771.0 to 21191.0


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.928 seconds) .. _sphx_glr_download_python_module_16_network_pet_relative.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_relative.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: pet_relative.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: pet_relative.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: pet_relative.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_