From 76f864ce393c53817415f504939234a208390e14 Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Thu, 2 Jan 2025 19:15:30 -0500 Subject: [PATCH 1/3] Ensure labels dtype matches the segmentation data --- photutils/segmentation/core.py | 4 ++-- photutils/segmentation/detect.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/photutils/segmentation/core.py b/photutils/segmentation/core.py index 0248a7b60..8d4cdd6ba 100644 --- a/photutils/segmentation/core.py +++ b/photutils/segmentation/core.py @@ -105,7 +105,7 @@ def _get_labels(data): :meth:`remove_masked_labels` on a masked version of the segmentation array. """ - # np.unique also sorts elements + # np.unique preserves dtype and also sorts elements return np.unique(data[data != 0]) @lazyproperty @@ -209,7 +209,7 @@ def labels(self): for label, slc in zip(labels_all, self._raw_slices, strict=True): if slc is not None: labels.append(label) - return np.array(labels) + return np.array(labels, dtype=self._data.dtype) return self._get_labels(self.data) diff --git a/photutils/segmentation/detect.py b/photutils/segmentation/detect.py index e9d0531ee..b55cb6a3e 100644 --- a/photutils/segmentation/detect.py +++ b/photutils/segmentation/detect.py @@ -202,7 +202,7 @@ def _detect_sources(data, threshold, npixels, footprint, inverse_mask, *, # NOTE: recasting segment_img to int and using output=segment_img # gives similar performance segment_img, nlabels = ndi_label(segment_img, structure=footprint) - labels = np.arange(nlabels) + 1 + labels = np.arange(nlabels, dtype=segment_img.dtype) + 1 # remove objects with less than npixels # NOTE: making cutout images and setting their pixels to 0 is From 88584fc984a80945467d56204ff47632a2d03459 Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Thu, 2 Jan 2025 19:23:05 -0500 Subject: [PATCH 2/3] Add tests for label dtype --- photutils/segmentation/tests/test_detect.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/photutils/segmentation/tests/test_detect.py b/photutils/segmentation/tests/test_detect.py index 0e192880a..cdbb11230 100644 --- a/photutils/segmentation/tests/test_detect.py +++ b/photutils/segmentation/tests/test_detect.py @@ -146,6 +146,9 @@ def test_detection(self): segm = detect_sources(self.data, threshold=0.9, npixels=2) assert_equal(segm.data, self.refdata) + assert segm.data.dtype == np.int32 + assert segm.labels.dtype == np.int32 + segm = detect_sources(self.data << u.uJy, threshold=0.9 * u.uJy, npixels=2) assert_equal(segm.data, self.refdata) From 201a7e7af6f5608238603d800f4ad4d086847bbb Mon Sep 17 00:00:00 2001 From: Larry Bradley Date: Thu, 2 Jan 2025 19:25:28 -0500 Subject: [PATCH 3/3] Add changelog entry --- CHANGES.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 6137d83c8..0bbe62615 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -38,6 +38,9 @@ Bug Fixes representation instead of ``str`` representation when using NumPy 2.0+. [#1956] + - Fixed a bug to ensure that the dtype of the ``SegmentationImage`` + ``labels`` always matches the image dtype. [#1986] + API Changes ^^^^^^^^^^^