예제 #1
0
def simulator_from_instrument(instrument):
    """
    Returns the *Simulator* from an instrument type based on real observations.

    These options are representative of VRO, Euclid, HST, over-sampled HST and Adaptive Optics image.

    Parameters
    ----------
    instrument : str
        A string giving the resolution of the desired instrument (VRO | Euclid | HST | HST_Up | AO).
    """

    grid = grid_from_instrument(instrument=instrument)
    psf = psf_from_instrument(instrument=instrument)

    if instrument in "vro":
        return al.SimulatorImaging(
            exposure_time=100.0,
            psf=psf,
            background_sky_level=1.0,
            add_poisson_noise=True,
        )
    elif instrument in "euclid":
        return al.SimulatorImaging(
            exposure_time=2260.0,
            psf=psf,
            background_sky_level=1.0,
            add_poisson_noise=True,
        )
    elif instrument in "hst":
        return al.SimulatorImaging(
            exposure_time=2000.0,
            psf=psf,
            background_sky_level=1.0,
            add_poisson_noise=True,
        )
    elif instrument in "hst_up":
        return al.SimulatorImaging(
            exposure_time=2000.0,
            psf=psf,
            background_sky_level=1.0,
            add_poisson_noise=True,
        )
    elif instrument in "ao":
        return al.SimulatorImaging(
            exposure_time=1000.0,
            psf=psf,
            background_sky_level=1.0,
            add_poisson_noise=True,
        )
    else:
        raise ValueError("An invalid instrument was entered - ", instrument)
예제 #2
0
def simulate_for_source_galaxy(source_galaxy):

    grid = al.Grid2D.uniform(shape_native=(150, 150), pixel_scales=0.05, sub_size=2)

    psf = al.Kernel2D.from_gaussian(
        shape_native=(11, 11), sigma=0.05, pixel_scales=0.05
    )

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        mass=al.mp.EllIsothermal(
            centre=(0.0, 0.0), elliptical_comps=(0.111111, 0.0), einstein_radius=1.6
        ),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(
        exposure_time=300.0,
        psf=psf,
        background_sky_level=100.0,
        add_poisson_noise=True,
        noise_seed=1,
    )

    return simulator.from_tracer_and_grid(tracer=tracer, grid=grid)
예제 #3
0
    def test__from_tracer_and_grid__same_as_tracer_image(self):
        psf = al.Kernel.from_gaussian(shape_2d=(7, 7),
                                      sigma=0.5,
                                      pixel_scales=1.0)

        grid = al.Grid.uniform(shape_2d=(20, 20),
                               pixel_scales=0.05,
                               sub_size=1)

        lens_galaxy = al.Galaxy(
            redshift=0.5,
            light=al.lp.EllipticalSersic(intensity=1.0),
            mass=al.mp.EllipticalIsothermal(einstein_radius=1.6),
        )

        source_galaxy = al.Galaxy(redshift=1.0,
                                  light=al.lp.EllipticalSersic(intensity=0.3))

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time_map=al.Array.full(fill_value=10000.0,
                                            shape_2d=grid.shape_2d),
            background_sky_map=al.Array.full(fill_value=100.0,
                                             shape_2d=grid.shape_2d),
            add_noise=True,
            noise_seed=1,
        )

        imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)

        assert (imaging.image.in_2d == imaging.image.in_2d).all()
        assert (imaging.psf == imaging.psf).all()
        assert (imaging.noise_map == imaging.noise_map).all()
예제 #4
0
def simulate_function(instance):
    """
    Set up the `Tracer` which is used to simulate the strong lens imaging, which may include the subhalo in
    addition to the lens and source galaxy.
    """
    tracer = al.Tracer.from_galaxies(galaxies=[
        instance.galaxies.lens,
        instance.perturbation,
        instance.galaxies.source,
    ])
    """
    Set up the grid, PSF and simulator settings used to simulate imaging of the strong lens. These should be tuned to
    match the S/N and noise properties of the observed data you are performing sensitivity mapping on.
    """
    grid = al.Grid2DIterate.uniform(
        shape_native=mask.shape_native,
        pixel_scales=mask.pixel_scales,
        fractional_accuracy=0.9999,
        sub_steps=[2, 4, 8, 16, 24],
    )

    simulator = al.SimulatorImaging(
        exposure_time=300.0,
        psf=imaging.psf,
        background_sky_level=0.1,
        add_poisson_noise=True,
    )

    simulated_imaging = simulator.via_tracer_from(tracer=tracer, grid=grid)
    """
    The data generated by the simulate function is that which is fitted, so we should apply the mask for the analysis 
    here before we return the simulated data.
    """
    return simulated_imaging.apply_mask(mask=mask)
