Skip to content

Commit cffc8f1

Browse files
committed
Add test and update to simplify function
1 parent 3e255fb commit cffc8f1

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/py_eddy_tracker/generic.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def flatten_line_matrix(l_matrix):
293293
@njit(cache=True)
294294
def simplify(x, y, precision=0.1):
295295
"""
296-
Will remove all middle point which are closer than precision.
296+
Will remove all middle/end point which are closer than precision.
297297
298298
:param array x:
299299
:param array y:
@@ -303,9 +303,19 @@ def simplify(x, y, precision=0.1):
303303
"""
304304
precision2 = precision ** 2
305305
nb = x.shape[0]
306-
x_previous, y_previous = x[0], y[0]
306+
# will be True for value keep
307307
mask = ones(nb, dtype=bool_)
308-
for i in range(1, nb):
308+
for j in range(0, nb):
309+
x_previous, y_previous = x[j], y[j]
310+
if isnan(x_previous) or isnan(y_previous):
311+
mask[j] = False
312+
continue
313+
break
314+
# Only nan
315+
if j == (nb - 1):
316+
return zeros(0, dtype=x.dtype), zeros(0, dtype=x.dtype)
317+
318+
for i in range(j + 1, nb):
309319
x_, y_ = x[i], y[i]
310320
if isnan(x_) or isnan(y_):
311321
continue

tests/test_generic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from numpy import arange, nan, zeros
2+
3+
from py_eddy_tracker.generic import simplify
4+
5+
6+
def test_simplify():
7+
x = arange(10, dtype="f4")
8+
y = zeros(10, dtype="f4")
9+
# Will jump one value on two
10+
x_, y_ = simplify(x, y, precision=1)
11+
assert x_.shape[0] == 5
12+
x_, y_ = simplify(x, y, precision=0.99)
13+
assert x_.shape[0] == 10
14+
# check nan management
15+
x[4] = nan
16+
x_, y_ = simplify(x, y, precision=1)
17+
assert x_.shape[0] == 6
18+
x[:4] = nan
19+
x_, y_ = simplify(x, y, precision=1)
20+
assert x_.shape[0] == 3
21+
x[:] = nan
22+
x_, y_ = simplify(x, y, precision=1)
23+
assert x_.shape[0] == 0

0 commit comments

Comments
 (0)