Example #1
0
def make_lens_image():
    ccd_data = ccd.CCDData(np.array(np.zeros(shape)),
                           pixel_scale=1.0,
                           psf=ccd.PSF(np.ones((3, 3)), pixel_scale=1.0),
                           noise_map=ccd.NoiseMap(np.ones(shape),
                                                  pixel_scale=1.0))
    mask = msk.Mask.circular(shape=shape, pixel_scale=1, radius_arcsec=3.0)
    return li.LensData(ccd_data=ccd_data, mask=mask)
Example #2
0
    def test__fit_figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, ccd_data):

        lens_galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=0.1))
        source_galaxy = g.Galaxy(
            pixelization=pix.Rectangular(shape=(4, 4)),
            regularization=reg.Constant(coefficients=(1.0, )))

        phase = ph.LensPlanePhase(lens_galaxies=[lens_galaxy],
                                  mask_function=ph.default_mask_function,
                                  cosmology=cosmo.FLRW,
                                  phase_name='test_phase')
        analysis = phase.make_analysis(data=ccd_data)
        instance = phase.constant
        fit_figure_of_merit = analysis.fit(instance=instance)

        mask = phase.mask_function(image=ccd_data.image)
        lens_data = li.LensData(ccd_data=ccd_data, mask=mask)
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = lens_fit.LensProfileFit(lens_data=lens_data, tracer=tracer)

        assert fit.likelihood == fit_figure_of_merit

        phase = ph.LensSourcePlanePhase(lens_galaxies=[lens_galaxy],
                                        source_galaxies=[source_galaxy],
                                        mask_function=ph.default_mask_function,
                                        cosmology=cosmo.FLRW,
                                        phase_name='test_phase')
        analysis = phase.make_analysis(data=ccd_data)
        instance = phase.constant
        fit_figure_of_merit = analysis.fit(instance=instance)

        mask = phase.mask_function(image=ccd_data.image)
        lens_data = li.LensData(ccd_data=ccd_data, mask=mask)
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = lens_fit.LensProfileInversionFit(lens_data=lens_data,
                                               tracer=tracer)

        assert fit.evidence == fit_figure_of_merit
def perform_fit_with_lens_and_source_galaxy(lens_galaxy, source_galaxy):

    ccd_data = simulate()
    mask = msk.Mask.circular_annular(shape=ccd_data.shape,
                                     pixel_scale=ccd_data.pixel_scale,
                                     inner_radius_arcsec=0.5,
                                     outer_radius_arcsec=2.2)
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=lens_data.border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
Example #4
0
    def test__constructor_inputs(self):

        psf = ccd.PSF(np.ones((7, 7)), 1)
        image = ccd.CCDData(np.ones((51, 51)), pixel_scale=3., psf=psf, noise_map=np.ones((51, 51)))
        mask = msk.Mask.unmasked_for_shape_and_pixel_scale(shape=(51, 51), pixel_scale=1.0, invert=True)
        mask[26, 26] = False

        lens_data = ld.LensData(image, mask, sub_grid_size=8, image_psf_shape=(5, 5),
                                 mapping_matrix_psf_shape=(3, 3), positions=[np.array([[1.0, 1.0]])])

        assert lens_data.sub_grid_size == 8
        assert lens_data.convolver_image.psf_shape == (5, 5)
        assert lens_data.convolver_mapping_matrix.psf_shape == (3, 3)
        assert (lens_data.positions[0] == np.array([[1.0, 1.0]])).all()

        assert lens_data.image_psf_shape == (5,5)
        assert lens_data.mapping_matrix_psf_shape == (3,3)
def perform_fit_with_source_galaxy(source_galaxy):

    ccd_data = simulate()
    mask = msk.Mask.circular_annular(shape=ccd_data.shape,
                                     pixel_scale=ccd_data.pixel_scale,
                                     inner_radius_arcsec=0.5,
                                     outer_radius_arcsec=2.2)
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6))
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=lens_data.border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
Example #6
0
def perform_fit_with_source_galaxy_mask_and_border(source_galaxy, mask,
                                                   use_border):

    ccd_data = simulate()
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6))

    if use_border:
        border = lens_data.border
    else:
        border = None

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=border)
    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
Example #7
0
def make_lens_data_blur():

    psf = ccd.PSF(array=(np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0],
                                   [1.0, 1.0, 1.0]])),
                  pixel_scale=1.0,
                  renormalize=False)
    im = ccd.CCDData(image=5.0 * np.ones((6, 6)),
                     pixel_scale=1.0,
                     psf=psf,
                     noise_map=np.ones((6, 6)),
                     exposure_time_map=3.0 * np.ones((6, 6)),
                     background_sky_map=4.0 * np.ones((6, 6)))

    ma = np.array([[True, True, True, True, True, True],
                   [True, False, False, False, False, True],
                   [True, False, False, False, False, True],
                   [True, False, False, False, False, True],
                   [True, False, False, False, False, True],
                   [True, True, True, True, True, True]])
    ma = mask.Mask(array=ma, pixel_scale=1.0)

    return ld.LensData(ccd_data=im, mask=ma, sub_grid_size=2)
