From d5eea9831869083e72281a319742b35aa25d5896 Mon Sep 17 00:00:00 2001 From: jessicasyu <15913767+jessicasyu@users.noreply.github.com> Date: Thu, 5 Dec 2024 17:11:25 -0500 Subject: [PATCH] Fix 2D image conversion to ignore z when dropping border --- .../convert/convert_to_images.py | 5 +- .../convert/test_convert_to_images.py | 87 +++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/arcade_collection/convert/convert_to_images.py b/src/arcade_collection/convert/convert_to_images.py index 641bb4d..fe3637e 100644 --- a/src/arcade_collection/convert/convert_to_images.py +++ b/src/arcade_collection/convert/convert_to_images.py @@ -107,7 +107,10 @@ def convert_to_images( object_id = object_id + 1 # Remove 1 pixel border. - array = raw_array[:, :, 1:-1, 1:-1, 1:-1].copy() + if height == 1: + array = raw_array[:, :, :, 1:-1, 1:-1].copy() + else: + array = raw_array[:, :, 1:-1, 1:-1, 1:-1].copy() if separate: chunks = [ diff --git a/tests/arcade_collection/convert/test_convert_to_images.py b/tests/arcade_collection/convert/test_convert_to_images.py index 9bf8521..cbd33ce 100644 --- a/tests/arcade_collection/convert/test_convert_to_images.py +++ b/tests/arcade_collection/convert/test_convert_to_images.py @@ -70,6 +70,79 @@ def setUp(self): self.locations_tar = build_tar_instance(contents) + def test_convert_to_images_full_without_chunks_2d_image(self): + frame_spec = (5, 6, 1) + box = (8, 8, 1) + chunk_size = 6 + image_type = ImageType.FULL + + contents = { + f"{self.series_key}_000005.LOCATIONS.json": [ + { + "id": 1, + "location": [ + {"region": "DEFAULT", "voxels": [[1, 1, 0], [2, 1, 0], [2, 2, 0]]}, + {"region": "REGION", "voxels": [[1, 2, 0]]}, + ], + }, + { + "id": 2, + "location": [ + {"region": "DEFAULT", "voxels": [[5, 3, 0], [5, 4, 0]]}, + {"region": "REGION", "voxels": [[6, 3, 0], [6, 4, 0]]}, + ], + }, + ] + } + + locations_tar = build_tar_instance(contents) + + chunk_00 = np.array( + [ + [ + [ + [ + [1, 1, 0, 0, 0, 0], + [1, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 2, 2], + [0, 0, 0, 0, 2, 2], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + ] + ], + [ + [ + [0, 0, 0, 0, 0, 0], + [1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 2], + [0, 0, 0, 0, 0, 2], + [0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0], + ] + ], + ] + ] + ) + + expected_images = [(0, 0, chunk_00, None)] + + images = convert_to_images( + self.series_key, + locations_tar, + frame_spec, + self.regions, + box, + chunk_size, + image_type, + ) + + self.assertEqual(len(expected_images), len(images)) + for expected_chunk, chunk in zip(expected_images, images): + self.assertEqual(expected_chunk[0], chunk[0]) + self.assertEqual(expected_chunk[1], chunk[1]) + self.assertTrue(np.array_equal(expected_chunk[2], chunk[2])) + self.assertIsNone(chunk[3]) + def test_convert_to_images_full_without_chunks(self): chunk_size = 6 image_type = ImageType.FULL @@ -157,6 +230,7 @@ def test_convert_to_images_full_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -258,6 +332,7 @@ def test_convert_to_images_full_by_frame_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -311,6 +386,7 @@ def test_convert_to_images_flat_by_frame_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -366,6 +442,7 @@ def test_convert_to_images_flat_rgba_by_frame_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -459,6 +536,7 @@ def test_convert_to_images_full_binary_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -560,6 +638,7 @@ def test_convert_to_images_full_binary_by_frame_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -613,6 +692,7 @@ def test_convert_to_images_flat_binary_by_frame_without_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -669,6 +749,7 @@ def test_convert_to_images_full_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -737,6 +818,7 @@ def test_convert_to_images_full_by_frame_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -783,6 +865,7 @@ def test_convert_to_images_flat_by_frame_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -853,6 +936,7 @@ def test_convert_to_images_flat_rgba_by_frame_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -909,6 +993,7 @@ def test_convert_to_images_full_binary_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -977,6 +1062,7 @@ def test_convert_to_images_full_binary_by_frame_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1]) @@ -1023,6 +1109,7 @@ def test_convert_to_images_flat_binary_by_frame_with_chunks(self): image_type, ) + self.assertEqual(len(expected_images), len(images)) for expected_chunk, chunk in zip(expected_images, images): self.assertEqual(expected_chunk[0], chunk[0]) self.assertEqual(expected_chunk[1], chunk[1])