Skip to content

Commit f617718

Browse files
committed
add docstring in numba function
1 parent 302a2c2 commit f617718

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

src/py_eddy_tracker/generic.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
@njit(cache=True)
4949
def reverse_index(index, nb):
5050
"""
51+
Compute a list of index, which are not in index.
52+
5153
:param array index: index of group which will be set to False
5254
:param array nb: Count for each group
5355
:return: mask of value selected
@@ -61,7 +63,7 @@ def reverse_index(index, nb):
6163

6264
@njit(cache=True)
6365
def build_index(groups):
64-
"""We expected that variable is monotonous, and return index for each step change
66+
"""We expected that variable is monotonous, and return index for each step change.
6567
6668
:param array groups: array which contain group to be separated
6769
:return: (first_index of each group, last_index of each group, value to shift group)
@@ -85,12 +87,15 @@ def build_index(groups):
8587

8688
@njit(cache=True)
8789
def hist_numba(x, bins):
90+
"""Call numba histogram to speed up."""
8891
return histogram(x, bins)
8992

9093

9194
@njit(cache=True, fastmath=True, parallel=False)
9295
def distance_grid(lon0, lat0, lon1, lat1):
9396
"""
97+
Get distance for every couple of point.
98+
9499
:param array lon0:
95100
:param array lat0:
96101
:param array lon1:
@@ -127,7 +132,7 @@ def distance_grid(lon0, lat0, lon1, lat1):
127132
@njit(cache=True, fastmath=True)
128133
def distance(lon0, lat0, lon1, lat1):
129134
"""
130-
Compute distance between points from each line
135+
Compute distance between points from each line.
131136
132137
:param float lon0:
133138
:param float lat0:
@@ -148,7 +153,7 @@ def distance(lon0, lat0, lon1, lat1):
148153
@njit(cache=True)
149154
def cumsum_by_track(field, track):
150155
"""
151-
Cumsum by track
156+
Cumsum by track.
152157
153158
:param array field: data to sum
154159
:pram array(int) track: id of track to separate data
@@ -172,7 +177,7 @@ def cumsum_by_track(field, track):
172177
@njit(cache=True, fastmath=True)
173178
def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
174179
"""
175-
For geographic grid, test of cicularity
180+
For geographic grid, test of cicularity.
176181
177182
:param array x_g: coordinates of grid
178183
:param array y_g: coordinates of grid
@@ -189,6 +194,7 @@ def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
189194
x_step = x_g[1] - x_ref
190195
y_step = y_g[1] - y_ref
191196
nb_x = x_g.shape[0]
197+
nb_y = y_g.shape[0]
192198
is_circular = (x_g[-1] + x_step) % 360 == x_g[0] % 360
193199
z = empty(x.shape, dtype=z_g.dtype)
194200
for i in prange(x.size):
@@ -197,11 +203,16 @@ def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
197203
i0 = int(floor(x_))
198204
i1 = i0 + 1
199205
xd = x_ - i0
206+
j0 = int(floor(y_))
207+
j1 = j0 + 1
200208
if is_circular:
201209
i0 %= nb_x
202210
i1 %= nb_x
203-
j0 = int(floor(y_))
204-
j1 = j0 + 1
211+
else:
212+
if i1 >= nb_x or i0 < 0 or j0 < 0 or j1 >= nb_y:
213+
z[i] = nan
214+
continue
215+
205216
yd = y_ - j0
206217
z00 = z_g[i0, j0]
207218
z01 = z_g[i0, j1]
@@ -219,7 +230,7 @@ def interp2d_geo(x_g, y_g, z_g, m_g, x, y):
219230
@njit(cache=True, fastmath=True)
220231
def uniform_resample(x_val, y_val, num_fac=2, fixed_size=None):
221232
"""
222-
Resample contours to have (nearly) equal spacing
233+
Resample contours to have (nearly) equal spacing.
223234
224235
:param array_like x_val: input x contour coordinates
225236
:param array_like y_val: input y contour coordinates
@@ -246,7 +257,7 @@ def uniform_resample(x_val, y_val, num_fac=2, fixed_size=None):
246257
@njit(cache=True)
247258
def flatten_line_matrix(l_matrix):
248259
"""
249-
Flat matrix and add on between each line
260+
Flat matrix and add on between each line.
250261
251262
:param l_matrix: matrix of position
252263
:return: array with nan between line
@@ -267,7 +278,7 @@ def flatten_line_matrix(l_matrix):
267278
@njit(cache=True)
268279
def simplify(x, y, precision=0.1):
269280
"""
270-
Will remove all middle point which are closer than precision
281+
Will remove all middle point which are closer than precision.
271282
272283
:param array x:
273284
:param array y:
@@ -307,7 +318,7 @@ def simplify(x, y, precision=0.1):
307318
@njit(cache=True)
308319
def split_line(x, y, i):
309320
"""
310-
Split x and y at each i change
321+
Split x and y at each i change.
311322
312323
:param x: array
313324
:param y: array
@@ -335,7 +346,7 @@ def split_line(x, y, i):
335346
@njit(cache=True)
336347
def wrap_longitude(x, y, ref, cut=False):
337348
"""
338-
Will wrap contiguous longitude with reference like west bound
349+
Will wrap contiguous longitude with reference like west bound.
339350
340351
:param array x:
341352
:param array y:
@@ -391,7 +402,7 @@ def wrap_longitude(x, y, ref, cut=False):
391402
@njit(cache=True, fastmath=True)
392403
def coordinates_to_local(lon, lat, lon0, lat0):
393404
"""
394-
Take latlong coordinates to transform in local coordinates (in m)
405+
Take latlong coordinates to transform in local coordinates (in m).
395406
396407
:param array x: coordinates to transform
397408
:param array y: coordinates to transform
@@ -420,7 +431,7 @@ def coordinates_to_local(lon, lat, lon0, lat0):
420431
@njit(cache=True, fastmath=True)
421432
def local_to_coordinates(x, y, lon0, lat0):
422433
"""
423-
Take local coordinates (in m) to transform to latlong
434+
Take local coordinates (in m) to transform to latlong.
424435
425436
:param array x: coordinates to transform
426437
:param array y: coordinates to transform
@@ -447,7 +458,7 @@ def local_to_coordinates(x, y, lon0, lat0):
447458
@njit(cache=True, fastmath=True)
448459
def nearest_grd_indice(x, y, x0, y0, xstep, ystep):
449460
"""
450-
Get nearest grid indice from a position
461+
Get nearest grid indice from a position.
451462
452463
:param x: longitude
453464
:param y: latitude
@@ -465,7 +476,7 @@ def nearest_grd_indice(x, y, x0, y0, xstep, ystep):
465476
@njit(cache=True)
466477
def bbox_indice_regular(vertices, x0, y0, xstep, ystep, N, circular, x_size):
467478
"""
468-
Get bbox indice of a contour in a regular grid
479+
Get bbox indice of a contour in a regular grid.
469480
470481
:param vertices: vertice of contour
471482
:param float x0: first grid longitude
@@ -492,7 +503,7 @@ def bbox_indice_regular(vertices, x0, y0, xstep, ystep, N, circular, x_size):
492503
@njit(cache=True, fastmath=True)
493504
def get_pixel_in_regular(vertices, x_c, y_c, x_start, x_stop, y_start, y_stop):
494505
"""
495-
Get a pixel list of a regular grid contain in a contour
506+
Get a pixel list of a regular grid contain in a contour.
496507
497508
:param array_like vertices: contour vertice (N,2)
498509
:param array_like x_c: longitude coordinate of grid
@@ -532,7 +543,7 @@ def get_pixel_in_regular(vertices, x_c, y_c, x_start, x_stop, y_start, y_stop):
532543

533544
def build_circle(x0, y0, r):
534545
"""
535-
Method to built circle from center coordinates
546+
Build circle from center coordinates.
536547
537548
:param float x0: center coordinate
538549
:param float y0: center coordinate

