Exemplo n.º 1
0
    def smbh_from_centre(self, centre, centre_sigma=0.1):
        """
        Create a _PriorModel_ of a _PointMass_ _MassProfile_ if *include_smbh* is True, which is fitted for in the
        mass-model too represent a super-massive black-hole (smbh).

        The centre of the smbh is an input parameter of the functiono, and this centre is either fixed to the input
        values as an instance or fitted for as a model.

        Parameters
        ----------
        centre : (float, float)
            The centre of the _PointMass_ that repreents the super-massive black hole.
        centre_fixed : bool
            If True, the centre is fixed to the input values, else it is fitted for as free parameters.
        centre_sigma : float
            If the centre is free, this is the sigma value of each centre's _GaussianPrior_.
        """
        if not self.include_smbh:
            return None

        smbh = af.PriorModel(mp.PointMass)

        if self.smbh_centre_fixed:
            smbh.centre = centre
        else:
            smbh.centre.centre_0 = af.GaussianPrior(mean=centre[0],
                                                    sigma=centre_sigma)
            smbh.centre.centre_1 = af.GaussianPrior(mean=centre[1],
                                                    sigma=centre_sigma)

        return smbh
    def make_factor_model(centre: float,
                          sigma: float,
                          optimiser=None) -> ep.ModelFactor:
        """
        We'll make a LikelihoodModel for each Gaussian we're fitting.

        First we'll make the actual data to be fit.

        Note that the intensity value is shared.
        """
        y = make_data(
            Gaussian(centre=centre, intensity=intensity, sigma=sigma), x)
        """
        Next we need a prior model.
    
        Note that the intensity prior is shared.
        """
        prior_model = af.PriorModel(
            Gaussian,
            centre=af.GaussianPrior(mean=50, sigma=20),
            intensity=intensity_prior,
            sigma=af.GaussianPrior(mean=10, sigma=10),
        )
        """
        Finally we combine the likelihood function with the prior model to produce a likelihood
        factor - this will be converted into a ModelFactor which is like any other factor in the
        factor graph.
        
        We can also pass a custom optimiser in here that will be used to fit the factor instead
        of the default optimiser.
        """
        return ep.ModelFactor(prior_model,
                              analysis=Analysis(x=x, y=y),
                              optimiser=optimiser)
Exemplo n.º 3
0
    def test__log_prior_from_value(self):

        gaussian_simple = af.GaussianPrior(mean=0.0, sigma=1.0)

        log_prior = gaussian_simple.log_prior_from_value(value=0.0)

        assert log_prior == 0.0

        log_prior = gaussian_simple.log_prior_from_value(value=1.0)

        assert log_prior == 0.5

        log_prior = gaussian_simple.log_prior_from_value(value=2.0)

        assert log_prior == 2.0

        gaussian_simple = af.GaussianPrior(mean=1.0, sigma=2.0)

        log_prior = gaussian_simple.log_prior_from_value(value=0.0)

        assert log_prior == 0.125

        log_prior = gaussian_simple.log_prior_from_value(value=1.0)

        assert log_prior == 0.0

        log_prior = gaussian_simple.log_prior_from_value(value=2.0)

        assert log_prior == 0.125

        gaussian_simple = af.GaussianPrior(mean=30.0, sigma=60.0)

        log_prior = gaussian_simple.log_prior_from_value(value=2.0)

        assert log_prior == pytest.approx(0.108888, 1.0e-4)
Exemplo n.º 4
0
def make_pipeline(
    phase_folders=None,
    sub_size=2,
    signal_to_noise_limit=None,
    bin_up_factor=None,
    optimizer_class=af.MultiNest,
):

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

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

    pipeline_name = "pipeline_initialize__x1_gaussian"

    pipeline_tag = toy.pipeline_tagging.pipeline_tag_from_pipeline_settings()

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

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

    ### PHASE 1 ###

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

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

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

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

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

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

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

    return toy.PipelineDataset(pipeline_name, phase1)
