def from_instance(instance, model_classes=tuple()): """ Recursively create an prior object model from an object model. Parameters ---------- model_classes instance A dictionary, list, class instance or model instance Returns ------- abstract_prior_model A concrete child of an abstract prior model """ from autofit.mapper.prior_model import collection if isinstance(instance, (Prior, AbstractPriorModel)): return instance elif isinstance(instance, list): result = collection.CollectionPriorModel([ AbstractPriorModel.from_instance(item, model_classes=model_classes) for item in instance ]) elif isinstance(instance, model.ModelInstance): from autofit.mapper import model_mapper result = model_mapper.ModelMapper() for key, value in instance.dict.items(): setattr( result, key, AbstractPriorModel.from_instance( value, model_classes=model_classes), ) elif isinstance(instance, dict): result = collection.CollectionPriorModel({ key: AbstractPriorModel.from_instance(value, model_classes=model_classes) for key, value in instance.items() }) elif isinstance(instance, np.ndarray): return instance else: from .prior_model import PriorModel try: result = PriorModel( instance.__class__, **{ key: AbstractPriorModel.from_instance( value, model_classes=model_classes) for key, value in instance.__dict__.items() if key != "cls" }, ) except AttributeError: return instance if any([isinstance(instance, cls) for cls in model_classes]): return result.as_model() return result
def __init__(self, model_image=None, galaxy_images=(), constant=None, analysis=None, optimizer=None): self.model_image = model_image self.galaxy_images = galaxy_images self.constant = constant or mm.ModelInstance() self.variable = mm.ModelMapper() self.analysis = analysis self.optimizer = optimizer
def test_integration(self): directory = os.path.dirname(os.path.realpath(__file__)) config = conf.DefaultPriorConfig( "{}/../../{}".format(directory, "test_files/configs/galaxy_model/priors/default")) print(config.path) limit_config = conf.LimitConfig( "{}/../../{}".format(directory, "test_files/configs/galaxy_model/priors/limit")) # Create a mapper. This can be used to convert values output by a non linear optimiser into class instances. mapper = mm.ModelMapper(config=config, limit_config=limit_config) # Create a model_galaxy prior for the source model_galaxy. Here we are describing only the light profile of # the source model_galaxy which comprises an elliptical exponential and elliptical sersic light profile. source_galaxy_prior = gm.GalaxyModel(variable_redshift=True, light_profile_one=light_profiles.EllipticalExponential, light_profile_2=light_profiles.EllipticalSersic, config=config, limit_config=limit_config) # Create a model_galaxy prior for the source model_galaxy. Here we are describing both the light and mass # profiles. We've also stipulated that the centres of any galaxies generated using the model_galaxy prior # should match. lens_galaxy_prior = gm.GalaxyModel(variable_redshift=True, light_profile=light_profiles.EllipticalExponential, mass_profile=mass_profiles.EllipticalExponential, align_centres=True, config=config, limit_config=limit_config) mapper.source_galaxy = source_galaxy_prior mapper.lens_galaxy = lens_galaxy_prior # Create a model instance. All the instances of the profile classes are created here. Normally we would do this # using the output of a non linear search but in this case we are using the median values from the priors. instance = mapper.instance_from_prior_medians() # Recover model_galaxy instances. We can pass the model instance to model_galaxy priors to recover a fully # constructed model_galaxy source_galaxy = instance.source_galaxy lens_galaxy = instance.lens_galaxy # Let's just check that worked assert len(source_galaxy.light_profiles) == 2 assert len(source_galaxy.mass_profiles) == 0 assert len(lens_galaxy.light_profiles) == 1 assert len(lens_galaxy.mass_profiles) == 1 assert source_galaxy.redshift == 1.5 assert lens_galaxy.redshift == 1.5
def test_set_prior_model(self): mapper = mm.ModelMapper() galaxy_prior = gp.GalaxyModel( variable_redshift=True, light_profile=light_profiles.EllipticalSersic, mass_profile=mass_profiles.EllipticalSersic) mapper.galaxy = galaxy_prior assert 16 == len(mapper.prior_tuples_ordered_by_id) galaxy_prior.light_profile = mm.PriorModel(light_profiles.LightProfile) assert 9 == len(mapper.prior_tuples_ordered_by_id)
def make_mapper(): return mm.ModelMapper()
from autofit.mapper import model_mapper from autofit.optimize import grid_search as gs from autolens.model.galaxy import galaxy_model as gm if __name__ == "__main__": mapper = model_mapper.ModelMapper() mapper.galaxy = gm.GalaxyModel(variable_redshift=True) assert mapper.prior_count == 1 grid_search = gs.GridSearch(model_mapper=mapper, number_of_steps=10) # GalaxyModel.redshift is a PriorModel; GalaxyModel.redshift.redshift is the underlying prior mappers = list( grid_search.model_mappers( grid_priors=[mapper.galaxy.redshift.redshift])) print([m.galaxy.redshift.redshift for m in mappers])