src/py_eddy_tracker/poly.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
@njit(cache=True)
3030
def is_left(x_line_0, y_line_0, x_line_1, y_line_1, x_test, y_test):
3131
"""
32+
Test if point is left of an infinit line.
33+
3234
http://geomalgorithms.com/a03-_inclusion.html
33-
isLeft(): tests if a point is Left|On|Right of an infinite line.
3435
See: Algorithm 1 "Area of Triangles and Polygons"
3536
3637
:param float x_line_0:
@@ -55,6 +56,8 @@ def is_left(x_line_0, y_line_0, x_line_1, y_line_1, x_test, y_test):
5556
@njit(cache=True)
5657
def poly_contain_poly(xy_poly_out, xy_poly_in):
5758
"""
59+
Check if poly_in is include in poly_out.
60+
5861
:param vertice xy_poly_out:
5962
:param vertice xy_poly_in:
6063
:return: True if poly_in is in poly_out
@@ -76,6 +79,8 @@ def poly_contain_poly(xy_poly_out, xy_poly_in):
7679
@njit(cache=True)
7780
def poly_area_vertice(v):
7881
"""
82+
Compute area from vertice.
83+
7984
:param vertice v: polygon vertice
8085
:return: area of polygon in coordinates unit
8186
:rtype: float
@@ -86,7 +91,7 @@ def poly_area_vertice(v):
8691
@njit(cache=True)
8792
def poly_area(x, y):
8893
"""
89-
Must be call with local coordinates (in m, to get an area in m²)
94+
Must be call with local coordinates (in m, to get an area in m²).
9095
9196
:param array x:
9297
:param array y:
@@ -103,7 +108,7 @@ def poly_area(x, y):
103108
@njit(cache=True)
104109
def winding_number_poly(x, y, xy_poly):
105110
"""
106-
Check if x,y is in poly
111+
Check if x,y is in poly.
107112
108113
:param float x: x to test
109114
:param float y: y to test
@@ -137,6 +142,8 @@ def winding_number_poly(x, y, xy_poly):
137142
@njit(cache=True)
138143
def winding_number_grid_in_poly(x_1d, y_1d, i_x0, i_x1, x_size, i_y0, xy_poly):
139144
"""
145+
Return index for each grid coordinates within contour.
146+
140147
http://geomalgorithms.com/a03-_inclusion.html
141148
142149
:param array x_1d: x of local grid
@@ -167,7 +174,7 @@ def winding_number_grid_in_poly(x_1d, y_1d, i_x0, i_x1, x_size, i_y0, xy_poly):
167174
@njit(cache=True, fastmath=True)
168175
def bbox_intersection(x0, y0, x1, y1):
169176
"""
170-
Compute bbox to check if there are a bbox intersection
177+
Compute bbox to check if there are a bbox intersection.
171178
172179
:param array x0: x for polygon list 0
173180
:param array y0: y for polygon list 0
@@ -207,9 +214,11 @@ def bbox_intersection(x0, y0, x1, y1):
207214
@njit(cache=True)
208215
def create_vertice(x, y):
209216
"""
217+
Return polygon vertice.
218+
210219
:param array x:
211220
:param array y:
212-
:return: Return the vertice of polygon
221+
:return: Return polygon vertice
213222
:rtype: vertice
214223
"""
215224
nb = x.shape[0]
@@ -223,7 +232,7 @@ def create_vertice(x, y):
223232
@njit(cache=True)
224233
def create_vertice_from_2darray(x, y, index):
225234
"""
226-
Choose a polygon in x,y list and return vertice
235+
Choose a polygon in x,y list and return vertice.
227236
228237
:param array x:
229238
:param array y:
@@ -242,7 +251,7 @@ def create_vertice_from_2darray(x, y, index):
242251
@njit(cache=True)
243252
def get_wrap_vertice(x0, y0, x1, y1, i):
244253
"""
245-
Return a vertice for each polygon and check that use same reference coordinates
254+
Return a vertice for each polygon and check that use same reference coordinates.
246255
247256
:param array x0: x for polygon list 0
248257
:param array y0: y for polygon list 0
@@ -261,6 +270,8 @@ def get_wrap_vertice(x0, y0, x1, y1, i):
261270

262271
def vertice_overlap(x0, y0, x1, y1, minimal_area=False):
263272
"""
273+
Return percent of overlap for each item.
274+
264275
:param array x0: x for polygon list 0
265276
:param array y0: y for polygon list 0
266277
:param array x1: x for polygon list 1
@@ -289,6 +300,8 @@ def vertice_overlap(x0, y0, x1, y1, minimal_area=False):
289300

