Skip to content

Commit

Permalink
DAS-2276: Compare array types in assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
flamingbear committed Dec 12, 2024
1 parent dd60d72 commit 8e67313
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
16 changes: 12 additions & 4 deletions tests/test_service/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,14 @@ def move_tif(*args, **kwargs):
mock_reproject.call_args_list, expected_reproject_calls
):
np.testing.assert_array_equal(
actual_call.kwargs['source'], expected_call.kwargs['source']
actual_call.kwargs['source'],
expected_call.kwargs['source'],
strict=True,
)
np.testing.assert_array_equal(
actual_call.kwargs['destination'], expected_call.kwargs['destination']
actual_call.kwargs['destination'],
expected_call.kwargs['destination'],
strict=True,
)
self.assertEqual(
actual_call.kwargs['src_transform'],
Expand Down Expand Up @@ -482,10 +486,14 @@ def move_tif(*args, **kwargs):
mock_reproject.call_args_list, expected_reproject_calls
):
np.testing.assert_array_equal(
actual_call.kwargs['source'], expected_call.kwargs['source']
actual_call.kwargs['source'],
expected_call.kwargs['source'],
strict=True,
)
np.testing.assert_array_equal(
actual_call.kwargs['destination'], expected_call.kwargs['destination']
actual_call.kwargs['destination'],
expected_call.kwargs['destination'],
strict=True,
)
self.assertEqual(
actual_call.kwargs['src_transform'],
Expand Down
54 changes: 30 additions & 24 deletions tests/unit/test_browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,14 @@ def test_create_browse_imagery_with_mocks(
reproject_mock.call_args_list, expected_calls
):
np.testing.assert_array_equal(
actual_call.kwargs['source'], expected_call.kwargs['source']
actual_call.kwargs['source'],
expected_call.kwargs['source'],
strict=True,
)
np.testing.assert_array_equal(
actual_call.kwargs['destination'], expected_call.kwargs['destination']
actual_call.kwargs['destination'],
expected_call.kwargs['destination'],
strict=True,
)
self.assertEqual(
actual_call.kwargs['src_transform'],
Expand Down Expand Up @@ -339,7 +343,7 @@ def test_convert_singleband_to_raster_without_colortable(self):
dtype='uint8',
)
actual_raster = convert_singleband_to_raster(ds, None)
assert_array_equal(expected_raster, actual_raster)
assert_array_equal(expected_raster, actual_raster, strict=True)

def test_convert_singleband_to_raster_with_colormap(self):
ds = DataArray(self.data).expand_dims('band')
Expand Down Expand Up @@ -376,7 +380,7 @@ def test_convert_singleband_to_raster_with_colormap(self):
# Read down: red, yellow, green, blue
image_palette = convert_colormap_to_palette(self.colormap)
actual_raster = convert_singleband_to_raster(ds, image_palette)
assert_array_equal(expected_raster, actual_raster)
assert_array_equal(expected_raster, actual_raster, strict=True)

def test_convert_singleband_to_raster_with_colormap_and_bad_data(self):
data_array = np.array(self.data, dtype='float')
Expand Down Expand Up @@ -419,10 +423,10 @@ def test_convert_singleband_to_raster_with_colormap_and_bad_data(self):

image_palette = convert_colormap_to_palette(colormap)
actual_raster = convert_singleband_to_raster(ds, image_palette)
assert_array_equal(expected_raster, actual_raster)
assert_array_equal(expected_raster, actual_raster, strict=True)

def test_convert_uint16_3_multiband_to_raster(self):
"""Test that not uint8 input scales the output."""
"""Test that uint16 input scales the output."""
bad_data = np.copy(self.data).astype('float64')
bad_data[1][1] = np.nan
bad_data[1][2] = np.nan
Expand Down Expand Up @@ -463,21 +467,26 @@ def test_convert_uint16_3_multiband_to_raster(self):
)

actual_raster = convert_mulitband_to_raster(ds)
assert_array_equal(expected_raster, actual_raster.data)
assert_array_equal(expected_raster, actual_raster.data, strict=True)

def test_convert_uint8_3_multiband_to_raster(self):
"""Ensure valid data is unchange when input is uint8."""
scale_data = np.copy(self.data / 10).astype('float32')
scale_data = np.array(
[
[10, 200, 30, 40],
[10, np.nan, np.nan, 40],
[10, 200, 30, 40],
[10, 200, 30, 40],
]
).astype('float32')

