示例#1
0
    def test__positions_threshold_tag(self):

        settings = al.SettingsLens(positions_threshold=None)
        assert settings.positions_threshold_tag == ""
        settings = al.SettingsLens(positions_threshold=1.0)
        assert settings.positions_threshold_tag == "__pos_1.00"
        settings = al.SettingsLens(positions_threshold=2.56)
        assert settings.positions_threshold_tag == "__pos_2.56"
示例#2
0
    def test__stochastic_likelihood_resamples_tag(self):

        settings = al.SettingsLens(stochastic_likelihood_resamples=None)
        assert settings.stochastic_likelihood_resamples_tag == ""
        settings = al.SettingsLens(stochastic_likelihood_resamples=2)
        assert settings.stochastic_likelihood_resamples_tag == "__lh_resamples_2"
        settings = al.SettingsLens(stochastic_likelihood_resamples=3)
        assert settings.stochastic_likelihood_resamples_tag == "__lh_resamples_3"
示例#3
0
    def test__positions_are_input__are_used_in_analysis(
            self, image_7x7, noise_map_7x7, mask_7x7):
        # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset.

        imaging_7x7 = al.Imaging(
            image=image_7x7,
            noise_map=noise_map_7x7,
            positions=al.GridCoordinates([[(1.0, 1.0), (2.0, 2.0)]]),
        )

        phase_imaging_7x7 = al.PhaseImaging(
            phase_name="test_phase",
            search=mock.MockSearch(),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                positions_threshold=0.2)),
        )

        phase_imaging_7x7.modify_dataset(dataset=imaging_7x7,
                                         results=mock.MockResults())
        phase_imaging_7x7.modify_settings(dataset=imaging_7x7,
                                          results=mock.MockResults())

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())

        assert (analysis.masked_dataset.positions.in_list[0][0] == np.array(
            [1.0, 1.0])).all()
        assert (analysis.masked_dataset.positions.in_list[0][1] == np.array(
            [2.0, 2.0])).all()
        assert analysis.settings.settings_lens.positions_threshold == 0.2

        # If position threshold is input (not None) and but no positions are supplied, raise an error

        with pytest.raises(exc.PhaseException):

            imaging_7x7 = al.Imaging(image=image_7x7,
                                     noise_map=noise_map_7x7,
                                     positions=None)

            phase_imaging_7x7 = al.PhaseImaging(
                phase_name="test_phase",
                search=mock.MockSearch(),
                settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                    positions_threshold=0.2)),
            )

            phase_imaging_7x7.modify_dataset(dataset=imaging_7x7,
                                             results=mock.MockResults())
            phase_imaging_7x7.modify_settings(dataset=imaging_7x7,
                                              results=mock.MockResults())
示例#4
0
    def test__results_include_positions__available_as_property(
            self, analysis_imaging_7x7, masked_imaging_7x7,
            samples_with_result):

        result = res.ResultDataset(
            samples=samples_with_result,
            analysis=analysis_imaging_7x7,
            model=None,
            search=None,
        )

        assert result.positions == None

        analysis = al.AnalysisImaging(
            dataset=masked_imaging_7x7,
            positions=al.Grid2DIrregular([[(1.0, 1.0)]]),
            settings_lens=al.SettingsLens(positions_threshold=1.0),
        )

        result = res.ResultDataset(samples=samples_with_result,
                                   analysis=analysis,
                                   model=None,
                                   search=None)

        assert (result.positions[0] == np.array([1.0, 1.0])).all()
    def test__positions_do_not_trace_within_threshold__raises_exception(
            self, phase_interferometer_7, interferometer_7, mask_7x7,
            visibilities_mask_7):
        interferometer_7.positions = al.GridIrregularGrouped([[(1.0, 100.0),
                                                               (200.0, 2.0)]])

        phase_interferometer_7 = al.PhaseInterferometer(
            real_space_mask=mask_7x7,
            galaxies=dict(
                lens=al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal()),
                source=al.Galaxy(redshift=1.0),
            ),
            settings=al.SettingsPhaseInterferometer(
                settings_lens=al.SettingsLens(positions_threshold=0.01)),
            search=mock.MockSearch("test_phase"),
        )

        analysis = phase_interferometer_7.make_analysis(
            dataset=interferometer_7,
            mask=visibilities_mask_7,
            results=mock.MockResults(),
        )
        instance = phase_interferometer_7.model.instance_from_unit_vector([])

        with pytest.raises(exc.RayTracingException):
            analysis.log_likelihood_function(instance=instance)
