Exemplo n.º 1
0
def phase():

    pixel_scale = 0.1
    image_shape = (150, 150)

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

    grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size(shape=image_shape, pixel_scale=pixel_scale,
                                                                          sub_grid_size=4)

    galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0))

    deflections = galaxy_util.deflections_of_galaxies_from_grid(galaxies=[galaxy], grid=grid_stack.sub)
    deflections_y = grid_stack.regular.scaled_array_from_array_1d(array_1d=deflections[:,0])
    deflections_x = grid_stack.regular.scaled_array_from_array_1d(array_1d=deflections[:,1])

    noise_map = scaled_array.ScaledSquarePixelArray(array=np.ones(deflections_y.shape), pixel_scale=pixel_scale)

    data_y = gd.GalaxyData(image=deflections_y, noise_map=noise_map, pixel_scale=pixel_scale)
    data_x = gd.GalaxyData(image=deflections_x, noise_map=noise_map, pixel_scale=pixel_scale)

    phase = ph.GalaxyFitPhase(galaxies=dict(gal=gm.GalaxyModel(light=mp.SphericalIsothermal)), use_deflections=True,
                              sub_grid_size=4,
                              optimizer_class=nl.MultiNest, phase_name=test_name+'/')

    phase.run(galaxy_data=[data_y, data_x])
Exemplo n.º 2
0
def phase():

    pixel_scale = 0.1
    image_shape = (150, 150)

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

    grid_stack = grids.GridStack.from_shape_pixel_scale_and_sub_grid_size(
        shape=image_shape, pixel_scale=pixel_scale, sub_grid_size=4)

    galaxy = g.Galaxy(mass=lp.SphericalExponential(
        centre=(0.0, 0.0), intensity=1.0, effective_radius=0.5))

    intensities = galaxy_util.intensities_of_galaxies_from_grid(
        galaxies=[galaxy], grid=grid_stack.sub)
    intensities = grid_stack.regular.scaled_array_from_array_1d(
        array_1d=intensities)

    noise_map = scaled_array.ScaledSquarePixelArray(array=np.ones(
        intensities.shape),
                                                    pixel_scale=pixel_scale)

    data = gd.GalaxyData(image=intensities,
                         noise_map=noise_map,
                         pixel_scale=pixel_scale)

    phase = ph.GalaxyFitPhase(
        galaxies=dict(gal=gm.GalaxyModel(light=lp.SphericalExponential)),
        use_intensities=True,
        sub_grid_size=4,
        optimizer_class=nl.MultiNest,
        phase_name=test_name + '/')

    phase.run(galaxy_data=[data])
Exemplo n.º 3
0
def make_galaxy_data(image, mask):
    noise_map = sca.ScaledSquarePixelArray(array=2.0 * np.ones((4, 4)),
                                           pixel_scale=3.0)
    galaxy_data = gd.GalaxyData(image=image,
                                noise_map=noise_map,
                                pixel_scale=3.0)
    return gd.GalaxyFitData(galaxy_data=galaxy_data,
                            mask=mask,
                            use_intensities=True)
Exemplo n.º 4
0
    def test__no_use_method__raises_exception(self, image, mask):

        galaxy_data = gd.GalaxyData(image=image,
                                    noise_map=2.0 * np.ones((4, 4)),
                                    pixel_scale=3.0)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2)
Exemplo n.º 5
0
    def test__galaxy_data_deflections_x(self, image, mask):

        galaxy_data = gd.GalaxyData(image=image,
                                    noise_map=2.0 * np.ones((4, 4)),
                                    pixel_scale=3.0)

        galaxy_fit_data = gd.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           sub_grid_size=2,
                                           use_deflections_x=True)

        assert galaxy_fit_data.pixel_scale == 3.0
        assert (galaxy_fit_data.image == np.ones((4, 4))).all()
        assert (galaxy_fit_data.noise_map == 2.0 * np.ones((4, 4))).all()
        assert (galaxy_fit_data.mask == np.array([[True, True, True, True],
                                                  [True, False, False, True],
                                                  [True, False, False, True],
                                                  [True, True, True,
                                                   True]])).all()
        assert (galaxy_fit_data.image_1d == np.ones(4)).all()
        assert (galaxy_fit_data.noise_map_1d == 2.0 * np.ones(4)).all()
        assert (galaxy_fit_data.mask_1d == np.array(
            [False, False, False, False])).all()

        galaxy = MockGalaxy(value=1, shape=4)

        deflections_x = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid(
            galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub)

        assert (deflections_x == np.array([1.0, 1.0, 1.0, 1.0])).all()

        galaxy = g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0))

        deflections_gal = galaxy.deflections_from_grid(
            grid=galaxy_fit_data.grid_stack.sub)
        deflections_gal = np.asarray([
            galaxy_fit_data.grid_stack.sub.sub_data_to_regular_data(
                deflections_gal[:, 0]),
            galaxy_fit_data.grid_stack.sub.sub_data_to_regular_data(
                deflections_gal[:, 1])
        ]).T

        deflections_gd = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid(
            galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub)

        assert (deflections_gal[:, 1] == deflections_gd).all()
Exemplo n.º 6
0
    def test__multiple_use_methods__raises_exception(self, image, mask):

        galaxy_data = gd.GalaxyData(image=image,
                                    noise_map=2.0 * np.ones((4, 4)),
                                    pixel_scale=3.0)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2,
                             use_intensities=True,
                             use_surface_density=True)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2,
                             use_intensities=True,
                             use_potential=True)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2,
                             use_intensities=True,
                             use_deflections_y=True)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2,
                             use_intensities=True,
                             use_surface_density=True,
                             use_potential=True)

        with pytest.raises(exc.GalaxyException):
            gd.GalaxyFitData(galaxy_data=galaxy_data,
                             mask=mask,
                             sub_grid_size=2,
                             use_intensities=True,
                             use_surface_density=True,
                             use_potential=True,
                             use_deflections_x=True)
