Skip to content

Commit 32f6b36

Browse files
committed
Use of mergesort to save time when array is alreay ordered
1 parent 1f5863a commit 32f6b36

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

examples/16_network/pet_follow_particle.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from py_eddy_tracker.data import get_path
1818
from py_eddy_tracker.dataset.grid import GridCollection
1919
from py_eddy_tracker.observations.network import NetworkObservations
20+
from py_eddy_tracker.poly import group_obs
2021

2122
start_logger().setLevel("ERROR")
2223

@@ -203,8 +204,12 @@ def get_matrix(i_start, i_end, translate_start, translate_end, i_target, pct):
203204
# ^^^^^^^^^^^^^^^^^^
204205
step = 1 / 60.0
205206

206-
x, y = meshgrid(arange(20, 36, step), arange(30, 46, step))
207+
x, y = meshgrid(arange(24, 36, step), arange(31, 36, step))
207208
x0, y0 = x.reshape(-1), y.reshape(-1)
209+
# Pre-order to speed up
210+
_, i = group_obs(x0, y0, 1, 360)
211+
x0, y0 = x0[i], y0[i]
212+
208213
t_start, t_end = n.period
209214
dt = 14
210215

notebooks/python_module/16_network/pet_follow_particle.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"import re\n\nfrom matplotlib import colors\nfrom matplotlib import pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom numba import njit\nfrom numba import types as nb_types\nfrom numpy import arange, meshgrid, ones, unique, where, zeros\n\nfrom py_eddy_tracker import start_logger\nfrom py_eddy_tracker.appli.gui import Anim\nfrom py_eddy_tracker.data import get_path\nfrom py_eddy_tracker.dataset.grid import GridCollection\nfrom py_eddy_tracker.observations.network import NetworkObservations\n\nstart_logger().setLevel(\"ERROR\")"
29+
"import re\n\nfrom matplotlib import colors\nfrom matplotlib import pyplot as plt\nfrom matplotlib.animation import FuncAnimation\nfrom numba import njit\nfrom numba import types as nb_types\nfrom numpy import arange, meshgrid, ones, unique, where, zeros\n\nfrom py_eddy_tracker import start_logger\nfrom py_eddy_tracker.appli.gui import Anim\nfrom py_eddy_tracker.data import get_path\nfrom py_eddy_tracker.dataset.grid import GridCollection\nfrom py_eddy_tracker.observations.network import NetworkObservations\nfrom py_eddy_tracker.poly import group_obs\n\nstart_logger().setLevel(\"ERROR\")"
3030
]
3131
},
3232
{
@@ -138,7 +138,7 @@
138138
},
139139
"outputs": [],
140140
"source": [
141-
"step = 1 / 60.0\n\nx, y = meshgrid(arange(20, 36, step), arange(30, 46, step))\nx0, y0 = x.reshape(-1), y.reshape(-1)\nt_start, t_end = n.period\ndt = 14\n\nshape = (n.obs.size, 2)\n# Forward run\ni_target_f, pct_target_f = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start, t_end - dt):\n particle_candidate(x0, y0, c, n, t, i_target_f, pct_target_f, delta_t=dt)\n\n# Backward run\ni_target_b, pct_target_b = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start + dt, t_end):\n particle_candidate(x0, y0, c, n, t, i_target_b, pct_target_b, delta_t=-dt)"
141+
"step = 1 / 60.0\n\nx, y = meshgrid(arange(24, 36, step), arange(31, 36, step))\nx0, y0 = x.reshape(-1), y.reshape(-1)\n# Pre-order to speed up\n_, i = group_obs(x0, y0, 1, 360)\nx0, y0 = x0[i], y0[i]\n\nt_start, t_end = n.period\ndt = 14\n\nshape = (n.obs.size, 2)\n# Forward run\ni_target_f, pct_target_f = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start, t_end - dt):\n particle_candidate(x0, y0, c, n, t, i_target_f, pct_target_f, delta_t=dt)\n\n# Backward run\ni_target_b, pct_target_b = -ones(shape, dtype=\"i4\"), zeros(shape, dtype=\"i1\")\nfor t in range(t_start + dt, t_end):\n particle_candidate(x0, y0, c, n, t, i_target_b, pct_target_b, delta_t=-dt)"
142142
]
143143
},
144144
{

src/py_eddy_tracker/poly.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ def group_obs(x, y, step, nb_x):
799799
i = empty(nb, dtype=numba_types.uint32)
800800
for k in range(nb):
801801
i[k] = box_index(x[k], y[k], step, nb_x)
802-
return i, i.argsort()
802+
return i, i.argsort(kind="mergesort")
803803

804804

805805
@njit(cache=True)
@@ -824,7 +824,9 @@ def poly_indexs(x_p, y_p, x_c, y_c):
824824
:param array x_c: longitude of contours
825825
:param array y_c: latitude of contours
826826
"""
827-
i, i_order = group_obs(x_p, y_p, 1, 360)
827+
nb_x = 360
828+
step = 1.0
829+
i, i_order = group_obs(x_p, y_p, step, nb_x)
828830
nb_p = x_p.shape[0]
829831
nb_c = x_c.shape[0]
830832
indexs = -ones(nb_p, dtype=numba_types.int32)
@@ -837,17 +839,17 @@ def poly_indexs(x_p, y_p, x_c, y_c):
837839
x_c_min, y_c_min = x_.min(), y_.min()
838840
x_c_max, y_c_max = x_.max(), y_.max()
839841
v = create_vertice(x_, y_)
840-
i0, j0 = box_indexes(x_c_min, y_c_min, 1)
841-
i1, j1 = box_indexes(x_c_max, y_c_max, 1)
842+
i0, j0 = box_indexes(x_c_min, y_c_min, step)
843+
i1, j1 = box_indexes(x_c_max, y_c_max, step)
842844
# i0 could be greater than i1, (x_c is always continious) so you could have a contour over bound
843845
if i0 > i1:
844-
i1 += 360
846+
i1 += nb_x
845847
for i_x in range(i0, i1 + 1):
846848
# we force i_x in 0 360 range
847-
i_x %= 360
849+
i_x %= nb_x
848850
for i_y in range(j0, j1 + 1):
849851
# Get box indices
850-
i_box = i_x + 360 * i_y - i_first
852+
i_box = i_x + nb_x * i_y - i_first
851853
# Indice must be in table range
852854
if i_box < 0 or i_box >= nb_bloc:
853855
continue
@@ -860,6 +862,7 @@ def poly_indexs(x_p, y_p, x_c, y_c):
860862
continue
861863
if y < y_c_min:
862864
continue
865+
# Normalize longitude at +-180° around x_c_min
863866
x = (x_p[i_p] - x_c_min + 180) % 360 + x_c_min - 180
864867
if x > x_c_max:
865868
continue

0 commit comments

Comments
 (0)