Esempio n. 1
0
    def test__masked_imaging_signal_to_noise_limit(self, imaging_7x7,
                                                   mask_7x7_1_pix):
        imaging_snr_limit = imaging_7x7.signal_to_noise_limited_from_signal_to_noise_limit(
            signal_to_noise_limit=1.0)

        phase_imaging_7x7 = toy.PhaseImaging(phase_name="phase_imaging_7x7",
                                             signal_to_noise_limit=1.0)

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7_1_pix)
        assert (analysis.masked_imaging.image.in_2d ==
                imaging_snr_limit.image.in_2d *
                np.invert(mask_7x7_1_pix)).all()
        assert (analysis.masked_imaging.noise_map.in_2d ==
                imaging_snr_limit.noise_map.in_2d *
                np.invert(mask_7x7_1_pix)).all()

        imaging_snr_limit = imaging_7x7.signal_to_noise_limited_from_signal_to_noise_limit(
            signal_to_noise_limit=0.1)

        phase_imaging_7x7 = toy.PhaseImaging(phase_name="phase_imaging_7x7",
                                             signal_to_noise_limit=0.1)

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7_1_pix)
        assert (analysis.masked_imaging.image.in_2d ==
                imaging_snr_limit.image.in_2d *
                np.invert(mask_7x7_1_pix)).all()
        assert (analysis.masked_imaging.noise_map.in_2d ==
                imaging_snr_limit.noise_map.in_2d *
                np.invert(mask_7x7_1_pix)).all()
Esempio n. 2
0
    def test__duplication(self):
        phase_dataset_7x7 = toy.PhaseImaging(
            phase_name="test_phase",
            gaussians=[
                af.PriorModel(cls=toy.SphericalGaussian),
                af.PriorModel(cls=toy.SphericalGaussian),
            ],
        )

        toy.PhaseImaging(phase_name="test_phase")

        assert phase_dataset_7x7.gaussians is not None
Esempio n. 3
0
def make_pipeline(
    phase_folders=None,
    sub_size=2,
    signal_to_noise_limit=None,
    bin_up_factor=None,
    optimizer_class=af.MultiNest,
):

    ### SETUP PIPELINE AND PHASE NAMES, TAGS AND PATHS ###

    # We setup the pipeline name using the tagging module. In this case, the pipeline name is not given a tag and
    # will be the string specified below However, its good practise to use the 'tag.' function below, incase
    # a pipeline does use customized tag names.

    pipeline_name = "pipeline_initialize__x1_gaussian"

    pipeline_tag = toy.pipeline_tagging.pipeline_tag_from_pipeline_settings()

    # This function uses the phase folders and pipeline name to set up the output directory structure,
    # e.g. 'autolens_workspace/output/pipeline_name/pipeline_tag/phase_name/phase_tag/'

    phase_folders.append(pipeline_name)
    phase_folders.append(pipeline_tag)

    ### PHASE 1 ###

    # In phase 1, we will fit the Gaussian profile, where we:

    # 1) Set our priors on the Gaussian's (y,x) centre such that we assume the image is centred around the Gaussian.

    gaussian = af.PriorModel(toy.SphericalGaussian)
    gaussian.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.1)
    gaussian.centre_1 = af.GaussianPrior(mean=0.0, sigma=0.1)

    phase1 = toy.PhaseImaging(
        phase_name="phase_1__x1_gaussian",
        phase_folders=phase_folders,
        gaussians=af.CollectionPriorModel(gaussian_0=gaussian),
        sub_size=sub_size,
        signal_to_noise_limit=signal_to_noise_limit,
        bin_up_factor=bin_up_factor,
        optimizer_class=optimizer_class,
    )

    # You'll see these lines throughout all of the example pipelines. They are used to make MultiNest sample the \
    # non-linear parameter space faster (if you haven't already, checkout 'tutorial_7_multinest_black_magic' in
    # 'howtolens/chapter_2_lens_modeling'.

    # Fitting the lens galaxy and source galaxy from uninitialized priors often risks MultiNest getting stuck in a
    # local maxima, especially for the image in this example which actually has two source galaxies. Therefore, whilst
    # I will continue to use constant efficiency mode to ensure fast run time, I've upped the number of live points
    # and decreased the sampling efficiency from the usual values to ensure the non-linear search is robust.

    phase1.optimizer.const_efficiency_mode = True
    phase1.optimizer.n_live_points = 20
    phase1.optimizer.sampling_efficiency = 0.5

    return toy.PipelineDataset(pipeline_name, phase1)
