Skip to content

Commit 9746a7a

Browse files
committed
first version to fit an ellips
1 parent 5f5a622 commit 9746a7a

File tree

5 files changed

+65
-11
lines changed

5 files changed

+65
-11
lines changed

examples/02_eddy_identification/pet_eddy_detection.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,15 @@ def update_axes(ax, mappable=None):
143143
# %%
144144
# Display the speed radius of the detected eddies
145145
ax = start_axes("Speed Radius (km)")
146-
a.scatter(ax, "radius_s", vmin=10, vmax=50, s=80, ref=-10, cmap="magma_r", factor=0.001)
147-
m = c.scatter(
148-
ax, "radius_s", vmin=10, vmax=50, s=80, ref=-10, cmap="magma_r", factor=0.001
149-
)
146+
kwargs = dict(vmin=10, vmax=50, s=80, ref=-10, cmap="magma_r", factor=0.001)
147+
a.scatter(ax, "radius_s", **kwargs)
148+
m = c.scatter(ax, "radius_s", **kwargs)
150149
update_axes(ax, m)
151150

152151
# %%
153152
# Filling the effective radius contours with the effective radius values
154153
ax = start_axes("Effective Radius (km)")
155154
kwargs = dict(vmin=10, vmax=80, cmap="magma_r", factor=0.001, lut=14, ref=-10)
156155
a.filled(ax, "effective_radius", **kwargs)
157-
m = c.filled(
158-
ax, "radius_e", vmin=10, vmax=80, cmap="magma_r", factor=0.001, lut=14, ref=-10
159-
)
156+
m = c.filled(ax, "radius_e", **kwargs)
160157
update_axes(ax, m)

examples/14_generic_tools/pet_fit_contour.py

Whitespace-only changes.

notebooks/python_module/02_eddy_identification/pet_eddy_detection.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
},
254254
"outputs": [],
255255
"source": [
256-
"ax = start_axes(\"Speed Radius (km)\")\na.scatter(ax, \"radius_s\", vmin=10, vmax=50, s=80, ref=-10, cmap=\"magma_r\", factor=0.001)\nm = c.scatter(\n ax, \"radius_s\", vmin=10, vmax=50, s=80, ref=-10, cmap=\"magma_r\", factor=0.001\n)\nupdate_axes(ax, m)"
256+
"ax = start_axes(\"Speed Radius (km)\")\nkwargs = dict(vmin=10, vmax=50, s=80, ref=-10, cmap=\"magma_r\", factor=0.001)\na.scatter(ax, \"radius_s\", **kwargs)\nm = c.scatter(\n ax, \"radius_s\", **kwargs\n)\nupdate_axes(ax, m)"
257257
]
258258
},
259259
{
@@ -271,7 +271,7 @@
271271
},
272272
"outputs": [],
273273
"source": [
274-
"ax = start_axes(\"Effective Radius (km)\")\nkwargs = dict(vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10)\na.filled(ax, \"effective_radius\", **kwargs)\nm = c.filled(\n ax, \"radius_e\", vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10\n)\nupdate_axes(ax, m)"
274+
"ax = start_axes(\"Effective Radius (km)\")\nkwargs = dict(vmin=10, vmax=80, cmap=\"magma_r\", factor=0.001, lut=14, ref=-10)\na.filled(ax, \"effective_radius\", **kwargs)\nm = c.filled(\n ax, \"radius_e\", **kwargs\n)\nupdate_axes(ax, m)"
275275
]
276276
}
277277
],

src/py_eddy_tracker/observations/tracking.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def split_network(self, intern=True, **kwargs):
619619
dtype=[
620620
("group", self.tracks.dtype),
621621
("time", self.time.dtype),
622-
("track", "u2"),
622+
("track", "u4"),
623623
("previous_cost", "f4"),
624624
("next_cost", "f4"),
625625
("previous_obs", "i4"),

src/py_eddy_tracker/poly.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from numba import njit, prange
99
from numba import types as numba_types
10-
from numpy import array, concatenate, empty, nan, ones, pi, where
10+
from numpy import arctan, array, concatenate, empty, nan, ones, pi, where
1111
from numpy.linalg import lstsq
1212
from Polygon import Polygon
1313

@@ -471,6 +471,7 @@ def polygon_overlap(p0, p1, minimal_area=False):
471471
return cost
472472

473473

474+
# FIXME: only one function are needed
474475
@njit(cache=True)
475476
def fit_circle(x, y):
476477
"""
@@ -517,6 +518,62 @@ def fit_circle(x, y):
517518
return x0, y0, radius, err
518519

519520

521+
@njit(cache=True)
522+
def fit_ellips(x, y):
523+
r"""
524+
From a polygon, function will fit an ellips.
525+
526+
Must be call with local coordinates (in m, to get a radius in m).
527+
528+
.. math:: (\frac{x - x_0}{a})^2 + (\frac{y - y_0}{b})^2 = 1
529+
530+
.. math:: (\frac{x^2 - 2 * x * x_0 + x_0 ^2}{a^2}) + \frac{y^2 - 2 * y * y_0 + y_0 ^2}{b^2}) = 1
531+
532+
In case of angle
533+
https://en.wikipedia.org/wiki/Ellipse
534+
535+
"""
536+
# x,y = x[1:],y[1:]
537+
nb = x.shape[0]
538+
datas = ones((nb, 5), dtype=x.dtype)
539+
datas[:, 0] = x ** 2
540+
datas[:, 1] = x * y
541+
datas[:, 2] = y ** 2
542+
datas[:, 3] = x
543+
datas[:, 4] = y
544+
(a, b, c, d, e), _, _, _ = lstsq(datas, ones(nb, dtype=x.dtype))
545+
det = b ** 2 - 4 * a * c
546+
if det > 0:
547+
print(det)
548+
x0 = (2 * c * d - b * e) / det
549+
y0 = (2 * a * e - b * d) / det
550+
551+
A = (
552+
-(
553+
(
554+
2
555+
* (a * e ** 2 + c * d ** 2 - b * d * e - det)
556+
* (a + c + ((a - c) ** 2 + b ** 2) ** 0.5)
557+
)
558+
** 0.5
559+
)
560+
/ det
561+
)
562+
B = (
563+
-(
564+
(
565+
2
566+
* (a * e ** 2 + c * d ** 2 - b * d * e - det)
567+
* (a + c - ((a - c) ** 2 + b ** 2) ** 0.5)
568+
)
569+
** 0.5
570+
)
571+
/ det
572+
)
573+
theta = arctan((c - a - ((a - c) ** 2 + b ** 2) ** 0.5) / b)
574+
return x0, y0, A, B, theta
575+
576+
520577
@njit(cache=True)
521578
def fit_circle_(x, y):
522579
r"""

0 commit comments

Comments
 (0)