예제 #1
0
    def source(self, numPix, deltaPix, center=None, image_orientation=True):
        """

        :param numPix: number of pixels per axes
        :param deltaPix: pixel size
        :param image_orientation: bool, if True, uses frame in orientation of the image, otherwise in RA-DEC coordinates
        :return: 2d surface brightness grid of the reconstructed source and Coordinates() instance of source grid
        """
        if image_orientation is True:
            Mpix2coord = self._coords.transform_pix2angle * deltaPix / self._deltaPix
            x_grid_source, y_grid_source = util.make_grid_transformed(
                numPix, Mpix2Angle=Mpix2coord)
            ra_at_xy_0, dec_at_xy_0 = x_grid_source[0], y_grid_source[0]
        else:
            x_grid_source, y_grid_source, ra_at_xy_0, dec_at_xy_0, x_at_radec_0, y_at_radec_0, Mpix2coord, Mcoord2pix = util.make_grid_with_coordtransform(
                numPix, deltaPix)

        center_x = 0
        center_y = 0
        if center is not None:
            center_x, center_y = center[0], center[1]
        elif len(self._kwargs_source_partial) > 0:
            center_x = self._kwargs_source_partial[0]['center_x']
            center_y = self._kwargs_source_partial[0]['center_y']
        x_grid_source += center_x
        y_grid_source += center_y

        coords_source = Coordinates(transform_pix2angle=Mpix2coord,
                                    ra_at_xy_0=ra_at_xy_0 + center_x,
                                    dec_at_xy_0=dec_at_xy_0 + center_y)

        source = self._bandmodel.SourceModel.surface_brightness(
            x_grid_source, y_grid_source, self._kwargs_source_partial)
        source = util.array2image(source) * deltaPix**2
        return source, coords_source
예제 #2
0
 def error_map_source_plot(self, ax, numPix, deltaPix_source, v_min=None, v_max=None, with_caustics=False):
     x_grid_source, y_grid_source = util.make_grid_transformed(numPix,
                                                               self._Mpix2coord * deltaPix_source / self._deltaPix)
     x_center = self._kwargs_source[0]['center_x']
     y_center = self._kwargs_source[0]['center_y']
     x_grid_source += x_center
     y_grid_source += y_center
     coords_source = Coordinates(self._Mpix2coord * deltaPix_source / self._deltaPix, ra_at_xy_0=x_grid_source[0],
                                 dec_at_xy_0=y_grid_source[0])
     error_map_source = self._analysis.error_map_source(self._kwargs_source, x_grid_source, y_grid_source, self._cov_param)
     error_map_source = util.array2image(error_map_source)
     d_s = numPix * deltaPix_source
     im = ax.matshow(error_map_source, origin='lower', extent=[0, d_s, 0, d_s],
                     cmap=self._cmap, vmin=v_min, vmax=v_max)  # source
     ax.get_xaxis().set_visible(False)
     ax.get_yaxis().set_visible(False)
     ax.autoscale(False)
     divider = make_axes_locatable(ax)
     cax = divider.append_axes("right", size="5%", pad=0.05)
     cb = plt.colorbar(im, cax=cax)
     cb.set_label(r'error variance', fontsize=15)
     if with_caustics:
         ra_caustic_list, dec_caustic_list = self._caustics()
         plot_line_set(ax, coords_source, ra_caustic_list, dec_caustic_list, color='b')
     scale_bar(ax, d_s, dist=0.1, text='0.1"', color='w', flipped=False)
     coordinate_arrows(ax, d_s, coords_source, arrow_size=self._arrow_size, color='w')
     text_description(ax, d_s, text="Error map in source", color="w", backgroundcolor='k', flipped=False)
     source_position_plot(ax, coords_source, self._kwargs_source)
     return ax
예제 #3
0
def test_make_grid_transform():
    numPix = 11
    theta = np.pi / 2
    deltaPix = 0.05
    Mpix2coord = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]]) * deltaPix
    ra_coord, dec_coord = util.make_grid_transformed(numPix, Mpix2coord)
    ra2d = util.array2image(ra_coord)
    assert ra2d[5, 5] == 0
    assert ra2d[4, 5] == deltaPix
    npt.assert_almost_equal(ra2d[5, 4], 0, decimal=10)