예제 #5
0
def simulate_imaging_in_adus(dataset_path):

    imaging_path = path.join(dataset_path, "imaging_in_adus")

    grid = al.Grid2D.uniform(shape_native=(130, 130), pixel_scales=0.1)

    psf = al.Kernel2D.from_gaussian(shape_native=(21, 21),
                                    sigma=0.05,
                                    pixel_scales=0.1)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.3,
                              effective_radius=1.0,
                              sersic_index=2.0),
        mass=al.mp.SphIsothermal(centre=(0.0, 0.0), einstein_radius=1.2),
    )

    source_galaxy = al.Galaxy(
        redshift=1.0,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.2,
                              effective_radius=1.0,
                              sersic_index=1.5),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(exposure_time=300.0,
                                    psf=psf,
                                    background_sky_level=0.1,
                                    add_poisson_noise=True)

    imaging = simulator.via_tracer_from(tracer=tracer, grid=grid)

    exposure_time_map = al.Array2D.full(
        fill_value=1000.0,
        shape_native=grid.shape_native,
        pixel_scales=grid.pixel_scales,
    )
    exposure_time_map.output_to_fits(file_path=path.join(
        imaging_path, "exposure_time_map.fits"),
                                     overwrite=True)

    imaging.data = al.preprocess.array_eps_to_adus(
        array_eps=imaging.image, exposure_time_map=exposure_time_map, gain=4.0)
    imaging.noise_map = al.preprocess.array_eps_to_adus(
        array_eps=imaging.noise_map,
        exposure_time_map=exposure_time_map,
        gain=4.0)

    imaging.output_to_fits(
        image_path=path.join(imaging_path, "image.fits"),
        noise_map_path=path.join(imaging_path, "noise_map.fits"),
        psf_path=path.join(imaging_path, "psf.fits"),
        overwrite=True,
    )
예제 #6
0
def simulate_imaging(dataset_path):

    imaging_path = path.join(dataset_path, "imaging")

    grid = al.Grid2D.uniform(shape_native=(130, 130), pixel_scales=0.1)

    psf = al.Kernel2D.from_gaussian(shape_native=(21, 21),
                                    sigma=0.05,
                                    pixel_scales=0.1)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.3,
                              effective_radius=1.0,
                              sersic_index=2.0),
        mass=al.mp.SphIsothermal(centre=(0.0, 0.0), einstein_radius=1.2),
    )

    source_galaxy = al.Galaxy(
        redshift=1.0,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.2,
                              effective_radius=1.0,
                              sersic_index=1.5),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(exposure_time=300.0,
                                    psf=psf,
                                    background_sky_level=0.1,
                                    add_poisson_noise=True)

    imaging = simulator.via_tracer_from(tracer=tracer, grid=grid)

    imaging.output_to_fits(
        image_path=path.join(imaging_path, "image.fits"),
        noise_map_path=path.join(imaging_path, "noise_map.fits"),
        psf_path=path.join(imaging_path, "psf.fits"),
        overwrite=True,
    )

    hdu_list = fits.HDUList()
    hdu_list.append(fits.ImageHDU(imaging.image.native))
    hdu_list.append(fits.ImageHDU(imaging.noise_map.native))
    hdu_list.append(fits.ImageHDU(imaging.psf.native))

    if path.exists(path.join(imaging_path, "multiple_hdus.fits")):
        os.remove(path.join(imaging_path, "multiple_hdus.fits"))

    hdu_list.writeto(path.join(imaging_path, "multiple_hdus.fits"))
def simulate_imaging_with_even_psf(dataset_path):

    imaging_path = path.join(dataset_path, "imaging_with_even_psf")

    grid = al.Grid2D.uniform(shape_native=(130, 130), pixel_scales=0.1)

    psf = al.Kernel2D.from_gaussian(shape_native=(21, 21),
                                    sigma=0.05,
                                    pixel_scales=0.1)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.3,
                              effective_radius=1.0,
                              sersic_index=2.0),
        mass=al.mp.SphIsothermal(centre=(0.0, 0.0), einstein_radius=1.2),
    )

    source_galaxy = al.Galaxy(
        redshift=1.0,
        bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                              intensity=0.2,
                              effective_radius=1.0,
                              sersic_index=1.5),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(exposure_time=300.0,
                                    psf=psf,
                                    background_sky_level=0.1,
                                    add_poisson_noise=True)

    imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)

    imaging.psf_unormalized = al.Kernel2D.from_gaussian(shape_native=(22, 22),
                                                        sigma=0.05,
                                                        pixel_scales=0.1)

    imaging.psf_normalized = al.Kernel2D.from_gaussian(shape_native=(22, 22),
                                                       sigma=0.05,
                                                       pixel_scales=0.1)

    imaging.output_to_fits(
        image_path=path.join(imaging_path, "image.fits"),
        noise_map_path=path.join(imaging_path, "noise_map.fits"),
        psf_path=path.join(imaging_path, "psf.fits"),
        overwrite=True,
    )
