From 7107bf23e512f96955e633a8ae05d73c4e1e85fa Mon Sep 17 00:00:00 2001 From: Wilfried Mercier Date: Sun, 11 Feb 2024 19:23:49 +0100 Subject: [PATCH 1/3] Implemented a to_boolean_array method that returns a boolean mask with True for pixels within at least one of the regions and False for pixels outside. --- regions/core/regions.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/regions/core/regions.py b/regions/core/regions.py index 17e563bc..e26d8eb3 100644 --- a/regions/core/regions.py +++ b/regions/core/regions.py @@ -278,3 +278,27 @@ def serialize(self, format=None, **kwargs): """ return RegionsRegistry.serialize(self.regions, self.__class__, format=format, **kwargs) + + def to_boolean_array(self, shape): + """ + Return a boolean array of the given shape with all regions applied as masks. In the output array, pixels that are within the regions are equal to True and pixels that are outside the regions are equal to False. + + Parameters + ---------- + shape : 2-tuple of int + The shape of the output array + + Returns + ------- + mask : `~numpy.ndarray` of dtype bool + A boolean array of the given shape with True for pixels within the regions and False for pixels outside them. + """ + + # No need to check for correct shape of parameter 'shape'. This is done for each region individually. + # We need to apply a logical_or on all boolean arrays by looping. + out_arr = self.regions[0].to_boolean_array(shape) + + for region in self.regions[1:]: + out_arr = out_arr | region.to_boolean_array(shape) + + return out_arr From 05cf34b24a53d31a5c3df438e96024eab7604895 Mon Sep 17 00:00:00 2001 From: Wilfried Mercier Date: Sun, 11 Feb 2024 19:24:06 +0100 Subject: [PATCH 2/3] Implemented a to_boolean_array method that returns a boolean array with True for pixels within the region and False for pixels outside of it --- regions/core/core.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/regions/core/core.py b/regions/core/core.py index 58df3bf5..36b15ba0 100644 --- a/regions/core/core.py +++ b/regions/core/core.py @@ -338,6 +338,26 @@ def to_mask(self, mode='center', subpixels=5): """ raise NotImplementedError + def to_boolean_array(self, shape): + """ + Return a boolean array of the given shape with the region applied as a mask. In the output array, pixels that are within the region are equal to True and pixels that are outside the region are equal to False. + + Parameters + ---------- + shape : 2-tuple of int + The shape of the output array + + Returns + ------- + mask : `~numpy.ndarray` of dtype bool + A boolean array of the given shape with True for pixels within the region and False for pixels outside it. + """ + + if len(shape) != 2: + raise ValueError('input shape must have 2 elements.') + + return self.to_mask().to_image(shape).astype(bool) + @staticmethod def _validate_mode(mode, subpixels): valid_modes = ('center', 'exact', 'subpixels') From 1850fdb298b9977d885d074d25e1b05d810ff9fa Mon Sep 17 00:00:00 2001 From: Wilfried Mercier Date: Thu, 15 Feb 2024 21:52:45 +0100 Subject: [PATCH 3/3] implemented the to_boolean_array method in SkyRegion. No tests yet --- regions/core/core.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/regions/core/core.py b/regions/core/core.py index 36b15ba0..3b7bcf33 100644 --- a/regions/core/core.py +++ b/regions/core/core.py @@ -494,3 +494,28 @@ def to_pixel(self, wcs): A pixel region. """ raise NotImplementedError + + def to_boolean_array(self, wcs, shape): + """ + Return a boolean array of the given shape with the region applied as a mask. In the output array, pixels that are within the region are equal to True and pixels that are outside the region are equal to False. + + .. note:: + This method is similar to calling first :py:meth:`SkyRegion.to_pixel` and then :py:meth:`PixelRegion.to_boolean_array`. + + Parameters + ---------- + wcs : `~astropy.wcs.WCS` + The world coordinate system transformation to use to convert + between sky and pixel coordinates. + shape : 2-tuple of int + The shape of the output array. + + + Returns + ------- + mask : `~numpy.ndarray` of dtype bool + A boolean array of the given shape with True for pixels within the region and False for pixels outside it. + """ + + # No need to perform checks because to_pixel and to_boolean_array will take care of them + return self.to_pixel(wcs).to_boolean_array(shape)