示例#6
0
    def test__tag(self):

        settings = al.SettingsLens(
            positions_threshold=1.0,
            auto_positions_factor=2.56,
            auto_positions_minimum_threshold=0.5,
        )
        assert settings.tag == "lens[pos_on]"

        settings = al.SettingsLens(
            positions_threshold=1.0,
            auto_positions_factor=2.56,
            auto_positions_minimum_threshold=0.5,
            stochastic_likelihood_resamples=2,
        )
        assert settings.tag == "lens[pos_on__lh_resamples_2]"
示例#7
0
    def test__results_of_phase_include_positions__available_as_property(
            self, imaging_7x7, mask_7x7, samples_with_result):

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase", samples=samples_with_result))

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

        assert result.positions == None

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(lens=al.Galaxy(redshift=0.5),
                          source=al.Galaxy(redshift=1.0)),
            search=mock.MockSearch("test_phase", samples=samples_with_result),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                positions_threshold=1.0)),
        )

        imaging_7x7.positions = al.GridIrregularGrouped([[(1.0, 1.0)]])

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

        assert (result.positions[0] == np.array([1.0, 1.0])).all()
示例#8
0
    def test__positions_are_input__are_used_in_analysis(
            self, image_7x7, noise_map_7x7, mask_7x7):
        # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset.

        imaging_7x7 = al.Imaging(
            image=image_7x7,
            noise_map=noise_map_7x7,
            positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]]),
        )

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase"),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                positions_threshold=0.2)),
        )

        phase_imaging_7x7.modify_dataset(dataset=imaging_7x7,
                                         results=mock.MockResults())
        phase_imaging_7x7.modify_settings(dataset=imaging_7x7,
                                          results=mock.MockResults())

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())

        assert (analysis.masked_dataset.positions.in_grouped_list[0][0] ==
                np.array([1.0, 1.0])).all()
        assert (analysis.masked_dataset.positions.in_grouped_list[0][1] ==
                np.array([2.0, 2.0])).all()
        assert analysis.settings.settings_lens.positions_threshold == 0.2
示例#9
0
    def test__auto_positions_tag(self):

        settings = al.SettingsLens(auto_positions_factor=None,
                                   auto_positions_minimum_threshold=None)
        assert settings.auto_positions_factor_tag == ""
        settings = al.SettingsLens(auto_positions_factor=1.0,
                                   auto_positions_minimum_threshold=None)
        assert settings.auto_positions_factor_tag == "__auto_pos_x1.00"
        settings = al.SettingsLens(auto_positions_factor=2.56,
                                   auto_positions_minimum_threshold=None)
        assert settings.auto_positions_factor_tag == "__auto_pos_x2.56"
        settings = al.SettingsLens(auto_positions_factor=None,
                                   auto_positions_minimum_threshold=0.5)
        assert settings.auto_positions_factor_tag == ""
        settings = al.SettingsLens(auto_positions_factor=2.56,
                                   auto_positions_minimum_threshold=0.5)
        assert settings.auto_positions_factor_tag == "__auto_pos_x2.56_min_0.5"
    def test__stochastic_log_likelihoods_for_instance(self, interferometer_7):

        lens_hyper_image = al.Array2D.ones(shape_native=(3, 3),
                                           pixel_scales=0.1)
        lens_hyper_image[4] = 10.0
        source_hyper_image = al.Array2D.ones(shape_native=(3, 3),
                                             pixel_scales=0.1)
        source_hyper_image[4] = 10.0
        hyper_model_image = al.Array2D.full(fill_value=0.5,
                                            shape_native=(3, 3),
                                            pixel_scales=0.1)

        hyper_galaxy_image_path_dict = {
            ("galaxies", "lens"): lens_hyper_image,
            ("galaxies", "source"): source_hyper_image,
        }

        result = mock.MockResult(
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=hyper_model_image,
        )

        analysis = al.AnalysisInterferometer(
            dataset=interferometer_7,
            settings_lens=al.SettingsLens(stochastic_samples=2),
            hyper_dataset_result=result,
        )

        galaxies = af.ModelInstance()
        galaxies.source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.VoronoiBrightnessImage(pixels=5),
            regularization=al.reg.Constant(),
        )

        instance = af.ModelInstance()
        instance.galaxies = galaxies

        log_evidences = analysis.stochastic_log_likelihoods_for_instance(
            instance=instance)

        assert len(log_evidences) == 2
        assert log_evidences[0] != log_evidences[1]

        galaxies.source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.DelaunayBrightnessImage(pixels=5),
            regularization=al.reg.Constant(),
        )

        instance = af.ModelInstance()
        instance.galaxies = galaxies

        log_evidences = analysis.stochastic_log_likelihoods_for_instance(
            instance=instance)

        assert len(log_evidences) == 2
        assert log_evidences[0] != log_evidences[1]