290301
def polygon_overlap(p0, p1, minimal_area=False):
291302
"""
303+
Return percent of overlap for each item.
304+
292305
:param list(Polygon) p0: List of polygon to compare with p1 list
293306
:param list(Polygon) p1: List of polygon to compare with p0 list
294307
:param bool minimal_area: If True, function will compute intersection/little polygon, else intersection/union
@@ -313,8 +326,9 @@ def polygon_overlap(p0, p1, minimal_area=False):
313326
@njit(cache=True)
314327
def fit_circle(x, y):
315328
"""
316-
From a polygon, function will fit a circle
317-
Must be call with local coordinates (in m, to get a radius in m)
329+
From a polygon, function will fit a circle.
330+
331+
Must be call with local coordinates (in m, to get a radius in m).
318332
319333
:param array x: x of polygon
320334
:param array y: y of polygon
@@ -357,7 +371,7 @@ def fit_circle(x, y):
357371

358372
@njit(cache=True)
359373
def fit_circle_(x, y):
360-
"""
374+
r"""
361375
From a polygon, function will fit a circle.
362376
363377
Must be call with local coordinates (in m, to get a radius in m).
@@ -377,7 +391,7 @@ def fit_circle_(x, y):
377391
378392
Solutions:
379393
380-
.. math:: x_0 = a / 2 , y_0 = b / 2 , r = \\sqrt{c + x_0^2 + y_0^2}
394+
.. math:: x_0 = a / 2 , y_0 = b / 2 , r = \sqrt{c + x_0^2 + y_0^2}
381395
382396
383397
:param array x: x of polygon
@@ -415,11 +429,12 @@ def fit_circle_(x, y):
415429

416430
@njit(cache=True, fastmath=True)
417431
def shape_error(x, y, x0, y0, r):
418-
"""
419-
With a polygon(x,y) in local coordinates
432+
r"""
433+
With a polygon(x,y) in local coordinates.
434+
420435
and circle properties(x0, y0, r), function compute a shape error:
421436
422-
.. math:: ShapeError = \\frac{Polygon_{area} + Circle_{area} - 2 * Intersection_{area}}{Circle_{area}} * 100
437+
.. math:: ShapeError = \frac{Polygon_{area} + Circle_{area} - 2 * Intersection_{area}}{Circle_{area}} * 100
423438
424439
When error > 100, area of difference is bigger than circle area
425440

0 commit comments

Comments
 (0)