Example #1
0
    def test__loads_from_config_file_if_not_input(self):
        dynesty = af.DynestyStatic(
            prior_passer=af.PriorPasser(sigma=2.0,
                                        use_errors=False,
                                        use_widths=False),
            nlive=151,
            dlogz=0.1,
            iterations_per_update=501,
            number_of_cores=2,
        )

        assert dynesty.prior_passer.sigma == 2.0
        assert dynesty.prior_passer.use_errors is False
        assert dynesty.prior_passer.use_widths is False
        assert dynesty.iterations_per_update == 501

        assert dynesty.config_dict_search["nlive"] == 151
        assert dynesty.config_dict_run["dlogz"] == 0.1
        assert dynesty.number_of_cores == 2

        dynesty = af.DynestyStatic()

        assert dynesty.prior_passer.sigma == 3.0
        assert dynesty.prior_passer.use_errors is True
        assert dynesty.prior_passer.use_widths is True
        assert dynesty.iterations_per_update == 500

        assert dynesty.config_dict_search["nlive"] == 150
        assert dynesty.config_dict_run["dlogz"] == None
        assert dynesty.number_of_cores == 1

        dynesty = af.DynestyDynamic(
            prior_passer=af.PriorPasser(sigma=2.0,
                                        use_errors=False,
                                        use_widths=False),
            facc=0.4,
            iterations_per_update=501,
            dlogz_init=0.2,
            number_of_cores=3,
        )

        assert dynesty.prior_passer.sigma == 2.0
        assert dynesty.prior_passer.use_errors is False
        assert dynesty.prior_passer.use_widths is False
        assert dynesty.iterations_per_update == 501

        assert dynesty.config_dict_search["facc"] == 0.4
        assert dynesty.config_dict_run["dlogz_init"] == 0.2
        assert dynesty.number_of_cores == 3

        dynesty = af.DynestyDynamic()

        assert dynesty.prior_passer.sigma == 3.0
        assert dynesty.prior_passer.use_errors is True
        assert dynesty.prior_passer.use_widths is True
        assert dynesty.iterations_per_update == 501

        assert dynesty.config_dict_search["facc"] == 0.6
        assert dynesty.config_dict_run["dlogz_init"] == 0.01
        assert dynesty.number_of_cores == 4
Example #2
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

    phase1 = al.PhaseImaging(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0),
    )

    pixeliation = af.PriorModel(al.pix.VoronoiBrightnessImage)
    pixeliation.pixels = 100

    phase1 = phase1.extend_with_multiple_hyper_phases(setup=al.SetupPipeline(),
                                                      include_inversion=False)

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(
                redshift=0.5, mass=phase1.result.instance.galaxies.lens.mass),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=pixeliation,
                regularization=al.reg.AdaptiveBrightness,
            ),
        ),
        search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0),
    )

    phase2 = phase2.extend_with_multiple_hyper_phases(setup=al.SetupPipeline(),
                                                      include_inversion=True)

    phase3 = al.PhaseImaging(
        phase_name="phase_3",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5,
                                mass=phase1.result.model.galaxies.lens.mass),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=phase2.result.instance.galaxies.source.
                pixelization,
                regularization=phase2.result.instance.galaxies.source.
                regularization,
            ),
        ),
        search=af.DynestyStatic(n_live_points=40, evidence_tolerance=10.0),
    )

    return al.PipelineDataset(name, phase1, phase2, phase3)
    def test__hyper_search(self):

        setup = ag.SetupHyper(search=None)
        assert setup.search.config_dict_search["nlive"] == 150
        assert setup.search.config_dict_search["dlogz"] == None

        setup = ag.SetupHyper(search=af.DynestyStatic(nlive=51))
        assert setup.search.config_dict_search["nlive"] == 51

        setup = ag.SetupHyper(hyper_galaxies=True, dlogz=0.5)
        assert setup.search.config_dict_search["dlogz"] == 0.5

        with pytest.raises(exc.PipelineException):
            ag.SetupHyper(search=af.DynestyStatic(nlive=51), dlogz=3.0)