示例#11
0
    def test__auto_einstein_radius_is_used__einstein_radius_used_in_analysis(
            self, imaging_7x7, mask_7x7):
        # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset.

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase"),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                auto_einstein_radius_factor=None)),
        )
        tracer = mock.MockTracer(einstein_radius=2.0)

        phase_imaging_7x7.modify_settings(
            dataset=imaging_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())

        assert analysis.settings.settings_lens.einstein_radius_estimate == None

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase"),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                auto_einstein_radius_factor=1.0)),
        )

        tracer = mock.MockTracer(einstein_radius=2.0)

        phase_imaging_7x7.modify_settings(
            dataset=imaging_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        analysis = phase_imaging_7x7.make_analysis(
            dataset=imaging_7x7,
            mask=mask_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        assert analysis.settings.settings_lens.einstein_radius_estimate == 2.0
    def test__analysis_no_positions__removes_positions_and_threshold(
            self, masked_imaging_7x7):

        analysis = al.AnalysisImaging(
            dataset=masked_imaging_7x7,
            positions=al.Grid2DIrregular([(1.0, 100.0), (200.0, 2.0)]),
            settings_lens=al.SettingsLens(positions_threshold=0.01),
        )

        assert analysis.no_positions.positions == None
        assert analysis.no_positions.settings_lens.positions_threshold == None
示例#13
0
    def test__positions_do_not_trace_within_threshold__raises_exception(self,):

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal()),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings = al.SettingsLens(positions_threshold=50.0)
        settings.check_positions_trace_within_threshold_via_tracer(
            tracer=tracer, positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]])
        )

        settings = al.SettingsLens(positions_threshold=0.0)
        with pytest.raises(exc.RayTracingException):
            settings.check_positions_trace_within_threshold_via_tracer(
                tracer=tracer,
                positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]]),
            )

        # No mass profile - doesnt raise exception

        tracer = al.Tracer.from_galaxies(
            galaxies=[al.Galaxy(redshift=0.5), al.Galaxy(redshift=1.0)]
        )

        settings.check_positions_trace_within_threshold_via_tracer(
            tracer=tracer, positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]])
        )

        # Single plane - doesnt raise exception

        tracer = al.Tracer.from_galaxies(
            galaxies=[al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal())]
        )

        settings.check_positions_trace_within_threshold_via_tracer(
            tracer=tracer, positions=al.GridIrregularGrouped([[(1.0, 1.0), (2.0, 2.0)]])
        )
    def test__stochastic_histogram_for_instance(self, masked_interferometer_7):

        galaxies = af.ModelInstance()
        galaxies.lens = al.Galaxy(
            redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.2)
        )
        galaxies.source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.VoronoiBrightnessImage(pixels=5),
            regularization=al.reg.Constant(),
        )

        instance = af.ModelInstance()
        instance.galaxies = galaxies

        lens_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1)
        lens_hyper_image[4] = 10.0
        source_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1)
        source_hyper_image[4] = 10.0
        hyper_model_image = al.Array2D.full(
            fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1
        )

        hyper_galaxy_image_path_dict = {
            ("galaxies", "lens"): lens_hyper_image,
            ("galaxies", "source"): source_hyper_image,
        }

        results = mock.MockResults(
            use_as_hyper_dataset=True,
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=hyper_model_image,
        )

        analysis = al.PhaseInterferometer.Analysis(
            masked_interferometer=masked_interferometer_7,
            settings=al.SettingsPhaseImaging(
                settings_lens=al.SettingsLens(stochastic_samples=2)
            ),
            results=results,
            cosmology=cosmo.Planck15,
        )

        log_evidences = analysis.stochastic_log_evidences_for_instance(
            instance=instance
        )

        assert len(log_evidences) == 2
        assert log_evidences[0] != log_evidences[1]