예제 #8
0
    def test__simulate_imaging_from_lens__source_galaxy__compare_to_imaging(
            self):

        lens_galaxy = al.Galaxy(
            redshift=0.5,
            mass=al.mp.EllipticalIsothermal(centre=(0.0, 0.0),
                                            einstein_radius=1.6,
                                            axis_ratio=0.7,
                                            phi=45.0),
        )

        source_galaxy = al.Galaxy(
            redshift=0.5,
            light=al.lp.EllipticalSersic(
                centre=(0.1, 0.1),
                axis_ratio=0.8,
                phi=60.0,
                intensity=0.3,
                effective_radius=1.0,
                sersic_index=2.5,
            ),
        )

        grid = al.Grid.uniform(shape_2d=(11, 11), pixel_scales=0.2, sub_size=1)

        psf = al.Kernel.no_blur(pixel_scales=0.2)

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time_map=al.Array.full(fill_value=10000.0,
                                            shape_2d=grid.shape_2d),
            background_sky_map=al.Array.full(fill_value=100.0,
                                             shape_2d=grid.shape_2d),
            add_noise=True,
            noise_seed=1,
        )

        imaging = simulator.from_galaxies_and_grid(
            galaxies=[lens_galaxy, source_galaxy], grid=grid)

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        imaging_via_image = simulator.from_image(
            image=tracer.profile_image_from_grid(grid=grid))

        assert (imaging.image == imaging_via_image.image).all()
        assert (imaging.psf == imaging_via_image.psf).all()
        assert imaging.noise_map == imaging_via_image.noise_map
예제 #9
0
    def test__simulate_imaging_from_lens__source_galaxy__compare_to_imaging(
            self):

        lens_galaxy = al.Galaxy(
            redshift=0.5,
            mass=al.mp.EllIsothermal(centre=(0.0, 0.0),
                                     einstein_radius=1.6,
                                     elliptical_comps=(0.17647, 0.0)),
        )

        source_galaxy = al.Galaxy(
            redshift=0.5,
            light=al.lp.EllSersic(
                centre=(0.1, 0.1),
                elliptical_comps=(0.096225, -0.055555),
                intensity=0.3,
                effective_radius=1.0,
                sersic_index=2.5,
            ),
        )

        grid = al.Grid2D.uniform(shape_native=(11, 11),
                                 pixel_scales=0.2,
                                 sub_size=1)

        psf = al.Kernel2D.no_blur(pixel_scales=0.2)

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time=10000.0,
            background_sky_level=100.0,
            add_poisson_noise=True,
            noise_seed=1,
        )

        imaging = simulator.from_galaxies_and_grid(
            galaxies=[lens_galaxy, source_galaxy], grid=grid)

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        imaging_via_image = simulator.from_image(
            image=tracer.image_2d_from_grid(grid=grid))

        assert imaging.shape_native == (11, 11)
        assert (imaging.image.native == imaging_via_image.image.native).all()
        assert (imaging.psf == imaging_via_image.psf).all()
        assert (imaging.noise_map == imaging_via_image.noise_map).all()
예제 #10
0
def test__simulate_imaging_data_and_fit__known_likelihood():

    grid = al.Grid2D.uniform(shape_native=(31, 31), pixel_scales=0.2, sub_size=1)

    psf = al.Kernel2D.from_gaussian(
        shape_native=(3, 3), pixel_scales=0.2, sigma=0.75, normalize=True
    )

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.EllSersic(centre=(0.1, 0.1), intensity=0.1),
        mass=al.mp.EllIsothermal(centre=(0.1, 0.1), einstein_radius=1.8),
    )
    source_galaxy_0 = al.Galaxy(
        redshift=1.0,
        pixelization=al.pix.Rectangular(shape=(16, 16)),
        regularization=al.reg.Constant(coefficient=(1.0)),
    )
    source_galaxy_1 = al.Galaxy(
        redshift=2.0,
        pixelization=al.pix.Rectangular(shape=(16, 16)),
        regularization=al.reg.Constant(coefficient=(1.0)),
    )
    tracer = al.Tracer.from_galaxies(
        galaxies=[lens_galaxy, source_galaxy_0, source_galaxy_1]
    )

    imaging = al.SimulatorImaging(exposure_time=300.0, psf=psf, noise_seed=1)

    imaging = imaging.via_tracer_from(tracer=tracer, grid=grid)

    mask = al.Mask2D.circular(
        shape_native=imaging.image.shape_native, pixel_scales=0.2, radius=2.0
    )

    masked_imaging = imaging.apply_mask(mask=mask)

    fit = al.FitImaging(dataset=masked_imaging, tracer=tracer)

    assert fit.figure_of_merit == pytest.approx(609.0653285500165, 1.0e-2)
