예제 #1
0
        def test__same_as_above__multiple_galaxies(self, grid_stack_0,
                                                   grid_stack_1):

            # Overwrite one value so intensity in each pixel is different
            grid_stack_0.blurring[1] = np.array([2.0, 2.0])
            grid_stack_1.blurring[1] = np.array([2.0, 2.0])

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0))
            g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0))

            g0_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_0.blurring, galaxies=[g0])
            g1_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_0.blurring, galaxies=[g1])

            plane_stack = pl_stack.PlaneStack(
                galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1])

            assert (plane_stack.image_plane_blurring_images_1d[0] ==
                    g0_image_grid_0 + g1_image_grid_0).all()

            g0_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_1.blurring, galaxies=[g0])
            g1_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_1.blurring, galaxies=[g1])

            plane_stack = pl_stack.PlaneStack(
                galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1])

            assert (plane_stack.image_plane_blurring_images_1d[1] ==
                    g0_image_grid_1 + g1_image_grid_1).all()
예제 #2
0
        def test__circle__one_profile_gal__integral_is_same_as_individual_profile(
                self):

            sersic = lp.EllipticalSersic(axis_ratio=1.0,
                                         phi=0.0,
                                         intensity=3.0,
                                         effective_radius=2.0,
                                         sersic_index=1.0)

            integral_radius = 5.5

            intensity_integral = sersic.luminosity_within_circle(
                radius=integral_radius)

            gal_sersic = g.Galaxy(redshift=0.5,
                                  light_profile_1=lp.EllipticalSersic(
                                      axis_ratio=1.0,
                                      phi=0.0,
                                      intensity=3.0,
                                      effective_radius=2.0,
                                      sersic_index=1.0))

            gal_intensity_integral = gal_sersic.luminosity_within_circle(
                radius=integral_radius)

            assert intensity_integral == gal_intensity_integral
예제 #3
0
    def test___same_as_above__use_galaxies_to_make_plane_images(self):

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

        padded_grid_stack = grids.GridStack.padded_grid_stack_from_mask_sub_grid_size_and_psf_shape(
            mask=mask, sub_grid_size=1, psf_shape=(3, 3))

        psf_0 = im.PSF(array=(np.array([[0.0, 0.0, 0.0], [0.0, 1.0, 0.0],
                                        [0.0, 0.0, 0.0]])),
                       pixel_scale=1.0)

        psf_1 = im.PSF(array=(np.array([[0.0, 3.0, 0.0], [0.0, 1.0, 2.0],
                                        [0.0, 0.0, 0.0]])),
                       pixel_scale=1.0)

        g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.1))
        g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=0.2))

        tracer = ray_tracing_stack.TracerImagePlaneStack(
            lens_galaxies=[g0, g1],
            image_plane_grid_stacks=[padded_grid_stack, padded_grid_stack])

        manual_blurred_image_00 = tracer.image_plane.image_plane_images_1d_of_galaxies[
            0][0]
        manual_blurred_image_00 = padded_grid_stack.regular.map_to_2d_keep_padded(
            padded_array_1d=manual_blurred_image_00)
        manual_blurred_image_00 = psf_0.convolve(array=manual_blurred_image_00)

        manual_blurred_image_01 = tracer.image_plane.image_plane_images_1d_of_galaxies[
            0][1]
        manual_blurred_image_01 = padded_grid_stack.regular.map_to_2d_keep_padded(
            padded_array_1d=manual_blurred_image_01)
        manual_blurred_image_01 = psf_0.convolve(array=manual_blurred_image_01)

        manual_blurred_image_10 = tracer.image_plane.image_plane_images_1d_of_galaxies[
            1][0]
        manual_blurred_image_10 = padded_grid_stack.regular.map_to_2d_keep_padded(
            padded_array_1d=manual_blurred_image_10)
        manual_blurred_image_10 = psf_1.convolve(array=manual_blurred_image_10)

        manual_blurred_image_11 = tracer.image_plane.image_plane_images_1d_of_galaxies[
            1][1]
        manual_blurred_image_11 = padded_grid_stack.regular.map_to_2d_keep_padded(
            padded_array_1d=manual_blurred_image_11)
        manual_blurred_image_11 = psf_1.convolve(array=manual_blurred_image_11)

        unmasked_blurred_image_of_datas_and_planes = \
            stack_util.unmasked_blurred_image_of_datas_and_planes_from_padded_grid_stacks_and_psf(
                planes=tracer.planes, padded_grid_stacks=[padded_grid_stack, padded_grid_stack], psfs=[psf_0, psf_1])

        assert unmasked_blurred_image_of_datas_and_planes[0][0] == \
                pytest.approx(manual_blurred_image_00[1:4, 1:4] + manual_blurred_image_01[1:4, 1:4], 1.0e-4)
        assert unmasked_blurred_image_of_datas_and_planes[1][0] == \
                pytest.approx(manual_blurred_image_10[1:4, 1:4] + manual_blurred_image_11[1:4, 1:4], 1.0e-4)
