Skip to content

Commit b7aa15e

Browse files
committed
Change to get remote data for network example
1 parent f50a9ae commit b7aa15e

File tree

6 files changed

+313
-27
lines changed

6 files changed

+313
-27
lines changed

examples/16_network/pet_ioannou_2017_case.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# %%
99
# We want to find the Ierapetra Eddy described above in the networks
1010

11+
import re
12+
1113
# %%
1214
from datetime import datetime, timedelta
1315

@@ -19,15 +21,18 @@
1921

2022
import py_eddy_tracker.gui
2123
from py_eddy_tracker.appli.gui import Anim
22-
from py_eddy_tracker.data import get_path
24+
from py_eddy_tracker.data import get_remote_sample
2325
from py_eddy_tracker.observations.network import NetworkObservations
2426

2527

2628
# %%
2729
class VideoAnimation(FuncAnimation):
2830
def _repr_html_(self, *args, **kwargs):
2931
"""To get video in html and have a player"""
30-
return self.to_html5_video()
32+
content = self.to_html5_video()
33+
return re.sub(
34+
r'width="[0-9]*"\sheight="[0-9]*"', 'width="100%" height="100%"', content
35+
)
3136

3237
def save(self, *args, **kwargs):
3338
if args[0].endswith("gif"):
@@ -72,7 +77,11 @@ def update_axes(ax, mappable=None):
7277
# We know the position and the time of a specific eddy
7378
#
7479
# `n.extract_with_mask` give us the corresponding network
75-
n = NetworkObservations.load_file(get_path("Anticyclonic_seg.nc"))
80+
n = NetworkObservations.load_file(
81+
get_remote_sample(
82+
"eddies_med_adt_allsat_dt2018_err70_filt500_order1/Anticyclonic_network.nc"
83+
)
84+
)
7685
i = np.where(
7786
(n.lat > 33)
7887
* (n.lat < 34)
@@ -85,9 +94,9 @@ def update_axes(ax, mappable=None):
8594
print(ioannou_case.infos())
8695

8796
# %%
88-
# It seems that this network is huge! Our case is in purple...
97+
# It seems that this network is huge! Our case is visible at 22E 33.5N
8998
ax = start_axes()
90-
ioannou_case.plot(ax)
99+
ioannou_case.plot(ax, color_cycle=ioannou_case.COLORS)
91100
update_axes(ax)
92101

93102
# %%
@@ -127,7 +136,7 @@ def update_axes(ax, mappable=None):
127136
figsize=(12, 4),
128137
cmap=cmap,
129138
nb_step=7,
130-
dpi=55,
139+
dpi=80,
131140
field_color="segment",
132141
field_txt="segment",
133142
)

examples/16_network/pet_relative.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ def formatter(x, pos):
215215
axs[0].legend()
216216