Exemplo n.º 5
0
        def customize_priors(self, results):

            self.galaxies.lens.mass.centre_0 = af.GaussianPrior(mean=4.0,
                                                                sigma=0.1)
            self.galaxies.lens.mass.centre_1 = af.GaussianPrior(mean=4.0,
                                                                sigma=0.1)
            self.galaxies.source.light.centre_0 = af.GaussianPrior(mean=4.0,
                                                                   sigma=0.1)
            self.galaxies.source.light.centre_1 = af.GaussianPrior(mean=4.0,
                                                                   sigma=0.1)
Exemplo n.º 6
0
def make_hierarchical_factor(
    model_factor_1,
    model_factor_2,
):
    hierarchical_factor = g.HierarchicalFactor(
        af.GaussianPrior,
        mean=af.GaussianPrior(mean=100, sigma=10),
        sigma=af.GaussianPrior(mean=10, sigma=5),
    )

    hierarchical_factor.add_drawn_variable(model_factor_1.centre)
    hierarchical_factor.add_drawn_variable(model_factor_2.centre)
    return hierarchical_factor
Exemplo n.º 7
0
    def test__non_zero_mean(self):
        gaussian_half = af.GaussianPrior(mean=0.5, sigma=2.0)

        assert gaussian_half.value_for(0.1) == pytest.approx(
            -2.0631031, 1.0e-4)
        assert gaussian_half.value_for(0.9) == pytest.approx(3.0631031, 1.0e-4)
        assert gaussian_half.value_for(0.5) == 0.5
Exemplo n.º 8
0
    def test_no_limits(self):
        prior = af.GaussianPrior(0, 1)

        prior.assert_within_limits(100)
        prior.assert_within_limits(-100)
        prior.assert_within_limits(0)
        prior.assert_within_limits(0.5)
Exemplo n.º 9
0
def _test_gaussian():
    n_observations = 100
    x = np.arange(n_observations)
    y = make_data(Gaussian(centre=50.0, normalization=25.0, sigma=10.0), x)

    prior_model = af.PriorModel(
        Gaussian,
        # centre=af.GaussianPrior(mean=50, sigma=10),
        # normalization=af.GaussianPrior(mean=25, sigma=10),
        sigma=af.GaussianPrior(mean=10, sigma=10),
        centre=af.UniformPrior(lower_limit=30, upper_limit=70),
        normalization=af.UniformPrior(lower_limit=15, upper_limit=35),
        # sigma=af.UniformPrior(lower_limit=5, upper_limit=15),
    )

    factor_model = ep.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y))

    # optimiser = ep.LaplaceOptimiser(
    #     transform_cls=DiagonalMatrix
    # )
    optimiser = af.DynestyStatic()
    model = factor_model.optimise(optimiser)

    assert model.centre.mean == pytest.approx(50, rel=0.1)
    assert model.normalization.mean == pytest.approx(25, rel=0.1)
    assert model.sigma.mean == pytest.approx(10, rel=0.1)
Exemplo n.º 10
0
 def test_raises_exception_for_bad_limits(self, grid_search, mapper):
     mapper.component.one_tuple.one_tuple_0 = af.GaussianPrior(
         0.0, 2.0, lower_limit=float("-inf"), upper_limit=float("inf"))
     with pytest.raises(exc.PriorException):
         list(
             grid_search.make_arguments(
                 [[0, 1]],
                 grid_priors=[mapper.component.one_tuple.one_tuple_0]))
Exemplo n.º 11
0
def make_modified_result(
        model,
        samples
):
    model.gaussian.sigma = af.GaussianPrior(
        mean=0.5,
        sigma=1
    )
    model.gaussian.centre = af.GaussianPrior(
        mean=0.5,
        sigma=1
    )
    return af.Result(
        samples,
        model,
        af.m.MockSearch()
    )
