def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, positions_x2, positions_x2_noise_map):

        point_source_dataset = al.PointSourceDataset(
            name="point_0",
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
        )

        point_source_dict = al.PointSourceDict(
            point_source_dataset_list=[point_source_dataset])

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

        solver = mock.MockPositionsSolver(model_positions=positions_x2)

        analysis = al.AnalysisPointSource(point_source_dict=point_source_dict,
                                          solver=solver)

        instance = model.instance_from_unit_vector([])
        analysis_log_likelihood = analysis.log_likelihood_function(
            instance=instance)

        tracer = analysis.tracer_for_instance(instance=instance)

        fit_positions = al.FitPositionsImage(
            name="point_0",
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=solver,
        )

        assert fit_positions.chi_squared == 0.0
        assert fit_positions.log_likelihood == analysis_log_likelihood

        model_positions = al.Grid2DIrregular([(0.0, 1.0), (1.0, 2.0)])
        solver = mock.MockPositionsSolver(model_positions=model_positions)

        analysis = al.AnalysisPointSource(point_source_dict=point_source_dict,
                                          solver=solver)

        analysis_log_likelihood = analysis.log_likelihood_function(
            instance=instance)

        fit_positions = al.FitPositionsImage(
            name="point_0",
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=solver,
        )

        assert fit_positions.residual_map.in_list == [1.0, 1.0]
        assert fit_positions.chi_squared == 2.0
        assert fit_positions.log_likelihood == analysis_log_likelihood
