Exemplo n.º 1
0
    def test__multi_plane_calculation(self, gal_x1_mp):

        g0 = al.Galaxy(redshift=0.5,
                       mass=al.mp.SphIsothermal(einstein_radius=1.0))
        g1 = al.Galaxy(redshift=1.0, point_0=al.ps.PointSourceFlux(flux=1.0))
        g2 = al.Galaxy(redshift=2.0, point_1=al.ps.PointSourceFlux(flux=2.0))

        tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

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

        fit_0 = al.FitFluxes(
            name="point_0",
            fluxes=fluxes,
            noise_map=noise_map,
            positions=positions,
            tracer=tracer,
        )

        deflections_func = partial(tracer.deflections_between_planes_from_grid,
                                   plane_i=0,
                                   plane_j=1)

        magnification_0 = tracer.magnification_via_hessian_from_grid(
            grid=positions, deflections_func=deflections_func)

        assert fit_0.magnifications[0] == magnification_0

        fit_1 = al.FitFluxes(
            name="point_1",
            fluxes=fluxes,
            noise_map=noise_map,
            positions=positions,
            tracer=tracer,
        )

        deflections_func = partial(tracer.deflections_between_planes_from_grid,
                                   plane_i=0,
                                   plane_j=2)

        magnification_1 = tracer.magnification_via_hessian_from_grid(
            grid=positions, deflections_func=deflections_func)

        assert fit_1.magnifications[0] == magnification_1

        assert fit_0.magnifications[0] != pytest.approx(
            fit_1.magnifications[0], 1.0e-1)
Exemplo n.º 2
0
    def test__one_set_of_fluxes__residuals_likelihood_correct(self):

        tracer = mock.MockTracer(
            magnification=al.ValuesIrregular([2.0, 2.0]),
            profile=al.ps.PointSourceFlux(flux=2.0),
        )

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

        fit = al.FitFluxes(
            name="point_0",
            fluxes=fluxes,
            noise_map=noise_map,
            positions=positions,
            tracer=tracer,
        )

        assert fit.fluxes.in_list == [1.0, 2.0]
        assert fit.noise_map.in_list == [3.0, 1.0]
        assert fit.model_fluxes.in_list == [4.0, 4.0]
        assert fit.residual_map.in_list == [-3.0, -2.0]
        assert fit.normalized_residual_map.in_list == [-1.0, -2.0]
        assert fit.chi_squared_map.in_list == [1.0, 4.0]
        assert fit.chi_squared == pytest.approx(5.0, 1.0e-4)
        assert fit.noise_normalization == pytest.approx(5.87297, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-5.43648, 1.0e-4)
Exemplo n.º 3
0
    def test__use_real_tracer(self, gal_x1_mp):

        point_source = al.ps.PointSourceFlux(centre=(0.1, 0.1), flux=2.0)
        galaxy_point_source = al.Galaxy(redshift=1.0, point_0=point_source)
        tracer = al.Tracer.from_galaxies(galaxies=[gal_x1_mp, galaxy_point_source])

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

        fit = al.FitFluxes(
            fluxes=fluxes, noise_map=noise_map, positions=positions, tracer=tracer
        )

        assert fit.model_fluxes.in_list[1] == pytest.approx(2.5, 1.0e-4)
        assert fit.log_likelihood == pytest.approx(-3.11702, 1.0e-4)
    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)