217217
for k in range(0, max_order + 1):
218-
219218
ax = axs[k + 1]
220219
sub_network = n.find_segments_relative(after[i_event], stopped[i_event], order=k)
221220

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [],
10+
"source": [
11+
"%matplotlib inline"
12+
]
13+
},
14+
{
15+
"cell_type": "markdown",
16+
"metadata": {},
17+
"source": [
18+
"\n# Ioannou case\nFigure 10 from https://doi.org/10.1002/2017JC013158\n"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"We want to find the Ierapetra Eddy described above in the networks\n\n"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": null,
31+
"metadata": {
32+
"collapsed": false
33+
},
34+
"outputs": [],
35+
"source": [
36+
"import re"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"from datetime import datetime, timedelta\n\nimport numpy as np\nfrom matplotlib import colors\nfrom matplotlib import pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom matplotlib.ticker import FuncFormatter\n\nimport py_eddy_tracker.gui\nfrom py_eddy_tracker.appli.gui import Anim\nfrom py_eddy_tracker.data import get_remote_sample\nfrom py_eddy_tracker.observations.network import NetworkObservations"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": null,
53+
"metadata": {
54+
"collapsed": false
55+
},
56+
"outputs": [],
57+
"source": [
58+
"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)\n\n\n@FuncFormatter\ndef formatter(x, pos):\n return (timedelta(x) + datetime(1950, 1, 1)).strftime(\"%d/%m/%Y\")\n\n\ndef start_axes(title=\"\"):\n fig = plt.figure(figsize=(13, 6))\n ax = fig.add_axes([0.03, 0.03, 0.90, 0.94], projection=\"full_axes\")\n ax.set_xlim(19, 29), ax.set_ylim(31, 35.5)\n ax.set_aspect(\"equal\")\n ax.set_title(title, weight=\"bold\")\n ax.update_env()\n return ax\n\n\ndef timeline_axes(title=\"\"):\n fig = plt.figure(figsize=(15, 5))\n ax = fig.add_axes([0.03, 0.06, 0.90, 0.88])\n ax.set_title(title, weight=\"bold\")\n ax.xaxis.set_major_formatter(formatter), ax.grid()\n return ax\n\n\ndef update_axes(ax, mappable=None):\n ax.grid(True)\n if mappable:\n return plt.colorbar(mappable, cax=ax.figure.add_axes([0.94, 0.05, 0.01, 0.9]))"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"We know the position and the time of a specific eddy\n\n`n.extract_with_mask` give us the corresponding network\n\n"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {
72+
"collapsed": false
73+
},
74+
"outputs": [],
75+
"source": [
76+
"n = NetworkObservations.load_file(\n get_remote_sample(\n \"eddies_med_adt_allsat_dt2018_err70_filt500_order1/Anticyclonic_network.nc\"\n )\n)\ni = np.where(\n (n.lat > 33)\n * (n.lat < 34)\n * (n.lon > 22)\n * (n.lon < 23)\n * (n.time > 20630)\n * (n.time < 20650)\n)[0][0]\nioannou_case = n.extract_with_mask(n.track == n.track[i])\nprint(ioannou_case.infos())"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"It seems that this network is huge! Our case is visible at 22E 33.5N\n\n"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": null,
89+
"metadata": {
90+
"collapsed": false
91+
},
92+
"outputs": [],
93+
"source": [
94+
"ax = start_axes()\nioannou_case.plot(ax, color_cycle=ioannou_case.COLORS)\nupdate_axes(ax)"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"## Full Timeline\nThe network span for many years... How to cut the interesting part?\n\n"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [],
111+
"source": [
112+
"fig = plt.figure(figsize=(15, 5))\nax = fig.add_axes([0.04, 0.05, 0.92, 0.92])\nax.xaxis.set_major_formatter(formatter), ax.grid()\n_ = ioannou_case.display_timeline(ax)"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"## Sub network and new numbering\nHere we chose to keep only the order 3 segments relatives to our chosen eddy\n\n"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {
126+
"collapsed": false
127+
},
128+
"outputs": [],
129+
"source": [
130+
"i = np.where(\n (ioannou_case.lat > 33)\n * (ioannou_case.lat < 34)\n * (ioannou_case.lon > 22)\n * (ioannou_case.lon < 23)\n * (ioannou_case.time > 20630)\n * (ioannou_case.time < 20650)\n)[0][0]\nclose_to_i3 = ioannou_case.relative(i, order=3)\nclose_to_i3.numbering_segment()"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"## Anim\nQuick movie to see better!\n\n"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {
144+
"collapsed": false
145+
},
146+
"outputs": [],
147+
"source": [
148+
"cmap = colors.ListedColormap(\n list(close_to_i3.COLORS), name=\"from_list\", N=close_to_i3.segment.max()\n)\na = Anim(\n close_to_i3,\n figsize=(12, 4),\n cmap=cmap,\n nb_step=7,\n dpi=80,\n field_color=\"segment\",\n field_txt=\"segment\",\n)\na.ax.set_xlim(19, 30), a.ax.set_ylim(32, 35.25)\na.ax.update_env()\na.txt.set_position((21.5, 32.7))\nkwargs = dict(frames=np.arange(*a.period), interval=100)\nani = VideoAnimation(a.fig, a.func_animation, **kwargs)"
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"metadata": {},
154+
"source": [
155+
"## Classic display\n\n"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {
162+
"collapsed": false
163+
},
164+
"outputs": [],
165+
"source": [
166+
"ax = timeline_axes()\n_ = close_to_i3.display_timeline(ax)"
167+
]
168+
},
169+
{
170+
"cell_type": "code",
171+
"execution_count": null,
172+
"metadata": {
173+
"collapsed": false
174+
},
175+
"outputs": [],
176+
"source": [
177+
"ax = start_axes(\"\")\nn_copy = close_to_i3.copy()\nn_copy.position_filter(2, 4)\nn_copy.plot(ax, color_cycle=n_copy.COLORS)\nupdate_axes(ax)"
178+
]
179+
},
180+
{
181+
"cell_type": "markdown",
182+
"metadata": {},
183+
"source": [
184+
"## Latitude Timeline\n\n"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"metadata": {
191+
"collapsed": false
192+
},
193+
"outputs": [],
194+
"source": [
195+
"ax = timeline_axes(f\"Close segments ({close_to_i3.infos()})\")\nn_copy = close_to_i3.copy()\nn_copy.median_filter(15, \"time\", \"latitude\")\n_ = n_copy.display_timeline(ax, field=\"lat\", method=\"all\")"
196+
]
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"metadata": {},
201+
"source": [
202+
"## Local radius timeline\nEffective (bold) and Speed (thin) Radius together\n\n"
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": null,
208+
"metadata": {
209+
"collapsed": false
210+
},
211+
"outputs": [],
212+
"source": [
213+
"n_copy.median_filter(2, \"time\", \"radius_e\")\nn_copy.median_filter(2, \"time\", \"radius_s\")\nfor b0, b1 in [\n (datetime(i, 1, 1), datetime(i, 12, 31)) for i in (2004, 2005, 2006, 2007)\n]:\n ref, delta = datetime(1950, 1, 1), 20\n b0_, b1_ = (b0 - ref).days, (b1 - ref).days\n ax = timeline_axes()\n ax.set_xlim(b0_ - delta, b1_ + delta)\n ax.set_ylim(10, 115)\n ax.axvline(b0_, color=\"k\", lw=1.5, ls=\"--\"), ax.axvline(\n b1_, color=\"k\", lw=1.5, ls=\"--\"\n )\n n_copy.display_timeline(\n ax, field=\"radius_e\", method=\"all\", lw=4, markersize=8, factor=1e-3\n )\n n_copy.display_timeline(\n ax, field=\"radius_s\", method=\"all\", lw=1, markersize=3, factor=1e-3\n )"
214+
]
215+
},
216+
{
217+
"cell_type": "markdown",
218+
"metadata": {},
219+
"source": [
220+
"## Parameters timeline\nEffective Radius\n\n"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {
227+
"collapsed": false
228+
},
229+
"outputs": [],
230+
"source": [
231+
"kw = dict(s=35, cmap=plt.get_cmap(\"Spectral_r\", 8), zorder=10)\nax = timeline_axes()\nm = close_to_i3.scatter_timeline(ax, \"radius_e\", factor=1e-3, vmin=20, vmax=100, **kw)\ncb = update_axes(ax, m[\"scatter\"])\ncb.set_label(\"Effective radius (km)\")"
232+
]
233+
},
234+
{
235+
"cell_type": "markdown",
236+
"metadata": {},
237+
"source": [
238+
"Shape error\n\n"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"metadata": {
245+
"collapsed": false
246+
},
247+
"outputs": [],
248+
"source": [
249+
"ax = timeline_axes()\nm = close_to_i3.scatter_timeline(ax, \"shape_error_e\", vmin=14, vmax=70, **kw)\ncb = update_axes(ax, m[\"scatter\"])\ncb.set_label(\"Effective shape error\")"
250+
]
251+
}
252+
],
253+
"metadata": {
254+
"kernelspec": {
255+
"display_name": "Python 3",
256+
"language": "python",
257+
"name": "python3"
258+
},
259+
"language_info": {
260+
"codemirror_mode": {
261+
"name": "ipython",
262+
"version": 3
263+
},
264+
"file_extension": ".py",
265+
"mimetype": "text/x-python",
266+
"name": "python",
267+
"nbconvert_exporter": "python",
268+
"pygments_lexer": "ipython3",
269+
"version": "3.7.7"
270+
}
271+
},
272+
"nbformat": 4,
273+
"nbformat_minor": 0
274+
}

src/py_eddy_tracker/data/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,21 @@ def get_path(name):
2121

2222

2323
def get_remote_sample(path):
24-
url = (
25-
f"https://github.com/AntSimi/py-eddy-tracker-sample-id/raw/master/{path}.tar.xz"
26-
)
24+
if path.startswith("/") or path.startswith("."):
25+
content = open(path, "rb").read()
26+
if path.endswith(".nc"):
27+
return io.BytesIO(content)
28+
else:
29+
if path.endswith(".nc"):
30+
content = requests.get(
31+
f"https://github.com/AntSimi/py-eddy-tracker-sample-id/raw/master/{path}"
32+
).content
33+
return io.BytesIO(content)
34+
content = requests.get(
35+
f"https://github.com/AntSimi/py-eddy-tracker-sample-id/raw/master/{path}.tar.xz"
36+
).content
2737

28-
content = requests.get(url).content
29-
30-
# Tar module could manage lzma tar, but it will apply un compress for each extractfile
38+
# Tar module could manage lzma tar, but it will apply uncompress for each extractfile
3139
tar = tarfile.open(mode="r", fileobj=io.BytesIO(lzma.decompress(content)))
3240
# tar = tarfile.open(mode="r:xz", fileobj=io.BytesIO(content))
3341
files_content = list()

0 commit comments

Comments
 (0)