def simulate():

    from autolens.data.array import grids
    from autolens.model.galaxy import galaxy as g
    from autolens.lens import ray_tracing

    psf = ccd.PSF.simulate_as_gaussian(shape=(11, 11),
                                       sigma=0.05,
                                       pixel_scale=0.05)

    image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(
        shape=(180, 180), pixel_scale=0.05, psf_shape=(11, 11))

    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,
                                                         effective_radius=0.2,
                                                         sersic_index=3.0))
    source_galaxy_2 = g.Galaxy(light=lp.EllipticalSersic(centre=(0.45, -0.35),
                                                         axis_ratio=0.6,
                                                         phi=90.0,
                                                         intensity=0.03,
                                                         effective_radius=0.3,
                                                         sersic_index=3.5))
    source_galaxy_3 = g.Galaxy(light=lp.EllipticalSersic(centre=(-0.05, -0.0),
                                                         axis_ratio=0.9,
                                                         phi=140.0,
                                                         intensity=0.03,
                                                         effective_radius=0.1,
                                                         sersic_index=4.0))

    tracer = ray_tracing.TracerImageSourcePlanes(
        lens_galaxies=[lens_galaxy],
        source_galaxies=[
            source_galaxy_0, source_galaxy_1, source_galaxy_2, source_galaxy_3
        ],
        image_plane_grid_stack=image_plane_grid_stack)

    return ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation,
                                pixel_scale=0.05,
                                exposure_time=300.0,
                                psf=psf,
                                background_sky_level=0.1,
                                add_noise=True)
예제 #5
0
        def test_2d_symmetry(self):
            sersic_1 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=1.0,
                                           effective_radius=0.6,
                                           sersic_index=4.0)

            sersic_2 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=1.0,
                                           effective_radius=0.6,
                                           sersic_index=4.0,
                                           centre=(100, 0))

            sersic_3 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=1.0,
                                           effective_radius=0.6,
                                           sersic_index=4.0,
                                           centre=(0, 100))

            sersic_4 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=1.0,
                                           effective_radius=0.6,
                                           sersic_index=4.0,
                                           centre=(100, 100))

            gal_sersic = g.Galaxy(redshift=0.5,
                                  light_profile_1=sersic_1,
                                  light_profile_2=sersic_2,
                                  light_profile_3=sersic_3,
                                  light_profile_4=sersic_4)

            assert gal_sersic.intensities_from_grid(np.array(
                [[49.0, 0.0]])) == pytest.approx(
                    gal_sersic.intensities_from_grid(np.array([[51.0, 0.0]])),
                    1e-5)

            assert gal_sersic.intensities_from_grid(np.array(
                [[0.0, 49.0]])) == pytest.approx(
                    gal_sersic.intensities_from_grid(np.array([[0.0, 51.0]])),
                    1e-5)

            assert gal_sersic.intensities_from_grid(np.array(
                [[100.0, 49.0]])) == pytest.approx(
                    gal_sersic.intensities_from_grid(np.array([[100.0,
                                                                51.0]])), 1e-5)

            assert gal_sersic.intensities_from_grid(np.array(
                [[49.0, 49.0]])) == pytest.approx(
                    gal_sersic.intensities_from_grid(np.array([[51.0, 51.0]])),
                    1e-5)
예제 #6
0
    def test__intensity_from_grid__change_geometry(self):
        sersic_0 = lp.EllipticalSersic(axis_ratio=0.5,
                                       phi=0.0,
                                       intensity=3.0,
                                       effective_radius=2.0,
                                       sersic_index=2.0)

        sersic_1 = lp.EllipticalSersic(axis_ratio=0.5,
                                       phi=90.0,
                                       intensity=3.0,
                                       effective_radius=2.0,
                                       sersic_index=2.0)

        assert sersic_0.intensities_from_grid(grid=np.array([[0.0, 1.0]])) == \
               sersic_1.intensities_from_grid(grid=np.array([[1.0, 0.0]]))
