-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest_nearest_point_to.py
225 lines (209 loc) · 6.31 KB
/
test_nearest_point_to.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from typing import Any
import numpy as np
import pytest
import xarray as xr
from movement.roi.base import BaseRegionOfInterest
from movement.roi.line import LineOfInterest
@pytest.fixture
def points_of_interest() -> dict[str, np.ndarray]:
return xr.DataArray(
np.array(
[
[-0.5, 0.50],
[0.00, 0.50],
[0.40, 0.45],
[2.00, 1.00],
[0.40, 0.75],
[0.95, 0.90],
[0.80, 0.76],
]
),
dims=["time", "space"],
coords={"space": ["x", "y"]},
)
@pytest.fixture()
def unit_line_in_x() -> LineOfInterest:
return LineOfInterest([[0.0, 0.0], [1.0, 0.0]])
@pytest.mark.parametrize(
["region", "other_fn_args", "expected_output"],
[
pytest.param(
"unit_square",
{"boundary": True},
np.array(
[
[0.00, 0.50],
[0.00, 0.50],
[0.00, 0.45],
[1.00, 1.00],
[0.40, 1.00],
[1.00, 0.90],
[1.00, 0.76],
]
),
id="Unit square, boundary only",
),
pytest.param(
"unit_square",
{},
np.array(
[
[0.00, 0.50],
[0.00, 0.50],
[0.40, 0.45],
[1.00, 1.00],
[0.40, 0.75],
[0.95, 0.90],
[0.80, 0.76],
]
),
id="Unit square, whole region",
),
pytest.param(
"unit_square_with_hole",
{"boundary": True},
np.array(
[
[0.00, 0.50],
[0.00, 0.50],
[0.25, 0.45],
[1.00, 1.00],
[0.40, 0.75],
[1.00, 0.90],
[0.75, 0.75],
]
),
id="Unit square w/ hole, boundary only",
),
pytest.param(
"unit_square_with_hole",
{},
np.array(
[
[0.00, 0.50],
[0.00, 0.50],
[0.25, 0.45],
[1.00, 1.00],
[0.40, 0.75],
[0.95, 0.90],
[0.80, 0.76],
]
),
id="Unit square w/ hole, whole region",
),
pytest.param(
"unit_line_in_x",
{},
np.array(
[
[0.00, 0.00],
[0.00, 0.00],
[0.40, 0.00],
[1.00, 0.00],
[0.40, 0.00],
[0.95, 0.00],
[0.80, 0.00],
]
),
id="Line, whole region",
),
pytest.param(
"unit_line_in_x",
{"boundary": True},
np.array(
[
[0.00, 0.00],
[0.00, 0.00],
[0.00, 0.00],
[1.00, 0.00],
[0.00, 0.00],
[1.00, 0.00],
[1.00, 0.00],
]
),
id="Line, boundary only",
),
],
)
def test_nearest_point_to(
region: BaseRegionOfInterest,
points_of_interest: xr.DataArray,
other_fn_args: dict[str, Any],
expected_output: xr.DataArray,
request,
) -> None:
if isinstance(region, str):
region = request.getfixturevalue(region)
if isinstance(points_of_interest, str):
points_of_interest = request.getfixturevalue(points_of_interest)
if isinstance(expected_output, str):
expected_output = request.get(expected_output)
elif isinstance(expected_output, np.ndarray):
expected_output = xr.DataArray(
expected_output,
dims=["time", "nearest point"],
)
points_of_interest = points_of_interest
nearest_points = region.nearest_point_to(
points_of_interest, **other_fn_args
)
xr.testing.assert_allclose(nearest_points, expected_output)
@pytest.mark.parametrize(
["region", "position", "fn_kwargs", "possible_nearest_points"],
[
pytest.param(
"unit_square",
[0.5, 0.5],
{"boundary": True},
[
np.array([0.0, 0.5]),
np.array([0.5, 0.0]),
np.array([1.0, 0.5]),
np.array([0.5, 1.0]),
],
id="Centre of the unit square",
),
pytest.param(
"unit_line_in_x",
[0.5, 0.0],
{"boundary": True},
[
np.array([0.0, 0.0]),
np.array([1.0, 0.0]),
],
id="Boundary of a line",
),
],
)
def test_tie_breaks(
region: BaseRegionOfInterest,
position: np.ndarray,
fn_kwargs: dict[str, Any],
possible_nearest_points: list[np.ndarray],
request,
) -> None:
"""Check behaviour when points are tied for nearest.
This can only occur when we have a Polygonal region, or a multi-line 1D
region. In this case, there may be multiple points in the region of
interest that are tied for closest. ``shapely`` does not actually document
how it breaks ties here, but we can at least check that it identifies one
of the possible correct points.
"""
if isinstance(region, str):
region = request.getfixturevalue(region)
if not isinstance(position, np.ndarray | xr.DataArray):
position = np.array(position)
nearest_point_found = region.nearest_point_to(position, **fn_kwargs)
sq_dist_to_nearest_pt = np.sum((nearest_point_found - position) ** 2)
n_matches = 0
for possibility in possible_nearest_points:
# All possibilities should be approximately the same distance away
# from the position
assert np.isclose(
np.sum((possibility - position) ** 2), sq_dist_to_nearest_pt
)
# We should match at least one possibility,
# track to see if we do.
if np.isclose(nearest_point_found, possibility).all():
n_matches += 1
assert n_matches == 1