예제 #11
0
    def test__from_tracer_and_grid__same_as_tracer_image(self):
        psf = al.Kernel2D.from_gaussian(shape_native=(7, 7),
                                        sigma=0.5,
                                        pixel_scales=1.0)

        grid = al.Grid2D.uniform(shape_native=(20, 20),
                                 pixel_scales=0.05,
                                 sub_size=1)

        lens_galaxy = al.Galaxy(
            redshift=0.5,
            light=al.lp.EllSersic(intensity=1.0),
            mass=al.mp.EllIsothermal(einstein_radius=1.6),
        )

        source_galaxy = al.Galaxy(redshift=1.0,
                                  light=al.lp.EllSersic(intensity=0.3))

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time=10000.0,
            background_sky_level=100.0,
            add_poisson_noise=False,
        )

        imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)

        imaging_via_image = simulator.from_image(
            image=tracer.image_2d_from_grid(grid=grid))

        assert imaging.shape_native == (20, 20)
        assert imaging.image.native[0, 0] != imaging_via_image.image.native[0,
                                                                            0]
        assert imaging.image.native[10,
                                    10] == imaging_via_image.image.native[10,
                                                                          10]
        assert (imaging.psf == imaging_via_image.psf).all()
        assert (imaging.noise_map == imaging_via_image.noise_map).all()
예제 #12
0
    def test__from_deflections_and_galaxies__same_as_calculation_using_tracer(
            self):

        psf = al.Kernel.no_blur(pixel_scales=0.05)

        grid = al.Grid.uniform(shape_2d=(20, 20),
                               pixel_scales=0.05,
                               sub_size=1)

        lens_galaxy = al.Galaxy(
            redshift=0.5, mass=al.mp.EllipticalIsothermal(einstein_radius=1.6))

        source_galaxy = al.Galaxy(redshift=1.0,
                                  light=al.lp.EllipticalSersic(intensity=0.3))

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time_map=al.Array.full(fill_value=10000.0,
                                            shape_2d=grid.shape_2d),
            background_sky_map=al.Array.full(fill_value=100.0,
                                             shape_2d=grid.shape_2d),
            add_noise=True,
            noise_seed=1,
        )

        imaging = simulator.from_deflections_and_galaxies(
            deflections=tracer.deflections_from_grid(grid=grid),
            galaxies=[source_galaxy],
        )

        imaging_via_image = simulator.from_image(
            image=tracer.profile_image_from_grid(grid=grid))

        assert (imaging.image.in_2d == imaging_via_image.image.in_2d).all()
        assert (imaging.psf == imaging_via_image.psf).all()
        assert (imaging.noise_map == imaging_via_image.noise_map).all()
예제 #13
0
    def test__from_deflections_and_galaxies__same_as_calculation_using_tracer(
            self):

        psf = al.Kernel2D.no_blur(pixel_scales=0.05)

        grid = al.Grid2D.uniform(shape_native=(20, 20),
                                 pixel_scales=0.05,
                                 sub_size=1)

        lens_galaxy = al.Galaxy(redshift=0.5,
                                mass=al.mp.EllIsothermal(einstein_radius=1.6))

        source_galaxy = al.Galaxy(redshift=1.0,
                                  light=al.lp.EllSersic(intensity=0.3))

        tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

        simulator = al.SimulatorImaging(
            psf=psf,
            exposure_time=10000.0,
            background_sky_level=100.0,
            add_poisson_noise=False,
        )

        imaging = simulator.from_deflections_and_galaxies(
            deflections=tracer.deflections_2d_from_grid(grid=grid),
            galaxies=[source_galaxy],
        )

        imaging_via_image = simulator.from_image(
            image=tracer.image_2d_from_grid(grid=grid))

        assert imaging.shape_native == (20, 20)
        assert (imaging.image.native == imaging_via_image.image.native).all()
        assert (imaging.psf == imaging_via_image.psf).all()
        assert (imaging.noise_map == imaging_via_image.noise_map).all()
예제 #14
0
def test__simulate_imaging_data_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct(
):

    grid = al.Grid.uniform(shape_2d=(11, 11), pixel_scales=0.2, sub_size=1)

    psf = al.Kernel.from_gaussian(shape_2d=(3, 3),
                                  pixel_scales=0.2,
                                  sigma=0.75,
                                  renormalize=True)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1),
        mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1),
                                        einstein_radius=1.8),
    )
    source_galaxy = al.Galaxy(
        redshift=1.0,
        light=al.lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5),
    )
    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(
        exposure_time_map=al.Array.full(fill_value=300.0,
                                        shape_2d=grid.shape_2d),
        psf=psf,
        background_sky_map=al.Array.zeros(shape_2d=grid.shape_2d),
        add_noise=False,
    )

    imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)
    imaging.noise_map = al.Array.ones(shape_2d=imaging.image.shape_2d)

    path = "{}/data_temp/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) is False:
        os.makedirs(path)

    imaging.output_to_fits(
        image_path=f"{path}/image.fits",
        noise_map_path=f"{path}/noise_map.fits",
        psf_path=f"{path}/psf.fits",
    )

    simulator = al.Imaging.from_fits(
        image_path=f"{path}/image.fits",
        noise_map_path=f"{path}/noise_map.fits",
        psf_path=f"{path}/psf.fits",
        pixel_scales=0.2,
    )

    mask = al.Mask.circular(shape_2d=simulator.image.shape_2d,
                            pixel_scales=0.2,
                            radius=0.8)

    masked_imaging = al.MaskedImaging(
        imaging=simulator,
        mask=mask,
        settings=al.SettingsMaskedImaging(sub_size=1))

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)

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

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

    if os.path.exists(path) == True:
        shutil.rmtree(path)