예제 #7
0
    def test__same_as_above__check_multipleis_by_conversion_factor(self):

        sersic = lp.EllipticalSersic(axis_ratio=0.5,
                                     phi=90.0,
                                     intensity=3.0,
                                     effective_radius=2.0,
                                     sersic_index=2.0)

        integral_radius = 0.5
        luminosity_tot = 0.0

        xs = np.linspace(-1.8, 1.8, 80)
        ys = np.linspace(-1.8, 1.8, 80)

        edge = xs[1] - xs[0]
        area = edge**2

        for x in xs:
            for y in ys:

                eta = sersic.grid_to_elliptical_radii(np.array([[x, y]]))

                if eta < integral_radius:
                    luminosity_tot += sersic.intensities_from_grid_radii(
                        eta) * area

        intensity_integral = sersic.luminosity_within_ellipse(
            major_axis=integral_radius, conversion_factor=3.0)

        assert 3.0 * luminosity_tot[0] == pytest.approx(
            intensity_integral, 0.02)
예제 #8
0
def pipeline():

    sersic = lp.EllipticalSersic(centre=(0.0, 0.0),
                                 axis_ratio=0.8,
                                 phi=90.0,
                                 intensity=1.0,
                                 effective_radius=1.3,
                                 sersic_index=3.0)

    lens_galaxy = galaxy.Galaxy(light_profile=sersic)

    tools.reset_paths(test_name=test_name, output_path=output_path)
    tools.simulate_integration_image(test_name=test_name,
                                     pixel_scale=0.1,
                                     lens_galaxies=[lens_galaxy],
                                     source_galaxies=[],
                                     target_signal_to_noise=30.0)

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

    pipeline = make_pipeline(test_name=test_name)
    pipeline.run(data=ccd_data)
    def test__grid_calculations__same_as_sersic(self):
        sersic_lp = lp.EllipticalSersic(axis_ratio=0.7,
                                        phi=1.0,
                                        intensity=1.0,
                                        effective_radius=0.6,
                                        sersic_index=2.0)
        sersic_mp = mp.EllipticalSersic(axis_ratio=0.7,
                                        phi=1.0,
                                        intensity=1.0,
                                        effective_radius=0.6,
                                        sersic_index=2.0,
                                        mass_to_light_ratio=2.0)
        sersic_lmp = lmp.EllipticalSersic(axis_ratio=0.7,
                                          phi=1.0,
                                          intensity=1.0,
                                          effective_radius=0.6,
                                          sersic_index=2.0,
                                          mass_to_light_ratio=2.0)

        assert (sersic_lp.intensities_from_grid(grid) ==
                sersic_lmp.intensities_from_grid(grid)).all()
        assert (sersic_mp.surface_density_from_grid(grid) ==
                sersic_lmp.surface_density_from_grid(grid)).all()
        #    assert (sersic_mp.potential_from_grid(grid) == sersic_lmp.potential_from_grid(grid)).all()
        assert (sersic_mp.deflections_from_grid(grid) ==
                sersic_lmp.deflections_from_grid(grid)).all()
def pipeline():

    tools.reset_paths(test_name=test_name, output_path=output_path)

    lens_light = lp.SphericalDevVaucouleurs(centre=(0.0, 0.0),
                                            intensity=0.1,
                                            effective_radius=0.5)
    lens_mass = mp.EllipticalIsothermal(centre=(0.0, 0.0),
                                        axis_ratio=0.8,
                                        phi=80.0,
                                        einstein_radius=1.6)
    source_light = lp.EllipticalSersic(centre=(0.0, 0.0),
                                       axis_ratio=0.6,
                                       phi=90.0,
                                       intensity=1.0,
                                       effective_radius=0.5,
                                       sersic_index=1.0)

    lens_galaxy = galaxy.Galaxy(dev=lens_light, sie=lens_mass)
    source_galaxy = galaxy.Galaxy(sersic=source_light)

    tools.simulate_integration_image(test_name=test_name,
                                     pixel_scale=0.1,
                                     lens_galaxies=[lens_galaxy],
                                     source_galaxies=[source_galaxy],
                                     target_signal_to_noise=30.0)

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

    pipeline = make_pipeline(test_name=test_name)
    pipeline.run(data=ccd_data)
예제 #11
0
    def test_fixed_light_property(self):
        galaxy_prior = gp.GalaxyModel(
            variable_redshift=True,
            light_profile=light_profiles.EllipticalSersic(),
        )

        assert len(galaxy_prior.constant_light_profiles) == 1
예제 #12
0
def make_galaxy_light():
    return g.Galaxy(light_profile=lp.EllipticalSersic(centre=(0.1, 0.1),
                                                      axis_ratio=1.0,
                                                      phi=0.0,
                                                      intensity=1.0,
                                                      effective_radius=0.6,
                                                      sersic_index=4.0))