Example #4
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

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

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5,
                                mass=phase1.result.model.galaxies.lens.mass),
            source_0=al.GalaxyModel(
                redshift=1.0,
                light=phase1.result.model.galaxies.source_0.light),
            source_1=al.GalaxyModel(redshift=1.0,
                                    light=al.lp.EllipticalSersic),
        ),
        search=search,
    )

    return al.PipelineDataset(name, phase1, phase2)
Example #5
0
def run(
        module,
        test_name=None,
        search=af.DynestyStatic(),
        config_folder="config",
        positions=None,
):
    test_name = test_name or module.test_name
    test_path = "{}/../../".format(os.path.dirname(os.path.realpath(__file__)))
    output_path = f"{test_path}/output/interferometer/"
    config_path = test_path + config_folder
    conf.instance = conf.Config(config_path=config_path,
                                output_path=output_path)

    interferometer = instrument_util.load_test_interferometer(
        data_name=module.data_name, instrument=module.instrument)

    pixel_scales = ag_instrument_util.pixel_scale_from_instrument(
        instrument=module.instrument)
    grid = ag_instrument_util.grid_from_instrument(
        instrument=module.instrument)

    real_space_mask = al.Mask.circular(shape_2d=grid.shape_2d,
                                       pixel_scales=pixel_scales,
                                       radius=2.0)

    visibilities_mask = np.full(fill_value=False,
                                shape=interferometer.visibilities.shape)

    module.make_pipeline(
        name=test_name,
        folders=[module.test_type, test_name],
        real_space_mask=real_space_mask,
        search=search,
    ).run(dataset=interferometer, mask=visibilities_mask)
Example #6
0
def make_pipeline(name, folders, search=af.DynestyStatic()):
    class LensPhase(al.PhaseImaging):
        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)

    phase1 = LensPhase(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=al.mp.SphericalIsothermal),
            source=al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic),
        ),
        positions_threshold=0.5,
        search=search,
    )

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

    return al.PipelineDataset(name, phase1)
Example #7
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

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

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

    # We want to set up the source from the result, where:

    # If it is parametric, it is a model (thus N = 12).
    # If it is an inversion, it is an instance (Thus N = 5)

    # When we use af.last, this fails, because the promise pixelizzation attribute is another promise.

    source = source_with_previous_model_or_instance()

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

    return al.PipelineDataset(name, phase1, phase2)
Example #8
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)
Example #9
0
def test_update_identifiers_from_dict():
    search = af.DynestyStatic(name="name")
    search.paths.model = af.PriorModel(af.Gaussian)
    old_directory_paths = search.paths

    initial_length = len(old_directory_paths._identifier.hash_list)

    old_directory_paths.save_all()
    old_directory_paths.zip_remove()

    update_identifiers_from_dict(output_directory,
                                 {"normalization": "magnitude"})

    filename, = listdir(output_directory / "name")

    identifier, suffix = filename.split(".")
    assert identifier != old_directory_paths.identifier
    assert suffix == "zip"

    unzipped = output_directory / "unzipped"
    with zipfile.ZipFile(output_directory / "name" / filename, "r") as f:
        f.extractall(unzipped)

    with open(unzipped / ".identifier") as f:
        lines = f.read().split("\n")
        assert "normalization" not in lines
        assert "magnitude" in lines

    assert len(lines) == initial_length
Example #10
0
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()):

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

    mass.centre.centre_0 = 0.0
    mass.centre.centre_1 = 0.0
    mass.einstein_radius = 1.6

    pixelization = af.PriorModel(al.pix.Rectangular)

    pixelization.shape_0 = 20.0
    pixelization.shape_1 = 20.0

    phase1 = al.PhaseInterferometer(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5,
                                bulge=al.lp.SphericalDevVaucouleurs,
                                mass=mass),
            source=al.GalaxyModel(redshift=1.0,
                                  pixelization=pixelization,
                                  regularization=al.reg.Constant),
        ),
        real_space_mask=real_space_mask,
        search=search,
    )

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

    return al.PipelineDataset(name, phase1)
