1- from numpy import array , pi , roll
1+ import numpy as np
22from pytest import approx
33
44from py_eddy_tracker .poly import (
77 get_convex_hull ,
88 poly_area_vertice ,
99 visvalingam ,
10+ get_pixel_in_regular ,
1011)
12+ from py_eddy_tracker .generic import bbox_indice_regular
1113
1214# Vertices for next test
13- V = array (((2 , 2 , 3 , 3 , 2 ), (- 10 , - 9 , - 9 , - 10 , - 10 )))
14- V_concave = array (((2 , 2 , 2.5 , 3 , 3 , 2 ), (- 10 , - 9 , - 9.5 , - 9 , - 10 , - 10 )))
15+ V = np . array (((2 , 2 , 3 , 3 , 2 ), (- 10 , - 9 , - 9 , - 10 , - 10 )))
16+ V_concave = np . array (((2 , 2 , 2.5 , 3 , 3 , 2 ), (- 10 , - 9 , - 9.5 , - 9 , - 10 , - 10 )))
1517
1618
1719def test_poly_area ():
@@ -23,7 +25,7 @@ def test_fit_circle():
2325 assert x0 == approx (2.5 , rel = 1e-10 )
2426 assert y0 == approx (- 9.5 , rel = 1e-10 )
2527 assert r == approx (2 ** 0.5 / 2 , rel = 1e-10 )
26- assert err == approx ((1 - 2 / pi ) * 100 , rel = 1e-10 )
28+ assert err == approx ((1 - 2 / np . pi ) * 100 , rel = 1e-10 )
2729
2830
2931def test_convex ():
@@ -38,8 +40,8 @@ def test_convex_hull():
3840
3941
4042def test_visvalingam ():
41- x = array ([1 , 2 , 3 , 4 , 5 , 6.75 , 6 , 1 ])
42- y = array ([- 0.5 , - 1.5 , - 1 , - 1.75 , - 1 , - 1 , - 0.5 , - 0.5 ])
43+ x = np . array ([1 , 2 , 3 , 4 , 5 , 6.75 , 6 , 1 ])
44+ y = np . array ([- 0.5 , - 1.5 , - 1 , - 1.75 , - 1 , - 1 , - 0.5 , - 0.5 ])
4345 x_target = [1 , 2 , 3 , 4 , 6 , 1 ]
4446 y_target = [- 0.5 , - 1.5 , - 1 , - 1.75 , - 0.5 , - 0.5 ]
4547 x_ , y_ = visvalingam (x , y , 6 )
@@ -48,6 +50,46 @@ def test_visvalingam():
4850 x_ , y_ = visvalingam (x [:- 1 ], y [:- 1 ], 6 )
4951 assert (x_target == x_ ).all ()
5052 assert (y_target == y_ ).all ()
51- x_ , y_ = visvalingam (roll (x , 2 ), roll (y , 2 ), 6 )
53+ x_ , y_ = visvalingam (np . roll (x , 2 ), np . roll (y , 2 ), 6 )
5254 assert (x_target [:- 1 ] == x_ [1 :]).all ()
5355 assert (y_target [:- 1 ] == y_ [1 :]).all ()
56+
57+
58+ def test_pixel_in_contour ():
59+ xmin , xmax , ymin , ymax = - 1 , 31 , - 2 , 11
60+ v = np .array ([
61+ [xmin , ymax ],
62+ [xmax , ymax ],
63+ [xmax , ymin ],
64+ [xmin , ymin ],
65+ [xmin , ymax ],
66+ ])
67+ xstep = ystep = 7.5
68+ # Global grid
69+ for x0_ in [- 10 , 0 ]:
70+ x0 , y0 = (x0_ , x0_ + 360 ), (- 10 , 20 )
71+ x_c , y_c = np .arange (* x0 , xstep ), np .arange (* y0 , ystep )
72+ (x_start , x_stop ), (y_start , y_stop ) = bbox_indice_regular (v , x0 , y0 , xstep , ystep , 1 , True , int (360 / 5 ))
73+ i , j = get_pixel_in_regular (v , x_c , y_c , x_start , x_stop , y_start , y_stop )
74+ assert (x_c [i ] < xmax ).all (), f"{ x0 = } "
75+ assert (x_c [i ] > xmin ).all (), f"{ x0 = } "
76+
77+ # Regional grid
78+ # contour over two bounds
79+ x0 , y0 = (20 , 370 ), (- 10 , 20 )
80+ x_c , y_c = np .arange (* x0 , xstep ), np .arange (* y0 , ystep )
81+ (x_start , x_stop ), (y_start , y_stop ) = bbox_indice_regular (v , x0 , y0 , xstep , ystep , 1 , False , int (360 / 5 ))
82+ i , j = get_pixel_in_regular (v , x_c , y_c , x_start , x_stop , y_start , y_stop )
83+ assert (x_c [i ] < xmax ).all (), f"{ x0 = } "
84+ assert (x_c [i ] > xmin ).all (), f"{ x0 = } "
85+
86+ # first case grid fully cover contour, and not in second case
87+ for x0_ in [- 2 , - .5 ]:
88+ x0 , y0 = (x0_ , 100 ), (- 10 , 20 )
89+ x_c , y_c = np .arange (* x0 , xstep ), np .arange (* y0 , ystep )
90+ (x_start , x_stop ), (y_start , y_stop ) = bbox_indice_regular (v , x0 , y0 , xstep , ystep , 1 , False , int (360 / 5 ))
91+ i , j = get_pixel_in_regular (v , x_c , y_c , x_start , x_stop , y_start , y_stop )
92+ assert (x_c [i ] < xmax ).all (), f"{ x0 = } "
93+ assert (x_c [i ] > xmin ).all (), f"{ x0 = } "
94+ assert (y_c [j ] < ymax ).all (), f"{ y0 = } "
95+ assert (y_c [j ] > ymin ).all (), f"{ y0 = } "
0 commit comments