示例#15
0
    def test__figure_of_merit__with_stochastic_likelihood_resamples_matches_galaxy_profiles(
            self, masked_imaging_7x7):

        galaxies = af.ModelInstance()
        galaxies.lens = al.Galaxy(
            redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.2))
        galaxies.source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.VoronoiBrightnessImage(pixels=5),
            regularization=al.reg.Constant(),
        )

        instance = af.ModelInstance()
        instance.galaxies = galaxies

        lens_hyper_image = al.Array2D.ones(shape_native=(3, 3),
                                           pixel_scales=0.1)
        lens_hyper_image[4] = 10.0
        source_hyper_image = al.Array2D.ones(shape_native=(3, 3),
                                             pixel_scales=0.1)
        source_hyper_image[4] = 10.0
        hyper_model_image = al.Array2D.full(fill_value=0.5,
                                            shape_native=(3, 3),
                                            pixel_scales=0.1)

        hyper_galaxy_image_path_dict = {
            ("galaxies", "lens"): lens_hyper_image,
            ("galaxies", "source"): source_hyper_image,
        }

        results = mock.MockResults(
            use_as_hyper_dataset=True,
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=hyper_model_image,
        )

        analysis = al.PhaseImaging.Analysis(
            masked_imaging=masked_imaging_7x7,
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                stochastic_likelihood_resamples=2)),
            results=results,
            cosmology=cosmo.Planck15,
        )

        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)
    def test__positions_do_not_trace_within_threshold__raises_exception(
            self, masked_imaging_7x7):

        model = af.Collection(galaxies=af.Collection(
            lens=al.Galaxy(redshift=0.5, mass=al.mp.SphIsothermal()),
            source=al.Galaxy(redshift=1.0),
        ))

        analysis = al.AnalysisImaging(
            dataset=masked_imaging_7x7,
            positions=al.Grid2DIrregular([(1.0, 100.0), (200.0, 2.0)]),
            settings_lens=al.SettingsLens(positions_threshold=0.01),
        )

        instance = model.instance_from_unit_vector([])

        with pytest.raises(exc.RayTracingException):
            analysis.log_likelihood_function(instance=instance)
示例#17
0
def test__positions__resample__raises_exception(interferometer_7, mask_2d_7x7):

    model = af.Collection(galaxies=af.Collection(
        lens=al.Galaxy(redshift=0.5, mass=al.mp.SphIsothermal()),
        source=al.Galaxy(redshift=1.0),
    ))

    positions_likelihood = al.PositionsLHResample(positions=al.Grid2DIrregular(
        [(1.0, 100.0), (200.0, 2.0)]),
                                                  threshold=0.01)

    analysis = al.AnalysisInterferometer(
        dataset=interferometer_7,
        positions_likelihood=positions_likelihood,
        settings_lens=al.SettingsLens(threshold=0.01),
    )

    instance = model.instance_from_unit_vector([])

    with pytest.raises(exc.RayTracingException):
        analysis.log_likelihood_function(instance=instance)
示例#18
0
    def test__positions_do_not_trace_within_threshold__raises_exception(
            self, phase_imaging_7x7, imaging_7x7, mask_7x7):

        imaging_7x7.positions = al.Grid2DIrregular([(1.0, 100.0),
                                                    (200.0, 2.0)])

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(
                lens=al.Galaxy(redshift=0.5, mass=al.mp.SphericalIsothermal()),
                source=al.Galaxy(redshift=1.0),
            ),
            settings=al.SettingsPhaseImaging(settings_lens=al.SettingsLens(
                positions_threshold=0.01)),
            search=mock.MockSearch(),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())
        instance = phase_imaging_7x7.model.instance_from_unit_vector([])

        with pytest.raises(exc.RayTracingException):
            analysis.log_likelihood_function(instance=instance)