Exemplo n.º 12
0
    def test__simple_assumptions(self):
        gaussian_simple = af.GaussianPrior(mean=0.0, sigma=1.0)

        assert gaussian_simple.value_for(0.1) == pytest.approx(
            -1.281551, 1.0e-4)
        assert gaussian_simple.value_for(0.9) == pytest.approx(
            1.281551, 1.0e-4)
        assert gaussian_simple.value_for(0.5) == 0.0
Exemplo n.º 13
0
def test_limits(source_gaussian, target_gaussian):
    source_gaussian.centre = af.GaussianPrior(mean=0,
                                              sigma=1,
                                              lower_limit=-1,
                                              upper_limit=1)
    target_gaussian.take_attributes(source_gaussian)
    assert target_gaussian.centre.lower_limit == -1
    assert target_gaussian.centre.upper_limit == 1
def make_pipeline(name, phase_folders, optimizer_class=af.MultiNest):

    # For this mass model, we fix the centre, making N = 10.

    mass = af.PriorModel(al.mp.EllipticalIsothermal)
    mass.centre.centre_0 = 0.0
    mass.centre.centre_1 = 0.0

    phase1 = al.PhaseImaging(
        phase_name="phase_1",
        phase_folders=phase_folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=mass),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        sub_size=1,
        optimizer_class=optimizer_class,
    )

    phase1.optimizer.const_efficiency_mode = True
    phase1.optimizer.n_live_points = 60
    phase1.optimizer.sampling_efficiency = 0.8

    # We now want to create a lens galaxy whose mass model centres are free parameters, increaisng N to 12.

    # This works, giving N = 12

    lens = phase1.result.model.galaxies.lens

    mass = af.PriorModel(al.mp.EllipticalIsothermal)

    mass.centre.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.05)
    mass.centre.centre_1 = af.GaussianPrior(mean=0.0, sigma=0.05)

    lens.mass = mass

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        phase_folders=phase_folders,
        galaxies=dict(lens=lens, source=phase1.result.model.galaxies.source),
        sub_size=1,
        optimizer_class=optimizer_class,
    )

    return al.PipelineDataset(name, phase1, phase2)
Exemplo n.º 15
0
        def customize_priors(self, results):

            centre_value = results.from_phase(
                "phase_1").instance.galaxies.lens.light.centre
            self.galaxies.lens.light.centre.centre_0 = af.GaussianPrior(
                mean=centre_value[0], sigma=0.5)
            self.galaxies.lens.light.centre.centre_1 = af.GaussianPrior(
                mean=centre_value[1], sigma=0.5)

            intensity = results.from_phase(
                "phase_1").instance.galaxies.lens.light.intensity
            self.galaxies.lens.light.intensity = af.GaussianPrior(
                mean=intensity, sigma=1.0)

            effective_radius_value = results.from_phase(
                "phase_1").instance.galaxies.lens.light.effective_radius
            self.galaxies.lens.light.effective_radius = af.GaussianPrior(
                mean=effective_radius_value, sigma=2.0)

            sersic_index_value = results.from_phase(
                "phase_1").instance.galaxies.lens.light.sersic_index
            self.galaxies.lens.light.sersic_index = af.GaussianPrior(
                mean=sersic_index_value, sigma=2.0)

            axis_ratio_value = results.from_phase(
                "phase_1").instance.galaxies.lens.light.axis_ratio
            self.galaxies.lens.light.axis_ratio = af.GaussianPrior(
                mean=axis_ratio_value, sigma=0.3)

            phi_value = results.from_phase(
                "phase_1").instance.galaxies.lens.light.phi
            self.galaxies.lens.light.phi = af.GaussianPrior(mean=phi_value,
                                                            sigma=30.0)
