Beispiel #1
0
    def manual(
            cls,
            mask: Union[List, np.ndarray],
            pixel_scales: Union[float, Tuple[float]],
            sub_size: int = 1,
            origin: Tuple[float] = (0.0, ),
            invert: bool = False,
    ) -> "Mask1D":

        if type(mask) is list:
            mask = np.asarray(mask).astype("bool")

        if invert:
            mask = np.invert(mask)

        if type(pixel_scales) is float:
            pixel_scales = (pixel_scales, )

        if len(mask.shape) != 1:
            raise exc.MaskException(
                "The input mask is not a one dimensional array")

        return Mask1D(mask=mask,
                      pixel_scales=pixel_scales,
                      sub_size=sub_size,
                      origin=origin)
Beispiel #2
0
 def pixel_scale(self):
     if self.pixel_scales[0] == self.pixel_scales[1]:
         return self.pixel_scales[0]
     else:
         raise exc.MaskException(
             "Cannot return a pixel_scale for a a grid where each dimension has a "
             "different pixel scale (e.g. pixel_scales[0] != pixel_scales[1]"
         )
def blurring_mask_2d_from(mask_2d: np.ndarray,
                          kernel_shape_native: Tuple[int, int]) -> np.ndarray:
    """
    Returns a blurring mask from an input mask and psf shape.

    The blurring mask corresponds to all pixels which are outside of the mask but will have a fraction of their
    light blur into the masked region due to PSF convolution. The PSF shape is used to determine which pixels these are.

    If a pixel is identified which is outside the 2D dimensions of the input mask, an error is raised and the user
    should pad the input mask (and associated images).

    Parameters
    -----------
    mask_2d : np.ndarray
        A 2D array of bools, where `False` values are unmasked.
    kernel_shape_native : (int, int)
        The 2D shape of the PSF which is used to compute the blurring mask.

    Returns
    -------
    ndarray
        The 2D blurring mask array whose unmasked values (`False`) correspond to where the mask will have PSF light
        blurred into them.

    Examples
    --------
    mask = np.array([[True, True, True],
                     [True, False, True]
                     [True, True, True]])

    blurring_mask = blurring_mask_from_mask_and_psf_shape(mask=mask)

    """

    blurring_mask_2d = np.full(mask_2d.shape, True)

    for y in range(mask_2d.shape[0]):
        for x in range(mask_2d.shape[1]):
            if not mask_2d[y, x]:
                for y1 in range(
                    (-kernel_shape_native[0] + 1) // 2,
                    (kernel_shape_native[0] + 1) // 2,
                ):
                    for x1 in range(
                        (-kernel_shape_native[1] + 1) // 2,
                        (kernel_shape_native[1] + 1) // 2,
                    ):
                        if (0 <= x + x1 <= mask_2d.shape[1] - 1
                                and 0 <= y + y1 <= mask_2d.shape[0] - 1):
                            if mask_2d[y + y1, x + x1]:
                                blurring_mask_2d[y + y1, x + x1] = False
                        else:
                            raise exc.MaskException(
                                "setup_blurring_mask extends beyond the sub_size "
                                "of the mask - pad the datas array before masking"
                            )

    return blurring_mask_2d
Beispiel #4
0
    def manual(
        cls,
        mask: np.ndarray or list,
        pixel_scales: (float, float),
        sub_size: int = 1,
        origin: (float, float) = (0.0, 0.0),
        invert: bool = False,
    ) -> "Mask2D":
        """
        Returns a Mask2D (see `AbstractMask2D.__new__`) by inputting the array values in 2D, for example:

        mask=np.array([[False, False],
                       [True, False]])

        mask=[[False, False],
               [True, False]]

        Parameters
        ----------
        mask : np.ndarray or list
            The `bool` values of the mask input as an `np.ndarray` of shape [total_y_pixels, total_x_pixels] or a
            list of lists.
        pixel_scales: (float, float) or float
            The (y,x) scaled units to pixel units conversion factors of every pixel. If this is input as a `float`,
            it is converted to a (float, float) structure.
        sub_size : int
            The size (sub_size x sub_size) of each unmasked pixels sub-array.
        origin : (float, float)
            The (y,x) scaled units origin of the mask's coordinate system.
        invert : bool
            If `True`, the `bool`'s of the input `mask` are inverted, for example `False`'s become `True`
            and visa versa.
        """
        if type(mask) is list:
            mask = np.asarray(mask).astype("bool")

        if invert:
            mask = np.invert(mask)

        pixel_scales = geometry_util.convert_pixel_scales_2d(
            pixel_scales=pixel_scales)

        if len(mask.shape) != 2:
            raise exc.MaskException(
                "The input mask is not a two dimensional array")

        return cls(mask=mask,
                   pixel_scales=pixel_scales,
                   sub_size=sub_size,
                   origin=origin)
Beispiel #5
0
    def pixel_scale(self):
        """
        For a mask with dimensions two or above check that are pixel scales are the same, and if so return this
        single value as a ``float``.
        """

        for pixel_scale in self.pixel_scales:
            if pixel_scale != self.pixel_scales[0]:
                raise exc.MaskException(
                    "Cannot return a pixel_scale for a grid where each dimension has a "
                    "different pixel scale (e.g. pixel_scales[0] != pixel_scales[1]"
                )

        return self.pixel_scales[0]
Beispiel #6
0
    def manual(cls, mask, pixel_scales, origin=(0.0, 0.0), invert=False):
        """Create a Mask2D (see *Mask2D.__new__*) by inputting the array values in 2D, for example:

        mask=np.array([[False, False],
                       [True, False]])

        mask=[[False, False],
               [True, False]]

        Parameters
        ----------
        mask : np.ndarray or list
            The bool values of the mask input as an ndarray of shape [total_y_pixels, total_x_pixels ]or a list of
            lists.
        pixel_scales: (float, float) or float
            The (y,x) scaled units to pixel units conversion factors of every pixel. If this is input as a ``float``,
            it is converted to a (float, float) structure.
        origin : (float, float)
            The (y,x) scaled units origin of the mask's coordinate system.
        invert : bool
            If `True`, the ``bool``'s of the input ``mask`` are inverted, for example `False`'s become `True`
            and visa versa.
        """
        if type(mask) is list:
            mask = np.asarray(mask).astype("bool")

        if invert:
            mask = np.invert(mask)

        pixel_scales = geometry_util.convert_pixel_scales_2d(
            pixel_scales=pixel_scales)

        if len(mask.shape) != 2:
            raise exc.MaskException(
                "The input mask is not a two dimensional array")

        return cls(mask=mask, pixel_scales=pixel_scales, origin=origin)
Beispiel #7
0
    def manual(cls,
               mask_2d,
               pixel_scales=None,
               sub_size=1,
               origin=(0.0, 0.0),
               invert=False):

        if type(mask_2d) is list:
            mask_2d = np.asarray(mask_2d).astype("bool")

        if invert:
            mask_2d = np.invert(mask_2d)

        if type(pixel_scales) is float:
            pixel_scales = (pixel_scales, pixel_scales)

        if len(mask_2d.shape) != 2:
            raise exc.MaskException(
                "The input mask_2d is not a two dimensional array")

        return Mask(mask_2d=mask_2d,
                    pixel_scales=pixel_scales,
                    sub_size=sub_size,
                    origin=origin)