Exemple #2
0
    def test__more_model_positions_than_data_positions__pairs_closest_positions(
            self):

        g0 = al.Galaxy(redshift=1.0,
                       point_0=al.ps.PointSource(centre=(0.1, 0.1)))

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

        positions = al.Grid2DIrregular([(0.0, 0.0), (3.0, 4.0)])
        noise_map = al.ValuesIrregular([0.5, 1.0])
        model_positions = al.Grid2DIrregular([(3.0, 1.0), (2.0, 3.0),
                                              (1.0, 0.0), (0.0, 1.0)])

        positions_solver = mock.MockPositionsSolver(
            model_positions=model_positions)

        fit = al.FitPositionsImage(
            name="point_0",
            positions=positions,
            noise_map=noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit.model_positions.in_list == [(1.0, 0.0), (2.0, 3.0)]
        assert fit.noise_map.in_list == [0.5, 1.0]
        assert fit.residual_map.in_list == [1.0, np.sqrt(2.0)]
        assert fit.normalized_residual_map.in_list == [2.0, np.sqrt(2.0) / 1.0]
        assert fit.chi_squared_map.in_list == [4.0, np.sqrt(2.0)**2.0]
        assert fit.chi_squared == pytest.approx(6.0, 1.0e-4)
        assert fit.noise_normalization == pytest.approx(2.289459, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-4.144729, 1.0e-4)
Exemple #3
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)
    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
    def test__two_sets_of_positions__residuals_likelihood_correct(self):

        tracer = MockTracerPositions(positions=None)

        positions = al.GridIrregularGrouped([[(0.0, 0.0), (3.0, 4.0)], [(3.0, 3.0)]])

        noise_map = al.ValuesIrregularGrouped([[0.5, 1.0], [1.0]])

        model_positions = al.GridIrregularGrouped(
            [[(3.0, 1.0), (2.0, 3.0)], [(3.0, 3.0)]]
        )

        positions_solver = mock.MockPositionsSolver(model_positions=model_positions)

        fit = al.FitPositionsImage(
            positions=positions,
            noise_map=noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit.model_positions_all.in_grouped_list == [
            [(3.0, 1.0), (2.0, 3.0)],
            [(3.0, 3.0)],
        ]
        assert fit.model_positions.in_grouped_list == [
            [(3.0, 1.0), (2.0, 3.0)],
            [(3.0, 3.0)],
        ]
        assert fit.noise_map.in_grouped_list == [[0.5, 1.0], [1.0]]
        assert fit.residual_map.in_grouped_list == [
            [np.sqrt(10.0), np.sqrt(2.0)],
            [0.0],
        ]
        assert fit.normalized_residual_map.in_grouped_list == [
            [np.sqrt(10.0) / 0.5, np.sqrt(2.0) / 1.0],
            [0.0],
        ]
        assert fit.chi_squared_map.in_grouped_list == [
            [(np.sqrt(10.0) / 0.5) ** 2, np.sqrt(2.0) ** 2.0],
            [0.0],
        ]
        assert fit.chi_squared == pytest.approx(42.0, 1.0e-4)
        assert fit.noise_normalization == pytest.approx(4.12733, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-23.06366, 1.0e-4)
    def test__more_model_positions_than_data_positions__pairs_closest_positions(self):

        tracer = MockTracerPositions(positions=None)

        positions = al.GridIrregularGrouped([[(0.0, 0.0), (3.0, 4.0)], [(3.0, 3.0)]])

        noise_map = al.ValuesIrregularGrouped([[0.5, 1.0], [1.0]])

        model_positions = al.GridIrregularGrouped(
            [[(3.0, 1.0), (2.0, 3.0), (1.0, 0.0), (0.0, 1.0)], [(3.0, 3.0), (4.0, 4.0)]]
        )

        positions_solver = mock.MockPositionsSolver(model_positions=model_positions)

        fit = al.FitPositionsImage(
            positions=positions,
            noise_map=noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit.model_positions_all.in_grouped_list == [
            [(3.0, 1.0), (2.0, 3.0), (1.0, 0.0), (0.0, 1.0)],
            [(3.0, 3.0), (4.0, 4.0)],
        ]
        assert fit.model_positions.in_grouped_list == [
            [(1.0, 0.0), (2.0, 3.0)],
            [(3.0, 3.0)],
        ]
        assert fit.noise_map.in_grouped_list == [[0.5, 1.0], [1.0]]
        assert fit.residual_map.in_grouped_list == [[1.0, np.sqrt(2.0)], [0.0]]
        assert fit.normalized_residual_map.in_grouped_list == [
            [2.0, np.sqrt(2.0) / 1.0],
            [0.0],
        ]
        assert fit.chi_squared_map.in_grouped_list == [
            [4.0, np.sqrt(2.0) ** 2.0],
            [0.0],
        ]
        assert fit.chi_squared == pytest.approx(6.0, 1.0e-4)
        assert fit.noise_normalization == pytest.approx(4.12733, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-5.06366, 1.0e-4)
    def test__fit_using_positions(self, positions_x2, positions_x2_noise_map,
                                  samples_with_result):

        phase_positions_x2 = al.PhasePointSource(
            galaxies=dict(
                lens=al.GalaxyModel(redshift=0.5,
                                    light=al.lp.EllipticalSersic),
                source=al.GalaxyModel(redshift=1.0, point=al.ps.PointSource),
            ),
            search=mock.MockSearch(samples=samples_with_result),
            positions_solver=mock.MockPositionsSolver(
                model_positions=positions_x2),
        )

        result = phase_positions_x2.run(
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
            results=mock.MockResults(),
        )

        assert isinstance(result.instance.galaxies[0], al.Galaxy)
        assert isinstance(result.instance.galaxies[0], al.Galaxy)
Exemple #8
0
    def test__two_sets_of_positions__residuals_likelihood_correct(self):

        point_source = al.ps.PointSource(centre=(0.1, 0.1))
        galaxy_point_source = al.Galaxy(redshift=1.0, point_0=point_source)
        tracer = al.Tracer.from_galaxies(
            galaxies=[al.Galaxy(redshift=0.5), galaxy_point_source])

        positions = al.Grid2DIrregular([(0.0, 0.0), (3.0, 4.0)])
        noise_map = al.ValuesIrregular([0.5, 1.0])
        model_positions = al.Grid2DIrregular([(3.0, 1.0), (2.0, 3.0)])

        positions_solver = mock.MockPositionsSolver(
            model_positions=model_positions)

        fit = al.FitPositionsImage(
            name="point_0",
            positions=positions,
            noise_map=noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit.model_positions.in_list == [(3.0, 1.0), (2.0, 3.0)]

        assert fit.model_positions.in_list == [(3.0, 1.0), (2.0, 3.0)]

        assert fit.noise_map.in_list == [0.5, 1.0]
        assert fit.residual_map.in_list == [np.sqrt(10.0), np.sqrt(2.0)]
        assert fit.normalized_residual_map.in_list == [
            np.sqrt(10.0) / 0.5,
            np.sqrt(2.0) / 1.0,
        ]
        assert fit.chi_squared_map.in_list == [
            (np.sqrt(10.0) / 0.5)**2,
            np.sqrt(2.0)**2.0,
        ]
        assert fit.chi_squared == pytest.approx(42.0, 1.0e-4)
        assert fit.noise_normalization == pytest.approx(2.28945, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-22.14472, 1.0e-4)
    def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
            self, positions_x2, positions_x2_noise_map):

        lens_galaxy = al.Galaxy(redshift=0.5,
                                light=al.ps.PointSource(centre=(0.0, 0.0)))

        phase_positions_x2 = al.PhasePointSource(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhasePositions(),
            search=mock.MockSearch(),
            positions_solver=mock.MockPositionsSolver(
                model_positions=positions_x2),
        )

        analysis = phase_positions_x2.make_analysis(
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
            results=mock.MockResults(),
        )
        instance = phase_positions_x2.model.instance_from_unit_vector([])
        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        tracer = analysis.tracer_for_instance(instance=instance)

        positions_solver = mock.MockPositionsSolver(
            model_positions=positions_x2)

        fit_positions = al.FitPositionsImage(
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit_positions.chi_squared == 0.0
        assert fit_positions.log_likelihood == fit_figure_of_merit

        model_positions = al.Grid2DIrregular([(0.0, 1.0), (1.0, 2.0)])
        positions_solver = mock.MockPositionsSolver(
            model_positions=model_positions)

        phase_positions_x2 = al.PhasePointSource(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhasePositions(),
            search=mock.MockSearch(),
            positions_solver=positions_solver,
        )

        analysis = phase_positions_x2.make_analysis(
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
            results=mock.MockResults(),
        )
        instance = phase_positions_x2.model.instance_from_unit_vector([])
        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        fit_positions = al.FitPositionsImage(
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit_positions.residual_map.in_list == [1.0, 1.0]
        assert fit_positions.chi_squared == 2.0
        assert fit_positions.log_likelihood == fit_figure_of_merit
    def test__figure_of_merit__includes_fit_fluxes(self, positions_x2,
                                                   positions_x2_noise_map,
                                                   fluxes_x2,
                                                   fluxes_x2_noise_map):

        lens_galaxy = al.Galaxy(
            redshift=0.5,
            sis=al.mp.SphericalIsothermal(einstein_radius=1.0),
            light=al.ps.PointSourceFlux(flux=1.0),
        )

        phase_positions_x2 = al.PhasePointSource(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhasePositions(),
            search=mock.MockSearch(),
            positions_solver=mock.MockPositionsSolver(
                model_positions=positions_x2),
        )

        analysis = phase_positions_x2.make_analysis(
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
            fluxes=fluxes_x2,
            fluxes_noise_map=fluxes_x2_noise_map,
            results=mock.MockResults(),
        )

        instance = phase_positions_x2.model.instance_from_unit_vector([])

        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        tracer = analysis.tracer_for_instance(instance=instance)

        positions_solver = mock.MockPositionsSolver(
            model_positions=positions_x2)

        fit_positions = al.FitPositionsImage(
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        fit_fluxes = al.FitFluxes(
            fluxes=fluxes_x2,
            noise_map=fluxes_x2_noise_map,
            positions=positions_x2,
            tracer=tracer,
        )

        assert (fit_positions.log_likelihood +
                fit_fluxes.log_likelihood == fit_figure_of_merit)

        model_positions = al.Grid2DIrregular([(0.0, 1.0), (1.0, 2.0)])
        positions_solver = mock.MockPositionsSolver(
            model_positions=model_positions)

        phase_positions_x2 = al.PhasePointSource(
            galaxies=dict(lens=lens_galaxy),
            settings=al.SettingsPhasePositions(),
            search=mock.MockSearch(),
            positions_solver=positions_solver,
        )

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

        instance = phase_positions_x2.model.instance_from_unit_vector([])
        fit_figure_of_merit = analysis.log_likelihood_function(
            instance=instance)

        fit_positions = al.FitPositionsImage(
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=positions_solver,
        )

        assert fit_positions.residual_map.in_list == [1.0, 1.0]
        assert fit_positions.chi_squared == 2.0
        assert fit_positions.log_likelihood == fit_figure_of_merit
    def test__figure_of_merit__includes_fit_fluxes(self, positions_x2,
                                                   positions_x2_noise_map,
                                                   fluxes_x2,
                                                   fluxes_x2_noise_map):

        point_source_dataset = al.PointSourceDataset(
            name="point_0",
            positions=positions_x2,
            positions_noise_map=positions_x2_noise_map,
            fluxes=fluxes_x2,
            fluxes_noise_map=fluxes_x2_noise_map,
        )

        point_source_dict = al.PointSourceDict(
            point_source_dataset_list=[point_source_dataset])

        model = af.Collection(galaxies=af.Collection(lens=al.Galaxy(
            redshift=0.5,
            sis=al.mp.SphIsothermal(einstein_radius=1.0),
            point_0=al.ps.PointSourceFlux(flux=1.0),
        )))

        solver = mock.MockPositionsSolver(model_positions=positions_x2)

        analysis = al.AnalysisPointSource(point_source_dict=point_source_dict,
                                          solver=solver)

        instance = model.instance_from_unit_vector([])

        analysis_log_likelihood = analysis.log_likelihood_function(
            instance=instance)

        tracer = analysis.tracer_for_instance(instance=instance)

        fit_positions = al.FitPositionsImage(
            name="point_0",
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=solver,
        )

        fit_fluxes = al.FitFluxes(
            name="point_0",
            fluxes=fluxes_x2,
            noise_map=fluxes_x2_noise_map,
            positions=positions_x2,
            tracer=tracer,
        )

        assert (fit_positions.log_likelihood +
                fit_fluxes.log_likelihood == analysis_log_likelihood)

        model_positions = al.Grid2DIrregular([(0.0, 1.0), (1.0, 2.0)])
        solver = mock.MockPositionsSolver(model_positions=model_positions)

        analysis = al.AnalysisPointSource(point_source_dict=point_source_dict,
                                          solver=solver)

        instance = model.instance_from_unit_vector([])
        analysis_log_likelihood = analysis.log_likelihood_function(
            instance=instance)

        fit_positions = al.FitPositionsImage(
            name="point_0",
            positions=positions_x2,
            noise_map=positions_x2_noise_map,
            tracer=tracer,
            positions_solver=solver,
        )

        fit_fluxes = al.FitFluxes(
            name="point_0",
            fluxes=fluxes_x2,
            noise_map=fluxes_x2_noise_map,
            positions=positions_x2,
            tracer=tracer,
        )

        assert fit_positions.residual_map.in_list == [1.0, 1.0]
        assert fit_positions.chi_squared == 2.0
        assert (fit_positions.log_likelihood +
                fit_fluxes.log_likelihood == analysis_log_likelihood)