예제 #4
0
    def error_map_source_plot(self, ax, numPix, deltaPix_source, v_min=None, v_max=None, with_caustics=False,
                              font_size=15, point_source_position=True):
        """
        plots the uncertainty in the surface brightness in the source from the linear inversion by taking the diagonal
        elements of the covariance matrix of the inversion of the basis set to be propagated to the source plane.
        #TODO illustration of the uncertainties in real space with the full covariance matrix is subtle. The best way is probably to draw realizations from the covariance matrix.

        :param ax: matplotlib axis instance
        :param numPix: number of pixels in plot per axis
        :param deltaPix_source: pixel spacing in the source resolution illustrated in plot
        :param v_min: minimum plotting scale of the map
        :param v_max: maximum plotting scale of the map
        :param with_caustics: plot the caustics on top of the source reconstruction (may take some time)
        :param font_size: font size of labels
        :param point_source_position: boolean, if True, plots a point at the position of the point source
        :return: plot of source surface brightness errors in the reconstruction on the axis instance
        """
        x_grid_source, y_grid_source = util.make_grid_transformed(numPix,
                                                                  self._coords.transform_pix2angle * deltaPix_source / self._deltaPix)
        x_center = self._kwargs_source_partial[0]['center_x']
        y_center = self._kwargs_source_partial[0]['center_y']
        x_grid_source += x_center
        y_grid_source += y_center
        coords_source = Coordinates(self._coords.transform_pix2angle * deltaPix_source / self._deltaPix, ra_at_xy_0=x_grid_source[0],
                                    dec_at_xy_0=y_grid_source[0])
        error_map_source = self.bandmodel.error_map_source(self._kwargs_source_partial, x_grid_source, y_grid_source,
                                                           self._cov_param, model_index_select=False)
        error_map_source = util.array2image(error_map_source)
        d_s = numPix * deltaPix_source
        im = ax.matshow(error_map_source, origin='lower', extent=[0, d_s, 0, d_s],
                        cmap=self._cmap, vmin=v_min, vmax=v_max)  # source
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.autoscale(False)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cb = plt.colorbar(im, cax=cax)
        cb.set_label(r'error variance', fontsize=font_size)
        if with_caustics:
            ra_caustic_list, dec_caustic_list = self._caustics()
            plot_util.plot_line_set(ax, coords_source, ra_caustic_list, dec_caustic_list, color='b')
        plot_util.scale_bar(ax, d_s, dist=0.1, text='0.1"', color='w', flipped=False, font_size=font_size)
        plot_util.coordinate_arrows(ax, d_s, coords_source,
                          arrow_size=self._arrow_size, color='w', font_size=font_size)
        plot_util.text_description(ax, d_s, text="Error map in source", color="w",
                         backgroundcolor='k', flipped=False, font_size=font_size)
        if point_source_position is True:
            ra_source, dec_source = self.bandmodel.PointSource.source_position(self._kwargs_ps_partial, self._kwargs_lens_partial)
            plot_util.source_position_plot(ax, coords_source, ra_source, dec_source)
        return ax
