Ejemplo n.º 1
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()
Ejemplo n.º 2
0
    def test__grid_classes_input__used_in_masked_imaging(
            self, imaging_7x7, mask_7x7):

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch(),
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    grid_inversion_class=al.Grid2D)),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())
        assert isinstance(analysis.masked_imaging.grid, al.Grid2D)
        assert isinstance(analysis.masked_imaging.grid_inversion, al.Grid2D)

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch(),
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    grid_class=al.Grid2DIterate,
                    grid_inversion_class=al.Grid2DIterate,
                    fractional_accuracy=0.2,
                    sub_steps=[2, 3],
                )),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())
        assert isinstance(analysis.masked_imaging.grid, al.Grid2DIterate)
        assert analysis.masked_imaging.grid.fractional_accuracy == 0.2
        assert analysis.masked_imaging.grid.sub_steps == [2, 3]
        assert isinstance(analysis.masked_imaging.grid_inversion,
                          al.Grid2DIterate)
        assert analysis.masked_imaging.grid_inversion.fractional_accuracy == 0.2
        assert analysis.masked_imaging.grid_inversion.sub_steps == [2, 3]

        phase_imaging_7x7 = al.PhaseImaging(
            search=mock.MockSearch(),
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    grid_class=al.Grid2DInterpolate,
                    grid_inversion_class=al.Grid2DInterpolate,
                    pixel_scales_interp=0.1,
                )),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7,
                                                   results=mock.MockResults())
        assert isinstance(analysis.masked_imaging.grid, al.Grid2DInterpolate)
        assert analysis.masked_imaging.grid.pixel_scales_interp == (0.1, 0.1)
        assert isinstance(analysis.masked_imaging.grid_inversion,
                          al.Grid2DInterpolate)
        assert analysis.masked_imaging.grid_inversion.pixel_scales_interp == (
            0.1, 0.1)
Ejemplo n.º 3
0
    def test__masked_imaging__settings_inputs_are_used_in_masked_imaging(
            self, imaging_7x7, mask_7x7):
        phase_imaging_7x7 = al.PhaseImaging(
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    grid_class=al.Grid,
                    grid_inversion_class=al.Grid,
                    sub_size=3,
                    signal_to_noise_limit=1.0,
                    bin_up_factor=2,
                    psf_shape_2d=(3, 3),
                ),
                settings_pixelization=al.SettingsPixelization(
                    use_border=False, is_stochastic=True),
            ),
            search=mock.MockSearch(),
        )

        assert phase_imaging_7x7.settings.settings_masked_imaging.sub_size == 3
        assert (phase_imaging_7x7.settings.settings_masked_imaging.
                signal_to_noise_limit == 1.0)
        assert phase_imaging_7x7.settings.settings_masked_imaging.bin_up_factor == 2
        assert phase_imaging_7x7.settings.settings_masked_imaging.psf_shape_2d == (
            3, 3)
        assert phase_imaging_7x7.settings.settings_pixelization.use_border == False
        assert phase_imaging_7x7.settings.settings_pixelization.is_stochastic == True

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

        assert isinstance(analysis.masked_dataset.grid, al.Grid)
        assert isinstance(analysis.masked_dataset.grid_inversion, al.Grid)

        phase_imaging_7x7 = al.PhaseImaging(
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    grid_class=al.GridIterate,
                    sub_size=3,
                    fractional_accuracy=0.99,
                    sub_steps=[2],
                )),
            search=mock.MockSearch(),
        )

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

        assert isinstance(analysis.masked_dataset.grid, al.GridIterate)
        assert analysis.masked_dataset.grid.sub_size == 1
        assert analysis.masked_dataset.grid.fractional_accuracy == 0.99
        assert analysis.masked_dataset.grid.sub_steps == [2]
Ejemplo n.º 4
0
    def test__results_of_phase_include_pixelization__available_as_property(
            self, imaging_7x7, mask_7x7):
        lens = al.Galaxy(redshift=0.5,
                         light=al.lp.EllipticalSersic(intensity=1.0))
        source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.VoronoiMagnification(shape=(2, 3)),
            regularization=al.reg.Constant(),
        )

        tracer = al.Tracer.from_galaxies(galaxies=[lens, source])

        samples = mock.MockSamples(max_log_likelihood_instance=tracer)

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

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

        assert isinstance(result.pixelization, al.pix.VoronoiMagnification)
        assert result.pixelization.shape == (2, 3)

        lens = al.Galaxy(redshift=0.5,
                         light=al.lp.EllipticalSersic(intensity=1.0))
        source = al.Galaxy(
            redshift=1.0,
            pixelization=al.pix.VoronoiBrightnessImage(pixels=6),
            regularization=al.reg.Constant(),
        )

        source.hyper_galaxy_image = np.ones(9)

        tracer = al.Tracer.from_galaxies(galaxies=[lens, source])

        samples = mock.MockSamples(max_log_likelihood_instance=tracer)

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

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

        assert isinstance(result.pixelization, al.pix.VoronoiBrightnessImage)
        assert result.pixelization.pixels == 6