Esempio n. 4
0
    def test__fit_using_imaging(self, imaging_7x7, mask_7x7):
        clean_images()

        phase_imaging_7x7 = toy.PhaseImaging(
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian, toy.SphericalGaussian],
            phase_name="test_phase_test_fit",
        )

        result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7)
        assert isinstance(result.instance.gaussians[0], toy.SphericalGaussian)
        assert isinstance(result.instance.gaussians[1], toy.SphericalGaussian)
Esempio n. 5
0
    def test__results_of_phase_are_available_as_properties(
            self, imaging_7x7, mask_7x7):
        clean_images()

        phase_imaging_7x7 = toy.PhaseImaging(
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian],
            phase_name="test_phase_2",
        )

        result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7)

        assert isinstance(result, toy.AbstractPhase.Result)
Esempio n. 6
0
    def test_assertion_failure(self, imaging_7x7, mask_7x7):
        def make_analysis(*args, **kwargs):
            return mock_pipeline.MockAnalysis(value=1)

        phase_imaging_7x7 = toy.PhaseImaging(
            phase_name="phase_name",
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian],
        )

        phase_imaging_7x7.make_analysis = make_analysis
        result = phase_imaging_7x7.run(dataset=imaging_7x7,
                                       results=None,
                                       mask=mask_7x7)
        assert result is not None

        phase_imaging_7x7 = toy.PhaseImaging(
            phase_name="phase_name",
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian],
        )

        phase_imaging_7x7.make_analysis = make_analysis
        result = phase_imaging_7x7.run(dataset=imaging_7x7,
                                       results=None,
                                       mask=mask_7x7)
        assert result is not None

        class CustomPhase(toy.PhaseImaging):
            def customize_priors(self, results):
                self.gaussians[0] = toy.SphericalGaussian()

        phase_imaging_7x7 = CustomPhase(
            phase_name="phase_name",
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian],
        )
        phase_imaging_7x7.make_analysis = make_analysis
Esempio n. 7
0
    def test__results_of_phase_include_mask__available_as_property(
            self, imaging_7x7, mask_7x7):
        clean_images()

        phase_imaging_7x7 = toy.PhaseImaging(
            optimizer_class=mock_pipeline.MockNLO,
            gaussians=[toy.SphericalGaussian],
            sub_size=2,
            phase_name="test_phase_2",
        )

        result = phase_imaging_7x7.run(dataset=imaging_7x7, mask=mask_7x7)

        assert (result.mask == mask_7x7).all()
Esempio n. 8
0
    def test__masked_imaging_is_binned_up(self, imaging_7x7, mask_7x7_1_pix):
        binned_up_imaging = imaging_7x7.binned_from_bin_up_factor(
            bin_up_factor=2)

        binned_up_mask = mask_7x7_1_pix.mapping.binned_mask_from_bin_up_factor(
            bin_up_factor=2)

        phase_imaging_7x7 = toy.PhaseImaging(phase_name="phase_imaging_7x7",
                                             bin_up_factor=2)

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7_1_pix)
        assert (analysis.masked_imaging.image.in_2d ==
                binned_up_imaging.image.in_2d *
                np.invert(binned_up_mask)).all()
        assert (analysis.masked_imaging.psf == binned_up_imaging.psf).all()
        assert (analysis.masked_imaging.noise_map.in_2d ==
                binned_up_imaging.noise_map.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_imaging.mask == binned_up_mask).all()

        masked_imaging = aa.masked.imaging(imaging=imaging_7x7,
                                           mask=mask_7x7_1_pix)

        binned_up_masked_imaging = masked_imaging.binned_from_bin_up_factor(
            bin_up_factor=2)

        assert (analysis.masked_imaging.image.in_2d ==
                binned_up_masked_imaging.image.in_2d *
                np.invert(binned_up_mask)).all()
        assert (
            analysis.masked_imaging.psf == binned_up_masked_imaging.psf).all()
        assert (analysis.masked_imaging.noise_map.in_2d ==
                binned_up_masked_imaging.noise_map.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_imaging.mask == binned_up_masked_imaging.mask
                ).all()

        assert (analysis.masked_imaging.image.in_1d ==
                binned_up_masked_imaging.image.in_1d).all()
        assert (analysis.masked_imaging.noise_map.in_1d ==
                binned_up_masked_imaging.noise_map.in_1d).all()
