Skip to content

Commit 5854912

Browse files
committed
add shape gallery
1 parent 2435ae9 commit 5854912

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
Shape error gallery
3+
===================
4+
5+
Gallery of contours with shape error
6+
7+
"""
8+
from matplotlib import pyplot as plt
9+
from numpy import arange, radians, linspace, cos, sin
10+
from py_eddy_tracker.dataset.grid import RegularGridDataset
11+
from py_eddy_tracker import data
12+
from py_eddy_tracker.eddy_feature import Contours
13+
from py_eddy_tracker.generic import local_to_coordinates
14+
15+
16+
# %%
17+
# Method to compute circle path
18+
def build_circle(x0, y0, r):
19+
angle = radians(linspace(0, 360, 50))
20+
x_norm, y_norm = cos(angle), sin(angle)
21+
return local_to_coordinates(x_norm * r, y_norm * r, x0, y0)
22+
23+
24+
# %%
25+
# We iterate over closed contours and sort with regards of shape error
26+
g = RegularGridDataset(
27+
data.get_path("dt_med_allsat_phy_l4_20160515_20190101.nc"), "longitude", "latitude"
28+
)
29+
c = Contours(g.x_c, g.y_c, g.grid("adt") * 100, arange(-50, 50, 0.2))
30+
contours = dict()
31+
for coll in c.iter():
32+
for current_contour in coll.get_paths():
33+
_, _, _, aerr = current_contour.fit_circle()
34+
i = int(aerr // 4) + 1
35+
if i not in contours:
36+
contours[i] = list()
37+
contours[i].append(current_contour)
38+
39+
# %%
40+
# Shape error gallery
41+
# -------------------
42+
# For each contour display, we display circle fitted, we work at different latitude circle could have distorsion
43+
fig = plt.figure(figsize=(12, 12))
44+
for i in range(1, 26):
45+
e_min, e_max = (i - 1) * 4, i * 4
46+
ax = plt.subplot(5, 5, i, title=f" {e_min} < err < {e_max}")
47+
ax.xaxis.set_ticklabels([])
48+
ax.yaxis.set_ticklabels([])
49+
ax.set_aspect("equal")
50+
ax.grid()
51+
if i in contours:
52+
for contour in contours[i]:
53+
x, y = contour.lon, contour.lat
54+
x0, y0, radius, _ = contour.fit_circle()
55+
if x.shape[0] > 30 and 30000 < radius < 70000:
56+
# Plot only first contour found
57+
m = ax.plot(x, y, "r")[0]
58+
ax.plot(*build_circle(x0, y0, radius), "g--")
59+
ax.plot(x0, y0, "k.")
60+
break
61+
plt.tight_layout()
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
"\nShape error gallery\n===================\n\nGallery of contours with shape error\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": null,
24+
"metadata": {
25+
"collapsed": false
26+
},
27+
"outputs": [],
28+
"source": [
29+
"from matplotlib import pyplot as plt\nfrom numpy import arange, radians, linspace, cos, sin\nfrom py_eddy_tracker.dataset.grid import RegularGridDataset\nfrom py_eddy_tracker import data\nfrom py_eddy_tracker.eddy_feature import Contours\nfrom py_eddy_tracker.generic import local_to_coordinates"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"Method to compute circle path\n\n"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {
43+
"collapsed": false
44+
},
45+
"outputs": [],
46+
"source": [
47+
"def build_circle(x0, y0, r):\n angle = radians(linspace(0, 360, 50))\n x_norm, y_norm = cos(angle), sin(angle)\n return local_to_coordinates(x_norm * r, y_norm * r, x0, y0)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"We iterate over closed contours and sort with regards of shape error\n\n"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": null,
60+
"metadata": {
61+
"collapsed": false
62+
},
63+
"outputs": [],
64+
"source": [
65+
"g = RegularGridDataset(\n data.get_path(\"dt_med_allsat_phy_l4_20160515_20190101.nc\"), \"longitude\", \"latitude\"\n)\nc = Contours(g.x_c, g.y_c, g.grid(\"adt\") * 100, arange(-50, 50, 0.2))\ncontours = dict()\nfor coll in c.iter():\n for current_contour in coll.get_paths():\n _, _, _, aerr = current_contour.fit_circle()\n i = int(aerr // 4) + 1\n if i not in contours:\n contours[i] = list()\n contours[i].append(current_contour)"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"Shape error gallery\n-------------------\nFor each contour display, we display circle fitted, we work at different latitude circle could have distorsion\n\n"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": false
80+
},
81+
"outputs": [],
82+
"source": [
83+
"fig = plt.figure(figsize=(12, 12))\nfor i in range(1, 26):\n e_min, e_max = (i - 1) * 4, i * 4\n ax = plt.subplot(5, 5, i, title=f\" {e_min} < err < {e_max}\")\n ax.xaxis.set_ticklabels([])\n ax.yaxis.set_ticklabels([])\n ax.set_aspect(\"equal\")\n ax.grid()\n if i in contours:\n for contour in contours[i]:\n x, y = contour.lon, contour.lat\n x0, y0, radius, _ = contour.fit_circle()\n if x.shape[0] > 30 and 30000 < radius < 70000:\n # Plot only first contour found\n m = ax.plot(x, y, \"r\")[0]\n ax.plot(*build_circle(x0, y0, radius), \"g--\")\n ax.plot(x0, y0, \"k.\")\n break\nplt.tight_layout()"
84+
]
85+
}
86+
],
87+
"metadata": {
88+
"kernelspec": {
89+
"display_name": "Python 3",
90+
"language": "python",
91+
"name": "python3"
92+
},
93+
"language_info": {
94+
"codemirror_mode": {
95+
"name": "ipython",
96+
"version": 3
97+
},
98+
"file_extension": ".py",
99+
"mimetype": "text/x-python",
100+
"name": "python",
101+
"nbconvert_exporter": "python",
102+
"pygments_lexer": "ipython3",
103+
"version": "3.7.7"
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 0
108+
}

0 commit comments

Comments
 (0)