Ejemplo n.º 5
0
    def test__max_log_likelihood_tracer_source_centres_correct(
        self, imaging_7x7, mask_7x7
    ):

        lens = al.Galaxy(redshift=0.5, light=al.lp.SphericalSersic(intensity=1.0))
        source = al.Galaxy(
            redshift=1.0,
            light=al.lp.SphericalSersic(centre=(9.0, 8.0), intensity=2.0),
            pixelization=al.pix.Rectangular((3, 3)),
            regularization=al.reg.Constant(coefficient=1.0),
        )

        tracer = al.Tracer.from_galaxies(galaxies=[lens, source])

        samples = mock.MockSamples(max_log_likelihood_instance=tracer)

        phase_dataset_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase_2", samples=samples)
        )

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

        assert result.source_plane_centre.in_list == [(0.0, 0.0)]
Ejemplo n.º 6
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
Ejemplo n.º 7
0
    def test__masked_imaging_is_binned_up(self, imaging_7x7, mask_7x7_1_pix):
        binned_up_imaging = imaging_7x7.binned_up_from(bin_up_factor=2)

        binned_up_mask = mask_7x7_1_pix.binned_mask_from_bin_up_factor(
            bin_up_factor=2)

        phase_imaging_7x7 = al.PhaseImaging(
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(
                    bin_up_factor=2)),
            search=mock.MockSearch(),
        )

        analysis = phase_imaging_7x7.make_analysis(dataset=imaging_7x7,
                                                   mask=mask_7x7_1_pix,
                                                   results=mock.MockResults())
        assert (analysis.masked_dataset.image.in_2d ==
                binned_up_imaging.image.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_dataset.psf == (1.0 / 9.0) *
                binned_up_imaging.psf).all()
        assert (analysis.masked_dataset.noise_map.in_2d ==
                binned_up_imaging.noise_map.in_2d *
                np.invert(binned_up_mask)).all()

        assert (analysis.masked_dataset.mask == binned_up_mask).all()
Ejemplo n.º 8
0
    def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, imaging_7x7, mask_7x7):
        lens_galaxy = al.Galaxy(redshift=0.5,
                                light=al.lp.EllipticalSersic(intensity=0.1))

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhaseImaging(
                settings_masked_imaging=al.SettingsMaskedImaging(sub_size=1)),
            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([])
        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        masked_imaging = al.MaskedImaging(
            imaging=imaging_7x7,
            mask=mask_7x7,
            settings=al.SettingsMaskedImaging(sub_size=1),
        )
        tracer = analysis.tracer_for_instance(instance=instance)

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

        assert fit.log_likelihood == fit_figure_of_merit
def test__fit_interferometer_generator_from_aggregator(
    interferometer_7, visibilities_mask_7, mask_7x7, samples
):

    phase_interferometer_7x7 = al.PhaseInterferometer(
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        search=mock.MockSearch("test_phase_aggregator", samples=samples),
        real_space_mask=mask_7x7,
    )

    phase_interferometer_7x7.run(
        dataset=interferometer_7,
        mask=visibilities_mask_7,
        results=mock.MockResults(samples=samples),
    )

    agg = af.Aggregator(directory=phase_interferometer_7x7.paths.output_path)

    fit_interferometer_gen = al.agg.FitInterferometer(aggregator=agg)

    for fit_interferometer in fit_interferometer_gen:
        assert (
            fit_interferometer.masked_interferometer.interferometer.visibilities
            == interferometer_7.visibilities
        ).all()
        assert (
            fit_interferometer.masked_interferometer.real_space_mask == mask_7x7
        ).all()