Example #8
0
def perform_fit_x2_lenses_with_source_galaxy_mask_and_border(
        source_galaxy, mask, use_border):
    simulate_image_x2_lenses()
    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
    lens_galaxy_0 = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(1.1, 0.51), axis_ratio=0.9, phi=110.0, einstein_radius=1.07))
    lens_galaxy_1 = g.Galaxy(mass=mp.EllipticalIsothermal(
        centre=(-0.20,
                -0.35), axis_ratio=0.56, phi=16.0, einstein_radius=0.71))

    if use_border:
        border = lens_data.border
    else:
        border = None

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy_0, lens_galaxy_1],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack,
        border=border)

    return lens_fit.fit_lens_data_with_tracer(lens_data=lens_data,
                                              tracer=tracer)
def make_lens_image(image, mask):
    return li.LensData(ccd_data=image, mask=mask)
# Again, before we checkout the pipeline, lets import it, and get it running.
from workspace.howtolens.chapter_3_pipelines import tutorial_3_pipeline_complex_source

pipeline_complex_source = tutorial_3_pipeline_complex_source.make_pipeline(
    pipeline_path='/howtolens/c3_t3_complex_source/')
pipeline_complex_source.run(data=ccd_data)

# Okay, so with 4 sources, we still couldn't get a good a fit to the source that didn't leave residuals. The thing is,
# I did infact simulate the lens with 4 sources. This means that there is a 'perfect fit', somewhere in that parameter
# space, that we unfortunately missed using the pipeline above.
#
# Lets confirm this, by manually fitting the ccd data with the true input model.

lens_data = ld.LensData(ccd_data=ccd_data,
                        mask=msk.Mask.circular(
                            shape=ccd_data.shape,
                            pixel_scale=ccd_data.pixel_scale,
                            radius_arcsec=3.0))

lens_galaxy = g.Galaxy(mass=mp.EllipticalIsothermal(
    centre=(0.0, 0.0), axis_ratio=0.8, phi=135.0, einstein_radius=1.6))
source_galaxy_0 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.1, 0.1),
                                                     axis_ratio=0.8,
                                                     phi=90.0,
                                                     intensity=0.2,
                                                     effective_radius=1.0,
                                                     sersic_index=1.5))
source_galaxy_1 = g.Galaxy(light=lp.EllipticalSersic(centre=(-0.25, 0.25),
                                                     axis_ratio=0.7,
                                                     phi=45.0,
                                                     intensity=0.1,
Example #11
0
def make_lens_ccd(ccd, mask):
    return ld.LensData(ccd_data=ccd, mask=mask)
Example #12
0
pixelization = pix.AdaptiveMagnification(shape=pixelization_shape)

source_galaxy = g.Galaxy(pixelization=pixelization,
                         regularization=reg.Constant(coefficients=(1.0, )))

for image_type in ['LSST', 'Euclid', 'HST', 'HST_Up', 'AO']:

    ccd_data = tools.load_profiling_ccd_data(image_type=image_type,
                                             lens_name='no_lens_source_smooth',
                                             psf_shape=psf_shape)
    mask = msk.Mask.circular(shape=ccd_data.shape,
                             pixel_scale=ccd_data.pixel_scale,
                             radius_arcsec=radius_arcsec)
    lens_data = ld.LensData(ccd_data=ccd_data,
                            mask=mask,
                            sub_grid_size=sub_grid_size)

    print('AdaptiveMagnification Inversion fit run times for image type ' +
          image_type + '\n')
    print('Number of points = ' + str(lens_data.grid_stack.sub.shape[0]) +
          '\n')

    start_overall = time.time()

    start = time.time()
    for i in range(repeats):
        tracer = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[lens_galaxy],
            source_galaxies=[source_galaxy],
            image_plane_grid_stack=lens_data.grid_stack)
Example #13
0
# Now we've loaded the ccd data and created a mask, we use them to create a 'lens data' object, which we'll perform
# using the lens_data module (imported as 'ld').

# A lens data object is a 'package' of all parts of a data-set we need in order to fit it with a lens model:

# 1) The ccd-data, e.g. the image, PSF (so that when we compare a tracer's image-plane image to the image data we
#    can include blurring due to the telescope optics) and noise-map (so our goodness-of-fit measure accounts for
#    noise in the observations).

# 2) The mask, so that only the regions of the image with a signal are fitted.

# 3) A grid-stack aligned to the ccd-imaging data's pixels: so the tracer's image-plane image is generated on the same
#    (masked) grid as the image.

lens_data = ld.LensData(ccd_data=ccd_data, mask=mask)
ccd_plotters.plot_image(ccd_data=ccd_data)

# By printing its attribute, we can see that it does indeed contain the image, mask, psf and so on
print('Image:')
print(lens_data.image)
print('Noise-Map:')
print(lens_data.noise_map)
print('PSF:')
print(lens_data.psf)
print('Mask')
print(lens_data.mask)
print('Grid')
print(lens_data.grid_stack.regular)

# The image, noise-map and grids are masked using the mask and mapped to 1D arrays for fast calcuations.
Example #14
0
 def test_default_mask_function(self, phase, ccd_data):
     lens_data = li.LensData(ccd_data=ccd_data,
                             mask=phase.mask_function(ccd_data.image))
     assert len(lens_data.image_1d) == 32
Example #15
0
                        radius_arcsec=2.0)
