Skip to content

Commit

Permalink
Merge pull request #140 from google:convert_0_to_NaN
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 607757926
  • Loading branch information
Xee authors committed Feb 16, 2024
2 parents 3598235 + a0e3d56 commit 3851238
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
6 changes: 5 additions & 1 deletion xee/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,11 @@ def image_to_array(
Returns:
An numpy array containing the pixels computed based on the given image.
"""
image = image.unmask(self.mask_value)
image = (
ee.Image(self.mask_value)
.rename([image.bandNames().get(0)])
.blend(image)
)
params = {
'expression': image,
'fileFormat': 'NUMPY_NDARRAY',
Expand Down
28 changes: 12 additions & 16 deletions xee/ext_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def test_can_create_object(self):

def test_basic_indexing(self):
arr = xee.EarthEngineBackendArray('B4', self.store)
self.assertEqual(arr[indexing.BasicIndexer((0, 0, 0))], 0)
self.assertEqual(arr[indexing.BasicIndexer((-1, -1, -1))], np.array([0]))
self.assertEqual(np.isnan(arr[indexing.BasicIndexer((0, 0, 0))]), True)
self.assertEqual(np.isnan(arr[indexing.BasicIndexer((-1, -1, -1))]), True)

def test_basic_indexing__nonzero(self):
arr = xee.EarthEngineBackendArray('longitude', self.lnglat_store)
Expand All @@ -117,23 +117,21 @@ def test_basic_indexing__nonzero(self):
def test_basic_indexing_multiple_images(self):
arr = xee.EarthEngineBackendArray('B4', self.store)
first_two = arr[indexing.BasicIndexer((slice(0, 2), 0, 0))]
self.assertTrue(np.allclose(first_two, np.array([0, 0])))
np.testing.assert_equal(first_two, np.full(2, np.nan))
first_three = arr[indexing.BasicIndexer((slice(0, 3), 0, 0))]
self.assertTrue(np.allclose(first_three, np.array([0, 0, 0])))
np.testing.assert_equal(first_three, np.full(3, np.nan))
last_two = arr[indexing.BasicIndexer((slice(-3, -1), 0, 0))]
self.assertTrue(np.allclose(last_two, np.array([0, 0])))
np.testing.assert_equal(last_two, np.full(2, np.nan))
last_three = arr[indexing.BasicIndexer((slice(-4, -1), 0, 0))]
self.assertTrue(np.allclose(last_three, np.array([0, 0, 0])))
np.testing.assert_equal(last_three, np.full(3, np.nan))

def test_slice_indexing(self):
arr = xee.EarthEngineBackendArray('B5', self.store)
first_10 = indexing.BasicIndexer((0, slice(0, 10), slice(0, 10)))
self.assertTrue(np.allclose(arr[first_10], np.zeros((10, 10))))
np.testing.assert_equal(arr[first_10], np.full((10, 10), np.nan))
last_5 = indexing.BasicIndexer((0, slice(-5, -1), slice(-5, -1)))
expected_last_5 = np.zeros((4, 4))
self.assertTrue(
np.allclose(expected_last_5, arr[last_5]), f'Actual:\n{arr[last_5]}'
)
expected_last_5 = np.full((4, 4), np.nan)
np.testing.assert_equal(expected_last_5, arr[last_5])

def test_slice_indexing__non_global(self):
arr = xee.EarthEngineBackendArray('spi2y', self.conus_store)
Expand Down Expand Up @@ -192,14 +190,12 @@ def test_keys_to_slices(self):
def test_slice_indexing_multiple_images(self):
arr = xee.EarthEngineBackendArray('B5', self.store)
first_10 = indexing.BasicIndexer((slice(0, 2), slice(0, 10), slice(0, 10)))
self.assertTrue(np.allclose(arr[first_10], np.zeros((2, 10, 10))))
np.testing.assert_equal(arr[first_10], np.full((2, 10, 10), np.nan))
last_5 = indexing.BasicIndexer(
(slice(-3, -1), slice(-5, -1), slice(-5, -1))
)
expected_last_5 = np.zeros((2, 4, 4))
self.assertTrue(
np.allclose(expected_last_5, arr[last_5]), f'Actual:\n{arr[last_5]}'
)
expected_last_5 = np.full((2, 4, 4), np.nan)
np.testing.assert_equal(expected_last_5, arr[last_5])

def test_slice_indexing__medium(self):
try:
Expand Down

0 comments on commit 3851238

Please sign in to comment.