def gen_data(parameters,
             pixel_scales=0.1,
             psf_shape=[11, 11],
             psf_sigma=0.1,
             grid_sub_size=2,
             grid_shape=[100, 100],
             sub_halo_mass=[],
             sub_halo_mass_fractions=[0.01],
             output_type='image',
             output_path='./lens_sub_spherical',
             file_name='particle'):
    '''
     
     Args:
     ______
     
     pixel_scales: float
        The arc-second to pixel conversion factor of each pixel.
     
     psf_shape: []
        Shape of the Gaussian kernel
     
     psf_sigma: float
        Standard deviation for Gaussian kernel
     
     grid_sub_size: int
        The size (sub_size x sub_size) of each unmasked pixels sub-grid.
     
     grid_shape: []
     
     sub_halo_mass: []
        Masses of substructures (in solar masses)
        
     sub_halo_mass_fractions: []
        Array of fractions with respect to the mass of the DM halo
     
     output_type: str
        'image': save the lensing images as .png files
        'numpy': save the lesning images as a numpy array
        'matlab': save the lesning images as a matlab (.MAT) file
        'hdf5': save the lensing images as a HDF file
     
     output_path: str
     
     file_name: str
     
    '''

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    bar = Bar('Processing lensing images', max=parameters.shape[0])
    lensing_images = []

    for i in range(parameters.shape[0]):

        params = parameters[i]
        psf = al.Kernel.from_gaussian(shape_2d=(psf_shape[0], psf_shape[1]),
                                      sigma=psf_sigma,
                                      pixel_scales=pixel_scales)
        grid = al.Grid.uniform(shape_2d=(grid_shape[0], grid_shape[1]),
                               pixel_scales=pixel_scales,
                               sub_size=grid_sub_size)

        spherical_profiles = []

        # Dark Matter Halo
        spherical_profiles.append(
            ("dmh_profile",
             al.mp.SphericalIsothermal(centre=(params[8], params[9]),
                                       einstein_radius=params[10])))

        # Get positional parameters for particle substructure
        rad_dist = np.random.uniform(params[25], params[26],
                                     int(params[23])).tolist()
        ang_pos = np.random.uniform(params[27], params[28],
                                    int(params[24])).tolist()

        pos_args = list(list(itertools.product(rad_dist, ang_pos)))
        random.shuffle(pos_args)

        if sub_halo_mass == []:

            if sub_halo_mass_fractions.all() == [0.01]:

                for j in range(int(params[21])):

                    x0 = params[0] + pos_args[j][0] * math.cos(pos_args[j][1])
                    y0 = params[1] + pos_args[j][0] * math.sin(pos_args[j][1])

                    spherical_profiles.append(
                        ("point_mass_profile_" + str(j + 1),
                         al.mp.PointMass(centre=(x0, y0),
                                         einstein_radius=((params[22])**0.5) /
                                         params[21] * params[10])))

            if sub_halo_mass_fractions.all() != [0.01]:

                fraction = np.asarray(sub_halo_mass_fractions)
                if fraction.shape[0] != int(params[21]):
                    raise Exception('Invalid number of sub halos')
                    sys.exit()

                for j in range(int(params[21])):

                    x0 = params[0] + pos_args[j][0] * math.cos(pos_args[j][1])
                    y0 = params[1] + pos_args[j][0] * math.sin(pos_args[j][1])

                    spherical_profiles.append(
                        ("point_mass_profile_" + str(j + 1),
                         al.mp.PointMass(centre=(x0, y0),
                                         einstein_radius=((fraction[j])**0.5) *
                                         params[10])))

        if sub_halo_mass != []:

            sub_halo_mass = np.asarray(sub_halo_mass)
            if sub_halo_mass.shape[0] != int(params[21]):
                raise Exception('Invalid number of sub halos')
                sys.exit()

            for j in range(int(params[21])):

                x0 = params[0] + pos_args[j][0] * math.cos(pos_args[j][1])
                y0 = params[1] + pos_args[j][0] * math.sin(pos_args[j][1])

                spherical_profiles.append(
                    ("point_mass_profile_" + str(j + 1),
                     al.mp.PointMass(centre=(x0, y0),
                                     einstein_radius=ER(
                                         sub_halo_mass[j], 0.5, params[15]))))

        # Lens galaxy
        lensing_galaxy = al.Galaxy(
            redshift=params[2],
            # Light Profile
            light=al.lp.EllipticalSersic(
                centre=(params[0], params[1]),
                axis_ratio=params[3],
                phi=params[4],
                intensity=params[5],
                effective_radius=params[7],
                sersic_index=params[6],
            ),
            # Mass Profile
            **dict(spherical_profiles),

            # External Shear
            shear=al.mp.ExternalShear(magnitude=params[11], phi=params[12]),
        )

        galaxies = [lensing_galaxy]

        # Calculate coordinates of lensed galaxy
        x = params[0] + params[13] * math.cos(params[14])
        y = params[1] + params[13] * math.sin(params[14])

        # Source galaxy
        lensed_galaxy = al.Galaxy(
            redshift=params[15],
            # Light Profile
            light=al.lp.EllipticalSersic(
                centre=(x, y),
                axis_ratio=params[16],
                phi=params[17],
                intensity=params[18],
                effective_radius=params[20],
                sersic_index=params[19],
            ),
        )

        galaxies.append(lensed_galaxy)

        tracer = al.Tracer.from_galaxies(galaxies)

        simulator = al.SimulatorImaging(
            exposure_time_map=al.Array.full(fill_value=300.0,
                                            shape_2d=grid.shape_2d),
            psf=psf,
            background_sky_map=al.Array.full(fill_value=0.1,
                                             shape_2d=grid.shape_2d),
            add_noise=True,
        )

        imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)
        image = imaging.image.in_2d

        # Export all the Lensing Images
        if output_type.lower() == 'image':

            output_file = os.path.join(output_path,
                                       file_name + str(i + 1) + '.png')
            plt.imsave(output_file, image, cmap='gray')

        if output_type.lower() in ('numpy', 'matlab', 'hdf5'):
            lensing_images.append(image)

        bar.next()

    bar.finish()

    lensing_images = np.asarray(lensing_images)

    # Dump all the Lensing Images into a numpy array
    if output_type.lower() == 'numpy':

        output_file = os.path.join(output_path, file_name + '.npy')
        np.save(output_file, lensing_images)
        print('Dimensions of the data: {}'.format(lensing_images.shape))

    # Dump all the Lensing Images into a matlab (.MAT) file
    if output_type.lower() == 'matlab':

        output_file = os.path.join(output_path, file_name + '.mat')
        scipy.io.savemat(output_file, mdict={'spherical': lensing_images})
        print('Dimensions of the data: {}'.format(lensing_images.shape))

    # Dump all the Lensing Images into a HDF file
    if output_type.lower() == 'hdf5':

        output_file = os.path.join(output_path, file_name + '.h5')
        with h5py.File(output_file, 'w') as hf:
            hf.create_dataset("spherical", data=lensing_images)
        print('Dimensions of the data: {}'.format(lensing_images.shape))
