From a0a884d10e24fb690e167dc4a03a1656f1fc6e37 Mon Sep 17 00:00:00 2001 From: Emily Laubscher <43769564+elaubsch@users.noreply.github.com> Date: Fri, 3 Feb 2023 16:01:12 -0800 Subject: [PATCH] Allow blank images in training data set (#42) * Check length of points list before distance transform * Add test case with no spots * Add kwarg fill_value and pass in dtype Co-authored-by: Ross Barnowski --------- Co-authored-by: Ross Barnowski --- deepcell_spots/utils.py | 13 ++++++++----- deepcell_spots/utils_test.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/deepcell_spots/utils.py b/deepcell_spots/utils.py index 3f00290..0fb799f 100644 --- a/deepcell_spots/utils.py +++ b/deepcell_spots/utils.py @@ -68,6 +68,14 @@ def subpixel_distance_transform(point_list, image_shape, dy=1, dx=1): # index in point_list of point nearest to pixel nearest_point = np.full(image_shape, np.nan) + # signed y distance to nearest point + delta_y = np.full(image_shape, fill_value=image_shape[1], dtype=np.float32) + # signed x distance to nearest point + delta_x = np.full(image_shape, fill_value=image_shape[0], dtype=np.float32) + + if len(point_list) == 0: + return delta_y, delta_x, nearest_point + # dictionary to be filled s.t.: pixel_to_contained_point_ind[(i,j)] = k if point_list[k] # is a point contained in pixel i,j of the image pixel_to_contained_point_ind = {} @@ -82,11 +90,6 @@ def subpixel_distance_transform(point_list, image_shape, dy=1, dx=1): edt, inds = distance_transform_edt( contains_point, return_indices=True, sampling=[dy, dx]) - # signed y distance to nearest point - delta_y = np.full(image_shape, np.inf) - # signed x distance to nearest point - delta_x = np.full(image_shape, np.inf) - Ly, Lx = image_shape for j in range(0, Lx): for i in range(0, Ly): diff --git a/deepcell_spots/utils_test.py b/deepcell_spots/utils_test.py index 768c901..f942e6a 100644 --- a/deepcell_spots/utils_test.py +++ b/deepcell_spots/utils_test.py @@ -56,6 +56,20 @@ def test_subpixel_distance_transform(self): self.assertEqual(np.shape(delta_x), image_shape) self.assertEqual(np.shape(nearest_point), image_shape) + # test image with no points + point_list = np.random.random((0, 2)) + image_shape = (30, 30) + delta_y, delta_x, nearest_point = subpixel_distance_transform( + point_list, image_shape) + + self.assertEqual(np.shape(delta_y), image_shape) + self.assertEqual(np.shape(delta_x), image_shape) + self.assertEqual(np.shape(nearest_point), image_shape) + + self.assertAllEqual(delta_y, np.full(image_shape, image_shape[1]).astype(float)) + self.assertAllEqual(delta_x, np.full(image_shape, image_shape[0]).astype(float)) + self.assertAllEqual(nearest_point, np.full(image_shape, np.nan)) + def test_generate_transformation_matrix(self): transform_parameters = { 'theta': 0,