Esempio n. 1
0
def test_split_kernel():
    kernel = np.zeros((9, 9))
    kernel[4, 4] = 1
    subgrid_res = 3
    subgrid_kernel = kernel_util.subgrid_kernel(kernel,
                                                subgrid_res=subgrid_res,
                                                odd=True)
    subsampling_size = 3
    kernel_hole, kernel_cutout = kernel_util.split_kernel(
        subgrid_kernel,
        supersampling_kernel_size=subsampling_size,
        supersampling_factor=subgrid_res)

    assert kernel_hole[4, 4] == 0
    assert len(kernel_cutout) == subgrid_res * subsampling_size
    npt.assert_almost_equal(np.sum(kernel_hole) + np.sum(kernel_cutout),
                            1,
                            decimal=4)

    subgrid_res = 2
    subgrid_kernel = kernel_util.subgrid_kernel(kernel,
                                                subgrid_res=subgrid_res,
                                                odd=True)
    subsampling_size = 3
    kernel_hole, kernel_cutout = kernel_util.split_kernel(
        subgrid_kernel,
        supersampling_kernel_size=subsampling_size,
        supersampling_factor=subgrid_res)

    assert kernel_hole[4, 4] == 0
    assert len(kernel_cutout) == subgrid_res * subsampling_size + 1
    npt.assert_almost_equal(np.sum(kernel_hole) + np.sum(kernel_cutout),
                            1,
                            decimal=4)
Esempio n. 2
0
    def __init__(self,
                 kernel_supersampled,
                 supersampling_factor,
                 supersampling_size=None,
                 convolution_type='fft'):
        """

        :param kernel_supersampled: kernel in supersampled pixels
        :param supersampling_factor: supersampling factor relative to the image pixel grid
        :param supersampling_size: number of pixels (in units of the image pixels) that are convolved with the
        supersampled kernel
        """
        n_high = len(kernel_supersampled)
        self._supersampling_factor = supersampling_factor
        numPix = int(n_high / self._supersampling_factor)
        if self._supersampling_factor % 2 == 0:
            self._kernel = kernel_util.averaging_even_kernel(
                kernel_supersampled, self._supersampling_factor)
        else:
            self._kernel = util.averaging(kernel_supersampled,
                                          numGrid=n_high,
                                          numPix=numPix)
        if supersampling_size is None:
            kernel_low_res, kernel_high_res = np.zeros_like(
                self._kernel), kernel_supersampled
            self._low_res_convolution = False
        else:
            kernel_low_res, kernel_high_res = kernel_util.split_kernel(
                self._kernel, kernel_supersampled, supersampling_size,
                self._supersampling_factor)
            self._low_res_convolution = True
        self._low_res_conv = PixelKernelConvolution(
            kernel_low_res, convolution_type=convolution_type)
        self._high_res_conv = PixelKernelConvolution(
            kernel_high_res, convolution_type=convolution_type)
def potential_from_kappa_grid_adaptive(kappa_high_res, grid_spacing,
                                       low_res_factor, high_res_kernel_size):
    """
    lensing potential on the convergence grid
    the computation is performed as a convolution of the Green's function with the convergence map using FFT

    :param kappa_high_res: 2d grid of convergence values
    :param grid_spacing: scale of an individual pixel (per axis) of grid
    :param low_res_factor: lower resolution factor of larger scale kernel.
    :param high_res_kernel_size: int, size of high resolution kernel in units of degraded pixels
    :return: lensing potential in a 2d grid at positions x_grid, y_grid
    """
    kappa_low_res = image_util.re_size(kappa_high_res, factor=low_res_factor)
    num_pix = len(kappa_high_res) * 2
    if num_pix % 2 == 0:
        num_pix += 1
    grid_spacing_low_res = grid_spacing * low_res_factor
    kernel = potential_kernel(num_pix, grid_spacing)
    kernel_low_res, kernel_high_res = kernel_util.split_kernel(
        kernel, high_res_kernel_size, low_res_factor, normalized=False)

    f_high_res = scp.fftconvolve(kappa_high_res, kernel_high_res,
                                 mode='same') / np.pi * grid_spacing**2
    f_high_res = image_util.re_size(f_high_res, low_res_factor)
    f_low_res = scp.fftconvolve(kappa_low_res, kernel_low_res,
                                mode='same') / np.pi * grid_spacing_low_res**2
    return f_high_res + f_low_res
Esempio n. 4
0
    def test_raise(self):
        with self.assertRaises(ValueError):
            kernel = np.zeros((2, 2))
            kernel_util.center_kernel(kernel, iterations=1)

        with self.assertRaises(ValueError):
            kernel_super = np.ones((9, 9))
            kernel_util.split_kernel(kernel_super, supersampling_kernel_size=2, supersampling_factor=3)
        with self.assertRaises(ValueError):
            kernel_util.split_kernel(kernel_super, supersampling_kernel_size=3, supersampling_factor=0)
        with self.assertRaises(ValueError):
            image = np.ones((10, 10))
            kernel_util.cutout_source(x_pos=3, y_pos=2, image=image, kernelsize=2)
        with self.assertRaises(ValueError):
            kernel_util.fwhm_kernel(kernel=np.ones((4, 4)))
        with self.assertRaises(ValueError):
            kernel_util.fwhm_kernel(kernel=np.ones((5, 5)))