Ejemplo n.º 10
0
def test__masked_imaging_generator_from_aggregator(imaging_7x7, mask_7x7, samples):

    phase_imaging_7x7 = al.PhaseImaging(
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        settings=al.SettingsPhaseImaging(
            settings_masked_imaging=al.SettingsMaskedImaging(
                grid_class=al.GridIterate,
                grid_inversion_class=al.GridInterpolate,
                fractional_accuracy=0.5,
                sub_steps=[2],
                pixel_scales_interp=0.1,
            )
        ),
        search=mock.MockSearch("test_phase_aggregator", samples=samples),
    )

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

    agg = af.Aggregator(directory=phase_imaging_7x7.paths.output_path)

    masked_imaging_gen = al.agg.MaskedImaging(aggregator=agg)

    for masked_imaging in masked_imaging_gen:
        assert (masked_imaging.imaging.image == imaging_7x7.image).all()
        assert isinstance(masked_imaging.grid, al.GridIterate)
        assert isinstance(masked_imaging.grid_inversion, al.GridInterpolate)
        assert masked_imaging.grid.sub_steps == [2]
        assert masked_imaging.grid.fractional_accuracy == 0.5
        assert masked_imaging.grid_inversion.pixel_scales_interp == (0.1, 0.1)
    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)
Ejemplo n.º 12
0
    def test__fit_interferometer_all_above_weight_gen(self, interferometer_7,
                                                      samples, model):

        path_prefix = "aggregator_fit_interferometer_gen"

        database_file = path.join(conf.instance.output_path,
                                  "fit_interferometer.sqlite")
        result_path = path.join(conf.instance.output_path, path_prefix)

        clean(database_file=database_file, result_path=result_path)

        search = mock.MockSearch(samples=samples,
                                 result=mock.MockResult(model=model,
                                                        samples=samples))
        search.paths = af.DirectoryPaths(path_prefix=path_prefix)
        analysis = al.AnalysisInterferometer(dataset=interferometer_7)
        search.fit(model=model, analysis=analysis)

        agg = af.Aggregator.from_database(filename=database_file)
        agg.add_directory(directory=result_path)

        fit_interferometer_agg = al.agg.FitInterferometerAgg(aggregator=agg)
        fit_interferometer_pdf_gen = fit_interferometer_agg.all_above_weight_gen(
            minimum_weight=-1.0)

        i = 0

        for fit_interferometer_gen in fit_interferometer_pdf_gen:

            for fit_interferometer in fit_interferometer_gen:

                i += 1

                if i == 1:
                    assert fit_interferometer.tracer.galaxies[
                        0].redshift == 0.5
                    assert fit_interferometer.tracer.galaxies[
                        0].light.centre == (
                            1.0,
                            1.0,
                        )
                    assert fit_interferometer.tracer.galaxies[
                        1].redshift == 1.0

                if i == 2:
                    assert fit_interferometer.tracer.galaxies[
                        0].redshift == 0.5
                    assert fit_interferometer.tracer.galaxies[
                        0].light.centre == (
                            10.0,
                            10.0,
                        )
                    assert fit_interferometer.tracer.galaxies[
                        1].redshift == 1.0

        assert i == 2

        clean(database_file=database_file, result_path=result_path)
Ejemplo n.º 13
0
 def __init__(self):
     self.name = "name"
     self.paths = autofit.non_linear.paths.Paths(name=self.name,
                                                 path_prefix="phase_path",
                                                 tag="")
     self.search = mock.MockSearch(paths=self.paths)
     self.model = af.ModelMapper()
     self.settings = al.SettingsPhaseImaging(log_likelihood_cap=None)
     self.meta_dataset = MockMetaDataset(settings=self.settings)
Ejemplo n.º 14
0
    def test__tracer_all_above_weight_gen(self, masked_imaging_7x7, samples,
                                          model):

        path_prefix = "aggregator_tracer_gen"

        database_file = path.join(conf.instance.output_path, "tracer.sqlite")
        result_path = path.join(conf.instance.output_path, path_prefix)

        clean(database_file=database_file, result_path=result_path)

        search = mock.MockSearch(samples=samples,
                                 result=mock.MockResult(model=model,
                                                        samples=samples))
        search.paths = af.DirectoryPaths(path_prefix=path_prefix)
        analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)
        search.fit(model=model, analysis=analysis)

        agg = af.Aggregator.from_database(filename=database_file)
        agg.add_directory(directory=result_path)

        tracer_agg = al.agg.TracerAgg(aggregator=agg)
        tracer_pdf_gen = tracer_agg.all_above_weight_gen(minimum_weight=-1.0)
        weight_pdf_gen = tracer_agg.weights_above_gen(minimum_weight=-1.0)

        i = 0

        for (tracer_gen, weight_gen) in zip(tracer_pdf_gen, weight_pdf_gen):

            for tracer in tracer_gen:

                i += 1

                if i == 1:

                    assert tracer.galaxies[0].redshift == 0.5
                    assert tracer.galaxies[0].light.centre == (1.0, 1.0)
                    assert tracer.galaxies[1].redshift == 1.0

                if i == 2:

                    assert tracer.galaxies[0].redshift == 0.5
                    assert tracer.galaxies[0].light.centre == (10.0, 10.0)
                    assert tracer.galaxies[1].redshift == 1.0

            for weight in weight_gen:

                if i == 0:

                    assert weight == 0.0

                if i == 1:

                    assert weight == 1.0

        assert i == 2

        clean(database_file=database_file, result_path=result_path)