scale_data[1][1] = np.nan
scale_data[1][2] = np.nan
ds = DataArray(
np.stack([scale_data, scale_data, scale_data]),
dims=('band', 'y', 'x'),
)
ds.encoding = {'dtype': 'uint8'}

expected_data = scale_data.astype('uint8')
expected_data = scale_data.copy()
expected_data[1][1] = 0
expected_data[1][2] = 0

Expand All @@ -497,7 +506,7 @@ def test_convert_uint8_3_multiband_to_raster(self):
)

actual_raster = convert_mulitband_to_raster(ds)
assert_array_equal(expected_raster, actual_raster.data)
assert_array_equal(expected_raster, actual_raster.data, strict=True)

def test_convert_4_multiband_uint8_to_raster(self):
"""4-band 'uint8' images are returned unchanged."""
Expand All @@ -523,10 +532,10 @@ def test_convert_4_multiband_uint8_to_raster(self):

ds.to_numpy.return_value = to_numpy_result

expected_raster = to_numpy_result
expected_raster = to_numpy_result.astype('uint8')

actual_raster = convert_mulitband_to_raster(ds)
assert_array_equal(expected_raster, actual_raster.data)
assert_array_equal(expected_raster, actual_raster.data, strict=True)

def test_convert_4_multiband_uint16_to_raster(self):
"""4-band 'uint16' images are scaled if the range exceeds 255."""
Expand Down Expand Up @@ -557,13 +566,10 @@ def test_convert_4_multiband_uint16_to_raster(self):
).astype('uint8')

actual_raster = convert_mulitband_to_raster(ds)
assert_array_equal(expected_raster, actual_raster.data)
assert_array_equal(expected_raster, actual_raster.data, strict=True)

def test_convert_4_multiband_masked_to_raster(self):
"""4-band images are returned as uint8s**
**Even if they are masked, but also nans are converted to 0s.
"""
"""4-band images are returned with nan -> 0"""
ds = Mock(DataArray)
ds.rio.count = 4
nan = np.nan
Expand Down Expand Up @@ -629,7 +635,7 @@ def test_convert_4_multiband_masked_to_raster(self):
)

actual_raster = convert_mulitband_to_raster(ds)
assert_array_equal(expected_raster.data, actual_raster.data)
assert_array_equal(expected_raster.data, actual_raster.data, strict=True)

def test_convert_5_multiband_to_raster(self):
ds = Mock(DataArray)
Expand All @@ -654,7 +660,7 @@ def test_prepare_raster_for_writing_jpeg_3band(self):

actual_raster, actual_color_map = prepare_raster_for_writing(raster, driver)
self.assertEqual(expected_color_map, actual_color_map)
np.testing.assert_array_equal(expected_raster, actual_raster)
np.testing.assert_array_equal(expected_raster, actual_raster, strict=True)

def test_prepare_raster_for_writing_jpeg_4band(self):
raster = self.random.integers(255, size=(4, 7, 8))
Expand All @@ -663,7 +669,7 @@ def test_prepare_raster_for_writing_jpeg_4band(self):
expected_color_map = None
actual_raster, actual_color_map = prepare_raster_for_writing(raster, driver)
self.assertEqual(expected_color_map, actual_color_map)
np.testing.assert_array_equal(expected_raster, actual_raster)
np.testing.assert_array_equal(expected_raster, actual_raster, strict=True)

@patch('hybig.browse.palettize_raster')
def test_prepare_raster_for_writing_png_4band(self, palettize_mock):
Expand Down Expand Up @@ -694,7 +700,7 @@ def test_palettize_raster_no_alpha_layer(self, get_color_map_mock, image_mock):
multiband_image_mock.quantize.assert_called_once_with(colors=254)
get_color_map_mock.assert_called_once_with(quantized_output)

np.testing.assert_array_equal(expected_out_raster, out_raster)
np.testing.assert_array_equal(expected_out_raster, out_raster, strict=True)

@patch('hybig.browse.Image')
@patch('hybig.browse.get_color_map_from_image')
Expand Down Expand Up @@ -722,7 +728,7 @@ def test_palettize_raster_with_alpha_layer(self, get_color_map_mock, image_mock)
multiband_image_mock.quantize.assert_called_once_with(colors=254)
get_color_map_mock.assert_called_once_with(quantized_output)

np.testing.assert_array_equal(expected_out_raster, out_raster)
np.testing.assert_array_equal(expected_out_raster, out_raster, strict=True)

def test_get_color_map_from_image(self):
"""PIL Image yields a color_map
Expand Down

0 comments on commit 8e67313

Please sign in to comment.