Example #11
0
def test_identifier_file(model):
    paths = af.DirectoryPaths()
    paths.model = model
    paths.search = af.DynestyStatic()
    paths.save_all({}, {}, [])

    assert os.path.exists(output_path / paths.identifier / ".identifier")
Example #12
0
def run_with_multi_nest(module):
    # noinspection PyTypeChecker
    run(
        module,
        test_name=f"{module.test_name}_nest",
        search=af.DynestyStatic(),
        config_folder="config_mock",
    )
Example #13
0
    def test_null_paths(self, factor_model):
        optimizer = af.DynestyStatic(maxcall=10)
        result, status = optimizer.optimise(
            factor_model, factor_model.mean_field_approximation()
        )

        assert isinstance(result, g.EPMeanField)
        assert isinstance(status, Status)
Example #14
0
    def __init__(
        self,
        hyper_galaxies: bool = False,
        hyper_image_sky: Optional[type(hd.HyperImageSky)] = None,
        hyper_background_noise: Optional[type(hd.HyperBackgroundNoise)] = None,
        search: Optional[af.NonLinearSearch] = None,
        dlogz: Optional[float] = None,
    ):
        """
        The hyper setup of a pipeline, which controls how hyper-features in PyAutoGalaxy template pipelines run,
        for example controlling whether hyper galaxies are used to scale the noise and the non-linear searches used
        in these searchs.

        Users can write their own pipelines which do not use or require the *SetupHyper* class.

        Parameters
        ----------
        hyper_galaxies : bool
            If a hyper-pipeline is being used, this determines if hyper-galaxy functionality is used to scale the
            noise-map of the dataset throughout the fitting.
        hyper_image_sky : bool
            If a hyper-pipeline is being used, this determines if hyper-galaxy functionality is used include the
            image's background sky component in the model.
        hyper_background_noise : bool
            If a hyper-pipeline is being used, this determines if hyper-galaxy functionality is used include the
            noise-map's background component in the model.
        hyper_search_no_inversion : af.NonLinearSearch or None
            The `NonLinearSearch` used by every inversion search.
        hyper_search_with_inversion : af.NonLinearSearch or None
            The `NonLinearSearch` used by every hyper combined search.
        dlogz : float
            The evidence tolerance of the non-linear searches used in the hyper searchs, whereby higher values will
            lead them to end earlier at the expense of accuracy.
        """

        self.dlogz = dlogz

        if dlogz is not None:
            if search is not None:
                raise exc.PipelineException(
                    "You have manually specified a search in the SetupPipeline, and an dlogz."
                    "You cannot manually specify both - remove one."
                    "(If you want the hyper search to use a specific evidence tolerance, include the evidence"
                    "tolerance in its parameters")

        self.hyper_galaxies = hyper_galaxies

        self.hyper_galaxy_names = None

        if search is None:
            self.search = af.DynestyStatic(n_live_points=50,
                                           dlogz=self.dlogz,
                                           sample="rstagger")
        elif search is not None:
            self.search = search

        self.hyper_image_sky = hyper_image_sky
        self.hyper_background_noise = hyper_background_noise