Ejemplo n.º 15
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
Ejemplo n.º 16
0
    def test__max_log_likelihood_tracer__multiple_image_positions_of_source_plane_centres_and_separations(
        self, imaging_7x7, mask_7x7
    ):

        lens = al.Galaxy(
            redshift=0.5,
            mass=al.mp.EllipticalIsothermal(
                centre=(0.001, 0.001),
                einstein_radius=1.0,
                elliptical_comps=(0.0, 0.111111),
            ),
        )

        source = al.Galaxy(
            redshift=1.0,
            light=al.lp.SphericalSersic(centre=(0.0, 0.0), intensity=2.0),
            light1=al.lp.SphericalSersic(centre=(0.0, 0.1), intensity=2.0),
            pixelization=al.pix.Rectangular((3, 3)),
            regularization=al.reg.Constant(coefficient=1.0),
        )

        tracer = al.Tracer.from_galaxies(galaxies=[lens, source])

        samples = mock.MockSamples(max_log_likelihood_instance=tracer)

        phase_dataset_7x7 = al.PhaseImaging(
            search=mock.MockSearch("test_phase_2", samples=samples)
        )

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

        mask = al.Mask2D.unmasked(
            shape_native=(100, 100), pixel_scales=0.05, sub_size=1
        )

        result.analysis.masked_dataset.mask = mask

        multiple_images = (
            result.image_plane_multiple_image_positions_of_source_plane_centres
        )

        grid = al.Grid2D.from_mask(mask=mask)

        solver = al.PositionsSolver(grid=grid, pixel_scale_precision=0.001)

        multiple_images_manual = solver.solve(
            lensing_obj=tracer,
            source_plane_coordinate=result.source_plane_inversion_centre[0],
        )

        assert multiple_images.in_list[0] == multiple_images_manual.in_list[0]
Ejemplo n.º 17
0
def make_stochastic():
    normal_phase = MockPhase()

    # noinspection PyUnusedLocal
    def run_hyper(*args, **kwargs):
        return mock.MockResult()

    return al.StochasticPhase(
        phase=normal_phase,
        hyper_search=mock.MockSearch(),
        model_classes=(al.mp.MassProfile, ),
    )
    def test__make_result__result_imaging_is_returned(self,
                                                      masked_imaging_7x7):

        model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(
            redshift=0.5)))

        search = mock.MockSearch(name="test_search")

        analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)

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

        assert isinstance(result, res.ResultImaging)
    def test__make_result__result_interferometer_is_returned(
            self, interferometer_7):

        model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(
            redshift=0.5)))

        search = mock.MockSearch(name="test_search")

        analysis = al.AnalysisInterferometer(dataset=interferometer_7)

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

        assert isinstance(result, res.ResultInterferometer)
    def test__max_log_likelihood_tracer_available_as_result(
            self, imaging_7x7, mask_7x7, samples_with_result):
        phase_dataset_7x7 = al.PhaseImaging(search=mock.MockSearch(
            "test_phase_2", samples=samples_with_result))

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

        assert isinstance(result.max_log_likelihood_tracer, al.Tracer)
        assert result.max_log_likelihood_tracer.galaxies[
            0].light.intensity == 1.0
        assert result.max_log_likelihood_tracer.galaxies[
            1].light.intensity == 2.0
Ejemplo n.º 21
0
    def test__make_result__result_imaging_is_returned(self, point_dict):

        model = af.Collection(galaxies=af.Collection(lens=al.Galaxy(
            redshift=0.5, point_0=al.ps.Point(centre=(0.0, 0.0)))))

        search = mock.MockSearch(name="test_search")

        solver = mock.MockPositionsSolver(
            model_positions=point_dict["point_0"].positions)

        analysis = al.AnalysisPoint(point_dict=point_dict, solver=solver)

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

        assert isinstance(result, res.ResultPoint)
