def test_de_shift(): kernel_size = 5 kernel = np.zeros((kernel_size, kernel_size)) kernel[2, 2] = 2 shift_x = 0.48 shift_y = 0.2 kernel_shifted = interp.shift(kernel, [-shift_y, -shift_x], order=1) kernel_de_shifted = kernel_util.de_shift_kernel(kernel_shifted, shift_x, shift_y, iterations=50) delta_max = np.max(kernel- kernel_de_shifted) assert delta_max < 0.01 npt.assert_almost_equal(kernel_de_shifted[2, 2], kernel[2, 2], decimal=2) kernel_size = 5 kernel = np.zeros((kernel_size, kernel_size)) kernel[2, 2] = 2 shift_x = 1.48 shift_y = 0.2 kernel_shifted = interp.shift(kernel, [-shift_y, -shift_x], order=1) kernel_de_shifted = kernel_util.de_shift_kernel(kernel_shifted, shift_x, shift_y, iterations=50) delta_max = np.max(kernel - kernel_de_shifted) assert delta_max < 0.01 npt.assert_almost_equal(kernel_de_shifted[2, 2], kernel[2, 2], decimal=2) kernel_size_x = 5 kernel_size_y = 4 kernel = np.zeros((kernel_size_x, kernel_size_y)) kernel[2, 2] = 2 shift_x = 1.48 shift_y = 0.2 kernel_shifted = interp.shift(kernel, [-shift_y, -shift_x], order=1) kernel_de_shifted = kernel_util.de_shift_kernel(kernel_shifted, shift_x, shift_y, iterations=50) delta_max = np.max(kernel - kernel_de_shifted) assert delta_max < 0.01 npt.assert_almost_equal(kernel_de_shifted[2, 2], kernel[2, 2], decimal=2)
def cutout_psf_single(x, y, image, mask, kernel_size, kernel_init): """ :param x: x-coordinate of point source :param y: y-coordinate of point source :param image: image (i.e. data - all models subtracted, except a single point source) :param mask: mask of pixels in the image not to be considered in the PSF estimate (being replaced by kernel_init) :param kernel_size: width in pixel of the kernel :param kernel_init: initial guess of kernel (pixels that are masked are replaced by those values) :return: estimate of the PSF based on the image and position of the point source """ # cutout the star x_int = int(round(x)) y_int = int(round(y)) star_cutout = kernel_util.cutout_source(x_int, y_int, image, kernel_size + 2, shift=False) # cutout the mask mask_cutout = kernel_util.cutout_source(x_int, y_int, mask, kernel_size + 2, shift=False) # enlarge the initial PSF kernel to the new cutout size kernel_enlarged = np.zeros((kernel_size + 2, kernel_size + 2)) kernel_enlarged[1:-1, 1:-1] = kernel_init # shift the initial kernel to the shift of the star shift_x = x_int - x shift_y = y_int - y kernel_shifted = ndimage.shift(kernel_enlarged, shift=[-shift_y, -shift_x], order=1) # compute normalization of masked and unmasked region of the shifted kernel # norm_masked = np.sum(kernel_shifted[mask_i == 0]) norm_unmasked = np.sum(kernel_shifted[mask_cutout == 1]) # normalize star within the unmasked region to the norm of the initial kernel of the same region star_cutout /= np.sum(star_cutout[mask_cutout == 1]) * norm_unmasked # replace mask with shifted initial kernel (+2 size) star_cutout[mask_cutout == 0] = kernel_shifted[mask_cutout == 0] star_cutout[star_cutout < 0] = 0 # de-shift kernel kernel_deshifted = kernel_util.de_shift_kernel( star_cutout, shift_x, shift_y, iterations=20, fractional_step_size=0.1) # re-size kernel kernel_deshifted = image_util.cut_edges(kernel_deshifted, kernel_size) # re-normalize kernel again kernel_deshifted = kernel_util.kernel_norm(kernel_deshifted) return kernel_deshifted
def psf_shift_ave(psfs_list, not_count=None, mode='direct', mask_list=['default.reg'], count_psf_std=True, num_iter=1): ''' Shifted the PSF to the center by fitting with the init_ave_PSF--- So that the final averaged PSF could be in the center (sharper). Parameter -------- Similar to psf_ave() count_psf_std: Whether consider the psf_std when doing the PSF fitting. num_iter: is the numbers for doing the interation. Return -------- A averaged PSF. ''' from fit_psf_pos import fit_psf_pos from lenstronomy.Util.kernel_util import de_shift_kernel psf_init_ave, psf_std = psf_ave(psfs_list, mode=mode, not_count=not_count, mask_list=mask_list) psf_final, psf_final_std = psf_init_ave, psf_std for iters in range(num_iter): # print("!!!!!iters is ", iters) shifted_psf_list = np.zeros_like(psfs_list) for i in range(len(psfs_list)): fitted_PSF = psfs_list[i] print("fiting PSF", i) if count_psf_std == True: ra_image, dec_image = fit_psf_pos(fitted_PSF, psf_final, psf_final_std) else: ra_image, dec_image = fit_psf_pos(fitted_PSF, psf_final) print(ra_image, dec_image) if abs(ra_image) > 0.3 or abs(dec_image) > 0.3: print("Warning, the fitted ra_image, dec_image for psf", i, 'is too large!!!:', ra_image, dec_image) shifted_psf_list[i] = de_shift_kernel(fitted_PSF, -ra_image, -dec_image) plt.imshow(shifted_psf_list[i], norm=LogNorm(), origin='low') plt.show() psf_final, psf_final_std = psf_ave(shifted_psf_list, mode=mode, not_count=not_count, mask_list=mask_list) return psf_final, psf_final_std
def test_shift_long_dist(): """ input is a shifted kernel by more than 1 pixel :return: """ kernel_size = 9 kernel = np.zeros((kernel_size, kernel_size)) kernel[4, 4] = 2. shift_x = 2. shift_y = 1. input_kernel = interp.shift(kernel, [-shift_y, -shift_x], order=1) old_style_kernel = interp.shift(input_kernel, [shift_y, shift_x], order=1) shifted_new = kernel_util.de_shift_kernel(input_kernel, shift_x, shift_y) assert kernel[3, 2] == shifted_new[3, 2] assert np.max(old_style_kernel - shifted_new) < 0.01
def cutout_psf(self, x, y, image_list, kernelsize, mask, mask_point_source_list, kernel_init, symmetry=1): """ :param x_: :param y_: :param image_list: list of images (i.e. data - all models subtracted, except a single point source) :param kernelsize: :return: """ n = len(x) * symmetry angle = 360. / symmetry kernel_list = np.zeros((n, kernelsize, kernelsize)) i = 0 for l in range(len(x)): # cutout the star x_, y_ = x[l], y[l] x_int = int(round(x_)) y_int = int(round(y_)) star_cutout = kernel_util.cutout_source(x_int, y_int, image_list[l], kernelsize + 2, shift=False) # cutout the mask mask_i = mask * mask_point_source_list[l] mask_cutout = kernel_util.cutout_source(x_int, y_int, mask_i, kernelsize + 2, shift=False) # enlarge the initial PSF kernel to the new cutout size kernel_enlarged = np.zeros((kernelsize+2, kernelsize+2)) kernel_enlarged[1:-1, 1:-1] = kernel_init # shift the initial kernel to the shift of the star shift_x = x_int - x_ shift_y = y_int - y_ kernel_shifted = interp.shift(kernel_enlarged, [-shift_y, -shift_x], order=1) # compute normalization of masked and unmasked region of the shifted kernel # norm_masked = np.sum(kernel_shifted[mask_i == 0]) norm_unmaksed = np.sum(kernel_shifted[mask_cutout == 1]) # normalize star within the unmasked region to the norm of the initial kernel of the same region star_cutout /= np.sum(star_cutout[mask_cutout == 1]) * norm_unmaksed # replace mask with shifted initial kernel (+2 size) star_cutout[mask_cutout == 0] = kernel_shifted[mask_cutout == 0] star_cutout[star_cutout < 0] = 0 # de-shift kernel kernel_deshifted = kernel_util.de_shift_kernel(star_cutout, shift_x, shift_y) # re-size kernel kernel_deshifted = image_util.cut_edges(kernel_deshifted, kernelsize) # re-normalize kernel again kernel_deshifted = kernel_util.kernel_norm(kernel_deshifted) """ kernel_shifted = kernel_util.cutout_source(x_[l], y_[l], image_list[l], kernelsize + 2) # don't de-shift it here mask_i = mask * mask_point_source_list[l] mask_cutout = kernel_util.cutout_source(int(round(x_[l])), int(round(x_[l])), mask_i, kernelsize + 2, shift=False) kernel_shifted[kernel_shifted < 0] = 0 kernel_shifted *= mask_cutout kernel_init = kernel_util.kernel_norm(kernel_init) mask_cutout = image_util.cut_edges(mask_cutout, kernelsize) kernel_shifted = image_util.cut_edges(kernel_shifted, kernelsize) kernel_norm = np.sum(kernel_init[mask_cutout == 1]) kernel_shifted = kernel_util.kernel_norm(kernel_shifted) kernel_shifted *= kernel_norm kernel_shifted[mask_cutout == 0] = kernel_init[mask_cutout == 0] #kernel_shifted[mask_cutout == 1] /= (np.sum(kernel_init[mask_cutout == 1]) * np.sum(kernel_shifted[mask_cutout == 1])) """ for k in range(symmetry): kernel_rotated = image_util.rotateImage(kernel_deshifted, angle * k) kernel_norm = kernel_util.kernel_norm(kernel_rotated) try: kernel_list[i, :, :] = kernel_norm except: raise ValueError("cutout kernel has not the same shape as the PSF." " This is probably because the cutout of the psf hits the boarder of the data." "Use a smaller PSF or a larger data frame for the modelling.") i += 1 return kernel_list