Ejemplo n.º 1
0
    def from_mask(cls, mask, pixel_scales_interp):
        """
        Create a Grid2DInterpolate (see `Grid2DInterpolate.__new__`) from a mask, where only unmasked pixels are included in
        the grid (if the grid is represented in 2D masked values are (0.0, 0.0)).

        The mask's pixel_scales and origin properties are used to compute the grid (y,x) coordinates.

        Parameters
        ----------
        mask : Mask2D
            The mask whose masked pixels are used to setup the sub-pixel grid.
        pixel_scales_interp : float
            The resolution of the sparse grid used to evaluate the function, from which the results are interpolated
            to the full resolution grid.
        """

        pixel_scales_interp = geometry_util.convert_pixel_scales_2d(
            pixel_scales=pixel_scales_interp)

        grid_slim = grid_2d_util.grid_2d_slim_via_mask_from(
            mask_2d=mask,
            pixel_scales=mask.pixel_scales,
            sub_size=mask.sub_size,
            origin=mask.origin,
        )

        return Grid2DInterpolate(grid=grid_slim,
                                 mask=mask,
                                 pixel_scales_interp=pixel_scales_interp)
Ejemplo n.º 2
0
    def from_mask(cls, mask, fractional_accuracy=0.9999, sub_steps=None):
        """
        Create a Grid2DIterate (see *Grid2DIterate.__new__*) from a mask, where only unmasked pixels are included in
        the grid (if the grid is represented in 2D masked values are (0.0, 0.0)).

        The mask's pixel_scales and origin properties are used to compute the grid (y,x) coordinates.

        Parameters
        ----------
        mask : Mask2D
            The mask whose masked pixels are used to setup the sub-pixel grid.
        fractional_accuracy : float
            The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio
            of the value at a higher sub_size to othe value computed using the previous sub_size.
        sub_steps : [int] or None
            The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None,
            they are setup as the default values [2, 4, 8, 16].
        """

        grid_slim = grid_2d_util.grid_2d_slim_via_mask_from(
            mask_2d=mask,
            pixel_scales=mask.pixel_scales,
            sub_size=1,
            origin=mask.origin)

        return Grid2DIterate(
            grid=grid_slim,
            mask=mask.mask_sub_1,
            fractional_accuracy=fractional_accuracy,
            sub_steps=sub_steps,
        )
Ejemplo n.º 3
0
    def masked_grid_sub_1(self):

        grid_slim = grid_2d_util.grid_2d_slim_via_mask_from(
            mask_2d=self,
            pixel_scales=self.pixel_scales,
            sub_size=1,
            origin=self.origin)
        return grid_2d.Grid2D(grid=grid_slim, mask=self.mask_sub_1)
Ejemplo n.º 4
0
 def masked_grid(self):
     sub_grid_1d = grid_2d_util.grid_2d_slim_via_mask_from(
         mask_2d=self,
         pixel_scales=self.pixel_scales,
         sub_size=self.sub_size,
         origin=self.origin,
     )
     return grid_2d.Grid2D(grid=sub_grid_1d, mask=self.edge_mask.mask_sub_1)
def sub_border_pixel_slim_indexes_from(mask_2d: np.ndarray,
                                       sub_size: int) -> np.ndarray:
    """
    Returns a slim array of shape [total_unmasked_border_pixels] listing all sub-borders pixel indexes in
    the mask.

    A borders pixel is a pixel which:

    1) is not fully surrounding by `False` mask values.
    2) Can reach the edge of the array without hitting a masked pixel in one of four directions (upwards, downwards,
       left, right).

    The borders pixels are thus pixels which are on the exterior edge of the mask. For example, the inner ring of
    edge pixels in an annular mask are edge pixels but not borders pixels.

    A sub-border pixel is, for a border-pixel, the pixel within that border pixel which is furthest from the origin
    of the mask.

    Parameters
    ----------
    mask_2d : np.ndarray
        The mask for which the 1D border pixel indexes are calculated.
    sub_size : int
        The size of the sub-grid in each mask pixel.

    Returns
    -------
    np.ndarray
        The 1D indexes of all border sub-pixels on the mask.
    """

    border_pixels = border_slim_indexes_from(mask_2d=mask_2d)

    sub_border_pixels = np.zeros(shape=border_pixels.shape[0])

    sub_slim_indexes_for_slim_index = sub_slim_indexes_for_slim_index_via_mask_2d_from(
        mask_2d=mask_2d, sub_size=sub_size)

    sub_grid_2d_slim = grid_2d_util.grid_2d_slim_via_mask_from(
        mask_2d=mask_2d,
        pixel_scales=(1.0, 1.0),
        sub_size=sub_size,
        origin=(0.0, 0.0))
    mask_centre = grid_2d_util.grid_2d_centre_from(
        grid_2d_slim=sub_grid_2d_slim)

    for (border_1d_index, border_pixel) in enumerate(border_pixels):
        sub_border_pixels_of_border_pixel = sub_slim_indexes_for_slim_index[
            int(border_pixel)]

        sub_border_pixels[
            border_1d_index] = grid_2d_util.furthest_grid_2d_slim_index_from(
                grid_2d_slim=sub_grid_2d_slim,
                slim_indexes=sub_border_pixels_of_border_pixel,
                coordinate=mask_centre,
            )

    return sub_border_pixels