Ejemplo n.º 22
0
    def test__max_log_likelihood_tracer_available_as_result(
            self, analysis_imaging_7x7, samples_with_result):

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

        search = mock.MockSearch(name="test_search_2",
                                 samples=samples_with_result)

        result = search.fit(model=model, analysis=analysis_imaging_7x7)

        assert isinstance(result.max_log_likelihood_tracer, al.Tracer)
        assert result.max_log_likelihood_tracer.galaxies[
            0].light.intensity == 1.0
        assert result.max_log_likelihood_tracer.galaxies[
            1].light.intensity == 2.0
Ejemplo n.º 23
0
    def test__results_of_phase_include_mask__available_as_property(
            self, imaging_7x7, mask_7x7, samples_with_result):

        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_masked_imaging=al.SettingsMaskedImaging(sub_size=2)),
        )

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

        assert (result.mask == mask_7x7).all()
Ejemplo n.º 24
0
    def test__positions__settings_inputs_are_used_in_positions(
            self, positions_x2, positions_x2_noise_map):
        phase_positions_x2 = al.PhasePointSource(
            settings=al.SettingsPhasePositions(),
            search=mock.MockSearch(),
            positions_solver=mock.MockPositionsSolver(
                model_positions=positions_x2),
        )

        assert isinstance(phase_positions_x2.settings,
                          al.SettingsPhasePositions)

        analysis = phase_positions_x2.make_analysis(
            positions=positions_x2, positions_noise_map=positions_x2_noise_map)

        assert analysis.positions.in_list == positions_x2.in_list
        assert analysis.noise_map.in_list == positions_x2_noise_map.in_list
Ejemplo n.º 25
0
    def test__phase_can_receive_hyper_image_and_noise_maps(self, mask_7x7):
        phase_interferometer_7 = al.PhaseInterferometer(
            galaxies=dict(
                lens=al.GalaxyModel(redshift=al.Redshift),
                lens1=al.GalaxyModel(redshift=al.Redshift),
            ),
            hyper_background_noise=al.hyper_data.HyperBackgroundNoise,
            search=mock.MockSearch("test_phase"),
            real_space_mask=mask_7x7,
        )

        instance = phase_interferometer_7.model.instance_from_vector(
            [0.1, 0.2, 0.3])

        assert instance.galaxies[0].redshift == 0.1
        assert instance.galaxies[1].redshift == 0.2
        assert instance.hyper_background_noise.noise_scale == 0.3
    def test__fit_figure_of_merit__includes_hyper_image_and_noise__matches_fit(
        self, interferometer_7, mask_7x7, visibilities_mask_7
    ):
        hyper_background_noise = al.hyper_data.HyperBackgroundNoise(noise_scale=1.0)

        lens_galaxy = al.Galaxy(
            redshift=0.5, light=al.lp.EllipticalSersic(intensity=0.1)
        )

        phase_interferometer_7 = al.PhaseInterferometer(
            galaxies=dict(lens=lens_galaxy),
            hyper_background_noise=hyper_background_noise,
            settings=al.SettingsPhaseInterferometer(
                settings_masked_interferometer=al.SettingsMaskedInterferometer(
                    sub_size=4
                )
            ),
            search=mock.MockSearch("test_phase"),
            real_space_mask=mask_7x7,
        )

        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([])
        fit_figure_of_merit = analysis.log_likelihood_function(instance=instance)

        assert analysis.masked_interferometer.real_space_mask.sub_size == 4

        masked_interferometer = al.MaskedInterferometer(
            interferometer=interferometer_7,
            visibilities_mask=visibilities_mask_7,
            real_space_mask=mask_7x7,
            settings=al.SettingsMaskedInterferometer(sub_size=4),
        )
        tracer = analysis.tracer_for_instance(instance=instance)
        fit = FitInterferometer(
            masked_interferometer=masked_interferometer,
            tracer=tracer,
            hyper_background_noise=hyper_background_noise,
        )

        assert fit.log_likelihood == fit_figure_of_merit