예제 #5
0
    def source_plot(self, ax, numPix, deltaPix_source, source_sigma=0.001, convolution=False, v_min=None, v_max=None, with_caustics=False):
        """

        :param ax:
        :param coords_source:
        :param source:
        :return:
        """
        if v_min is None:
            v_min = self._v_min_default
        if v_max is None:
            v_max = self._v_max_default
        d_s = numPix * deltaPix_source
        x_grid_source, y_grid_source = util.make_grid_transformed(numPix,
                                                                  self._Mpix2coord * deltaPix_source / self._deltaPix)
        if len(self._kwargs_source) > 0:
            x_center = self._kwargs_source[0]['center_x']
            y_center = self._kwargs_source[0]['center_y']
            x_grid_source += x_center
            y_grid_source += y_center
        coords_source = Coordinates(self._Mpix2coord * deltaPix_source / self._deltaPix, ra_at_xy_0=x_grid_source[0],
                                    dec_at_xy_0=y_grid_source[0])

        source = self._imageModel.SourceModel.surface_brightness(x_grid_source, y_grid_source, self._kwargs_source)
        source = util.array2image(source)
        if convolution is True:
            source = ndimage.filters.gaussian_filter(source, sigma=source_sigma / deltaPix_source, mode='nearest',
                                                      truncate=20)

        im = ax.matshow(np.log10(source), origin='lower', extent=[0, d_s, 0, d_s],
                        cmap=self._cmap, vmin=v_min, vmax=v_max)  # source
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.autoscale(False)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", size="5%", pad=0.05)
        cb = plt.colorbar(im, cax=cax)
        cb.set_label(r'log$_{10}$ flux', fontsize=15)
        if with_caustics:
            ra_caustic_list, dec_caustic_list = self._caustics()
            plot_line_set(ax, coords_source, ra_caustic_list, dec_caustic_list, color='b')
        scale_bar(ax, d_s, dist=0.1, text='0.1"', color='w', flipped=False)
        coordinate_arrows(ax, d_s, coords_source, arrow_size=self._arrow_size, color='w')
        text_description(ax, d_s, text="Reconstructed source", color="w", backgroundcolor='k', flipped=False)
        source_position_plot(ax, coords_source, self._kwargs_source)
        return ax
    def test_rescaled_grid(self):
        import lenstronomy.Util.util as util
        numPix = 10
        theta = 0.5
        deltaPix = 0.05
        subgrid_res = 3
        Mpix2a = np.array([[np.cos(theta), -np.sin(theta)],
                           [np.sin(theta), np.cos(theta)]]) * deltaPix
        x_grid, y_grid = util.make_grid_transformed(numPix, Mpix2a)
        coords = Coordinates(Mpix2a,
                             ra_at_xy_0=x_grid[0],
                             dec_at_xy_0=y_grid[0])
        x_grid_high_res, y_grid_high_res = util.make_subgrid(
            x_grid, y_grid, subgrid_res=subgrid_res)
        coords_sub = Coordinates(Mpix2a / subgrid_res,
                                 ra_at_xy_0=x_grid_high_res[0],
                                 dec_at_xy_0=y_grid_high_res[0])

        x, y = coords_sub.map_coord2pix(x_grid[1], y_grid[1])
        npt.assert_almost_equal(x, 4, decimal=10)
        npt.assert_almost_equal(y, 1, decimal=10)
        x, y = coords_sub.map_coord2pix(x_grid[0], y_grid[0])
        npt.assert_almost_equal(x, 1, decimal=10)
        npt.assert_almost_equal(y, 1, decimal=10)

        ra, dec = coords_sub.map_pix2coord(1, 1)
        npt.assert_almost_equal(ra, x_grid[0], decimal=10)
        npt.assert_almost_equal(dec, y_grid[0], decimal=10)

        ra, dec = coords_sub.map_pix2coord(1 + 2 * subgrid_res, 1)
        npt.assert_almost_equal(ra, x_grid[2], decimal=10)
        npt.assert_almost_equal(dec, y_grid[2], decimal=10)

        x_2d = util.array2image(x_grid)
        y_2d = util.array2image(y_grid)

        ra, dec = coords_sub.map_pix2coord(1 + 2 * subgrid_res,
                                           1 + 3 * subgrid_res)
        npt.assert_almost_equal(ra, x_2d[3, 2], decimal=10)
        npt.assert_almost_equal(dec, y_2d[3, 2], decimal=10)

        ra, dec = coords.map_pix2coord(2, 3)
        npt.assert_almost_equal(ra, x_2d[3, 2], decimal=10)
        npt.assert_almost_equal(dec, y_2d[3, 2], decimal=10)
    def source(self, num_pix, delta_pix, center=None):
        """
        source in the same coordinate system as the image

        :param num_pix: number of pixels per axes
        :param delta_pix: pixel size
        :param center: list with two entries [center_x, center_y] (optional)
        :return: 2d surface brightness grid of the reconstructed source and PixelGrid() instance of source grid
        """
        Mpix2coord = self._pixel_grid_joint.transform_pix2angle * delta_pix / self._pixel_grid_joint.pixel_width
        x_grid_source, y_grid_source = util.make_grid_transformed(
            num_pix, Mpix2Angle=Mpix2coord)
        ra_at_xy_0, dec_at_xy_0 = x_grid_source[0], y_grid_source[0]

        image_model = self.model_band_list[0].image_model_class
        kwargs_model = self.model_band_list[0].kwargs_model
        kwargs_source = kwargs_model['kwargs_source']

        center_x = 0
        center_y = 0
        if center is not None:
            center_x, center_y = center[0], center[1]
        elif len(kwargs_source) > 0:
            center_x = kwargs_source[0]['center_x']
            center_y = kwargs_source[0]['center_y']
        x_grid_source += center_x
        y_grid_source += center_y

        pixel_grid = PixelGrid(nx=num_pix,
                               ny=num_pix,
                               transform_pix2angle=Mpix2coord,
                               ra_at_xy_0=ra_at_xy_0 + center_x,
                               dec_at_xy_0=dec_at_xy_0 + center_y)

        source = image_model.SourceModel.surface_brightness(
            x_grid_source, y_grid_source, kwargs_source)
        source = util.array2image(source) * delta_pix**2
        return source, pixel_grid