예제 #16
0
def test__simulate_imaging_data_and_fit__no_psf_blurring__chi_squared_is_0__noise_normalization_correct():

    grid = al.GridIterate.uniform(shape_2d=(11, 11), pixel_scales=0.2)

    psf = al.Kernel.manual_2d(
        array=[[0.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 0.0]], pixel_scales=0.2
    )

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.EllipticalSersic(centre=(0.1, 0.1), intensity=0.1),
        mass=al.mp.EllipticalIsothermal(centre=(0.1, 0.1), einstein_radius=1.8),
    )

    source_galaxy = al.Galaxy(
        redshift=1.0,
        light=al.lp.EllipticalExponential(centre=(0.1, 0.1), intensity=0.5),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    simulator = al.SimulatorImaging(
        exposure_time=300.0, psf=psf, add_poisson_noise=False
    )

    imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)

    imaging.noise_map = al.Array.ones(shape_2d=imaging.image.shape_2d, pixel_scales=0.2)

    file_path = path.join(
        "{}".format(path.dirname(path.realpath(__file__))),
        "data_temp",
        "simulate_and_fit",
    )

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

    if path.exists(file_path) is False:
        os.makedirs(file_path)

    imaging.output_to_fits(
        image_path=path.join(file_path, "image.fits"),
        noise_map_path=path.join(file_path, "noise_map.fits"),
        psf_path=path.join(file_path, "psf.fits"),
    )

    imaging = al.Imaging.from_fits(
        image_path=path.join(file_path, "image.fits"),
        noise_map_path=path.join(file_path, "noise_map.fits"),
        psf_path=path.join(file_path, "psf.fits"),
        pixel_scales=0.2,
    )

    mask = al.Mask2D.circular(
        shape_2d=imaging.image.shape_2d, pixel_scales=0.2, sub_size=2, radius=0.8
    )

    masked_imaging = al.MaskedImaging(
        imaging=imaging,
        mask=mask,
        settings=al.SettingsMaskedImaging(grid_class=al.GridIterate),
    )

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    fit = al.FitImaging(masked_imaging=masked_imaging, tracer=tracer)

    assert fit.chi_squared == 0.0

    file_path = path.join(
        "{}".format(path.dirname(path.realpath(__file__))), "data_temp"
    )
    if path.exists(file_path) == True:
        shutil.rmtree(file_path)