Ejemplo n.º 27
0
def test__masked_interferometer_generator_from_aggregator(
    interferometer_7, visibilities_mask_7, mask_7x7, samples
):

    phase_interferometer_7x7 = al.PhaseInterferometer(
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, light=al.lp.EllipticalSersic),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        settings=al.SettingsPhaseInterferometer(
            settings_masked_interferometer=al.SettingsMaskedInterferometer(
                transformer_class=al.TransformerDFT,
                grid_class=al.GridIterate,
                grid_inversion_class=al.GridInterpolate,
                fractional_accuracy=0.5,
                sub_steps=[2],
                pixel_scales_interp=0.1,
            )
        ),
        search=mock.MockSearch("test_phase_aggregator", samples=samples),
        real_space_mask=mask_7x7,
    )

    phase_interferometer_7x7.run(
        dataset=interferometer_7,
        mask=visibilities_mask_7,
        results=mock.MockResults(samples=samples),
    )

    agg = af.Aggregator(directory=phase_interferometer_7x7.paths.output_path)

    masked_interferometer_gen = al.agg.MaskedInterferometer(aggregator=agg)

    for masked_interferometer in masked_interferometer_gen:
        assert (
            masked_interferometer.interferometer.visibilities
            == interferometer_7.visibilities
        ).all()
        assert (masked_interferometer.real_space_mask == mask_7x7).all()
        assert isinstance(masked_interferometer.grid, al.GridIterate)
        assert isinstance(masked_interferometer.grid_inversion, al.GridInterpolate)
        assert masked_interferometer.grid.sub_steps == [2]
        assert masked_interferometer.grid.fractional_accuracy == 0.5
        assert masked_interferometer.grid_inversion.pixel_scales_interp == (0.1, 0.1)
        assert isinstance(masked_interferometer.transformer, al.TransformerDFT)
Ejemplo n.º 28
0
    def test__extend_with_hyper_phases__sets_up_hyper_dataset_from_results(
            self, imaging_7x7, mask_7x7):
        galaxies = af.ModelInstance()
        galaxies.lens = al.Galaxy(redshift=0.5)
        galaxies.source = al.Galaxy(redshift=1.0)

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

        hyper_galaxy_image_path_dict = {
            ("galaxies", "lens"):
            al.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0),
            ("galaxies", "source"):
            al.Array2D.full(fill_value=2.0,
                            shape_native=(3, 3),
                            pixel_scales=1.0),
        }

        results = mock.MockResults(
            hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict,
            hyper_model_image=al.Array2D.full(fill_value=3.0,
                                              shape_native=(3, 3),
                                              pixel_scales=1.0),
            mask=mask_7x7,
            use_as_hyper_dataset=True,
        )

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(lens=al.GalaxyModel(redshift=0.5,
                                              hyper_galaxy=al.HyperGalaxy)),
            search=mock.MockSearch(),
        )

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

        assert (analysis.hyper_galaxy_image_path_dict[(
            "galaxies", "lens")].native == np.ones((3, 3))).all()

        assert (analysis.hyper_galaxy_image_path_dict[(
            "galaxies", "source")].native == 2.0 * np.ones((3, 3))).all()

        assert (analysis.hyper_model_image.native == 3.0 * np.ones(
            (3, 3))).all()
Ejemplo n.º 29
0
    def test__phase_can_receive_hyper_image_and_noise_maps(self):
        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(
                lens=al.GalaxyModel(redshift=al.Redshift),
                lens1=al.GalaxyModel(redshift=al.Redshift),
            ),
            hyper_image_sky=al.hyper_data.HyperImageSky,
            hyper_background_noise=al.hyper_data.HyperBackgroundNoise,
            search=mock.MockSearch(),
        )

        instance = phase_imaging_7x7.model.instance_from_vector(
            [0.1, 0.2, 0.3, 0.4])

        assert instance.galaxies[0].redshift == 0.1
        assert instance.galaxies[1].redshift == 0.2
        assert instance.hyper_image_sky.sky_scale == 0.3
        assert instance.hyper_background_noise.noise_scale == 0.4
Ejemplo n.º 30
0
    def test__fit_using_imaging(self, imaging_7x7, mask_7x7,
                                samples_with_result):

        phase_imaging_7x7 = al.PhaseImaging(
            galaxies=dict(
                lens=al.GalaxyModel(redshift=0.5,
                                    light=al.lp.EllipticalSersic),
                source=al.GalaxyModel(redshift=1.0,
                                      light=al.lp.EllipticalSersic),
            ),
            search=mock.MockSearch(samples=samples_with_result),
        )

        result = phase_imaging_7x7.run(dataset=imaging_7x7,
                                       mask=mask_7x7,
                                       results=mock.MockResults())
        assert isinstance(result.instance.galaxies[0], al.Galaxy)
        assert isinstance(result.instance.galaxies[0], al.Galaxy)