Beispiel #1
0
    def convolved_array_from_array_and_mask(self, array, mask):
        """
        Convolve an array with this Kernel2D

        Parameters
        ----------
        image : np.ndarray
            An array representing the image the Kernel2D is convolved with.

        Returns
        -------
        convolved_image : np.ndarray
            An array representing the image after convolution.

        Raises
        ------
        KernelException if either Kernel2D psf dimension is odd
        """

        if self.mask.shape[0] % 2 == 0 or self.mask.shape[1] % 2 == 0:
            raise exc.KernelException("Kernel2D Kernel2D must be odd")

        convolved_array_2d = scipy.signal.convolve2d(array,
                                                     self.native,
                                                     mode="same")

        convolved_array_1d = array_2d_util.array_2d_slim_from(
            mask_2d=mask, array_2d_native=convolved_array_2d, sub_size=1)

        return array_2d.Array2D(array=convolved_array_1d, mask=mask.mask_sub_1)
Beispiel #2
0
    def magnification_2d_from_grid(self, grid):

        jacobian = self.jacobian_from_grid(grid=grid)

        det_jacobian = jacobian[0][0] * jacobian[1][1] - jacobian[0][1] * jacobian[1][0]

        return array_2d.Array2D(array=1 / det_jacobian, mask=grid.mask)
Beispiel #3
0
    def grid_pixel_indexes_from_grid_scaled_1d(self, grid_scaled_1d):
        """
        Convert a grid of (y,x) scaled coordinates to a grid of (y,x) pixel 1D indexes. Pixel coordinates are \
        returned as integers such that they are the pixel from the top-left of the 2D grid going rights and then \
        downwards.

        For example:

        - The pixel at the top-left, whose 2D index is [0,0], corresponds to 1D index 0.
        - The fifth pixel on the top row, whose 2D index is [0,5], corresponds to 1D index 4.
        - The first pixel on the second row, whose 2D index is [0,1], has 1D index 10 if a row has 10 pixels.

        The scaled coordinate origin is defined by the class attribute origin, and coordinates are shifted to this \
        origin before computing their 1D grid pixel indexes.

        Parameters
        ----------
        grid_scaled_1d: np.ndarray
            The grid of (y,x) coordinates in scaled units.
        """
        grid_pixel_indexes_1d = grid_2d_util.grid_pixel_indexes_2d_slim_from(
            grid_scaled_2d_slim=grid_scaled_1d,
            shape_native=self.shape,
            pixel_scales=self.pixel_scales,
            origin=self.origin,
        ).astype("int")

        return array_2d.Array2D(array=grid_pixel_indexes_1d,
                                mask=self.edge_mask.mask_sub_1)
Beispiel #4
0
    def shear_via_jacobian_from_grid(self, grid, jacobian=None):

        shear_y = -0.5 * (jacobian[0][1] + jacobian[1][0])
        shear_x = 0.5 * (jacobian[1][1] - jacobian[0][0])

        return array_2d.Array2D(
            array=(shear_x ** 2 + shear_y ** 2) ** 0.5, mask=grid.mask
        )
    def mapped_reconstructed_image(self):
        reconstructed_image = inversion_util.mapped_reconstructed_data_from(
            mapping_matrix=self.blurred_mapping_matrix,
            reconstruction=self.reconstruction,
        )

        return array_2d.Array2D(
            array=reconstructed_image, mask=self.mapper.source_grid_slim.mask.mask_sub_1
        )
Beispiel #6
0
    def radial_eigen_value_from_grid(self, grid, jacobian=None):

        convergence = self.convergence_via_jacobian_from_grid(
            grid=grid, jacobian=jacobian
        )

        shear = self.shear_via_jacobian_from_grid(grid=grid, jacobian=jacobian)

        return array_2d.Array2D(array=1 - convergence + shear, mask=grid.mask)
Beispiel #7
0
    def interpolated_array_from_array_interp(self, array_interp):
        """Use the precomputed vertexes and weight_list of a Delaunay gridding to interpolate a set of values computed on
        the interpolation grid to the Grid2DInterpolate's full grid.

        This function is taken from the SciPy interpolation method griddata
        (see https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html). It is adapted here
        to reuse pre-computed interpolation vertexes and weight_list for efficiency.

        Parameters
        ----------
        array_interp : Array2D
            The results of the function evaluated using the interpolation grid, which is interpolated to the native
            resolution Array2D.
        """

        interpolated_array = np.einsum("nj,nj->n",
                                       np.take(array_interp, self.vtx),
                                       self.wts)
        return array_2d.Array2D(array=interpolated_array, mask=self.mask)
Beispiel #8
0
    def return_iterated_array_result(
            self, iterated_array: np.ndarray) -> array_2d.Array2D:
        """
        Returns the resulting iterated array, by mapping it to 1D and then passing it back as an `Array2D` structure.

        Parameters
        ----------
        iterated_array : np.ndarray

        Returns
        -------
        iterated_array
            The resulting array computed via iteration.
        """

        iterated_array_1d = array_2d_util.array_2d_slim_from(
            mask_2d=self.mask, array_2d_native=iterated_array, sub_size=1)

        return array_2d.Array2D(array=iterated_array_1d,
                                mask=self.mask.mask_sub_1)
Beispiel #9
0
    def transformed_mapping_matrix_from_mapping_matrix(self, mapping_matrix):

        transformed_mapping_matrix = 0 + 0j * np.zeros(
            (self.uv_wavelengths.shape[0], mapping_matrix.shape[1]))

        for source_pixel_1d_index in range(mapping_matrix.shape[1]):

            image_2d = array_2d_util.array_2d_native_from(
                array_2d_slim=mapping_matrix[:, source_pixel_1d_index],
                mask_2d=self.grid.mask,
                sub_size=1,
            )

            image = array_2d.Array2D(array=image_2d, mask=self.grid.mask)

            visibilities = self.visibilities_from_image(image=image)

            transformed_mapping_matrix[:, source_pixel_1d_index] = visibilities

        return transformed_mapping_matrix
Beispiel #10
0
    def structure_2d_from_result(self, result: np.ndarray):
        """
        Convert a result from an ndarray to an aa.Array2D or aa.Grid2D structure, where the conversion depends on
        type(result) as follows:

        - 1D np.ndarray   -> aa.Array2D
        - 2D np.ndarray   -> aa.Grid2D

        This function is used by the grid_2d_to_structure decorator to convert the output result of a function
        to an autoarray structure when a `Grid2D` instance is passed to the decorated function.

        Parameters
        ----------
        result : np.ndarray or [np.ndarray]
            The input result (e.g. of a decorated function) that is converted to a PyAutoArray structure.
        """
        if len(result.shape) == 1:
            return array_2d.Array2D(array=result, mask=self.mask)
        else:
            if isinstance(result, Grid2DTransformedNumpy):
                return Grid2DTransformed(grid=result, mask=self.mask)
            return Grid2D(grid=result, mask=self.mask)
Beispiel #11
0
    def convergence_via_jacobian_from_grid(self, grid, jacobian=None):

        convergence = 1 - 0.5 * (jacobian[0][0] + jacobian[1][1])

        return array_2d.Array2D(array=convergence, mask=grid.mask)