Esempio n. 9
0
    def test__phase_can_receive_list_of_gaussian_models(self):
        phase_dataset_7x7 = toy.PhaseImaging(
            gaussians=[
                af.PriorModel(cls=toy.SphericalGaussian),
                af.PriorModel(cls=toy.SphericalGaussian),
            ],
            optimizer_class=af.MultiNest,
            phase_name="test_phase",
        )

        for item in phase_dataset_7x7.model.path_priors_tuples:
            print(item)

        gaussian_0 = phase_dataset_7x7.model.gaussians[0]
        gaussian_1 = phase_dataset_7x7.model.gaussians[1]

        arguments = {
            gaussian_0.centre[0]: 0.1,
            gaussian_0.centre[1]: 0.2,
            gaussian_0.intensity.priors[0]: 0.3,
            gaussian_0.sigma.priors[0]: 0.4,
            gaussian_1.centre[0]: 0.5,
            gaussian_1.centre[1]: 0.6,
            gaussian_1.intensity.priors[0]: 0.7,
            gaussian_1.sigma.priors[0]: 0.8,
        }

        instance = phase_dataset_7x7.model.instance_for_arguments(
            arguments=arguments)

        assert instance.gaussians[0].centre[0] == 0.1
        assert instance.gaussians[0].centre[1] == 0.2
        assert instance.gaussians[0].intensity == 0.3
        assert instance.gaussians[0].sigma == 0.4
        assert instance.gaussians[1].centre[0] == 0.5
        assert instance.gaussians[1].centre[1] == 0.6
        assert instance.gaussians[1].intensity == 0.7
        assert instance.gaussians[1].sigma == 0.8
Esempio n. 10
0
    def test__fit_figure_of_merit__matches_correct_fit_given_gaussian_profiles(
            self, imaging_7x7, mask_7x7):
        gaussian = toy.SphericalGaussian(intensity=0.1)

        phase_imaging_7x7 = toy.PhaseImaging(gaussians=[gaussian],
                                             sub_size=2,
                                             phase_name="test_phase")

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7)
        instance = phase_imaging_7x7.model.instance_from_unit_vector([])
        fit_figure_of_merit = analysis.fit(instance=instance)

        mask = phase_imaging_7x7.meta_imaging_fit.mask_with_phase_sub_size_from_mask(
            mask=mask_7x7)
        masked_imaging = aa.masked.imaging(imaging=imaging_7x7, mask=mask)

        model_image = gaussian.profile_image_from_grid(
            grid=masked_imaging.grid)
        fit = fit_masked_dataset(masked_dataset=masked_imaging,
                                 model_data=model_image.in_1d_binned)

        assert fit.likelihood == fit_figure_of_merit