예제 #13
0
 def test__intensity_from_grid__correct_values(self):
     sersic = lp.EllipticalSersic(axis_ratio=0.5,
                                  phi=0.0,
                                  intensity=3.0,
                                  effective_radius=2.0,
                                  sersic_index=2.0)
     assert sersic.intensities_from_grid(
         grid=np.array([[1.0, 0.0]])) == pytest.approx(5.38066670129, 1e-3)
예제 #14
0
def make_lens_and_source_smooth(sub_grid_size):

    # This source-only system has a smooth source (low Sersic Index) and simple SIE mass profile.

    lens_galaxy = g.Galaxy(ight=lp.EllipticalSersic(centre=(0.0, 0.0), axis_ratio=0.9, phi=45.0,
                                                 intensity=0.5, effective_radius=0.8, sersic_index=4.0),
                           mass=mp.EllipticalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6,
                                                        axis_ratio=0.7, phi=45.0))

    source_galaxy = g.Galaxy(light=lp.EllipticalSersic(centre=(0.0, 0.0), axis_ratio=0.8, phi=60.0,
                                                       intensity=0.4, effective_radius=0.5, sersic_index=1.0))

    for pixel_scale, shape in zip(pixel_scales, shapes):

        simulate_image_from_galaxies_and_output_to_fits(lens_name='lens_and_source_smooth', shape=shape,
                                                        pixel_scale=pixel_scale, sub_grid_size=sub_grid_size,
                                                        lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy])
예제 #15
0
    def test__intensity_at_radius__correct_value(self):
        sersic = lp.EllipticalSersic(axis_ratio=1.0,
                                     phi=0.0,
                                     intensity=1.0,
                                     effective_radius=0.6,
                                     sersic_index=4.0)
        assert sersic.intensities_from_grid_radii(
            grid_radii=1.0) == pytest.approx(0.351797, 1e-3)

        sersic = lp.EllipticalSersic(axis_ratio=1.0,
                                     phi=0.0,
                                     intensity=3.0,
                                     effective_radius=2.0,
                                     sersic_index=2.0)
        # 3.0 * exp(-3.67206544592 * (1,5/2.0) ** (1.0 / 2.0)) - 1) = 0.351797
        assert sersic.intensities_from_grid_radii(
            grid_radii=1.5) == pytest.approx(4.90657319276, 1e-3)
예제 #16
0
def test_pipeline():

    bulge_0 = lp.EllipticalSersic(centre=(-1.0, -1.0),
                                  axis_ratio=0.9,
                                  phi=90.0,
                                  intensity=1.0,
                                  effective_radius=1.0,
                                  sersic_index=4.0)
    disk_0 = lp.EllipticalSersic(centre=(-1.0, -1.0),
                                 axis_ratio=0.6,
                                 phi=90.0,
                                 intensity=20.0,
                                 effective_radius=2.5,
                                 sersic_index=1.0)
    bulge_1 = lp.EllipticalSersic(centre=(1.0, 1.0),
                                  axis_ratio=0.9,
                                  phi=90.0,
                                  intensity=1.0,
                                  effective_radius=1.0,
                                  sersic_index=4.0)
    disk_1 = lp.EllipticalSersic(centre=(1.0, 1.0),
                                 axis_ratio=0.6,
                                 phi=90.0,
                                 intensity=20.0,
                                 effective_radius=2.5,
                                 sersic_index=1.0)

    lens_galaxy_0 = galaxy.Galaxy(bulge=bulge_0, disk=disk_0)
    lens_galaxy_1 = galaxy.Galaxy(bulge=bulge_1, disk=disk_1)

    tools.reset_paths(test_name=test_name, output_path=output_path)
    tools.simulate_integration_image(
        test_name=test_name,
        pixel_scale=0.1,
        lens_galaxies=[lens_galaxy_0, lens_galaxy_1],
        source_galaxies=[],
        target_signal_to_noise=30.0)

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

    pipeline = make_pipeline(test_name=test_name)
    pipeline.run(data=ccd_data)
