Beispiel #1
0
    def effective_einstein_radius(self, kwargs_lens, center_x=None, center_y=None, model_bool_list=None, grid_num=200,
                                  grid_spacing=0.05, get_precision=False, verbose=True):
        """
        computes the radius with mean convergence=1

        :param kwargs_lens: list of lens model keyword arguments
        :param spacing: number of annular bins to compute the convergence (resolution of the Einstein radius estimate)
        :param get_precision: If `True`, return the precision of estimated Einstein radius
        :return: estimate of the Einstein radius
        """
        center_x, center_y = analysis_util.profile_center(kwargs_lens, center_x, center_y)

        x_grid, y_grid = util.make_grid(numPix=grid_num, deltapix=grid_spacing)
        x_grid += center_x
        y_grid += center_y
        kappa = self._lens_model.kappa(x_grid, y_grid, kwargs_lens, k=model_bool_list)
        if self._lens_model.lens_model_list[0] in ['INTERPOL', 'INTERPOL_SCALED']:
            center_x = x_grid[kappa == np.max(kappa)][0]
            center_y = y_grid[kappa == np.max(kappa)][0]
        #kappa = util.array2image(kappa)
        r_array = np.linspace(0, grid_num*grid_spacing/2., grid_num*2)
        for r in r_array:
            mask = np.array(1 - mask_util.mask_center_2d(center_x, center_y, r, x_grid, y_grid))
            sum_mask = np.sum(mask)
            if sum_mask > 0:
                kappa_mean = np.sum(kappa*mask)/np.sum(mask)
                if kappa_mean < 1:
                    if get_precision:
                        return r, r_array[1] - r_array[0]
                    else:
                        return r
        if verbose:
            Warning("No Einstein radius computed for the following model!", kwargs_lens)
        return np.nan
Beispiel #2
0
def test_get_mask():
    x = np.linspace(0, 10, 100)
    y = np.linspace(0, 10, 100)
    center_x = 5
    center_y = 5
    r = 1
    mask = mask_util.mask_center_2d(center_x, center_y, r, x, y)
    assert mask[0] == 1
    assert mask[50] == 0
Beispiel #3
0
    def effective_einstein_radius(self,
                                  kwargs_lens,
                                  center_x=None,
                                  center_y=None,
                                  model_bool_list=None,
                                  grid_num=200,
                                  grid_spacing=0.05,
                                  get_precision=False,
                                  verbose=True):
        """
        computes the radius with mean convergence=1

        :param kwargs_lens: list of lens model keyword arguments
        :param center_x: position of the center (if not set, is attempting to find it from the parameters kwargs_lens)
        :param center_y: position of the center (if not set, is attempting to find it from the parameters kwargs_lens)
        :param model_bool_list: list of booleans indicating the addition (=True) of a model component in computing the
         Einstein radius
        :param grid_num: integer, number of grid points to numerically evaluate the convergence and estimate the
         Einstein radius
        :param grid_spacing: spacing in angular units of the grid
        :param get_precision: If `True`, return the precision of estimated Einstein radius
        :param verbose: boolean, if True prints warning if indication of insufficient result
        :return: estimate of the Einstein radius
        """
        center_x, center_y = analysis_util.profile_center(
            kwargs_lens, center_x, center_y)

        x_grid, y_grid = util.make_grid(numPix=grid_num, deltapix=grid_spacing)
        x_grid += center_x
        y_grid += center_y
        kappa = self._lens_model.kappa(x_grid,
                                       y_grid,
                                       kwargs_lens,
                                       k=model_bool_list)
        if self._lens_model.lens_model_list[0] in [
                'INTERPOL', 'INTERPOL_SCALED'
        ]:
            center_x = x_grid[kappa == np.max(kappa)][0]
            center_y = y_grid[kappa == np.max(kappa)][0]
        #kappa = util.array2image(kappa)
        r_array = np.linspace(0, grid_num * grid_spacing / 2., grid_num * 2)
        for r in r_array:
            mask = np.array(1 - mask_util.mask_center_2d(
                center_x, center_y, r, x_grid, y_grid))
            sum_mask = np.sum(mask)
            if sum_mask > 0:
                kappa_mean = np.sum(kappa * mask) / np.sum(mask)
                if kappa_mean < 1:
                    if get_precision:
                        return r, r_array[1] - r_array[0]
                    else:
                        return r
        if verbose:
            Warning("No Einstein radius computed for the following model!",
                    kwargs_lens)
        return np.nan