Ejemplo n.º 6
0
    def from_mask(cls, mask):
        """
        Create a Grid2D (see *Grid2D.__new__*) from a mask, where only unmasked pixels are included in the grid (if the
        grid is represented in its native 2D masked values are (0.0, 0.0)).

        The mask's pixel_scales, sub_size and origin properties are used to compute the grid (y,x) coordinates.

        Parameters
        ----------
        mask : Mask2D
            The mask whose masked pixels are used to setup the sub-pixel grid.
        """

        sub_grid_1d = grid_2d_util.grid_2d_slim_via_mask_from(
            mask_2d=mask,
            pixel_scales=mask.pixel_scales,
            sub_size=mask.sub_size,
            origin=mask.origin,
        )

        return Grid2D(grid=sub_grid_1d, mask=mask)
Ejemplo n.º 7
0
    def __new__(cls, grid, mask, pixel_scales_interp, *args, **kwargs):
        """
        Represents a grid of coordinates as described in the `Grid2D` class, but allows for a sparse grid to be used
        to evaluate functions on the grid, the results of which are then interpolated to the grid's native resolution.

        This sparse grid, termed the 'interpolation grid', is computed from the full resolution grid and an input
        pixel_scales_interp. The interpolation grid is laid over the full resolution grid, with all unmasked
        pixels used to set up the interpolation grid. The neighbors of masked pixels are also included, to ensure the
        interpolation evaluate pixels at the edge of the mask

        The decision whether to evaluate the function using the sparse grid and interpolate to th full resolution grid
        is made in the `grid_like_to_grid` decorator. For every function that can receive a Grid2DInterpolate, there is
        an entry in the 'interpolate.ini' config file where a bool determines if the interpolation is used.

        For functions which can be evaluated fast the interpolation should be turned off, ensuring the calculation is
        accurate and precise. However, if the function is slow to evaluate (e.g. it requires numerical integration)
        its bool in this config file should be True, such that the interpolation method is used instead.

        This feature is used in the light profiles and mass profiles of the projects PyAutoGalaxy and PyAutoLens.
        For example, for many mass profiles computing their deflection angles requires expensive numerical integration.
        However, the deflection angles do not vary much locally, so drastically fewer function evaluations can be
        performed by calculating it on a sparse grid interpolating to the full resolution grid.

        Parameters
        ----------
        grid : np.ndarray
            The (y,x) coordinates of the grid.
        mask : msk.Mask2D
            The 2D mask associated with the grid, defining the pixels each grid coordinate is paired with and
            originates from.
        pixel_scales_interp : float
            The resolution of the sparse grid used to evaluate the function, from which the results are interpolated
            to the full resolution grid.
        """

        obj = grid.view(cls)
        obj.mask = mask
        obj.pixel_scales_interp = pixel_scales_interp

        rescale_factor = mask.pixel_scale / pixel_scales_interp[0]

        mask = mask.mask_sub_1

        rescaled_mask = mask.rescaled_mask_from_rescale_factor(
            rescale_factor=rescale_factor)

        mask_interp = rescaled_mask.edge_buffed_mask

        grid_interp = grid_2d_util.grid_2d_slim_via_mask_from(
            mask_2d=mask_interp,
            pixel_scales=pixel_scales_interp,
            sub_size=1,
            origin=mask.origin,
        )

        obj.grid_interp = grid_2d.Grid2D.manual_mask(grid=grid_interp,
                                                     mask=mask_interp)

        obj.vtx, obj.wts = obj.interp_weight_list

        return obj