예제 #17
0
        def test__ensure_index_of_plane_image_has_negative_arcseconds_at_start(
                self, grid_stack_0):
            # The grid coordinates -2.0 -> 2.0 mean a plane of shape (5,5) has arc second coordinates running over
            # -1.6, -0.8, 0.0, 0.8, 1.6. The origin -1.6, -1.6 of the model_galaxy means its brighest pixel should be
            # index 0 of the 1D grid and (0,0) of the 2d plane datas_.

            msk = mask.Mask(array=np.full((5, 5), False), pixel_scale=1.0)

            grid_stack_0.regular = grids.RegularGrid(np.array([[-2.0, -2.0],
                                                               [2.0, 2.0]]),
                                                     mask=msk)

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(1.6, -1.6),
                                                            intensity=1.0))
            plane_stack = pl_stack.PlaneStack(galaxies=[g0],
                                              grid_stacks=[grid_stack_0])

            assert plane_stack.plane_images[0].shape == (5, 5)
            assert np.unravel_index(plane_stack.plane_images[0].argmax(),
                                    plane_stack.plane_images[0].shape) == (0,
                                                                           0)

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(1.6, 1.6),
                                                            intensity=1.0))
            plane_stack = pl_stack.PlaneStack(galaxies=[g0],
                                              grid_stacks=[grid_stack_0])
            assert np.unravel_index(plane_stack.plane_images[0].argmax(),
                                    plane_stack.plane_images[0].shape) == (0,
                                                                           4)

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(
                centre=(-1.6, -1.6), intensity=1.0))
            plane_stack = pl_stack.PlaneStack(galaxies=[g0],
                                              grid_stacks=[grid_stack_0])
            assert np.unravel_index(plane_stack.plane_images[0].argmax(),
                                    plane_stack.plane_images[0].shape) == (4,
                                                                           0)

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(centre=(-1.6, 1.6),
                                                            intensity=1.0))
            plane_stack = pl_stack.PlaneStack(galaxies=[g0],
                                              grid_stacks=[grid_stack_0])
            assert np.unravel_index(plane_stack.plane_images[0].argmax(),
                                    plane_stack.plane_images[0].shape) == (4,
                                                                           4)
예제 #18
0
    def test__tracer_and_tracer_sensitive_are_identical__added__likelihood_is_noise_term(
            self, lens_data_blur):

        g0 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
        g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0))

        tracer = ray_tracing.TracerImageSourcePlanes(
            lens_galaxies=[g0],
            source_galaxies=[g1],
            image_plane_grid_stack=lens_data_blur.grid_stack)

        fit = sensitivity_fit.SensitivityProfileFit(lens_data=lens_data_blur,
                                                    tracer_normal=tracer,
                                                    tracer_sensitive=tracer)

        assert (fit.fit_normal.image == lens_data_blur.image).all()
        assert (fit.fit_normal.noise_map == lens_data_blur.noise_map).all()

        model_image_1d = util.blurred_image_1d_from_1d_unblurred_and_blurring_images(
            unblurred_image_1d=tracer.image_plane_image_1d,
            blurring_image_1d=tracer.image_plane_blurring_image_1d,
            convolver=lens_data_blur.convolver_image)

        model_image = lens_data_blur.map_to_scaled_array(
            array_1d=model_image_1d)

        assert (fit.fit_normal.model_image == model_image).all()

        residual_map = fit_util.residual_map_from_data_mask_and_model_data(
            data=lens_data_blur.image,
            mask=lens_data_blur.mask,
            model_data=model_image)
        assert (fit.fit_normal.residual_map == residual_map).all()

        chi_squared_map = fit_util.chi_squared_map_from_residual_map_noise_map_and_mask(
            residual_map=residual_map,
            mask=lens_data_blur.mask,
            noise_map=lens_data_blur.noise_map)

        assert (fit.fit_normal.chi_squared_map == chi_squared_map).all()

        assert (fit.fit_sensitive.image == lens_data_blur.image).all()
        assert (fit.fit_sensitive.noise_map == lens_data_blur.noise_map).all()
        assert (fit.fit_sensitive.model_image == model_image).all()
        assert (fit.fit_sensitive.residual_map == residual_map).all()
        assert (fit.fit_sensitive.chi_squared_map == chi_squared_map).all()

        chi_squared = fit_util.chi_squared_from_chi_squared_map_and_mask(
            chi_squared_map=chi_squared_map, mask=lens_data_blur.mask)
        noise_normalization = fit_util.noise_normalization_from_noise_map_and_mask(
            mask=lens_data_blur.mask, noise_map=lens_data_blur.noise_map)
        assert fit.fit_normal.likelihood == -0.5 * (chi_squared +
                                                    noise_normalization)
        assert fit.fit_sensitive.likelihood == -0.5 * (chi_squared +
                                                       noise_normalization)

        assert fit.figure_of_merit == 0.0
