Skip to content

Commit 1594501

Browse files
authored
Revert making all raster.array non-public (#625)
1 parent c80ef6c commit 1594501

File tree

6 files changed

+38
-42
lines changed

6 files changed

+38
-42
lines changed

geoutils/interface/raster_point.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import geoutils as gu
1414
from geoutils._typing import NDArrayNum
15-
from geoutils.raster.array import _get_mask_from_array
15+
from geoutils.raster.array import get_mask_from_array
1616
from geoutils.raster.georeferencing import _default_nodata, _xy2ij
1717
from geoutils.raster.sampling import subsample_array
1818

@@ -168,11 +168,11 @@ def _raster_to_pointcloud(
168168
if skip_nodata:
169169
if source_raster.is_loaded:
170170
if source_raster.count == 1:
171-
self_mask = _get_mask_from_array(
171+
self_mask = get_mask_from_array(
172172
source_raster.data
173173
) # This is to avoid the case where the mask is just "False"
174174
else:
175-
self_mask = _get_mask_from_array(
175+
self_mask = get_mask_from_array(
176176
source_raster.data[data_band - 1, :, :]
177177
) # This is to avoid the case where the mask is just "False"
178178
valid_mask = ~self_mask

geoutils/raster/array.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from geoutils._typing import MArrayNum, NDArrayBool, NDArrayNum
1111

1212

13-
def _get_mask_from_array(array: NDArrayNum | NDArrayBool | MArrayNum) -> NDArrayBool:
13+
def get_mask_from_array(array: NDArrayNum | NDArrayBool | MArrayNum) -> NDArrayBool:
1414
"""
1515
Return the mask of invalid values, whether array is a ndarray with NaNs or a np.ma.masked_array.
1616
@@ -22,7 +22,7 @@ def _get_mask_from_array(array: NDArrayNum | NDArrayBool | MArrayNum) -> NDArray
2222
return mask.squeeze()
2323

2424

25-
def _get_array_and_mask(
25+
def get_array_and_mask(
2626
array: NDArrayNum | MArrayNum, check_shape: bool = True, copy: bool = True
2727
) -> tuple[NDArrayNum, NDArrayBool]:
2828
"""
@@ -59,19 +59,19 @@ def _get_array_and_mask(
5959
array_data = np.array(array).squeeze() if copy else np.asarray(array).squeeze()
6060

6161
# Get the mask of invalid pixels and set nans if it is occupied.
62-
invalid_mask = _get_mask_from_array(array)
62+
invalid_mask = get_mask_from_array(array)
6363
if np.any(invalid_mask):
6464
array_data[invalid_mask] = np.nan
6565

6666
return array_data, invalid_mask
6767

6868

69-
def _get_valid_extent(array: NDArrayNum | NDArrayBool | MArrayNum) -> tuple[int, ...]:
69+
def get_valid_extent(array: NDArrayNum | NDArrayBool | MArrayNum) -> tuple[int, ...]:
7070
"""
7171
Return (rowmin, rowmax, colmin, colmax), the first/last row/column of array with valid pixels
7272
"""
7373
if not array.dtype == "bool":
74-
valid_mask = ~_get_mask_from_array(array)
74+
valid_mask = ~get_mask_from_array(array)
7575
else:
7676
# Not sure why Mypy is not recognizing that the type of the array can only be bool here
7777
valid_mask = array # type: ignore
@@ -80,7 +80,7 @@ def _get_valid_extent(array: NDArrayNum | NDArrayBool | MArrayNum) -> tuple[int,
8080
return rows_nonzero[0], rows_nonzero[-1], cols_nonzero[0], cols_nonzero[-1]
8181

8282

83-
def _get_xy_rotated(raster: gu.Raster, along_track_angle: float) -> tuple[NDArrayNum, NDArrayNum]:
83+
def get_xy_rotated(raster: gu.Raster, along_track_angle: float) -> tuple[NDArrayNum, NDArrayNum]:
8484
"""
8585
Rotate x, y axes of image to get along- and cross-track distances.
8686
:param raster: Raster to get x,y positions from.

geoutils/raster/multiraster.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import geoutils as gu
1414
from geoutils._typing import NDArrayNum
15-
from geoutils.raster.array import _get_array_and_mask
15+
from geoutils.raster.array import get_array_and_mask
1616
from geoutils.raster.geotransformations import _resampling_method_from_str
1717
from geoutils.raster.raster import RasterType, _default_nodata
1818

@@ -194,7 +194,7 @@ def stack_rasters(
194194
# Optionally calculate difference
195195
if diff:
196196
diff_to_ref = (reference_raster.data - reprojected_raster.data).squeeze()
197-
diff_to_ref, _ = _get_array_and_mask(diff_to_ref)
197+
diff_to_ref, _ = get_array_and_mask(diff_to_ref)
198198
data.append(diff_to_ref)
199199
else:
200200
# img_data, _ = get_array_and_mask(reprojected_raster.data.squeeze())

geoutils/raster/sampling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import numpy as np
88

99
from geoutils._typing import MArrayNum, NDArrayNum
10-
from geoutils.raster.array import _get_mask_from_array
10+
from geoutils.raster.array import get_mask_from_array
1111

1212

1313
@overload
@@ -60,7 +60,7 @@ def subsample_array(
6060
rng = np.random.default_rng(random_state)
6161

6262
# Remove invalid values and flatten array
63-
mask = _get_mask_from_array(array) # -> need to remove .squeeze in get_mask
63+
mask = get_mask_from_array(array) # -> need to remove .squeeze in get_mask
6464
valids = np.argwhere(~mask.flatten()).squeeze()
6565

6666
# Get number of points to extract

tests/test_raster/test_array.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
import rasterio as rio
1010

1111
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
1713

1814

1915
class TestArray:
@@ -59,13 +55,13 @@ def test_get_array_and_mask(
5955
# Validate that incorrect shapes raise the correct error.
6056
if not check_should_pass:
6157
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)
6359

6460
# Stop the test here as the failure is now validated.
6561
return
6662

6763
# 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)
6965

7066
# Validate that the array is a copy
7167
assert not np.shares_memory(arr, array)
@@ -82,7 +78,7 @@ def test_get_array_and_mask(
8278
warnings.simplefilter("always")
8379

8480
# 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)
8682

8783
# If it should be possible, validate that there were no warnings.
8884
if view_should_be_possible:
@@ -108,36 +104,36 @@ def test_get_valid_extent(self) -> None:
108104

109105
# For no invalid values, the function should return the edges
110106
# For the array
111-
assert (0, 4, 0, 4) == _get_valid_extent(arr)
107+
assert (0, 4, 0, 4) == get_valid_extent(arr)
112108
# 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)
114110

115111
# 1/ First column:
116112
# If we mask it in the masked array
117113
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)
119115

120116
# If we changed the array to NaNs
121117
arr[0, :] = np.nan
122-
assert (1, 4, 0, 4) == _get_valid_extent(arr)
118+
assert (1, 4, 0, 4) == get_valid_extent(arr)
123119
mask_ma.data[0, :] = np.nan
124120
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)
126122

127123
# 2/ First row:
128124
arr = np.ones(shape=(5, 5))
129125
arr_mask = np.zeros(shape=(5, 5), dtype=bool)
130126
mask_ma = np.ma.masked_array(data=arr, mask=arr_mask)
131127
# If we mask it in the masked array
132128
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)
134130

135131
# If we changed the array to NaNs
136132
arr[:, 0] = np.nan
137-
assert (0, 4, 1, 4) == _get_valid_extent(arr)
133+
assert (0, 4, 1, 4) == get_valid_extent(arr)
138134
mask_ma.data[:, 0] = np.nan
139135
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)
141137

142138
# 3/ Last column:
143139
arr = np.ones(shape=(5, 5))
@@ -146,14 +142,14 @@ def test_get_valid_extent(self) -> None:
146142

147143
# If we mask it in the masked array
148144
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)
150146

151147
# If we changed the array to NaNs
152148
arr[-1, :] = np.nan
153-
assert (0, 3, 0, 4) == _get_valid_extent(arr)
149+
assert (0, 3, 0, 4) == get_valid_extent(arr)
154150
mask_ma.data[-1, :] = np.nan
155151
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)
157153

158154
# 4/ Last row:
159155
arr = np.ones(shape=(5, 5))
@@ -162,14 +158,14 @@ def test_get_valid_extent(self) -> None:
162158

163159
# If we mask it in the masked array
164160
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)
166162

167163
# If we changed the array to NaNs
168164
arr[:, -1] = np.nan
169-
assert (0, 4, 0, 3) == _get_valid_extent(arr)
165+
assert (0, 4, 0, 3) == get_valid_extent(arr)
170166
mask_ma.data[:, -1] = np.nan
171167
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)
173169

174170
def test_get_xy_rotated(self) -> None:
175171
"""Check the function to rotate array."""
@@ -184,27 +180,27 @@ def test_get_xy_rotated(self) -> None:
184180
xx, yy = r1.coords(grid=True, force_offset="ll")
185181

186182
# 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)
188184
assert np.allclose(np.rot90(xx90), xx)
189185
assert np.allclose(np.rot90(yy90), yy)
190186

191187
# 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)
193189
assert np.allclose(np.rot90(xx180, k=2), xx)
194190
assert np.allclose(np.rot90(yy180, k=2), yy)
195191

196192
# 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)
198194
assert np.allclose(np.rot90(xx270, k=3), xx)
199195
assert np.allclose(np.rot90(yy270, k=3), yy)
200196

201197
# 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)
203199
assert np.allclose(xx360, xx)
204200
assert np.allclose(yy360, yy)
205201

206202
# 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)
208204
# Should have zero on the upper left corner for xx
209205
assert xx45[0, 0] == pytest.approx(0)
210206
# Then a multiple of sqrt2 along each dimension
@@ -215,4 +211,4 @@ def test_get_xy_rotated(self) -> None:
215211
# Finally, yy should be rotated by 90
216212
assert np.allclose(np.rot90(xx45), yy45)
217213

218-
xx, yy = _get_xy_rotated(r1, along_track_angle=90)
214+
xx, yy = get_xy_rotated(r1, along_track_angle=90)

tests/test_raster/test_raster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ def test_copy(self, example: str) -> None:
10091009

10101010
# When passing the new array as a NaN ndarray, only the valid data is equal, because masked data is NaN in one
10111011
# case, and -9999 in the other
1012-
r_arr = gu.raster.array._get_array_and_mask(r)[0]
1012+
r_arr = gu.raster.array.get_array_and_mask(r)[0]
10131013
r2 = r.copy(new_array=r_arr)
10141014
assert np.ma.allequal(r.data, r2.data)
10151015
# If a nodata value exists, and we update the NaN pixels to be that nodata value, then the two Rasters should

0 commit comments

Comments
 (0)