Esempio n. 5
0
def deflection_from_kappa_grid_adaptive(kappa_high_res, grid_spacing,
                                        low_res_factor, high_res_kernel_size):
    """
    deflection angles on the convergence grid with adaptive FFT
    the computation is performed as a convolution of the Green's function with the convergence map using FFT
    The grid is returned in the lower resolution grid

    :param kappa_high_res: convergence values for each pixel (2-d array)
    :param grid_spacing: pixel size of high resolution grid
    :param low_res_factor: lower resolution factor of larger scale kernel.
    :param high_res_kernel_size: int, size of high resolution kernel in units of degraded pixels
    :return: numerical deflection angles in x- and y- direction
    """
    kappa_low_res = image_util.re_size(kappa_high_res, factor=low_res_factor)
    num_pix = len(kappa_high_res) * 2
    if num_pix % 2 == 0:
        num_pix += 1

    #if high_res_kernel_size % low_res_factor != 0:
    #    assert ValueError('fine grid kernel size needs to be a multiplicative factor of low_res_factor! Settings used: '
    #                      'fine_grid_kernel_size=%s, low_res_factor=%s' % (high_res_kernel_size, low_res_factor))
    kernel_x, kernel_y = deflection_kernel(num_pix, grid_spacing)
    grid_spacing_low_res = grid_spacing * low_res_factor

    kernel_low_res_x, kernel_high_res_x = kernel_util.split_kernel(
        kernel_x, high_res_kernel_size, low_res_factor, normalized=False)
    f_x_high_res = scp.fftconvolve(kappa_high_res,
                                   kernel_high_res_x,
                                   mode='same') / np.pi * grid_spacing**2
    f_x_high_res = image_util.re_size(f_x_high_res, low_res_factor)
    f_x_low_res = scp.fftconvolve(
        kappa_low_res, kernel_low_res_x,
        mode='same') / np.pi * grid_spacing_low_res**2
    f_x = f_x_high_res + f_x_low_res

    kernel_low_res_y, kernel_high_res_y = kernel_util.split_kernel(
        kernel_y, high_res_kernel_size, low_res_factor, normalized=False)
    f_y_high_res = scp.fftconvolve(kappa_high_res,
                                   kernel_high_res_y,
                                   mode='same') / np.pi * grid_spacing**2
    f_y_high_res = image_util.re_size(f_y_high_res, low_res_factor)
    f_y_low_res = scp.fftconvolve(
        kappa_low_res, kernel_low_res_y,
        mode='same') / np.pi * grid_spacing_low_res**2
    f_y = f_y_high_res + f_y_low_res
    return f_x, f_y
Esempio n. 6
0
    def psf_convolution_new(self,
                            unconvolved_image,
                            subgrid_res=1,
                            subsampling_size=11,
                            psf_subgrid=True):
        """

        :param unconvolved_image: 2d image with subsampled pixels with subgrid_res
        :param subgrid_res: subsampling resolution
        :param subsampling_size: size of the subsampling convolution in units of image pixels
        :param psf_subgrid: bool, if False, the convolution is performed on the pixel size of the data
        :return: convolved 2d image in units of the pixels
        """
        unconvolved_image_resized = image_util.re_size(unconvolved_image,
                                                       subgrid_res)
        if self.psf_type == 'NONE':
            image_conv_resized = unconvolved_image_resized
        elif self.psf_type == 'GAUSSIAN':
            if psf_subgrid is True:
                grid_scale = self._pixel_size / float(subgrid_res)
                sigma = self._sigma_gaussian / grid_scale
                image_conv = ndimage.filters.gaussian_filter(
                    unconvolved_image,
                    sigma,
                    mode='nearest',
                    truncate=self._truncation)
                image_conv_resized = image_util.re_size(
                    image_conv, subgrid_res)
            else:
                sigma = self._sigma_gaussian / self._pixel_size
                image_conv_resized = ndimage.filters.gaussian_filter(
                    unconvolved_image_resized,
                    sigma,
                    mode='nearest',
                    truncate=self._truncation)

        elif self.psf_type == 'PIXEL':
            kernel = self._kernel_pixel
            if subgrid_res > 1 and psf_subgrid is True:
                kernel_subgrid = self.subgrid_pixel_kernel(subgrid_res)
                kernel, kernel_subgrid = kernel_util.split_kernel(
                    kernel, kernel_subgrid, subsampling_size, subgrid_res)
                image_conv_subgrid = signal.fftconvolve(unconvolved_image,
                                                        kernel_subgrid,
                                                        mode='same')
                image_conv_resized_1 = image_util.re_size(
                    image_conv_subgrid, subgrid_res)
                image_conv_resized_2 = signal.fftconvolve(
                    unconvolved_image_resized, kernel, mode='same')
                image_conv_resized = image_conv_resized_1 + image_conv_resized_2
            else:
                image_conv_resized = signal.fftconvolve(
                    unconvolved_image_resized, kernel, mode='same')
        else:
            raise ValueError('PSF type %s not valid!' % self.psf_type)
        return image_conv_resized