Skip to content

Commit 70ac81e

Browse files
author
adelepoulle
committed
Replace nearest_path func by nearest_path which bbox which contain pt to speed up process
1 parent 64f529f commit 70ac81e

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

src/py_eddy_tracker/property_functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ def get_uavg(eddy, contours, centlon_e, centlat_e, poly_eff, grd,
237237
corrected_coll_index = nb_coll - coll_index - 1
238238

239239
# Leave loop if no contours at level citer.index
240-
theindex = eddy.swirl.get_index_nearest_path(
240+
#~ theindex = eddy.swirl.get_index_nearest_path(
241+
theindex = eddy.swirl.get_index_nearest_path_bbox_contain_pt(
241242
corrected_coll_index, centlon_e, centlat_e)
242243
if theindex is None:
243244
continue

src/py_eddy_tracker/property_objects.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from scipy.ndimage import binary_erosion
3434
from scipy.ndimage import minimum_filter
3535
from numpy import array, isfinite, ma, where, ones
36-
from .tools import index_from_nearest_path, distance_matrix
36+
from .tools import index_from_nearest_path, \
37+
index_from_nearest_path_with_pt_in_bbox, distance_matrix
3738
import logging
3839

3940

@@ -224,28 +225,61 @@ class SwirlSpeed(object):
224225
grd:
225226
A grid object
226227
"""
227-
def __init__(self, contour):
228+
def __init__(self, contour, nearest_contain_in_bbox=False):
228229
"""
229230
c_i : index to contours
230231
l_i : index to levels
231232
"""
232233
x_list, y_list, ci_list, li_list = [], [], [], []
234+
x_min_list, y_min_list, x_max_list, y_max_list = [], [], [], []
233235

234236
for cont in contour.collections:
235237
for coll in cont.get_paths():
236-
x_list.append(coll.vertices[:, 0])
237-
y_list.append(coll.vertices[:, 1])
238-
ci_list.append(len(coll.vertices[:, 0]))
238+
x_val, y_val = coll.vertices[:, 0], coll.vertices[:, 1]
239+
x_min_list.append(x_val.min())
240+
x_max_list.append(x_val.max())
241+
y_min_list.append(y_val.min())
242+
y_max_list.append(y_val.max())
243+
x_list.append(x_val)
244+
y_list.append(y_val)
245+
ci_list.append(coll.vertices.shape[0])
239246
li_list.append(len(cont.get_paths()))
240247

241248
self.x_value = array([val for sublist in x_list for val in sublist])
242249
self.y_value = array([val for sublist in y_list for val in sublist])
250+
251+
self.x_min_per_c = array(x_min_list)
252+
self.y_min_per_c = array(y_min_list)
253+
self.x_max_per_c = array(x_max_list)
254+
self.y_max_per_c = array(y_max_list)
255+
243256
self.nb_pt_per_c = array(ci_list, dtype='u4')
244257
self.c_i = array(self.nb_pt_per_c.cumsum() - self.nb_pt_per_c,
245258
dtype='u4')
246259
self.nb_c_per_l = array(li_list, dtype='u4')
247260
self.l_i = array(self.nb_c_per_l.cumsum() - self.nb_c_per_l,
248261
dtype='u4')
262+
263+
self.nearest_contain_in_bbox = nearest_contain_in_bbox
264+
265+
def get_index_nearest_path_bbox_contain_pt(self, level, xpt, ypt):
266+
"""
267+
"""
268+
return index_from_nearest_path_with_pt_in_bbox(
269+
level,
270+
self.l_i,
271+
self.nb_c_per_l,
272+
self.nb_pt_per_c,
273+
self.c_i,
274+
self.x_value,
275+
self.y_value,
276+
self.x_min_per_c,
277+
self.y_min_per_c,
278+
self.x_max_per_c,
279+
self.y_max_per_c,
280+
xpt,
281+
ypt
282+
)
249283

250284
def get_index_nearest_path(self, level, xpt, ypt):
251285
"""

src/py_eddy_tracker/tools.pyx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cdef DTYPE_coord D2R = 0.017453292519943295
1515
cdef DTYPE_coord PI = 3.141592653589793
1616
cdef DTYPE_coord EARTH_DIAMETER = 6371.3150 * 2
1717

18+
1819
@wraparound(False)
1920
@boundscheck(False)
2021
def fit_circle_c(
@@ -200,6 +201,7 @@ def winding_number_poly(
200201
wn -= 1
201202
return wn
202203

204+
203205
@wraparound(False)
204206
@boundscheck(False)
205207
def distance(
@@ -284,9 +286,8 @@ cdef dist_array_size(
284286
):
285287
"""Give slice to select data
286288
"""
287-
cdef DTYPE_ui i_elt, nb_pts
289+
cdef DTYPE_ui i_elt
288290
i_end[0] = i_start + nb_c_per_l
289-
nb_pts = 0
290291

291292
c_start[0] = c_i[i_start]
292293
c_end[0] = c_start[0]
@@ -370,3 +371,53 @@ cdef nearest_contour_index(
370371
dist_ref = dist
371372
i_ref = i_elt
372373
nearesti[0] = i_ref - start
374+
375+
376+
@wraparound(False)
377+
@boundscheck(False)
378+
def index_from_nearest_path_with_pt_in_bbox(
379+
DTYPE_ui level_index,
380+
ndarray[DTYPE_ui] l_i,
381+
ndarray[DTYPE_ui] nb_c_per_l,
382+
ndarray[DTYPE_ui] nb_pt_per_c,
383+
ndarray[DTYPE_ui] indices_of_first_pts,
384+
ndarray[DTYPE_coord] x_value,
385+
ndarray[DTYPE_coord] y_value,
386+
ndarray[DTYPE_coord] x_min_per_c,
387+
ndarray[DTYPE_coord] y_min_per_c,
388+
ndarray[DTYPE_coord] x_max_per_c,
389+
ndarray[DTYPE_coord] y_max_per_c,
390+
DTYPE_coord xpt,
391+
DTYPE_coord ypt,
392+
):
393+
"""Get index from nearest path in edge bbox contain pt
394+
"""
395+
cdef DTYPE_ui i_start_c, i_end_c, i_elt_c, i_start_pt, i_end_pt, i_elt_pt
396+
cdef DTYPE_ui i_ref, nb_contour, find_contour
397+
cdef DTYPE_coord dist_ref, dist
398+
find_contour = 0
399+
i_start_c = l_i[level_index]
400+
nb_contour = nb_c_per_l[level_index]
401+
if nb_contour == 0:
402+
return None
403+
i_end_c = i_start_c + nb_c_per_l[level_index]
404+
405+
i_ref = i_start_c
406+
i_start_pt = indices_of_first_pts[i_start_c]
407+
dist_ref = (x_value[i_start_pt] - xpt) ** 2 + (y_value[i_start_pt] - ypt) ** 2
408+
409+
for i_elt_c from i_start_c <= i_elt_c < i_end_c:
410+
if x_min_per_c[i_elt_c] > xpt or x_max_per_c[i_elt_c] < xpt or y_min_per_c[i_elt_c] > ypt or y_max_per_c[i_elt_c] < ypt:
411+
continue
412+
i_start_pt = indices_of_first_pts[i_elt_c]
413+
i_end_pt = i_start_pt + nb_pt_per_c[i_elt_c]
414+
find_contour = 1
415+
416+
for i_elt_pt from i_start_pt <= i_elt_pt < i_end_pt:
417+
dist = (x_value[i_elt_pt] - xpt) ** 2 + (y_value[i_elt_pt] - ypt) ** 2
418+
if dist < dist_ref:
419+
dist_ref = dist
420+
i_ref = i_elt_c
421+
if find_contour == 0:
422+
return None
423+
return i_ref - i_start_c

0 commit comments

Comments
 (0)