예제 #17
0
    pixel_scales=0.1,
    fractional_accuracy=0.9999,
    sub_steps=[2, 4, 8, 16, 24],
)
"""
Simulate a simple Gaussian PSF for the image.
"""
psf = al.Kernel2D.from_gaussian(shape_native=(11, 11),
                                sigma=0.1,
                                pixel_scales=grid.pixel_scales)
"""
To simulate the `Imaging` dataset we first create a simulator, which defines the exposure time, background sky,
noise levels and psf of the dataset that is simulated.
"""
simulator = al.SimulatorImaging(exposure_time=300.0,
                                psf=psf,
                                background_sky_level=0.1,
                                add_poisson_noise=True)
"""
__Ray Tracing__

Setup the mass models of the three lens galaxies using the `SphIsothermal` model and the source galaxy light using 
an elliptical Sersic for this simulated lens.
"""
lens_galaxy_0 = al.Galaxy(
    redshift=0.5,
    bulge=al.lp.SphSersic(centre=(0.0, 0.0),
                          intensity=0.7,
                          effective_radius=2.0,
                          sersic_index=4.0),
    mass=al.mp.SphIsothermal(centre=(0.0, 0.0), einstein_radius=4.0),
)
예제 #18
0
    shape_2d=(100, 100),
    pixel_scales=0.1,
    fractional_accuracy=0.9999,
    sub_steps=[2, 4, 8, 16, 24],
)
"""Simulate a simple Gaussian PSF for the image."""
psf = al.Kernel.from_gaussian(shape_2d=(11, 11),
                              sigma=0.1,
                              pixel_scales=grid.pixel_scales)