Example #15
0
    def __init__(
        self,
        subhalo_prior_model: af.PriorModel(
            mp.MassProfile) = mp.SphericalNFWMCRLudlow,
        subhalo_search: af.NonLinearSearch = None,
        source_is_model: bool = True,
        mass_is_model: bool = True,
        grid_size: int = 5,
        grid_dimension_arcsec: float = 3.0,
        parallel: bool = False,
        subhalo_instance=None,
    ):
        """
        The setup of a subhalo pipeline, which controls how PyAutoLens template pipelines runs.

        Users can write their own pipelines which do not use or require the *SetupPipeline* class.

        This class enables pipeline tagging, whereby the setup of the pipeline is used in the template pipeline
        scripts to tag the output path of the results depending on the setup parameters. This allows one to fit
        different models to a dataset in a structured path format.

        Parameters
        ----------
        subhalo_search : af.NonLinearSearch
            The search used to sample parameter space in the subhalo pipeline.
        source_is_model : bool
            If `True`, the source is included as a model in the fit (for both `LightProfile` or `Inversion` sources).
            If `False` its parameters are fixed to those inferred in a previous pipeline.
        mass_is_model : bool
            If `True`, the mass is included as a model in the fit. If `False` its parameters are fixed to those
            inferred in a previous pipeline.
        grid_size : int
            The 2D dimensions of the grid (e.g. grid_size x grid_size) that the subhalo search is performed for.
        grid_dimension_arcsec : float
            the arc-second dimensions of the grid in the y and x directions. An input value of 3.0" means the grid in
            all four directions extends to 3.0" giving it dimensions 6.0" x 6.0".
        parallel : bool
            If `True` the `Python` `multiprocessing` module is used to parallelize the fitting over the cpus available
            on the system.
        subhalo_instance : ag.MassProfile
            An instance of the mass-profile used as a fixed model for a subhalo pipeline.
        """

        if subhalo_search is None:
            subhalo_search = af.DynestyStatic(n_live_points=50,
                                              walks=5,
                                              facc=0.2)

        self.subhalo_prior_model = self._cls_to_prior_model(
            cls=subhalo_prior_model)

        self.subhalo_search = subhalo_search
        self.source_is_model = source_is_model
        self.mass_is_model = mass_is_model
        self.grid_size = grid_size
        self.grid_dimensions_arcsec = grid_dimension_arcsec
        self.parallel = parallel
        self.subhalo_instance = subhalo_instance
Example #16
0
def make_pipeline(name, folders, real_space_mask, search=af.DynestyStatic()):

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

    mass.centre.centre_0 = 2.0
    mass.centre.centre_1 = 2.0
    mass.einstein_radius = 1.6

    pixelization = af.PriorModel(al.pix.VoronoiMagnification)

    pixelization.shape_0 = 20.0
    pixelization.shape_1 = 20.0

    phase1 = al.PhaseInterferometer(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=mass),
            source=al.GalaxyModel(redshift=1.0,
                                  pixelization=pixelization,
                                  regularization=al.reg.Constant),
        ),
        real_space_mask=real_space_mask,
        search=search,
    )

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

    phase1 = phase1.extend_with_inversion_phase()

    phase2 = al.PhaseInterferometer(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5,
                                mass=phase1.result.model.galaxies.lens.mass),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=phase1.results.settings_inversion.instance.
                galaxies.source.pixelization,
                regularization=phase1.results.settings_inversion.instance.
                galaxies.source.regularization,
            ),
        ),
        real_space_mask=real_space_mask,
        search=search,
    )

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

    phase2 = phase2.extend_with_inversion_phase()

    return al.PipelineDataset(name, phase1, phase2)
Example #17
0
def test_identifier_file():
    paths = af.DirectoryPaths()
    paths.model = af.Model(
        af.Gaussian
    )
    paths.search = af.DynestyStatic()

    assert os.path.exists(
        output_path / paths.identifier / ".identifier"
    )
Example #18
0
    def test__paths(self):

        galaxy = al.Galaxy(mass=al.mp.SphericalIsothermal(), redshift=1.0)

        phase = al.PhaseImaging(
            galaxies=dict(galaxy=galaxy),
            search=af.DynestyStatic(name="test_phase", n_live_points=1),
            settings=al.SettingsPhaseImaging(),
        )

        phase_stochastic = phase.extend_with_stochastic_phase(
            stochastic_search=af.DynestyStatic(n_live_points=1))

        hyper_phase = phase_stochastic.make_hyper_phase()

        assert (path.join(
            "test_phase",
            "stochastic__settings__grid_sub_2",
            "dynesty_static__nlive_1",
        ) in hyper_phase.paths.output_path)