Exemplo n.º 7
0
    def test__galaxy_data_intensities(self, image, mask):

        galaxy_data = gd.GalaxyData(image=image,
                                    noise_map=2.0 * np.ones((4, 4)),
                                    pixel_scale=3.0)

        galaxy_fit_data = gd.GalaxyFitData(galaxy_data=galaxy_data,
                                           mask=mask,
                                           sub_grid_size=2,
                                           use_intensities=True)

        assert galaxy_fit_data.pixel_scale == 3.0
        assert (galaxy_fit_data.image == np.ones((4, 4))).all()
        assert (galaxy_fit_data.noise_map == 2.0 * np.ones((4, 4))).all()
        assert (galaxy_fit_data.mask == np.array([[True, True, True, True],
                                                  [True, False, False, True],
                                                  [True, False, False, True],
                                                  [True, True, True,
                                                   True]])).all()
        assert (galaxy_fit_data.image_1d == np.ones(4)).all()
        assert (galaxy_fit_data.noise_map_1d == 2.0 * np.ones(4)).all()
        assert (galaxy_fit_data.mask_1d == np.array(
            [False, False, False, False])).all()

        galaxy = MockGalaxy(value=1, shape=4)

        intensities = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid(
            galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub)

        assert (intensities == np.array([1.0, 1.0, 1.0, 1.0])).all()

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

        intensities_gal = galaxy.intensities_from_grid(
            grid=galaxy_fit_data.grid_stack.sub)
        intensities_gal = galaxy_fit_data.grid_stack.sub.sub_data_to_regular_data(
            sub_array=intensities_gal)

        intensities_gd = galaxy_fit_data.profile_quantity_from_galaxy_and_sub_grid(
            galaxies=[galaxy], sub_grid=galaxy_fit_data.grid_stack.sub)

        assert (intensities_gal == intensities_gd).all()
Exemplo n.º 8
0
# Now lets create a galaxy, using a simple singular isothermal sphere.
galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.0))

# Next, we'll generate its surface density profile. Note that, because we're using the galaxy_util surface density
# function, the sub-grid of the grid-stack that is passed to this function is used to over-sample the surface density.
# The surface density averages over this sub-grid, such that it is the shape of the image (250, 250).
surface_density = galaxy_util.surface_density_of_galaxies_from_grid(galaxies=[galaxy], grid=grid_stack.sub)
surface_density = grid_stack.regular.scaled_array_from_array_1d(array_1d=surface_density)

# Now, we'll set this surface density up as our 'galaxy-data', meaning that it is what we'll fit via a non-linear
# search phase. To perform a fit we need a noise-map to help define our chi-squared. Given we are fitting a direct
# lensing quantity the actual values of this noise-map arn't particularly important, so we'll just use a noise-map of
# all 0.1's
noise_map = scaled_array.ScaledSquarePixelArray(array=0.1*np.ones(surface_density.shape), pixel_scale=pixel_scale)
data = gd.GalaxyData(image=surface_density, noise_map=noise_map, pixel_scale=pixel_scale)

# The fit will use a mask, which we setup like any other fit. Lets use a circular mask of 2.0"
def mask_function_circular(image):
    return msk.Mask.circular(shape=image.shape, pixel_scale=image.pixel_scale, radius_arcsec=2.0)

# We can now use a special phase, called a 'GalaxyFitPhase', to fit this surface density with a model galaxy the
# mass-profiles of which we get to choose. We'll fit it with a singular isothermal sphere and should see we retrieve
# the input model above.
phase = ph.GalaxyFitPhase(galaxies=dict(lens=gm.GalaxyModel(mass=mp.SphericalIsothermal)), use_surface_density=True,
                          sub_grid_size=4, mask_function=mask_function_circular,
                          optimizer_class=nl.MultiNest, phase_name='/galaxy_surface_density_fit')

phase.run(galaxy_data=[data])

# If you check your output folder, you should see that this fit has been performed and visualization specific to a
def make_galaxy_data(image, noise_map):
    return gd.GalaxyData(image=image, noise_map=noise_map, pixel_scale=3.0)
Exemplo n.º 10
0
tracer = ray_tracing.TracerMultiPlanes(
    galaxies=[lens_galaxy, perturber, source_galaxy],
    image_plane_grid_stack=grid_stack)

# We'll now extract the deflection angles from the tracer - we will extract the two deflection angle maps (y and x)
# separately.
deflections_y = tracer.deflections_y
deflections_x = tracer.deflections_x

# Next, we create each deflection angle map as its own GalaxyData object. Again, this needs a somewhat arbritary
# noise-map to be used in a fit.
noise_map = scaled_array.ScaledSquarePixelArray(array=0.1 *
                                                np.ones(deflections_y.shape),
                                                pixel_scale=pixel_scale)
data_y = gd.GalaxyData(image=deflections_y,
                       noise_map=noise_map,
                       pixel_scale=pixel_scale)
data_x = gd.GalaxyData(image=deflections_x,
                       noise_map=noise_map,
                       pixel_scale=pixel_scale)


# The fit will use a mask, which we setup like any other fit. Lets use a circular mask of 2.0"
def mask_function_circular(image):
    return msk.Mask.circular(shape=image.shape,
                             pixel_scale=image.pixel_scale,
                             radius_arcsec=2.5)


# Again, we'll use a special phase, called a 'GalaxyFitPhase', to fit the deflections with our model galaxies. We'll
# fit it with two singular isothermal spheres at the same lens-plane, thus we should see how the absence of multi-plane