How to use the lenstronomy.Util.util.image2array function in lenstronomy

To help you get started, we’ve selected a few lenstronomy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sibirrer / lenstronomy / test / test_Workflow / test_psf_fitting.py View on Github external
def test_mask_point_source(self):
        ra_image, dec_image, amp = self.imageModel.PointSource.point_source_list(self.kwargs_ps, self.kwargs_lens)
        print(ra_image, dec_image, amp)
        x_grid, y_grid = self.imageModel.Data.pixel_coordinates
        x_grid = util.image2array(x_grid)
        y_grid = util.image2array(y_grid)
        radius = 0.5
        mask_point_source = self.psf_fitting.mask_point_source(ra_image, dec_image, x_grid, y_grid, radius, i=0)
        assert mask_point_source[10, 10] == 1
github sibirrer / lenstronomy / lenstronomy / ImSim / image_linear_solve.py View on Github external
def image2array_masked(self, image):
        """
        returns 1d array of values in image that are not masked out for the likelihood computation/linear minimization
        :param image: 2d numpy array of full image
        :return: 1d array
        """
        array = util.image2array(image)
        return array[self._mask1d]
github sibirrer / lenstronomy / lenstronomy / Extensions / Itterative / iterative_lens.py View on Github external
x_s, y_s = self.mapping_IS(x_grid, y_grid, kwargs_else, **kwargs_lens)


        image = np.zeros(numPix**2)
        center_x = kwargs_source['center_x']
        center_y = kwargs_source['center_y']
        num_param_shapelets = (num_order + 2) * (num_order + 1) / 2
        H_x, H_y = self.shapelets.pre_calc(x_s, y_s, beta, num_order, center_x, center_y)
        n1 = 0
        n2 = 0
        for i in range(len(param) - num_param_shapelets, len(param)):
            kwargs_source_shapelet = {'center_x': center_x, 'center_y': center_y, 'n1': n1, 'n2': n2, 'beta': beta, 'amp': param[i]}
            image_i = self.shapelets.function(H_x, H_y, **kwargs_source_shapelet)
            image_i = util.array2image(image_i)
            image_i = self.re_size_convolve(image_i, numPix, deltaPix, subgrid_res, kwargs_psf)
            image += util.image2array(image_i*mask)
            if n1 == 0:
                n1 = n2 + 1
                n2 = 0
            else:
                n1 -= 1
                n2 += 1
        return image
github sibirrer / lenstronomy / lenstronomy / Plots / lens_plot.py View on Github external
:param numPix:
    :param deltaPix:
    :param sourcePos_x:
    :param sourcePos_y:
    :param with_caustics:
    :return:
    """
    kwargs_data = sim_util.data_configure_simple(numPix, deltaPix)
    data = ImageData(**kwargs_data)
    _frame_size = numPix * deltaPix
    _coords = data
    x_grid, y_grid = data.pixel_coordinates
    lensModelExt = LensModelExtensions(lensModel)
    #ra_crit_list, dec_crit_list, ra_caustic_list, dec_caustic_list = lensModelExt.critical_curve_caustics(
    #    kwargs_lens, compute_window=_frame_size, grid_scale=deltaPix/2.)
    x_grid1d = util.image2array(x_grid)
    y_grid1d = util.image2array(y_grid)
    fermat_surface = lensModel.fermat_potential(x_grid1d, y_grid1d, kwargs_lens, sourcePos_x, sourcePos_y)
    fermat_surface = util.array2image(fermat_surface)

        #, cmap='Greys', vmin=-1, vmax=1) #, cmap=self._cmap, vmin=v_min, vmax=v_max)
    if with_caustics is True:
        ra_crit_list, dec_crit_list = lensModelExt.critical_curve_tiling(kwargs_lens, compute_window=_frame_size,
                                                                             start_scale=deltaPix/5, max_order=10)
        ra_caustic_list, dec_caustic_list = lensModel.ray_shooting(ra_crit_list, dec_crit_list, kwargs_lens)
        plot_util.plot_line_set(ax, _coords, ra_caustic_list, dec_caustic_list, shift=_frame_size/2., color='g')
        plot_util.plot_line_set(ax, _coords, ra_crit_list, dec_crit_list, shift=_frame_size/2., color='r')
    if point_source is True:
        from lenstronomy.LensModel.Solver.lens_equation_solver import LensEquationSolver
        solver = LensEquationSolver(lensModel)
        theta_x, theta_y = solver.image_position_from_source(sourcePos_x, sourcePos_y, kwargs_lens,
                                                                 min_distance=deltaPix, search_window=deltaPix*numPix)
github sibirrer / lenstronomy / lenstronomy / Workflow / psf_fitting.py View on Github external
def cutout_psf(self, ra_image, dec_image, x, y, image_list, kernelsize, kernel_init, block_center_neighbour=0):
        """

        :param x_:
        :param y_:
        :param image_list: list of images (i.e. data - all models subtracted, except a single point source)
        :param kernelsize:
        :return:
        """
        mask = self._image_model_class.likelihood_mask
        ra_grid, dec_grid = self._image_model_class.Data.pixel_coordinates
        ra_grid = util.image2array(ra_grid)
        dec_grid = util.image2array(dec_grid)
        radius = block_center_neighbour

        kernel_list = []
        for l in range(len(x)):
            mask_point_source = self.mask_point_source(ra_image, dec_image, ra_grid, dec_grid, radius, i=l)
            mask_i = mask * mask_point_source
            kernel_deshifted = self.cutout_psf_single(x[l], y[l], image_list[l], mask_i, kernelsize, kernel_init)
            kernel_list.append(kernel_deshifted)
        return kernel_list
github sibirrer / lenstronomy / lenstronomy / LensModel / Profiles / interpol.py View on Github external
:param f_yy: 2d numpy array of df/dyy, matching the grids in grid_interp_x and grid_interp_y
        :param f_xy: 2d numpy array of df/dxy, matching the grids in grid_interp_x and grid_interp_y
        :return: f_x, f_y at interpolated positions (x, y)
        """
        n = len(np.atleast_1d(x))
        if n <= 1 and np.shape(x) == ():
        #if type(x) == float or type(x) == int or type(x) == type(np.float64(1)) or len(x) <= 1:
            f_x_out = self.f_x_interp(x, y, grid_interp_x, grid_interp_y, f_x)
            f_y_out = self.f_y_interp(x, y, grid_interp_x, grid_interp_y, f_y)
            return f_x_out[0][0], f_y_out[0][0]
        else:
            if self._grid and n >= self._min_grid_number:
                x_, y_ = util.get_axes(x, y)
                f_x_out = self.f_x_interp(x_, y_, grid_interp_x, grid_interp_y, f_x)
                f_y_out = self.f_y_interp(x_, y_, grid_interp_x, grid_interp_y, f_y)
                f_x_out = util.image2array(f_x_out)
                f_y_out = util.image2array(f_y_out)
            else:
                #n = len(x)
                f_x_out, f_y_out = np.zeros(n), np.zeros(n)
                for i in range(n):
                    f_x_out[i] = self.f_x_interp(x[i], y[i], grid_interp_x, grid_interp_y, f_x)
                    f_y_out[i] = self.f_y_interp(x[i], y[i], grid_interp_x, grid_interp_y, f_y)
        return f_x_out, f_y_out