Esempio n. 11
0
def make_pipeline(
    phase_folders=None,
    sub_size=2,
    signal_to_noise_limit=None,
    bin_up_factor=None,
    optimizer_class=af.MultiNest,
):

    ### SETUP PIPELINE AND PHASE NAMES, TAGS AND PATHS ###

    # We setup the pipeline name using the tagging module. In this case, the pipeline name is not given a tag and
    # will be the string specified below However, its good practise to use the 'tag.' function below, incase
    # a pipeline does use customized tag names.

    pipeline_name = "pipeline_initialize__x2_gaussian_separate"

    pipeline_tag = toy.pipeline_tagging.pipeline_tag_from_pipeline_settings()

    # This function uses the phase folders and pipeline name to set up the output directory structure,
    # e.g. 'autolens_workspace/output/pipeline_name/pipeline_tag/phase_name/phase_tag/'

    phase_folders.append(pipeline_name)
    phase_folders.append(pipeline_tag)

    ### PHASE 1 ###

    # In phase 1, we will fit the Gaussian profile, where we:

    # 1) Set our priors on the Gaussian's (y,x) centre such that we assume the Gaussian is centred around (0.0, -1.0).

    gaussian_0 = af.PriorModel(toy.SphericalGaussian)
    gaussian_0.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.1)
    gaussian_0.centre_1 = af.GaussianPrior(mean=-1.0, sigma=0.1)

    phase1 = toy.PhaseImaging(
        phase_name="phase_1__left_gaussian",
        phase_folders=phase_folders,
        gaussians=af.CollectionPriorModel(gaussian_0=gaussian_0),
        sub_size=sub_size,
        signal_to_noise_limit=signal_to_noise_limit,
        bin_up_factor=bin_up_factor,
        optimizer_class=optimizer_class,
    )

    phase1.optimizer.const_efficiency_mode = True
    phase1.optimizer.n_live_points = 20
    phase1.optimizer.sampling_efficiency = 0.5

    ### PHASE 2 ###

    # In phase 2, we will fit the Gaussian profile, where we:

    # 1) Use the resulting Gaussian from the result of phase 1 to fit its light.
    # 2) Set our priors on the second Gaussian's (y,x) centre such that we assume the Gaussian is centred around (0.0, 1.0).

    gaussian_1 = af.PriorModel(toy.SphericalGaussian)
    gaussian_1.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.1)
    gaussian_1.centre_1 = af.GaussianPrior(mean=1.0, sigma=0.1)

    phase2 = toy.PhaseImaging(
        phase_name="phase_2__right_gaussian",
        phase_folders=phase_folders,
        gaussians=af.CollectionPriorModel(
            gaussian_0=phase1.result.instance.gaussians.gaussian_0,
            gaussian_1=gaussian_1,
        ),
        sub_size=sub_size,
        signal_to_noise_limit=signal_to_noise_limit,
        bin_up_factor=bin_up_factor,
        optimizer_class=optimizer_class,
    )

    phase2.optimizer.const_efficiency_mode = True
    phase2.optimizer.n_live_points = 20
    phase2.optimizer.sampling_efficiency = 0.5

    ### PHASE 2 ###

    # In phase 3, we will fit both Gaussian profiles, where we:

    # 1) Use the resulting Gaussian models from the results of phase 1 and 2 to initialize their model parameters.

    phase3 = toy.PhaseImaging(
        phase_name="phase_3__both_gaussian",
        phase_folders=phase_folders,
        gaussians=af.CollectionPriorModel(
            gaussian_0=phase1.result.model.gaussians.gaussian_0,
            gaussian_1=phase2.result.model.gaussians.gaussian_1,
        ),
        sub_size=sub_size,
        signal_to_noise_limit=signal_to_noise_limit,
        bin_up_factor=bin_up_factor,
        optimizer_class=optimizer_class,
    )

    phase3.optimizer.const_efficiency_mode = True
    phase3.optimizer.n_live_points = 20
    phase3.optimizer.sampling_efficiency = 0.5

    return toy.PipelineDataset(pipeline_name, phase1, phase2, phase3)
Esempio n. 12
0
# Create the path where the dataset will be loaded from, which in this case is
# '/autolens_workspace/dataset/imaging/lens_light_mass_and_x1_source/'
dataset_path = af.path_util.make_and_return_path_from_path_and_folder_names(
    path=workspace_path, folder_names=["dataset", dataset_label])

imaging = toy.imaging.from_fits(
    image_path=dataset_path + "image.fits",
    noise_map_path=dataset_path + "noise_map.fits",
    pixel_scales=pixel_scales,
)

# toy.plot.imaging.subplot(imaging=imaging)

phase = toy.PhaseImaging(
    phase_name="phase_gaussian_example_loads",
    gaussians=af.CollectionPriorModel(gaussian_0=toy.SphericalGaussian),
    sub_size=1,
    optimizer_class=af.Emcee,
)

phase.optimizer.nwalkers = 100
phase.optimizer.nsteps = 200000

phase.run(dataset=imaging)

# Another example pipeline is shown below, which fits the dataset using a pixelized inversion for the source light.
# If you commented out the 3 lines of code above, and uncomment the code below, you can easily set this pipeline
# off running!

# from autolens_workspace.pipelines.simple import lens_sersic_sie_shear_source_inversion
# pipeline = lens_sersic_sie_shear_source_inversion.make_pipeline(phase_folders=[dataset_label, dataset_name])
# pipeline.run(dataset_label=imaging)
Esempio n. 13
0
def make_phase_imaging_7x7():
    return toy.PhaseImaging(optimizer_class=mock_pipeline.MockNLO,
                            phase_name="test_phase")