예제 #19
0
        def test__image_plane_image_of_galaxies__use_multiple_grids__get_multiple_images(
                self, grid_stack_0, grid_stack_1):

            # Overwrite one value so intensity in each pixel is different
            grid_stack_0.sub[5] = np.array([2.0, 2.0])

            g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0))
            g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0))

            plane_stack = pl_stack.PlaneStack(
                galaxies=[g0, g1], grid_stacks=[grid_stack_0, grid_stack_1])

            g0_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_0.sub, galaxies=[g0])
            g1_image_grid_0 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_0.sub, galaxies=[g1])

            assert plane_stack.image_plane_images_1d[0] == pytest.approx(
                g0_image_grid_0 + g1_image_grid_0, 1.0e-4)
            assert (plane_stack.image_plane_images[0] ==
                    grid_stack_0.regular.scaled_array_from_array_1d(
                        plane_stack.image_plane_images_1d[0])).all()

            assert (plane_stack.image_plane_images_1d_of_galaxies[0][0] ==
                    g0_image_grid_0).all()
            assert (plane_stack.image_plane_images_1d_of_galaxies[0][1] ==
                    g1_image_grid_0).all()

            g0_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_1.sub, galaxies=[g0])
            g1_image_grid_1 = galaxy_util.intensities_of_galaxies_from_grid(
                grid_stack_1.sub, galaxies=[g1])

            assert plane_stack.image_plane_images_1d[1] == pytest.approx(
                g0_image_grid_1 + g1_image_grid_1, 1.0e-4)
            assert (plane_stack.image_plane_images[1] ==
                    grid_stack_0.regular.scaled_array_from_array_1d(
                        plane_stack.image_plane_images_1d[1])).all()

            assert (plane_stack.image_plane_images_1d_of_galaxies[1][0] ==
                    g0_image_grid_1).all()
            assert (plane_stack.image_plane_images_1d_of_galaxies[1][1] ==
                    g1_image_grid_1).all()
예제 #20
0
    def test_fixed_light(self):
        galaxy_prior = gp.GalaxyModel(
            variable_redshift=True,
            light_profile=light_profiles.EllipticalSersic(),
        )

        arguments = {galaxy_prior.redshift.redshift: 2.0}

        galaxy = galaxy_prior.instance_for_arguments(arguments)

        assert len(galaxy.light_profiles) == 1
예제 #21
0
def pipeline():

    lens_mass = mp.EllipticalIsothermal(centre=(0.01, 0.01),
                                        axis_ratio=0.8,
                                        phi=80.0,
                                        einstein_radius=1.6)

    source_bulge_0 = lp.EllipticalSersic(centre=(0.01, 0.01),
                                         axis_ratio=0.9,
                                         phi=90.0,
                                         intensity=1.0,
                                         effective_radius=1.0,
                                         sersic_index=4.0)

    source_bulge_1 = lp.EllipticalSersic(centre=(0.1, 0.1),
                                         axis_ratio=0.9,
                                         phi=90.0,
                                         intensity=1.0,
                                         effective_radius=1.0,
                                         sersic_index=4.0)

    lens_galaxy = galaxy.Galaxy(sie=lens_mass)
    source_galaxy = galaxy.Galaxy(bulge_0=source_bulge_0,
                                  bulge_1=source_bulge_1)

    tools.reset_paths(test_name=test_name, output_path=output_path)
    tools.simulate_integration_image(test_name=test_name,
                                     pixel_scale=0.1,
                                     lens_galaxies=[lens_galaxy],
                                     source_galaxies=[source_galaxy],
                                     target_signal_to_noise=30.0)

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

    pipeline = make_pipeline(test_name=test_name)
    pipeline.run(data=ccd_data)
예제 #22
0
    def test__spherical_and_elliptical_match(self):
        elliptical = lp.EllipticalSersic(axis_ratio=1.0,
                                         phi=0.0,
                                         intensity=3.0,
                                         effective_radius=2.0,
                                         sersic_index=2.0)

        spherical = lp.SphericalSersic(intensity=3.0,
                                       effective_radius=2.0,
                                       sersic_index=2.0)

        assert (elliptical.intensities_from_grid(grid) ==
                spherical.intensities_from_grid(grid)).all()
예제 #23
0
        def test__ellipse__same_as_above__include_conversion_factor(self):
            sersic_1 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=3.0,
                                           effective_radius=2.0,
                                           sersic_index=1.0)

            sersic_2 = lp.EllipticalSersic(axis_ratio=0.5,
                                           phi=0.0,
                                           intensity=7.0,
                                           effective_radius=3.0,
                                           sersic_index=2.0)

            integral_radius = 5.5

            intensity_integral = sersic_1.luminosity_within_ellipse(
                major_axis=integral_radius, conversion_factor=2.0)
            intensity_integral += sersic_2.luminosity_within_ellipse(
                major_axis=integral_radius, conversion_factor=2.0)

            gal_sersic = g.Galaxy(
                redshift=0.5,
                light_profile_1=lp.EllipticalSersic(axis_ratio=1.0,
                                                    phi=0.0,
                                                    intensity=3.0,
                                                    effective_radius=2.0,
                                                    sersic_index=1.0),
                light_profile_2=lp.EllipticalSersic(axis_ratio=0.5,
                                                    phi=0.0,
                                                    intensity=7.0,
                                                    effective_radius=3.0,
                                                    sersic_index=2.0))

            gal_intensity_integral = gal_sersic.luminosity_within_ellipse(
                major_axis=integral_radius)
            assert intensity_integral == 2.0 * gal_intensity_integral
            gal_intensity_integral = gal_sersic.luminosity_within_ellipse(
                major_axis=integral_radius, conversion_factor=2.0)
            assert intensity_integral == gal_intensity_integral