Example #19
0
def make_pipeline(slam, settings):
    """SETUP PIPELINE & PHASE NAMES, TAGS AND PATHS"""

    pipeline_name = "pipeline_mass[total]"
    """
    This pipeline is tagged according to whether:

        1) Hyper-fitting settings (galaxies, sky, background noise) are used.
        2) The lens galaxy mass model includes an  `ExternalShear`.
        3) The lens`s light model is fixed or variable.
    """

    path_prefix = f"{slam.path_prefix}/{pipeline_name}/{slam.source_tag}/{slam.light_parametric_tag}/{slam.mass_tag}"
    """SLaM: Set whether shear is included in the mass model using the `ExternalShear` model of the Source pipeline."""

    shear = slam.pipeline_mass.shear_from_previous_pipeline(index=-1)
    """
    Phase 1: Fit the lens `Galaxy`'s light and mass and one source galaxy, where we:

        1) Use the source galaxy of the `source` pipeline.
        2) Use the lens galaxy light of the `light` pipeline.
        3) Set priors on the lens galaxy `MassProfile`'s using the `EllipticalIsothermal` and `ExternalShear` of 
           previous pipelines.
    """

    mass = slam.pipeline_mass.setup_mass.mass_prior_model_with_updated_priors(
        index=-1, unfix_mass_centre=True)
    """SLaM: Use the source and lens light models from the previous *Source* and *Light* pipelines."""

    lens = slam.lens_from_light_parametric_pipeline_for_mass_total_pipeline(
        mass=mass, shear=shear)

    source = slam.source_from_previous_pipeline_model_if_parametric()

    phase1 = al.PhaseImaging(
        search=af.DynestyStatic(
            name="phase[1]_light[parametric]_mass[total]_source",
            n_live_points=100),
        galaxies=dict(lens=lens, source=source),
        hyper_image_sky=af.last.hyper_combined.instance.optional.
        hyper_image_sky,
        hyper_background_noise=af.last.hyper_combined.instance.optional.
        hyper_background_noise,
        settings=settings,
    )

    if not slam.setup_hyper.hyper_fixed_after_source:

        phase1 = phase1.extend_with_multiple_hyper_phases(
            setup_hyper=slam.setup_hyper, include_inversion=True)

    return al.PipelineDataset(pipeline_name, path_prefix, phase1)
Example #20
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

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

    mass.centre.centre_0 = 0.0
    mass.centre.centre_1 = 0.0
    mass.einstein_radius = 1.6

    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,
                pixelization=al.pix.Rectangular,
                regularization=al.reg.Constant,
            ),
        ),
        search=search,
    )

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

    phase1.extend_with_multiple_hyper_phases(hyper_galaxies_search=True)

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(
                redshift=0.5,
                mass=phase1.result.model.galaxies.lens.mass,
                hyper_galaxy=al.HyperGalaxy,
            ),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=phase1.result.model.galaxies.source.pixelization,
                regularization=phase1.result.model.galaxies.source.regularization,
                hyper_galaxy=phase1.result.hyper_combined.instance.galaxies.source.hyper_galaxy,
            ),
        ),
        search=search,
    )

    phase2.search.const_efficiency_mode = True
    phase2.search.n_live_points = 40
    phase2.search.facc = 0.8

    return al.PipelineDataset(name, phase1, phase2)
Example #21
0
    def test_passes_attributes(self):
        grid_search = af.SearchGridSearch(number_of_steps=10,
                                          search=af.DynestyStatic())
        grid_search.paths = af.DirectoryPaths(name="")

        grid_search.nlive = 20
        grid_search.facc = 0.3

        search = grid_search.search_instance("name_path")

        assert search.nlive is grid_search.nlive
        assert grid_search.paths.path != search.paths.path
        assert grid_search.paths.output_path != search.paths.output_path