# lens_data = ld.LensData(ccd_data=ccd_data_small_stamp, mask=mask) # Gives an error due to the mask's blurring region hitting an edge

# We can overcome this using the same input as before. However, now, the resized regular shape is bigger than the regular,
# thus a padding of zeros is introduced to the edges.
ccd_data_small_stamp_padded = ccd.load_ccd_data_from_fits(
    image_path=path + 'data/image_small_stamp/image.fits',
    pixel_scale=0.1,
    noise_map_path=path + 'data/image_small_stamp/noise_map.fits',
    psf_path=path + 'data/image_small_stamp/psf.fits',
    resized_ccd_shape=(140, 140))
mask = ma.Mask.circular(shape=ccd_data_small_stamp_padded.shape,
                        pixel_scale=ccd_data_small_stamp_padded.pixel_scale,
                        radius_arcsec=2.0)
ccd_plotters.plot_ccd_subplot(ccd_data=ccd_data_small_stamp_padded, mask=mask)
lens_data = ld.LensData(ccd_data=ccd_data_small_stamp_padded,
                        mask=mask)  # No error anymore!

########## IVE INCLUDED THE TEXT FOR 5 BELOW SO YOU CAN BE AWARE OF CENTERING, BUT THE BUILT IN FUNCTIONALITY FOR #####
########## RECENTERING CURRENTLY DOES NOT WORK :( ###########

# 5) Lens Galaxy Centering - The regular should place the lens galaxy in the origin of the regular, as opposed to a
#    corner. This ensures the origin of the lens galaxy's light and mass profiles will be near the origin (0.0", 0.0"),
#    as wel as the origin of the masks, which is a more intuitive coordinate system. The priors on the light
#    profiles and mass profile also assume a origin of (0.0", 0.0"), as well as the default masks origin.

# Lets look at an off-center regular - clearly both the lens galaxy and Einstein ring are offset in the positive y and x d
# directions.

# ccd_data_offset_centre = ccd.load_ccd_data_from_fits(image_path=path+'data/image_offset_centre/image.fits', pixel_scale=0.1,
#                                   noise_map_path=path+'data/image_offset_centre/noise_map.fits',
#                                   psf_path=path+'data/image_offset_centre/psf.fits')
def test__simulate_lensed_source_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct(
):

    psf = ccd.PSF.simulate_as_gaussian(shape=(3, 3),
                                       pixel_scale=0.2,
                                       sigma=0.75)

    grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(11, 11),
                                                           pixel_scale=0.2,
                                                           psf_shape=psf.shape,
                                                           sub_grid_size=1)

    lens_galaxy = g.Galaxy(light=lp.EllipticalSersic(centre=(0.1, 0.1),
                                                     intensity=0.1),
                           mass=mp.EllipticalIsothermal(centre=(0.1, 0.1),
                                                        einstein_radius=1.8))
    source_galaxy = g.Galaxy(
        light=lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5))
    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=grid_stack)

    ccd_simulated = ccd.CCDData.simulate(
        array=tracer.image_plane_image_for_simulation,
        pixel_scale=0.2,
        exposure_time=300.0,
        psf=psf,
        background_sky_level=None,
        add_noise=False)
    ccd_simulated.noise_map = np.ones(ccd_simulated.image.shape)

    path = "{}/data/simulate_and_fit".format(
        os.path.dirname(os.path.realpath(
            __file__)))  # Setup path so we can output the simulated image.

    try:
        shutil.rmtree(path)
    except FileNotFoundError:
        pass

    if os.path.exists(path) == False:
        os.makedirs(path)

    array_util.numpy_array_to_fits(array=ccd_simulated.image,
                                   file_path=path + '/image.fits')
    array_util.numpy_array_to_fits(array=ccd_simulated.noise_map,
                                   file_path=path + '/noise_map.fits')
    array_util.numpy_array_to_fits(array=psf, file_path=path + '/psf.fits')

    ccd_data = ccd.load_ccd_data_from_fits(image_path=path + '/image.fits',
                                           noise_map_path=path +
                                           '/noise_map.fits',
                                           psf_path=path + '/psf.fits',
                                           pixel_scale=0.2)

    mask = msk.Mask.circular(shape=ccd_data.image.shape,
                             pixel_scale=0.2,
                             radius_arcsec=0.8)

    lens_data = ld.LensData(ccd_data=ccd_data, mask=mask, sub_grid_size=1)

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[source_galaxy],
        image_plane_grid_stack=lens_data.grid_stack)

    fitter = lens_fit.LensProfileFit(lens_data=lens_data, tracer=tracer)

    assert fitter.chi_squared == pytest.approx(0.0, 1e-4)