github sibirrer / lenstronomy / lenstronomy / Extensions / Itterative / iterative_source.py View on Github external
"""
        num_clump = len(x_pos)
        numShapelets = (num_order+2)*(num_order+1)/2
        num_param = numShapelets*num_clump
        A = np.zeros((num_param, numPix**2))
        k = 0
        for j in range(0, num_clump):
            H_x, H_y = self.shapelets.pre_calc(x_source, y_source, sigma[j], num_order, x_pos[j], y_pos[j])
            n1 = 0
            n2 = 0
            for i in range(0, numShapelets):
                kwargs_source_shapelet = {'center_x': x_pos[j], 'center_y': y_pos[j], 'n1': n1, 'n2': n2, 'beta': sigma[j], 'amp': 1}
                image = self.shapelets.function(H_x, H_y, **kwargs_source_shapelet)
                image = util.array2image(image)
                image = self.re_size_convolve(image, numPix, deltaPix, subgrid_res, kwargs_psf)
                response = util.image2array(image*mask)
                A[k, :] = response
                if n1 == 0:
                    n1 = n2 + 1
                    n2 = 0
                else:
                    n1 -= 1
                    n2 += 1
                k += 1
        return A
github sibirrer / lenstronomy / lenstronomy / Workflow / psf_fitting.py View on Github external
def cutout_psf(self, ra_image, dec_image, x, y, image_list, kernelsize, kernel_init, block_center_neighbour=0):
        """

        :param x_:
        :param y_:
        :param image_list: list of images (i.e. data - all models subtracted, except a single point source)
        :param kernelsize:
        :return:
        """
        mask = self._image_model_class.likelihood_mask
        ra_grid, dec_grid = self._image_model_class.Data.pixel_coordinates
        ra_grid = util.image2array(ra_grid)
        dec_grid = util.image2array(dec_grid)
        radius = block_center_neighbour

        kernel_list = []
        for l in range(len(x)):
            mask_point_source = self.mask_point_source(ra_image, dec_image, ra_grid, dec_grid, radius, i=l)
            mask_i = mask * mask_point_source
            kernel_deshifted = self.cutout_psf_single(x[l], y[l], image_list[l], mask_i, kernelsize, kernel_init)
            kernel_list.append(kernel_deshifted)
        return kernel_list
github sibirrer / lenstronomy / lenstronomy / Extensions / Itterative / iterative_lens.py View on Github external
def step(self, im_sim, model_error, param, x_grid, y_grid, kwargs_lens, kwargs_source, kwargs_psf, kwargs_else, numPix, deltaPix, subgrid_res, coeff_num=0, delta=0.01, fraction=1.):
        A = self.get_lens_perturb_matrix(param, x_grid, y_grid, kwargs_lens, kwargs_source, kwargs_psf, kwargs_else,
                                         numPix, deltaPix, subgrid_res, coeff_num, delta)
        im_sim_iter, param_iter = self.get_new_lens_model(A, util.image2array(model_error), im_sim)
        coeffs_new = kwargs_lens['coeffs'].copy()
        factor = np.sign(param_iter[0])*np.minimum(np.abs(param_iter[0]), 10)
        factor = param_iter[0]
        coeffs_new[coeff_num] -= factor * delta * fraction
        kwargs_lens_new = kwargs_lens.copy()
        kwargs_lens_new.update({'coeffs': coeffs_new})
        return kwargs_lens_new, im_sim_iter+im_sim