Exemplo n.º 16
0
 def align_centre_of_mass_to_light(self, mass, light_centre):
     """Align the centre of a mass profile to the centre of a light profile, if the align_light_mass_centre
     SLaM setting is True.
     
     Parameters
     ----------
     mass : ag.mp.MassProfile
         The mass profile whose centre may be aligned with the lens_light_centre attribute.
     light : (float, float)
         The centre of the light profile the mass profile is aligned with.
     """
     if self.align_light_mass_centre:
         mass.centre = light_centre
     else:
         mass.centre.centre_0 = af.GaussianPrior(mean=light_centre[0],
                                                 sigma=0.1)
         mass.centre.centre_1 = af.GaussianPrior(mean=light_centre[1],
                                                 sigma=0.1)
     return mass
Exemplo n.º 17
0
def test_set_model_identifier(dynesty, prior_model, analysis):
    dynesty.fit(prior_model, analysis)

    identifier = dynesty.paths.identifier
    assert identifier is not None

    prior_model.centre = af.GaussianPrior(mean=20, sigma=20)
    dynesty.fit(prior_model, analysis)

    assert identifier != dynesty.paths.identifier
Exemplo n.º 18
0
    def test_log_prior_list_from_vector(self):
        mapper = af.ModelMapper()
        mapper.mock_class = af.PriorModel(af.m.MockClassx2)
        mapper.mock_class.one = af.GaussianPrior(mean=1.0, sigma=2.0)
        mapper.mock_class.two = af.LogUniformPrior(lower_limit=1e-8,
                                                   upper_limit=10.0)

        log_prior_list = mapper.log_prior_list_from_vector(vector=[0.0, 5.0])

        assert log_prior_list == [0.125, 0.2]
Exemplo n.º 19
0
def test_gaussian():
    n_observations = 100
    x = np.arange(n_observations)
    y = make_data(Gaussian(centre=50.0, normalization=25.0, sigma=10.0), x)

    prior_model = af.PriorModel(
        Gaussian,
        centre=af.GaussianPrior(mean=50, sigma=20),
        normalization=af.GaussianPrior(mean=25, sigma=10),
        sigma=af.GaussianPrior(mean=10, sigma=10),
    )

    factor_model = ep.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y))

    laplace = ep.LaplaceOptimiser()
    model = factor_model.optimise(laplace)

    assert model.centre.mean == pytest.approx(50, rel=0.1)
    assert model.normalization.mean == pytest.approx(25, rel=0.1)
    assert model.sigma.mean == pytest.approx(10, rel=0.1)
Exemplo n.º 20
0
def make_approx(factor):
    return g.EPMeanField(
        factor_graph=g.FactorGraph([factor]),
        factor_mean_field={
            factor:
            g.MeanField({
                variable: af.GaussianPrior(mean=0, sigma=1)
                for variable in factor.variables
            })
        },
    )
Exemplo n.º 21
0
def test_hierarchical_factor(centre_model):
    centre_model.add_drawn_variable(af.GaussianPrior(100, 10))
    factor = centre_model.factors[0]

    assert len(factor.priors) == 3

    laplace = g.LaplaceOptimiser()

    gaussian = factor.optimise(laplace, max_steps=10)
    assert gaussian.instance_from_prior_medians().drawn_prior.mean(
    ) == pytest.approx(100, abs=1)
Exemplo n.º 22
0
    def unfix_lens_mass_centre(self, mass):
        """If the centre of a mass model was previously fixed to an input value (e.g. lens_mass_centre), unaligned it
        by making its centre GaussianPriors.
        """

        if self.lens_mass_centre is not None:

            mass.centre.centre_0 = af.GaussianPrior(
                mean=self.lens_mass_centre[0], sigma=0.05)
            mass.centre.centre_1 = af.GaussianPrior(
                mean=self.lens_mass_centre[1], sigma=0.05)

        else:

            mass.centre.centre_0 = af.last[
                -1].model.galaxies.lens.mass.centre.centre_0
            mass.centre.centre_1 = af.last[
                -1].model.galaxies.lens.mass.centre.centre_1

        return mass
Exemplo n.º 23
0
    def test_in_or_out(self):
        prior = af.GaussianPrior(0, 1, 0, 1)
        with pytest.raises(af.exc.PriorLimitException):
            prior.assert_within_limits(-1)

        with pytest.raises(af.exc.PriorLimitException):
            prior.assert_within_limits(1.1)

        prior.assert_within_limits(0.0)
        prior.assert_within_limits(0.5)
        prior.assert_within_limits(1.0)
