diff --git a/regions/core/core.py b/regions/core/core.py index 58df3bf5..3b7bcf33 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') @@ -474,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) 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