9
9
import rasterio as rio
10
10
11
11
import geoutils as gu
12
- from geoutils .raster .array import (
13
- _get_array_and_mask ,
14
- _get_valid_extent ,
15
- _get_xy_rotated ,
16
- )
12
+ from geoutils .raster .array import get_array_and_mask , get_valid_extent , get_xy_rotated
17
13
18
14
19
15
class TestArray :
@@ -59,13 +55,13 @@ def test_get_array_and_mask(
59
55
# Validate that incorrect shapes raise the correct error.
60
56
if not check_should_pass :
61
57
with pytest .raises (ValueError , match = "Invalid array shape given" ):
62
- _get_array_and_mask (array , check_shape = True )
58
+ get_array_and_mask (array , check_shape = True )
63
59
64
60
# Stop the test here as the failure is now validated.
65
61
return
66
62
67
63
# Get a copy of the array and check its shape (it should always pass at this point)
68
- arr , _ = _get_array_and_mask (array , copy = True , check_shape = True )
64
+ arr , _ = get_array_and_mask (array , copy = True , check_shape = True )
69
65
70
66
# Validate that the array is a copy
71
67
assert not np .shares_memory (arr , array )
@@ -82,7 +78,7 @@ def test_get_array_and_mask(
82
78
warnings .simplefilter ("always" )
83
79
84
80
# Try to create a view.
85
- arr_view , mask = _get_array_and_mask (array , copy = False )
81
+ arr_view , mask = get_array_and_mask (array , copy = False )
86
82
87
83
# If it should be possible, validate that there were no warnings.
88
84
if view_should_be_possible :
@@ -108,36 +104,36 @@ def test_get_valid_extent(self) -> None:
108
104
109
105
# For no invalid values, the function should return the edges
110
106
# For the array
111
- assert (0 , 4 , 0 , 4 ) == _get_valid_extent (arr )
107
+ assert (0 , 4 , 0 , 4 ) == get_valid_extent (arr )
112
108
# For the masked-array
113
- assert (0 , 4 , 0 , 4 ) == _get_valid_extent (mask_ma )
109
+ assert (0 , 4 , 0 , 4 ) == get_valid_extent (mask_ma )
114
110
115
111
# 1/ First column:
116
112
# If we mask it in the masked array
117
113
mask_ma [0 , :] = np .ma .masked
118
- assert (1 , 4 , 0 , 4 ) == _get_valid_extent (mask_ma )
114
+ assert (1 , 4 , 0 , 4 ) == get_valid_extent (mask_ma )
119
115
120
116
# If we changed the array to NaNs
121
117
arr [0 , :] = np .nan
122
- assert (1 , 4 , 0 , 4 ) == _get_valid_extent (arr )
118
+ assert (1 , 4 , 0 , 4 ) == get_valid_extent (arr )
123
119
mask_ma .data [0 , :] = np .nan
124
120
mask_ma .mask = False
125
- assert (1 , 4 , 0 , 4 ) == _get_valid_extent (mask_ma )
121
+ assert (1 , 4 , 0 , 4 ) == get_valid_extent (mask_ma )
126
122
127
123
# 2/ First row:
128
124
arr = np .ones (shape = (5 , 5 ))
129
125
arr_mask = np .zeros (shape = (5 , 5 ), dtype = bool )
130
126
mask_ma = np .ma .masked_array (data = arr , mask = arr_mask )
131
127
# If we mask it in the masked array
132
128
mask_ma [:, 0 ] = np .ma .masked
133
- assert (0 , 4 , 1 , 4 ) == _get_valid_extent (mask_ma )
129
+ assert (0 , 4 , 1 , 4 ) == get_valid_extent (mask_ma )
134
130
135
131
# If we changed the array to NaNs
136
132
arr [:, 0 ] = np .nan
137
- assert (0 , 4 , 1 , 4 ) == _get_valid_extent (arr )
133
+ assert (0 , 4 , 1 , 4 ) == get_valid_extent (arr )
138
134
mask_ma .data [:, 0 ] = np .nan
139
135
mask_ma .mask = False
140
- assert (0 , 4 , 1 , 4 ) == _get_valid_extent (mask_ma )
136
+ assert (0 , 4 , 1 , 4 ) == get_valid_extent (mask_ma )
141
137
142
138
# 3/ Last column:
143
139
arr = np .ones (shape = (5 , 5 ))
@@ -146,14 +142,14 @@ def test_get_valid_extent(self) -> None:
146
142
147
143
# If we mask it in the masked array
148
144
mask_ma [- 1 , :] = np .ma .masked
149
- assert (0 , 3 , 0 , 4 ) == _get_valid_extent (mask_ma )
145
+ assert (0 , 3 , 0 , 4 ) == get_valid_extent (mask_ma )
150
146
151
147
# If we changed the array to NaNs
152
148
arr [- 1 , :] = np .nan
153
- assert (0 , 3 , 0 , 4 ) == _get_valid_extent (arr )
149
+ assert (0 , 3 , 0 , 4 ) == get_valid_extent (arr )
154
150
mask_ma .data [- 1 , :] = np .nan
155
151
mask_ma .mask = False
156
- assert (0 , 3 , 0 , 4 ) == _get_valid_extent (mask_ma )
152
+ assert (0 , 3 , 0 , 4 ) == get_valid_extent (mask_ma )
157
153
158
154
# 4/ Last row:
159
155
arr = np .ones (shape = (5 , 5 ))
@@ -162,14 +158,14 @@ def test_get_valid_extent(self) -> None:
162
158
163
159
# If we mask it in the masked array
164
160
mask_ma [:, - 1 ] = np .ma .masked
165
- assert (0 , 4 , 0 , 3 ) == _get_valid_extent (mask_ma )
161
+ assert (0 , 4 , 0 , 3 ) == get_valid_extent (mask_ma )
166
162
167
163
# If we changed the array to NaNs
168
164
arr [:, - 1 ] = np .nan
169
- assert (0 , 4 , 0 , 3 ) == _get_valid_extent (arr )
165
+ assert (0 , 4 , 0 , 3 ) == get_valid_extent (arr )
170
166
mask_ma .data [:, - 1 ] = np .nan
171
167
mask_ma .mask = False
172
- assert (0 , 4 , 0 , 3 ) == _get_valid_extent (mask_ma )
168
+ assert (0 , 4 , 0 , 3 ) == get_valid_extent (mask_ma )
173
169
174
170
def test_get_xy_rotated (self ) -> None :
175
171
"""Check the function to rotate array."""
@@ -184,27 +180,27 @@ def test_get_xy_rotated(self) -> None:
184
180
xx , yy = r1 .coords (grid = True , force_offset = "ll" )
185
181
186
182
# Rotating the coordinates 90 degrees should be the same as rotating the array
187
- xx90 , yy90 = _get_xy_rotated (r1 , along_track_angle = 90 )
183
+ xx90 , yy90 = get_xy_rotated (r1 , along_track_angle = 90 )
188
184
assert np .allclose (np .rot90 (xx90 ), xx )
189
185
assert np .allclose (np .rot90 (yy90 ), yy )
190
186
191
187
# Same for 180 degrees
192
- xx180 , yy180 = _get_xy_rotated (r1 , along_track_angle = 180 )
188
+ xx180 , yy180 = get_xy_rotated (r1 , along_track_angle = 180 )
193
189
assert np .allclose (np .rot90 (xx180 , k = 2 ), xx )
194
190
assert np .allclose (np .rot90 (yy180 , k = 2 ), yy )
195
191
196
192
# Same for 270 degrees
197
- xx270 , yy270 = _get_xy_rotated (r1 , along_track_angle = 270 )
193
+ xx270 , yy270 = get_xy_rotated (r1 , along_track_angle = 270 )
198
194
assert np .allclose (np .rot90 (xx270 , k = 3 ), xx )
199
195
assert np .allclose (np .rot90 (yy270 , k = 3 ), yy )
200
196
201
197
# 360 degrees should get us back on our feet
202
- xx360 , yy360 = _get_xy_rotated (r1 , along_track_angle = 360 )
198
+ xx360 , yy360 = get_xy_rotated (r1 , along_track_angle = 360 )
203
199
assert np .allclose (xx360 , xx )
204
200
assert np .allclose (yy360 , yy )
205
201
206
202
# Test that the values make sense for 45 degrees
207
- xx45 , yy45 = _get_xy_rotated (r1 , along_track_angle = 45 )
203
+ xx45 , yy45 = get_xy_rotated (r1 , along_track_angle = 45 )
208
204
# Should have zero on the upper left corner for xx
209
205
assert xx45 [0 , 0 ] == pytest .approx (0 )
210
206
# Then a multiple of sqrt2 along each dimension
@@ -215,4 +211,4 @@ def test_get_xy_rotated(self) -> None:
215
211
# Finally, yy should be rotated by 90
216
212
assert np .allclose (np .rot90 (xx45 ), yy45 )
217
213
218
- xx , yy = _get_xy_rotated (r1 , along_track_angle = 90 )
214
+ xx , yy = get_xy_rotated (r1 , along_track_angle = 90 )
0 commit comments