Beispiel #4
0
    def get_masks(self):
        """
        Create masks.

        :return:
        :rtype:
        """
        if 'mask' in self.settings:
            if self.settings['mask'] is not None:
                if 'provided' in self.settings['mask'] \
                        and self.settings['mask']['provided'] is not None:
                    return self.settings['mask']['provided']
                else:
                    masks = []
                    mask_options = deepcopy(self.settings['mask'])

                    for n in range(self.band_number):
                        ra_at_xy_0 = mask_options['ra_at_xy_0'][n]
                        dec_at_xy_0 = mask_options['dec_at_xy_0'][n]
                        transform_pix2angle = np.array(
                            mask_options['transform_matrix'][n]
                        )
                        num_pixel = mask_options['size'][n]
                        radius = mask_options['radius'][n]
                        offset = mask_options['centroid_offset'][n]

                        coords = Coordinates(transform_pix2angle,
                                             ra_at_xy_0, dec_at_xy_0)

                        x_coords, y_coords = coords.coordinate_grid(num_pixel,
                                                                    num_pixel)

                        mask_outer = mask_util.mask_center_2d(
                            self.deflector_center_ra + offset[0],
                            self.deflector_center_dec + offset[1],
                            radius,
                            util.image2array(x_coords),
                            util.image2array(y_coords)
                        )

                        extra_masked_regions = []
                        try:
                            self.settings['mask']['extra_regions']
                        except (NameError, KeyError):
                            pass
                        else:
                            if self.settings['mask']['extra_regions'] is \
                                    not None:
                                for reg in self.settings['mask'][
                                                        'extra_regions'][n]:
                                    extra_masked_regions.append(
                                        mask_util.mask_center_2d(
                                            self.deflector_center_ra + reg[0],
                                            self.deflector_center_dec + reg[1],
                                            reg[2],
                                            util.image2array(x_coords),
                                            util.image2array(y_coords)
                                        )
                                    )

                        mask = 1. - mask_outer

                        for extra_region in extra_masked_regions:
                            mask *= extra_region

                        # sanity check
                        mask[mask >= 1.] = 1.
                        mask[mask <= 0.] = 0.

                        masks.append(util.array2image(mask))

                return masks

        return None
Beispiel #5
0
    def get_masks(self):
        """
        Create masks.

        :return:
        :rtype:
        """
        if 'mask' in self.settings:
            if self.settings['mask'] is not None:
                if 'provided' in self.settings['mask'] \
                        and self.settings['mask']['provided'] is not None:
                    return self.settings['mask']['provided']
                else:
                    masks = []
                    mask_options = deepcopy(self.settings['mask'])

                    for n in range(self.band_number):
                        ra_at_xy_0 = mask_options['ra_at_xy_0'][n]
                        dec_at_xy_0 = mask_options['dec_at_xy_0'][n]
                        transform_pix2angle = np.array(
                            mask_options['transform_matrix'][n]
                        )
                        num_pixel = mask_options['size'][n]
                        radius = mask_options['radius'][n]
                        offset = mask_options['centroid_offset'][n]

                        coords = Coordinates(transform_pix2angle,
                                             ra_at_xy_0, dec_at_xy_0)

                        x_coords, y_coords = coords.coordinate_grid(num_pixel,
                                                                    num_pixel)

                        mask_outer = mask_util.mask_center_2d(
                            self.deflector_center_ra + offset[0],
                            self.deflector_center_dec + offset[1],
                            radius,
                            util.image2array(x_coords),
                            util.image2array(y_coords)
                        )

                        extra_masked_regions = []
                        try:
                            self.settings['mask']['extra_regions']
                        except (NameError, KeyError):
                            pass
                        else:
                            if self.settings['mask']['extra_regions'] is \
                                    not None:
                                for reg in self.settings['mask'][
                                                        'extra_regions'][n]:
                                    extra_masked_regions.append(
                                        mask_util.mask_center_2d(
                                            self.deflector_center_ra + reg[0],
                                            self.deflector_center_dec + reg[1],
                                            reg[2],
                                            util.image2array(x_coords),
                                            util.image2array(y_coords)
                                        )
                                    )

                        mask = 1. - mask_outer

                        for extra_region in extra_masked_regions:
                            mask *= extra_region
                        # Mask Edge Pixels
                        try:
                            self.settings['mask']['mask_edge_pixels']
                        except (NameError, KeyError):
                            pass
                        else:
                            border_length = \
                                self.settings['mask']['mask_edge_pixels'][n]
                            if border_length > 0:
                                edge_mask = 0 * np.ones(
                                 (num_pixel, num_pixel), dtype=int)

                                edge_mask[border_length:-border_length,
                                          border_length:-border_length] = 1
                                edge_mask = (edge_mask.flatten()).tolist()
                            elif border_length == 0:
                                edge_mask = 1 * np.ones(
                                   (num_pixel, num_pixel), dtype=int)
                                edge_mask = (edge_mask.flatten()).tolist()

                            mask *= edge_mask
                        # Add custom Mask
                        try:
                            self.settings['mask']['custom_mask']
                        except (NameError, KeyError):
                            pass
                        else:
                            if self.settings['mask']['custom_mask'][n]\
                                    is not None:
                                provided_mask = \
                                    self.settings['mask']['custom_mask'][n]
                                provided_mask = np.array(provided_mask)
                                # make sure that mask consist of only 0 and 1
                                provided_mask[provided_mask > 0.] = 1.
                                provided_mask[provided_mask <= 0.] = 0.
                                mask *= provided_mask

                        # sanity check
                        mask[mask >= 1.] = 1.
                        mask[mask <= 0.] = 0.

                        masks.append(util.array2image(mask))

                return masks

        return None