示例#19
0
def test__results_include_positions__available_as_property(
        analysis_imaging_7x7, masked_imaging_7x7, samples_with_result):

    result = res.ResultDataset(samples=samples_with_result,
                               analysis=analysis_imaging_7x7,
                               model=None)

    assert result.positions == None

    positions_likelihood = al.PositionsLHResample(positions=al.Grid2DIrregular(
        [(1.0, 100.0), (200.0, 2.0)]),
                                                  threshold=1.0)

    analysis = al.AnalysisImaging(
        dataset=masked_imaging_7x7,
        positions_likelihood=positions_likelihood,
        settings_lens=al.SettingsLens(threshold=1.0),
    )

    result = res.ResultDataset(samples=samples_with_result,
                               analysis=analysis,
                               model=None)

    assert (result.positions[0] == np.array([1.0, 100.0])).all()
that reconstructs the source galaxy's light. It begins by fitting a `VoronoiMagnification` pixelization with `Constant` 
regularization, to set up the model and hyper images, and then:

 - Uses a `VoronoiBrightnessImage` pixelization.
 - Uses an `AdaptiveBrightness` regularization.
 - Carries the lens redshift, source redshift and `ExternalShear` of the SOURCE PARAMETRIC PIPELINE through to the
 SOURCE INVERSION PIPELINE.

__Settings__:

 - Positions: We update the positions and positions threshold using the previous model-fitting result (as described 
 in `chaining/examples/parametric_to_inversion.py`) to remove unphysical solutions from the `Inversion` model-fitting.
"""
settings_lens = al.SettingsLens(
    positions_threshold=source_parametric_results.last.positions_threshold_from(
        factor=3.0, minimum_threshold=0.2
    )
)

analysis = al.AnalysisImaging(
    dataset=imaging,
    positions=source_parametric_results.last.image_plane_multiple_image_positions,
    settings_lens=settings_lens,
)

source_inversion_results = slam.source_inversion.with_lens_light(
    path_prefix=path_prefix,
    unique_tag=dataset_name,
    analysis=analysis,
    setup_hyper=setup_hyper,
    source_parametric_results=source_parametric_results,
        try:
            imaging.psf = al.preprocess.array_with_new_shape(array=imaging.psf,
                                                             new_shape=(21,
                                                                        21))
            yay = True
        except OSError:
            yay = False

if not yay:
    raise IOError("Nope")

settings_masked_imaging = al.SettingsMaskedImaging(grid_class=al.Grid)

settings_lens = al.SettingsLens(
    positions_threshold=0.7,
    auto_positions_factor=3.0,
    auto_positions_minimum_threshold=0.2,
)

settings = al.SettingsPhaseImaging(
    settings_masked_imaging=settings_masked_imaging,
    settings_lens=settings_lens)

# %%
"""
__PIPELINE SETUP__

Transdimensional pipelines used the _SetupPipeline_ object to customize the analysis performed by the pipeline,
for example if a shear was included in the mass model and the model used for the source galaxy.

SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong 
                                                   sub_size=2)
"""
`Inversion`'s may infer unphysical solution where the source reconstruction is a demagnified reconstruction of the 
lensed source (see **HowToLens** chapter 4). 

To prevent this, auto-positioning is used, which uses the lens mass model of earlier phases to automatically set 
positions and a threshold that resample inaccurate mass models (see `examples/model/positions.py`).

The `auto_positions_factor` is a factor that the threshold of the inferred positions using the previous mass model are 
multiplied by to set the threshold in the next phase. The *auto_positions_minimum_threshold* is the minimum value this
threshold can go to, even after multiplication.
"""

