Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover the boolean mask associated to a region #544

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions regions/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
24 changes: 24 additions & 0 deletions regions/core/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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