Exemplo n.º 24
0
def test_model_factor(data, centres):
    y = data[0]
    centre_argument = af.GaussianPrior(mean=50, sigma=20)
    prior_model = af.PriorModel(af.Gaussian,
                                centre=centre_argument,
                                normalization=20,
                                sigma=5)
    factor = g.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y))
    laplace = g.LaplaceOptimiser()

    gaussian = factor.optimise(laplace, max_steps=10)
    assert gaussian.centre.mean == pytest.approx(centres[0], abs=0.1)
Exemplo n.º 25
0
def set_lens_light_model_centre_priors(
        lens: af.Model, light_centre_gaussian_prior_values: Tuple[float,
                                                                  float]):
    """
    Set the mean and sigma of every `GaussianPrior` of every light profile in the lens light model to the same value,
    for the y and x coordinates.

    This can be used to specifically customize only the prior on the lens light model centre, given that in many
    datasets this is clearly visible by simply looking at the image itself.

    Parameters
    ----------
    lens : af.Model(al.Galaxy)
        The `Galaxy` containing the light models of the distribution of the lens galaxy's bulge, disk and envelope.
    light_centre_gaussian_prior_values : (float, float) or None
       If input, the mean and sigma of every light model centre is set using these values as (mean, sigma).
    """

    mean = light_centre_gaussian_prior_values[0]
    sigma = light_centre_gaussian_prior_values[1]

    if lens.bulge is not None:
        lens.bulge.centre_0 = af.GaussianPrior(mean=mean, sigma=sigma)
        lens.bulge.centre_1 = af.GaussianPrior(mean=mean, sigma=sigma)

    if lens.disk is not None:
        lens.disk.centre_0 = af.GaussianPrior(mean=mean, sigma=sigma)
        lens.disk.centre_1 = af.GaussianPrior(mean=mean, sigma=sigma)

    if lens.envelope is not None:
        lens.envelope.centre_0 = af.GaussianPrior(mean=mean, sigma=sigma)
        lens.envelope.centre_1 = af.GaussianPrior(mean=mean, sigma=sigma)
Exemplo n.º 26
0
def mass__from_result(mass,
                      result: af.Result,
                      unfix_mass_centre: bool = False) -> af.Model:
    """
    Returns an updated mass `Model` whose priors are initialized from previous results in a pipeline.

    It includes an option to unfix the input `mass_centre` used in the SOURCE PIPELINE, such that if the `mass_centre`
    were fixed (e.g. to (0.0", 0.0")) it becomes a free parameter in this pipeline.

    This function generically links any `MassProfile` to any `MassProfile`, pairing parameters which share the
    same path.

    Parameters
    ----------
    results
        The result of a previous SOURCE PARAMETRIC PIPELINE or SOURCE INVERSION PIPELINE.
    unfix_mass_centre
        If the `mass_centre` was fixed to an input value in a previous pipeline, then `True` will unfix it and make it
        free parameters that are fitted for.

    Returns
    -------
    af.Model(mp.MassProfile)
        The total mass profile whose priors are initialized from a previous result.
    """

    mass.take_attributes(source=result.model.galaxies.lens.mass)

    if unfix_mass_centre and isinstance(mass.centre, tuple):

        centre_tuple = mass.centre

        mass.centre = af.Model(mass.cls).centre

        mass.centre.centre_0 = af.GaussianPrior(mean=centre_tuple[0],
                                                sigma=0.05)
        mass.centre.centre_1 = af.GaussianPrior(mean=centre_tuple[1],
                                                sigma=0.05)

    return mass