settings = al.SettingsPhaseImaging(
    settings_masked_imaging=settings_masked_imaging,
    settings_lens=al.SettingsLens(stochastic_samples=1),
)
"""
__PIPELINE SETUP__

Transdimensional pipelines used the `SetupPipeline` object to customize the analysis performed by the pipeline,
for example if a shear was included in the mass model and the model used for the source galaxy.

SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong 
lens, first the Source, then the (lens) Light and finally the Mass. Each of these pipelines has it own setup object 
which is equivalent to the `SetupPipeline` object, customizing the analysis in that pipeline. Each pipeline therefore
has its own `SetupMass`, `SetupLightParametric` and `SetupSourceParametric` object.

The `Setup` used in earlier pipelines determine the model used in later pipelines. For example, if the `Source` 
pipeline is given a `Pixelization` and `Regularization`, than this `Inversion` will be used in the subsequent 
_SLaMPipelineLight_ and Mass pipelines. The assumptions regarding the lens light chosen by the `Light` object are 
    def test__auto_einstein_radius_is_used__einstein_radius_used_in_analysis(
        self, imaging_7x7, mask_7x7
    ):
        # If position threshold is input (not None) and positions are input, make the positions part of the lens dataset.

        class MockTracer:
            def __init__(self, einstein_radius_via_tangential_critical_curve):

                self.einstein_radius_via_tangential_critical_curve = (
                    einstein_radius_via_tangential_critical_curve
                )

            def einstein_radius_from_grid(self, grid):
                return self.einstein_radius_via_tangential_critical_curve

            @property
            def has_mass_profile(self):
                return True

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase"),
            settings=al.SettingsPhaseImaging(
                settings_lens=al.SettingsLens(auto_einstein_radius_factor=None)
            ),
        )
        tracer = MockTracer(einstein_radius_via_tangential_critical_curve=2.0)

        phase_imaging_7x7.modify_settings(
            dataset=imaging_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        analysis = phase_imaging_7x7.make_analysis(
            dataset=imaging_7x7, mask=mask_7x7, results=mock.MockResults()
        )

        assert analysis.settings.settings_lens.einstein_radius_estimate == None

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase"),
            settings=al.SettingsPhaseImaging(
                settings_lens=al.SettingsLens(auto_einstein_radius_factor=1.0)
            ),
        )

        tracer = MockTracer(einstein_radius_via_tangential_critical_curve=2.0)

        phase_imaging_7x7.modify_settings(
            dataset=imaging_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        analysis = phase_imaging_7x7.make_analysis(
            dataset=imaging_7x7,
            mask=mask_7x7,
            results=mock.MockResults(max_log_likelihood_tracer=tracer),
        )

        print(analysis.settings.settings_lens.einstein_radius_estimate)

        assert analysis.settings.settings_lens.einstein_radius_estimate == 2.0
The SOURCE INVERSION PIPELINE (with lens light) uses four searches to initialize a robust model for the `Inversion` 
that reconstructs the source galaxy's light. It begins by fitting a `DelaunayMagnification` pixelization with `Constant` 
regularization, to set up the model and hyper images, and then:

 - Uses a `DelaunayBrightnessImage` pixelization.
 - Uses an `AdaptiveBrightness` regularization.
 - Carries the lens redshift, source redshift and `ExternalShear` of the SOURCE PARAMETRIC PIPELINE through to the
 SOURCE INVERSION PIPELINE.

__Settings__:

 - Positions: We update the positions and positions threshold using the previous model-fitting result (as described 
 in `chaining/examples/parametric_to_inversion.py`) to remove unphysical solutions from the `Inversion` model-fitting.
"""
settings_lens = al.SettingsLens(positions_threshold=0.2)

analysis = al.AnalysisInterferometer(
    dataset=interferometer,
    positions=source_parametric_results.last.
    image_plane_multiple_image_positions,
    settings_lens=settings_lens,
    settings_inversion=settings_inversion,
)