Example #22
0
    def test__samples_from_model(self):
        # Setup pickle of mock Dynesty sampler that the samples_from_model function uses.

        results = MockDynestyResults(
            samples=np.array([[1.0, 2.0, 3.0, 5.0], [1.0, 2.0, 3.0, 4.0],
                              [1.0, 2.0, 3.0, 4.0]]),
            logl=[1.0, 2.0, 3.0],
            logwt=[np.log(1.0), np.log(2.0),
                   np.log(3.0)],
            ncall=[5.0, 5.0],
            logz=[-2.0, -1.0, 0.0],
            nlive=3,
        )

        sampler = MockDynestySampler(results=results)

        paths = af.DirectoryPaths(
            path_prefix=path.join("non_linear", "dynesty"))
        paths._identifier = "tag"

        dynesty = af.DynestyStatic(nlive=3)
        dynesty.paths = paths

        with open(path.join(dynesty.paths.samples_path, "dynesty.pickle"),
                  "wb") as f:
            dill.dump(sampler, f)

        model = af.ModelMapper(mock_class=mock.MockClassx4)
        model.mock_class.two = af.LogUniformPrior(lower_limit=1e-8,
                                                  upper_limit=10.0)

        samples = dynesty.samples_from(model=model)

        assert isinstance(samples.parameter_lists, list)
        assert isinstance(samples.parameter_lists[0], list)
        assert isinstance(samples.log_likelihood_list, list)
        assert isinstance(samples.log_prior_list, list)
        assert isinstance(samples.log_posterior_list, list)
        assert isinstance(samples.weight_list, list)

        assert samples.parameter_lists == [
            [1.0, 2.0, 3.0, 5.0],
            [1.0, 2.0, 3.0, 4.0],
            [1.0, 2.0, 3.0, 4.0],
        ]
        assert samples.log_likelihood_list == [1.0, 2.0, 3.0]
        assert samples.log_prior_list == [0.2, 0.25, 0.25]
        assert samples.weight_list == pytest.approx([1.0, 2.0, 3.0], 1.0e-4)
        assert samples.total_samples == 10
        assert samples.log_evidence == 0.0
        assert samples.number_live_points == 3
Example #23
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

    lens = al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal)

    lens.mass.centre_0 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01)
    lens.mass.centre_1 = af.UniformPrior(lower_limit=-0.01, upper_limit=0.01)
    lens.mass.einstein_radius = af.UniformPrior(lower_limit=1.55,
                                                upper_limit=1.65)

    source = al.GalaxyModel(redshift=1.0, light=al.lp.EllipticalSersic)

    source.light.centre_0 = af.UniformPrior(lower_limit=-0.01,
                                            upper_limit=0.01)
    source.light.centre_1 = af.UniformPrior(lower_limit=-0.01,
                                            upper_limit=0.01)
    source.light.intensity = af.UniformPrior(lower_limit=0.35,
                                             upper_limit=0.45)
    source.light.effective_radius = af.UniformPrior(lower_limit=0.45,
                                                    upper_limit=0.55)
    source.light.sersic_index = af.UniformPrior(lower_limit=0.9,
                                                upper_limit=1.1)

    class GridPhase(
            af.as_grid_search(phase_class=al.PhaseImaging, parallel=True)):
        @property
        def grid_priors(self):
            return [
                self.model.galaxies.subhalo.mass.centre_0,
                self.model.galaxies.subhalo.mass.centre_1,
            ]

    subhalo = al.GalaxyModel(redshift=0.5,
                             mass=al.mp.SphericalTruncatedNFWMCRLudlow)

    subhalo.mass.mass_at_200 = af.LogUniformPrior(lower_limit=1.0e6,
                                                  upper_limit=1.0e11)

    subhalo.mass.centre_0 = af.UniformPrior(lower_limit=-2.5, upper_limit=2.5)
    subhalo.mass.centre_1 = af.UniformPrior(lower_limit=-2.5, upper_limit=2.5)

    phase1 = GridPhase(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(lens=lens, subhalo=subhalo, source=source),
        search=search,
        settings=al.SettingsPhaseImaging(),
        number_of_steps=2,
    )

    return al.PipelineDataset(name, phase1)