Exemplo n.º 27
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

    # For this mass model, we fix the centre, making N = 10.

    mass = af.PriorModel(al.mp.EllipticalIsothermal)
    mass.centre.centre_0 = 0.0
    mass.centre.centre_1 = 0.0

    phase1 = al.PhaseImaging(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=mass),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        sub_size=1,
        search=search,
    )

    phase1.search.const_efficiency_mode = True
    phase1.search.n_live_points = 60
    phase1.search.facc = 0.8

    # We now want to create a lens galaxy whose mass model centres are free parameters, increaisng N to 12.

    # It doesn't work - N stays at 10 and the centres do not take on the priors specified.

    lens = phase1.result.model.galaxies.lens
    lens.mass.centre.centre_0 = af.GaussianPrior(mean=0.0, sigma=0.05)
    lens.mass.centre.centre_1 = af.GaussianPrior(mean=0.0, sigma=0.05)

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(lens=lens, source=phase1.result.model.galaxies.source),
        sub_size=1,
        search=search,
    )

    return al.PipelineDataset(name, phase1, phase2)
Exemplo n.º 28
0
    def test_gaussian_prior_model_for_arguments(self):
        galaxy_model = aast.GalaxyModel(
            redshift=aast.Redshift,
            align_centres=True,
            light_profile=aast.lp.EllipticalSersic,
            mass_profile=aast.mp.SphericalIsothermal,
        )

        redshift_prior = af.GaussianPrior(1, 1)
        einstein_radius_prior = af.GaussianPrior(4, 1)
        intensity_prior = af.GaussianPrior(7, 1)

        arguments = {
            galaxy_model.redshift.redshift:
            redshift_prior,
            galaxy_model.mass_profile.centre.centre_0:
            af.GaussianPrior(2, 1),
            galaxy_model.mass_profile.centre.centre_1:
            af.GaussianPrior(3, 1),
            galaxy_model.mass_profile.einstein_radius.value:
            einstein_radius_prior,
            galaxy_model.light_profile.axis_ratio:
            af.GaussianPrior(5, 1),
            galaxy_model.light_profile.phi:
            af.GaussianPrior(6, 1),
            galaxy_model.light_profile.intensity.value:
            intensity_prior,
            galaxy_model.light_profile.effective_radius.value:
            af.GaussianPrior(8, 1),
            galaxy_model.light_profile.sersic_index:
            af.GaussianPrior(9, 1),
        }

        gaussian_galaxy_model_model = galaxy_model.gaussian_prior_model_for_arguments(
            arguments)

        assert gaussian_galaxy_model_model.redshift.redshift == redshift_prior
        assert (gaussian_galaxy_model_model.mass_profile.einstein_radius.value
                == einstein_radius_prior)
        assert (gaussian_galaxy_model_model.light_profile.intensity.value ==
                intensity_prior)
Exemplo n.º 29
0
def test_full_fit(centre_model, data, centres):
    graph = g.FactorGraphModel()
    for i, y in enumerate(data):
        prior_model = af.PriorModel(
            af.Gaussian,
            centre=af.GaussianPrior(mean=100, sigma=20),
            normalization=20,
            sigma=5,
        )
        graph.add(g.AnalysisFactor(prior_model, analysis=Analysis(x=x, y=y)))
        centre_model.add_drawn_variable(prior_model.centre)

    graph.add(centre_model)

    optimiser = g.LaplaceOptimiser()

    collection = graph.optimise(optimiser, max_steps=10).model
Exemplo n.º 30
0
def make_pipeline(
    phase_folders=None,
    sub_size=2,
    signal_to_noise_limit=None,
    bin_up_factor=None,
    optimizer_class=af.MultiNest,
):

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

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

    pipeline_name = "pipeline_initialize__x2_gaussian_separate"

    pipeline_tag = toy.pipeline_tagging.pipeline_tag_from_pipeline_settings()

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

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

    ### PHASE 1 ###

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

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

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

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

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

    ### PHASE 2 ###

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

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

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

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

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

    ### PHASE 2 ###

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

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

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

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

    return toy.PipelineDataset(pipeline_name, phase1, phase2, phase3)