예제 #24
0
        def test__ellipse__two_profile_gal__integral_is_sum_of_individual_profiles(
                self):
            sersic_1 = lp.EllipticalSersic(axis_ratio=1.0,
                                           phi=0.0,
                                           intensity=3.0,
                                           effective_radius=2.0,
                                           sersic_index=1.0)

            sersic_2 = lp.EllipticalSersic(axis_ratio=0.5,
                                           phi=0.0,
                                           intensity=7.0,
                                           effective_radius=3.0,
                                           sersic_index=2.0)

            integral_radius = 5.5

            intensity_integral = sersic_1.luminosity_within_ellipse(
                major_axis=integral_radius)
            intensity_integral += sersic_2.luminosity_within_ellipse(
                major_axis=integral_radius)

            gal_sersic = g.Galaxy(
                redshift=0.5,
                light_profile_1=lp.EllipticalSersic(axis_ratio=1.0,
                                                    phi=0.0,
                                                    intensity=3.0,
                                                    effective_radius=2.0,
                                                    sersic_index=1.0),
                light_profile_2=lp.EllipticalSersic(axis_ratio=0.5,
                                                    phi=0.0,
                                                    intensity=7.0,
                                                    effective_radius=3.0,
                                                    sersic_index=2.0))

            gal_intensity_integral = gal_sersic.luminosity_within_ellipse(
                major_axis=integral_radius)

            assert intensity_integral == gal_intensity_integral
예제 #25
0
    def test__constructor(self):
        sersic = lp.EllipticalSersic(axis_ratio=1.0,
                                     phi=0.0,
                                     intensity=1.0,
                                     effective_radius=0.6,
                                     sersic_index=4.0)

        assert sersic.centre == (0.0, 0.0)
        assert sersic.axis_ratio == 1.0
        assert sersic.phi == 0.0
        assert sersic.intensity == 1.0
        assert sersic.effective_radius == 0.6
        assert sersic.sersic_index == 4.0
        assert sersic.sersic_constant == pytest.approx(7.66925, 1e-3)
        assert sersic.elliptical_effective_radius == 0.6
예제 #26
0
    def test__results_of_phase_are_available_as_properties(self, ccd_data):
        clean_images()

        phase = ph.LensPlanePhase(
            optimizer_class=NLO,
            lens_galaxies=[g.Galaxy(light=lp.EllipticalSersic(intensity=1.0))],
            phase_name='test_phase')

        result = phase.run(data=ccd_data)

        assert hasattr(result, "most_likely_tracer")
        assert hasattr(result, "most_likely_padded_tracer")
        assert hasattr(result, "most_likely_fit")
        assert hasattr(result, "unmasked_model_image")
        assert hasattr(result, "unmasked_model_image_of_planes")
        assert hasattr(result, "unmasked_model_image_of_planes_and_galaxies")
예제 #27
0
    def test_constants_work(self):
        name = "const_float"
        test_name = '/const_float'

        tools.reset_paths(test_name, output_path)

        sersic = lp.EllipticalSersic(centre=(0.0, 0.0), axis_ratio=0.8, phi=90.0, intensity=1.0, effective_radius=1.3,
                                     sersic_index=3.0)

        lens_galaxy = galaxy.Galaxy(light_profile=sersic)

        tools.simulate_integration_image(test_name=test_name, pixel_scale=0.5, lens_galaxies=[lens_galaxy],
                                         source_galaxies=[], target_signal_to_noise=10.0)
        path = "{}/".format(
            os.path.dirname(os.path.realpath(__file__)))  # Setup path so we can output the simulated image.

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

        class MMPhase(ph.LensPlanePhase):

            def pass_priors(self, previous_results):
                self.lens_galaxies.lens.sersic.axis_ratio = 0.2
                self.lens_galaxies.lens.sersic.phi = 90.0
                self.lens_galaxies.lens.sersic.intensity = 1.0
                self.lens_galaxies.lens.sersic.effective_radius = 1.3
                self.lens_galaxies.lens.sersic.sersic_index = 3.0

        phase = MMPhase(lens_galaxies=dict(lens=gm.GalaxyModel(sersic=lp.EllipticalSersic)),
                        optimizer_class=nl.MultiNest, phase_name="{}/phase1".format(name))

        phase.optimizer.n_live_points = 20
        phase.optimizer.sampling_efficiency = 0.8

        phase.make_analysis(data=ccd_data)

        sersic = phase.variable.lens_galaxies[0].sersic

        assert isinstance(sersic, mm.PriorModel)

        assert isinstance(sersic.axis_ratio, prior.Constant)
        assert isinstance(sersic.phi, prior.Constant)
        assert isinstance(sersic.intensity, prior.Constant)
        assert isinstance(sersic.effective_radius, prior.Constant)
        assert isinstance(sersic.sersic_index, prior.Constant)