Example #24
0
def test_update_identifiers_from_dict_no_change():
    search = af.DynestyStatic(name="name")
    search.paths.model = af.PriorModel(af.Gaussian)
    old_directory_paths = search.paths

    old_directory_paths.save_all()
    old_directory_paths.zip_remove()

    update_identifiers_from_dict(output_directory, {})

    filename, = listdir(output_directory / "name")

    identifier, suffix = filename.split(".")
    assert identifier == old_directory_paths.identifier
    assert suffix == "zip"
Example #25
0
def test_database(session):
    conf.instance["general"]["output"]["identifier_version"] = 1
    search = af.DynestyStatic(name="name", session=session)
    search.paths.model = af.PriorModel(af.Gaussian)
    search.paths.save_all(search_config_dict=search.config_dict_search,
                          info={},
                          pickle_files=[])

    fit, = Fit.all(session=session)
    assert fit.id == search.paths.identifier

    conf.instance["general"]["output"]["identifier_version"] = 3
    update_database_identifiers(session)

    fit, = Fit.all(session=session)
    assert fit.id != search.paths.identifier
Example #26
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

    phase1 = al.PhaseImaging(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, sersic=al.lp.EllipticalSersic)),
        sub_size=2,
        search=search,
    )

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

    return al.PipelineDataset(name, phase1)
def make_pipeline(slam, settings):
    """SETUP PIPELINE & PHASE NAMES, TAGS AND PATHS"""

    pipeline_name = "pipeline_source[parametric]"
    """
    This pipeline is tagged according to whether:

        1) Hyper-fitting settings (galaxies, sky, background noise) are used.
        2) The lens galaxy mass model includes an `ExternalShear`.
        3) The source model determined from `SetupSourceParametric` (e.g. `bulge_prior_model`, `disk_prior_model`, 
           etc.)
    """

    path_prefix = f"{slam.path_prefix}/{pipeline_name}/{slam.source_parametric_tag}"
    """
    Phase 1: Fit the lens`s `MassProfile`'s and source galaxy.
    """

    phase1 = al.PhaseImaging(
        search=af.DynestyStatic(name="phase[1]_mass[total]_source[parametric]",
                                n_live_points=200,
                                walks=10),
        galaxies=dict(
            lens=al.GalaxyModel(
                redshift=slam.redshift_lens,
                mass=slam.pipeline_source_parametric.setup_mass.
                mass_prior_model,
                shear=slam.pipeline_source_parametric.setup_mass.
                shear_prior_model,
            ),
            source=al.GalaxyModel(
                redshift=slam.redshift_source,
                bulge=slam.pipeline_source_parametric.setup_source.
                bulge_prior_model,
                disk=slam.pipeline_source_parametric.setup_source.
                disk_prior_model,
                envelope=slam.pipeline_source_parametric.setup_source.
                envelope_prior_model,
            ),
        ),
        settings=settings,
    )

    phase1 = phase1.extend_with_multiple_hyper_phases(
        setup_hyper=slam.setup_hyper)

    return al.PipelineDataset(pipeline_name, path_prefix, phase1)
Example #28
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

    phase1 = al.PhaseImaging(
        phase_name="phase_1",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5, mass=al.mp.EllipticalIsothermal),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=al.pix.VoronoiMagnification,
                regularization=al.reg.Constant,
            ),
        ),
        search=search,
    )

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

    phase1 = phase1.extend_with_inversion_phase()

    phase2 = al.PhaseImaging(
        phase_name="phase_2",
        folders=folders,
        galaxies=dict(
            lens=al.GalaxyModel(redshift=0.5,
                                mass=phase1.result.model.galaxies.lens.mass),
            source=al.GalaxyModel(
                redshift=1.0,
                pixelization=phase1.result.instance.galaxies.source.
                pixelization,
                regularization=phase1.result.instance.galaxies.source.
                regularization,
            ),
        ),
        search=search,
    )

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

    phase2 = phase2.extend_with_inversion_phase()

    return al.PipelineDataset(name, phase1, phase2)
Example #29
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

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

    return al.PipelineDataset(name, phase1)
Example #30
0
def make_pipeline(name, folders, search=af.DynestyStatic()):

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

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

    return al.PipelineDataset(name, phase1)