source_inversion_results = slam.source_inversion.with_lens_light(
    settings_autofit=settings_autofit,
    analysis=analysis,
    setup_hyper=setup_hyper,
    source_parametric_results=source_parametric_results,
    pixelization=al.pix.DelaunayBrightnessImage,
示例#25
0
def test__tag__mixture_of_values():

    settings = al.SettingsPhaseImaging(
        settings_masked_imaging=al.SettingsMaskedImaging(
            grid_class=al.Grid,
            grid_inversion_class=al.Grid,
            sub_size=2,
            signal_to_noise_limit=2,
            bin_up_factor=None,
            psf_shape_2d=None,
        ),
        settings_lens=al.SettingsLens(positions_threshold=2.0),
    )

    assert settings.phase_tag_no_inversion == "settings__grid_sub_2__snr_2__pos_2.00"
    assert (
        settings.phase_tag_with_inversion
        == "settings__grid_sub_2_inv_sub_2__snr_2__pos_2.00"
    )

    settings = al.SettingsPhaseImaging(
        settings_masked_imaging=al.SettingsMaskedImaging(
            grid_class=al.GridIterate,
            grid_inversion_class=al.GridInterpolate,
            fractional_accuracy=0.5,
            pixel_scales_interp=0.3,
            bin_up_factor=3,
            psf_shape_2d=(2, 2),
        ),
        settings_lens=al.SettingsLens(auto_positions_factor=0.5),
        settings_pixelization=al.SettingsPixelization(
            use_border=False, is_stochastic=True
        ),
        log_likelihood_cap=200.01,
    )

    assert (
        settings.phase_tag_no_inversion
        == "settings__grid_facc_0.5__bin_3__psf_2x2__auto_pos_x0.50__lh_cap_200.0"
    )
    assert (
        settings.phase_tag_with_inversion
        == "settings__grid_facc_0.5_inv_interp_0.300__bin_3__psf_2x2__auto_pos_x0.50__no_border__stochastic__lh_cap_200.0"
    )

    settings = al.SettingsPhaseInterferometer(
        masked_interferometer=al.SettingsMaskedInterferometer(
            grid_class=al.GridIterate,
            grid_inversion_class=al.GridInterpolate,
            fractional_accuracy=0.5,
            pixel_scales_interp=0.3,
            transformer_class=al.TransformerDFT,
        ),
        settings_pixelization=al.SettingsPixelization(
            use_border=False, is_stochastic=True
        ),
        log_likelihood_cap=100.01,
    )

    assert (
        settings.phase_tag_no_inversion == "settings__grid_facc_0.5__dft__lh_cap_100.0"
    )
    assert (
        settings.phase_tag_with_inversion
        == "settings__grid_facc_0.5_inv_interp_0.300__dft__no_border__stochastic__lh_cap_100.0"
    )

    settings = al.SettingsPhaseInterferometer(
        masked_interferometer=al.SettingsMaskedInterferometer(
            transformer_class=al.TransformerNUFFT
        ),
        settings_inversion=al.SettingsInversion(use_linear_operators=True),
    )

    assert settings.phase_tag_no_inversion == "settings__grid_sub_2__nufft"
    assert (
        settings.phase_tag_with_inversion
        == "settings__grid_sub_2_inv_sub_2__nufft__lop"
    )
The SOURCE INVERSION PIPELINE (no lens light) uses four searches to initialize a robust model for the `Inversion` that
reconstructs the source galaxy's light. It begins by fitting a `VoronoiMagnification` pixelization with `Constant` 
regularization, to set up the model and hyper images, and then:

 - Uses a `VoronoiBrightnessImage` pixelization.
 - Uses an `AdaptiveBrightness` regularization.
 - Carries the lens redshift, source redshift and `ExternalShear` of the SOURCE PARAMETRIC PIPELINE through to the
 SOURCE INVERSION PIPELINE.

__Settings__:

 - Positions: We update the positions and positions threshold using the previous model-fitting result (as described 
 in `chaining/examples/parametric_to_inversion.py`) to remove unphysical solutions from the `Inversion` model-fitting.
"""
settings_lens = al.SettingsLens(positions_threshold=0.2)

analysis = al.AnalysisInterferometer(
    dataset=interferometer,
    positions=source_parametric_results.last.
    image_plane_multiple_image_positions,
    settings_lens=settings_lens,
)

source_inversion_results = slam.source_inversion.no_lens_light(
    path_prefix=path_prefix,
    unique_tag=dataset_name,
    analysis=analysis,
    setup_hyper=setup_hyper,
    source_parametric_results=source_parametric_results,
    pixelization=al.pix.VoronoiBrightnessImage,
示例#27
0
    def test__positions_threshold_tag(self):

        settings = al.SettingsLens(positions_threshold=None)
        assert settings.positions_threshold_tag == "pos_off"
        settings = al.SettingsLens(positions_threshold=1.0)
        assert settings.positions_threshold_tag == "pos_on"
示例#28
0
mass models where the source's brightest lensed pixels do not trace close to one another. With search chaining, we can
in fact use the model-fit of a previous search (in this example, search 1) to compute the positions that we use in a 
later search.

Below, we use the results of the first search to compute the lensed source positions that are input into search 2. The
code below uses the  maximum log likelihood model mass model and source galaxy centre, to determine where the source
positions are located in the image-plane. 

We also use this result to set the `position_threshold`, whereby the threshold value is based on how close these 
positions trace to one another in the source-plane (using the best-fit mass model again). This threshold is multiplied 
by a `factor` to ensure it is not too small (and thus does not remove plausible mass  models). If, after this 
multiplication, the threshold is below the `minimum_threshold`, it is rounded up to this minimum value.
"""
settings_lens = al.SettingsLens(
    positions_threshold=result_2.positions_threshold_from(
        factor=3.0, minimum_threshold=0.2
    )
)

analysis = al.AnalysisImaging(
    dataset=imaging,
    positions=result_2.image_plane_multiple_image_positions,
    settings_lens=settings_lens,
)

result_3 = search.fit(model=model, analysis=analysis)

"""
__Wrap Up__

And with that, we now have a pipeline to model strong lenses using an inversion! 
示例#29
0
                                                   sub_size=2)
"""
`Inversion`'s may infer unphysical solution where the source reconstruction is a demagnified reconstruction of the 
lensed source (see **HowToLens** chapter 4). 

To prevent this, auto-positioning is used, which uses the lens mass model of earlier phases to automatically set 
positions and a threshold that resample inaccurate mass models (see `examples/model/positions.py`).

The `auto_positions_factor` is a factor that the threshold of the inferred positions using the previous mass model are 
multiplied by to set the threshold in the next phase. The *auto_positions_minimum_threshold* is the minimum value this
threshold can go to, even after multiplication.
"""

settings = al.SettingsPhaseImaging(
    settings_masked_imaging=settings_masked_imaging,
    settings_lens=al.SettingsLens(auto_einstein_radius_factor=0.2,
                                  stochastic_samples=1),
)
"""
__PIPELINE SETUP__

Transdimensional pipelines used the `SetupPipeline` object to customize the analysis performed by the pipeline,
for example if a shear was included in the mass model and the model used for the source galaxy.

SLaM pipelines break the analysis down into multiple pipelines which focus on modeling a specific aspect of the strong 
lens, first the Source, then the (lens) Light and finally the Mass. Each of these pipelines has it own setup object 
which is equivalent to the `SetupPipeline` object, customizing the analysis in that pipeline. Each pipeline therefore
has its own `SetupMass`, `SetupLightParametric` and `SetupSourceParametric` object.

The `Setup` used in earlier pipelines determine the model used in later pipelines. For example, if the `Source` 
pipeline is given a `Pixelization` and `Regularization`, than this `Inversion` will be used in the subsequent 
_SLaMPipelineLight_ and Mass pipelines. The assumptions regarding the lens light chosen by the `Light` object are 
示例#30
0
    def test__einstein_radius_outside_auto_range__raises_exception(self):

        grid = al.Grid.uniform(shape_2d=(40, 40), pixel_scales=0.2)

        settings = al.SettingsLens(auto_einstein_radius_factor=0.2)
        settings = settings.modify_einstein_radius_estimate(
            einstein_radius_estimate=1.0
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.0)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings.check_einstein_radius_with_threshold_via_tracer(
            tracer=tracer, grid=grid
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=0.81)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings.check_einstein_radius_with_threshold_via_tracer(
            tracer=tracer, grid=grid
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.19)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings.check_einstein_radius_with_threshold_via_tracer(
            tracer=tracer, grid=grid
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.21)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        with pytest.raises(exc.RayTracingException):
            settings.check_einstein_radius_with_threshold_via_tracer(
                tracer=tracer, grid=grid
            )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=0.79)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        with pytest.raises(exc.RayTracingException):
            settings.check_einstein_radius_with_threshold_via_tracer(
                tracer=tracer, grid=grid
            )

        settings = al.SettingsLens(auto_einstein_radius_factor=0.5)
        settings = settings.modify_einstein_radius_estimate(
            einstein_radius_estimate=2.0
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=1.01)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings.check_einstein_radius_with_threshold_via_tracer(
            tracer=tracer, grid=grid
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=2.99)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        settings.check_einstein_radius_with_threshold_via_tracer(
            tracer=tracer, grid=grid
        )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=0.99)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        with pytest.raises(exc.RayTracingException):
            settings.check_einstein_radius_with_threshold_via_tracer(
                tracer=tracer, grid=grid
            )

        tracer = al.Tracer.from_galaxies(
            galaxies=[
                al.Galaxy(
                    redshift=0.5, mass=al.mp.SphericalIsothermal(einstein_radius=3.02)
                ),
                al.Galaxy(redshift=1.0),
            ]
        )

        with pytest.raises(exc.RayTracingException):
            settings.check_einstein_radius_with_threshold_via_tracer(
                tracer=tracer, grid=grid
            )