예제 #28
0
    def test__3x2_grid__shape_change_correct_and_coordinates_shift(self):

        galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0))

        grid = np.array([[-1.5, -1.5], [1.5, 1.5]])

        plane_image = lens_util.plane_image_of_galaxies_from_grid(
            shape=(3, 2), grid=grid, galaxies=[galaxy], buffer=0.0)

        plane_image_galaxy = galaxy.intensities_from_grid(
            grid=np.array([[-1.0, -0.75], [-1.0, 0.75], [0.0, -0.75],
                           [0.0, 0.75], [1.0, -0.75], [1.0, 0.75]]))

        plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape(
            array_1d=plane_image_galaxy, shape=(3, 2))

        assert (plane_image == plane_image_galaxy).all()
예제 #29
0
    def test__3x3_grid__extracts_max_min_coordinates__creates_regular_grid_including_half_pixel_offset_from_edge(
            self):

        galaxy = g.Galaxy(light=lp.EllipticalSersic(intensity=1.0))

        grid = np.array([[-1.5, -1.5], [1.5, 1.5]])

        plane_image = lens_util.plane_image_of_galaxies_from_grid(
            shape=(3, 3), grid=grid, galaxies=[galaxy], buffer=0.0)

        plane_image_galaxy = galaxy.intensities_from_grid(grid=np.array(
            [[-1.0, -1.0], [-1.0, 0.0], [-1.0, 1.0], [0.0, -1.0], [0.0, 0.0],
             [0.0, 1.0], [1.0, -1.0], [1.0, 0.0], [1.0, 1.0]]))

        plane_image_galaxy = mapping_util.map_unmasked_1d_array_to_2d_array_from_array_1d_and_shape(
            array_1d=plane_image_galaxy, shape=(3, 3))

        assert (plane_image == plane_image_galaxy).all()
예제 #30
0
    def test_pairing_works(self):
        test_name = 'pair_floats'

        tools.reset_paths(test_name, output_path)

        sersic = lp.EllipticalSersic(centre=(0.0, 0.0), axis_ratio=0.8, phi=90.0, intensity=1.0, effective_radius=1.3,
                                     sersic_index=3.0)

        lens_galaxy = galaxy.Galaxy(light_profile=sersic)

        tools.simulate_integration_image(test_name=test_name, pixel_scale=0.5, lens_galaxies=[lens_galaxy],
                                         source_galaxies=[], target_signal_to_noise=10.0)

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

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

        class MMPhase(ph.LensPlanePhase):

            def pass_priors(self, previous_results):
                self.lens_galaxies.lens.sersic.intensity = self.lens_galaxies.lens.sersic.axis_ratio

        phase = MMPhase(lens_galaxies=dict(lens=gm.GalaxyModel(sersic=lp.EllipticalSersic)),
                        optimizer_class=nl.MultiNest, phase_name="{}/phase1".format(test_name))

        initial_total_priors = phase.variable.prior_count
        phase.make_analysis(data=ccd_data)

        assert phase.lens_galaxies[0].sersic.intensity == phase.lens_galaxies[0].sersic.axis_ratio
        assert initial_total_priors - 1 == phase.variable.prior_count
        assert len(phase.variable.flat_prior_model_tuples) == 1

        lines = list(
            filter(lambda line: "axis_ratio" in line or "intensity" in line, phase.variable.info.split("\n")))

        assert len(lines) == 2
        assert "lens_axis_ratio                                             UniformPrior, lower_limit = 0.2, " \
               "upper_limit = 1.0" in lines
        assert "lens_intensity                                              UniformPrior, lower_limit = 0.2, " \
               "upper_limit = 1.0" in lines