"""
To simulate the _Imaging_ dataset we first create a simulator, which defines the expoosure time, background sky,
noise levels and psf of the dataset that is simulated.
"""
simulator = al.SimulatorImaging(
    exposure_time_map=al.Array.full(fill_value=300.0, shape_2d=grid.shape_2d),
    psf=psf,
    background_sky_map=al.Array.full(fill_value=0.1, shape_2d=grid.shape_2d),
    add_noise=True,
)
"""
Setup the lens galaxy's mass (SIE+Shear) and source galaxy light (elliptical Sersic) for this simulated lens.

For lens modeling, defining ellipticity in terms of the  'elliptical_comps' improves the model-fitting procedure.

However, for simulating a strong lens you may find it more intuitive to define the elliptical geometry using the 
axis-ratio of the profile (axis_ratio = semi-major axis / semi-minor axis = b/a) and position angle phi, where phi is
in degrees and defined counter clockwise from the positive x-axis.

We can use the **__PyAutoLens__** *convert* module to determine the elliptical components from the axis-ratio and phi.
"""
lens_galaxy = al.Galaxy(redshift=0.5,
                        mass=al.mp.SphericalIsothermal(centre=(0.0, 0.0),
예제 #19
0
def test__simulate_imaging_data_and_fit__include_psf_blurring__chi_squared_is_0__noise_normalization_correct(
):

    grid = al.Grid2D.uniform(shape_native=(11, 11),
                             pixel_scales=0.2,
                             sub_size=1)

    psf = al.Kernel2D.from_gaussian(shape_native=(3, 3),
                                    pixel_scales=0.2,
                                    sigma=0.75,
                                    normalize=True)

    lens_galaxy = al.Galaxy(
        redshift=0.5,
        light=al.lp.EllSersic(centre=(0.1, 0.1), intensity=0.1),
        mass=al.mp.EllIsothermal(centre=(0.1, 0.1), einstein_radius=1.8),
    )
    source_galaxy = al.Galaxy(redshift=1.0,
                              light=al.lp.EllExponential(centre=(0.1, 0.1),
                                                         intensity=0.5))
    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    imaging = al.SimulatorImaging(exposure_time=300.0,
                                  psf=psf,
                                  add_poisson_noise=False)

    imaging = imaging.from_tracer_and_grid(tracer=tracer, grid=grid)
    imaging.noise_map = al.Array2D.ones(
        shape_native=imaging.image.shape_native, pixel_scales=0.2)

    file_path = path.join(
        "{}".format(path.dirname(path.realpath(__file__))),
        "data_temp",
        "simulate_and_fit",
    )

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

    if path.exists(file_path) is False:
        os.makedirs(file_path)

    imaging.output_to_fits(
        image_path=path.join(file_path, "image.fits"),
        noise_map_path=path.join(file_path, "noise_map.fits"),
        psf_path=path.join(file_path, "psf.fits"),
    )

    imaging = al.Imaging.from_fits(
        image_path=path.join(file_path, "image.fits"),
        noise_map_path=path.join(file_path, "noise_map.fits"),
        psf_path=path.join(file_path, "psf.fits"),
        pixel_scales=0.2,
    )

    mask = al.Mask2D.circular(shape_native=imaging.image.shape_native,
                              pixel_scales=0.2,
                              radius=0.8)

    masked_imaging = imaging.apply_mask(mask=mask)
    masked_imaging = masked_imaging.apply_settings(settings=al.SettingsImaging(
        sub_size=1))

    tracer = al.Tracer.from_galaxies(galaxies=[lens_galaxy, source_galaxy])

    fit = al.FitImaging(imaging=masked_imaging, tracer=tracer)

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

    file_path = path.join("{}".format(path.dirname(path.realpath(__file__))),
                          "data_temp")

    if path.exists(file_path) is True:
        shutil.rmtree(file_path)
def gen_data(parameters,
             pixel_scales=0.1,
             psf_shape=[11, 11],
             psf_sigma=0.1,
             grid_sub_size=2,
             grid_shape=[100, 100],
             output_type='image',
             output_path='./lens_nosub',
             file_name='base'):
    '''
     
     Args:
     ______
     
     pixel_scales: float
        The arc-second to pixel conversion factor of each pixel.
     
     psf_shape: []
        Shape of the Gaussian kernel
     
     psf_sigma: float
        Standard deviation for Gaussian kernel
     
     grid_sub_size: int
        The size (sub_size x sub_size) of each unmasked pixels sub-grid.
     
     grid_shape: []
     
     output_type: str
        'image': save the lensing images as .png files
        'numpy': save the lesning images as a numpy array
        'matlab': save the lesning images as a matlab (.MAT) file
        'hdf5': save the lensing images as a HDF file
     
     output_path: str
     
     file_name: str
     
    '''

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    bar = Bar('Processing lensing images', max=parameters.shape[0])
    lensing_images = []
    for i in range(parameters.shape[0]):

        params = parameters[i]
        psf = al.Kernel.from_gaussian(shape_2d=(psf_shape[0], psf_shape[1]),
                                      sigma=psf_sigma,
                                      pixel_scales=pixel_scales)
        grid = al.Grid.uniform(shape_2d=(grid_shape[0], grid_shape[1]),
                               pixel_scales=pixel_scales,
                               sub_size=grid_sub_size)

        # Lens galaxy
        lensing_galaxy = al.Galaxy(
            redshift=params[2],
            # Light Profile
            light=al.lp.EllipticalSersic(
                centre=(params[0], params[1]),
                axis_ratio=params[3],
                phi=params[4],
                intensity=params[5],
                effective_radius=params[7],
                sersic_index=params[6],
            ),
            # Dark Matter Halo
            mass=al.mp.SphericalIsothermal(centre=(params[8], params[9]),
                                           einstein_radius=params[10]),
            # External Shear
            shear=al.mp.ExternalShear(magnitude=params[11], phi=params[12]),
        )

        galaxies = [lensing_galaxy]

        # Calculate coordinates of lensed galaxy
        x = params[0] + params[13] * math.cos(params[14])
        y = params[1] + params[13] * math.sin(params[14])

        # Source galaxy
        lensed_galaxy = al.Galaxy(
            redshift=params[15],
            # Light Profile
            light=al.lp.EllipticalSersic(
                centre=(x, y),
                axis_ratio=params[16],
                phi=params[17],
                intensity=params[18],
                effective_radius=params[20],
                sersic_index=params[19],
            ),
        )

        galaxies.append(lensed_galaxy)

        tracer = al.Tracer.from_galaxies(galaxies)

        simulator = al.SimulatorImaging(
            exposure_time_map=al.Array.full(fill_value=300.0,
                                            shape_2d=grid.shape_2d),
            psf=psf,
            background_sky_map=al.Array.full(fill_value=0.1,
                                             shape_2d=grid.shape_2d),
            add_noise=True,
        )

        imaging = simulator.from_tracer_and_grid(tracer=tracer, grid=grid)
        image = imaging.image.in_2d

        # Export all the Lensing Images
        if output_type.lower() == 'image':

            output_file = os.path.join(output_path,
                                       file_name + str(i + 1) + '.png')
            plt.imsave(output_file, image, cmap='gray')

        if output_type.lower() in ('numpy', 'matlab', 'hdf5'):
            lensing_images.append(image)

        bar.next()

    bar.finish()

    lensing_images = np.asarray(lensing_images)

    # Dump all the Lensing Images into a numpy array
    if output_type.lower() == 'numpy':

        output_file = os.path.join(output_path, file_name + '.npy')
        np.save(output_file, lensing_images)
        print('Dimensions of the data: {}'.format(lensing_images.shape))

    # Dump all the Lensing Images into a matlab (.MAT) file
    if output_type.lower() == 'matlab':

        output_file = os.path.join(output_path, file_name + '.mat')
        scipy.io.savemat(output_file, mdict={'vortex': lensing_images})
        print('Dimensions of the data: {}'.format(lensing_images.shape))

    # Dump all the Lensing Images into a HDF file
    if output_type.lower() == 'hdf5':

        output_file = os.path.join(output_path, file_name + '.h5')
        with h5py.File(output_file, 'w') as hf:
            hf.create_dataset("vortex", data=lensing_images)
        print('Dimensions of the data: {}'.format(lensing_images.shape))