Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test__extract_centres_of_all_mass_profiles_of_all_galaxies(self):
g0 = g.Galaxy(redshift=0.5, mass=mp.SphericalIsothermal(centre=(1.0, 1.0)))
g1 = g.Galaxy(redshift=0.5, mass=mp.SphericalIsothermal(centre=(2.0, 2.0)))
g2 = g.Galaxy(
redshift=0.5,
mass0=mp.SphericalIsothermal(centre=(3.0, 3.0)),
mass1=mp.SphericalIsothermal(centre=(4.0, 4.0)),
)
plane = pl.Plane(galaxies=[g.Galaxy(redshift=0.5)], redshift=None)
assert plane.centres_of_galaxy_mass_profiles == []
plane = pl.Plane(galaxies=[g0], redshift=None)
assert plane.centres_of_galaxy_mass_profiles == [[(1.0, 1.0)]]
plane = pl.Plane(galaxies=[g1], redshift=None)
assert plane.centres_of_galaxy_mass_profiles == [[(2.0, 2.0)]]
plane = pl.Plane(galaxies=[g0, g1], redshift=None)
assert plane.centres_of_galaxy_mass_profiles == [[(1.0, 1.0)], [(2.0, 2.0)]]
plane = pl.Plane(galaxies=[g1, g0], redshift=None)
def make_galaxy_mass():
return g.Galaxy(mass=mp.SphericalIsothermal(einstein_radius=1.0), redshift=1.0)
def test__mass_within_circle__conversion_factor_multiplies(self):
sis = mp.SphericalIsothermal(einstein_radius=2.0)
integral_radius = 2.0
mass_integral = sis.mass_within_circle(radius=integral_radius, conversion_factor=2.0)
assert 2.0 * math.pi * sis.einstein_radius * integral_radius == pytest.approx(mass_integral, 1e-3)
sis = mp.SphericalIsothermal(einstein_radius=2.0)
integral_radius = 4.0
mass_integral = sis.mass_within_circle(radius=integral_radius, conversion_factor=8.0)
assert 8.0 * math.pi * sis.einstein_radius * integral_radius == pytest.approx(mass_integral, 1e-3)
def test__move_centre_of_galaxy__interpolated_accurately(self):
shape = (20, 20)
pixel_scale = 1.0
mask = msk.Mask.circular_annular(shape=shape, pixel_scale=pixel_scale, inner_radius_arcsec=4.0,
outer_radius_arcsec=8.0, centre=(3.0, 3.0))
regular_grid = grids.RegularGrid.from_mask(mask=mask)
isothermal = mp.SphericalIsothermal(centre=(3.0, 3.0), einstein_radius=1.0)
deflections = isothermal.deflections_from_grid(grid=regular_grid)
interp_grid_arcsec_1d = interp_util.interp_grid_arcsec_1d_from_grid_1d_arcsec_and_interp_pixel_scales_and_origin(
grid_arcsec_1d=regular_grid, interp_pixel_scales=(0.25, 0.25))
interp_deflections = isothermal.deflections_from_grid(grid=interp_grid_arcsec_1d)
interpolated_deflections = interp_util.interpolated_grid_from_values_grid_arcsec_1d_and_interp_grid_arcsec_2d(
values=interp_deflections, grid_arcsec_1d=regular_grid, interp_grid_arcsec_1d=interp_grid_arcsec_1d)
assert np.max(deflections - interpolated_deflections) < 0.001
def test__within_ellipse_different_distance_units__same_as_galaxy_masses(self):
g0 = g.Galaxy(
redshift=0.5, mass=mp.SphericalIsothermal(einstein_radius=1.0)
)
g1 = g.Galaxy(
redshift=0.5, mass=mp.SphericalIsothermal(einstein_radius=2.0)
)
major_axis = dim.Length(1.0, "arcsec")
g0_mass = g0.mass_within_ellipse_in_units(
major_axis=major_axis, redshift_source=1.0
)
g1_mass = g1.mass_within_ellipse_in_units(
major_axis=major_axis, redshift_source=1.0
)
plane = pl.Plane(galaxies=[g0, g1], redshift=0.5)
plane_masses = plane.masses_of_galaxies_within_ellipses_in_units(
major_axis=major_axis, redshift_source=1.0
)
assert plane_masses[0] == g0_mass
def test___manual_image_and_psf(self, lens_data_stack_manual):
g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0))
g1 = g.Galaxy(mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
tracer = ray_tracing_stack.TracerImageSourcePlanesStack(lens_galaxies=[g0, g1], source_galaxies=[g0],
image_plane_grid_stacks=lens_data_stack_manual.grid_stacks)
padded_tracer = ray_tracing_stack.TracerImageSourcePlanesStack(lens_galaxies=[g0, g1], source_galaxies=[g0],
image_plane_grid_stacks=lens_data_stack_manual.padded_grid_stacks)
fit = lens_fit_stack.fit_lens_image_stack_with_tracer(lens_data_stack=lens_data_stack_manual,
tracer=tracer, padded_tracer=padded_tracer)
assert lens_data_stack_manual.noise_maps[0] == pytest.approx(fit.noise_maps[0], 1e-4)
assert lens_data_stack_manual.noise_maps[1] == pytest.approx(fit.noise_maps[1], 1e-4)
model_image_1d_0 = lens_fit_util.blurred_image_1d_from_1d_unblurred_and_blurring_images(
unblurred_image_1d=tracer.image_plane_images_1d[0],
blurring_image_1d=tracer.image_plane_blurring_images_1d[0],
def test__traced_grid_same_as_manual_deflections_calc_via_galaxy___use_multiple_galaxies(
self, sub_grid_7x7
):
# Overwrite one value so intensity in each pixel is different
sub_grid_7x7[5] = np.array([2.0, 2.0])
g0 = g.Galaxy(
redshift=0.5, mass_profile=mp.SphericalIsothermal(einstein_radius=1.0)
)
g1 = g.Galaxy(
redshift=0.5, mass_profile=mp.SphericalIsothermal(einstein_radius=2.0)
)
g0_deflections = g0.deflections_from_grid(
grid=sub_grid_7x7, return_in_2d=False, return_binned=False
)
g1_deflections = g1.deflections_from_grid(
grid=sub_grid_7x7, return_in_2d=False, return_binned=False
)
traced_grid = sub_grid_7x7 - (g0_deflections + g1_deflections)
plane = pl.Plane(galaxies=[g0, g1], redshift=None)
def test__galaxy_light_mass_sis__source_plane_image_includes_deflections(self, grid_stack):
g0 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=1.0),
mass_profile=mp.SphericalIsothermal(einstein_radius=1.0))
g1 = g.Galaxy(light_profile=lp.EllipticalSersic(intensity=2.0))
image_plane = pl.Plane(galaxies=[g0], grid_stack=grid_stack, compute_deflections=True)
deflections_grid = galaxy_util.deflections_of_galaxies_from_grid_stack(grid_stack, galaxies=[g0])
source_grid_stack = lens_util.traced_collection_for_deflections(grid_stack, deflections_grid)
source_plane = pl.Plane(galaxies=[g1], grid_stack=source_grid_stack, compute_deflections=False)
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[g0], source_galaxies=[g1],
image_plane_grid_stack=grid_stack)
image_plane_image_1d = image_plane.image_plane_image_1d + source_plane.image_plane_image_1d
assert (image_plane_image_1d == tracer.image_plane_image_1d).all()
image_plane_image_2d = grid_stack.regular.scaled_array_from_array_1d(image_plane.image_plane_image_1d) + \
grid_stack.regular.scaled_array_from_array_1d(source_plane.image_plane_image_1d)
print(light_profile_intensities[1])
print()
# The 1D flattening occurs from the top-left pixel, and goes rightwards and downwards. Thus, because the
# light profile is centered at (0.0, 0.0), the central pixels are the brightest.
print('intensity of central regular-pixels:')
print(light_profile_intensities[4949])
print(light_profile_intensities[4950])
print(light_profile_intensities[5049])
print(light_profile_intensities[5050])
# We can use a profile plotter to plot this intensity map (the regular is mapped to 2D before plotting).
profile_plotters.plot_intensities(light_profile=sersic_light_profile, grid=data_grids.regular)
# Lets create a singular isothermal sphere (SIS) mass-profile using the 'mass-profiles' module.
sis_mass_profile = mass_profiles.SphericalIsothermal(centre=(0.1, 0.1), einstein_radius=1.6)
print(sis_mass_profile)
# Just like above, we can pass a grid to a mass-profile to compute its deflection angles (Yep, still in 1D)
mass_profile_deflections = sis_mass_profile.deflections_from_grid(grid=data_grids.regular)
print('deflection-angles of regular-pixel 1:')
print(mass_profile_deflections[0])
print('deflection-angles of regular-pixel 2:')
print(mass_profile_deflections[1])
print()
print('deflection-angles of central regular-pixels:')
print(mass_profile_deflections[4949])
print(mass_profile_deflections[4950])
print(mass_profile_deflections[5049])
print(mass_profile_deflections[5050])
# And again, a profile plotter can plot these deflection angles in 2D.
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.1, pixel_scale=0.1)
image_plane_grid_stack = grids.GridStack.grid_stack_for_simulation(shape=(130, 130), pixel_scale=0.1, psf_shape=(11, 11))
lens_galaxy = g.Galaxy(mass=mp.SphericalIsothermal(centre=(0.0, 0.0), einstein_radius=1.6))
source_galaxy = g.Galaxy(light=lp.SphericalExponential(centre=(0.0, 0.0), intensity=0.2, effective_radius=0.2))
tracer = ray_tracing.TracerImageSourcePlanes(lens_galaxies=[lens_galaxy], source_galaxies=[source_galaxy],
image_plane_grid_stack=image_plane_grid_stack)
ccd_simulated = ccd.CCDData.simulate(array=tracer.image_plane_image_for_simulation, pixel_scale=0.1,
exposure_time=300.0, psf=psf, background_sky_level=0.1, add_noise=True)
return ccd_simulated