def test_target_is_dict(source_gaussian, target_gaussian, prior): source = af.CollectionPriorModel(collection=af.CollectionPriorModel( gaussian=source_gaussian)) target = af.CollectionPriorModel(collection=dict(gaussian=target_gaussian)) target.take_attributes(source) assert target.collection.gaussian.centre == prior
def make_gaussian_phase(n_components): if n_components == 1: return Phase( phase_name="phase_1_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian) ) elif n_components == 2: return Phase( phase_name="phase_2_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian, gaussian_1=profile_models.Gaussian) ) elif n_components == 3: return Phase( phase_name="phase_3_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian, gaussian_1=profile_models.Gaussian, gaussian_2=profile_models.Gaussian) ) elif n_components == 4: return Phase( phase_name="phase_4_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian, gaussian_1=profile_models.Gaussian, gaussian_2=profile_models.Gaussian, gaussian_3=profile_models.Gaussian) ) elif n_components == 5: return Phase( phase_name="phase_5_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian, gaussian_1=profile_models.Gaussian, gaussian_2=profile_models.Gaussian, gaussian_3=profile_models.Gaussian, gaussian_4=profile_models.Gaussian) )
def test_collection(): identifier = af.CollectionPriorModel( gaussian=af.PriorModel(Gaussian, centre=af.UniformPrior())).identifier assert identifier == af.CollectionPriorModel( gaussian=af.PriorModel(Gaussian, centre=af.UniformPrior())).identifier assert identifier != af.CollectionPriorModel(gaussian=af.PriorModel( Gaussian, centre=af.UniformPrior(upper_limit=0.5))).identifier
def test_tuple_in_collection(source_gaussian, target_gaussian, prior): source_gaussian.centre = (prior, 1.0) source = af.CollectionPriorModel(gaussian=source_gaussian) target = af.CollectionPriorModel(gaussian=target_gaussian) target.take_attributes(source) assert target.gaussian.centre == (prior, 1.0)
def test_tuple_in_instance_in_collection(target_gaussian, prior): # noinspection PyTypeChecker source_gaussian = af.Gaussian(centre=(prior, 1.0)) source = af.CollectionPriorModel(gaussian=source_gaussian) target = af.CollectionPriorModel(gaussian=target_gaussian) target.take_attributes(source) assert target.gaussian.centre == (prior, 1.0)
def test_instance(): identifier = af.CollectionPriorModel( gaussian=Gaussian() ).identifier assert identifier == af.CollectionPriorModel( gaussian=Gaussian() ).identifier assert identifier != af.CollectionPriorModel( gaussian=Gaussian( centre=0.5 ) ).identifier
def test_unlabelled_in_collection( source_gaussian, target_gaussian, prior ): target = af.CollectionPriorModel( [target_gaussian] ) source = af.CollectionPriorModel( [source_gaussian] ) target.take_attributes( source ) assert target[0].centre == prior
def test__extracted_fits_from_instance_and_line_ci(self, dataset_line_7, mask_1d_7_unmasked, traps_x1, ccd, clocker_1d): model = af.CollectionPriorModel( cti=af.Model(ac.CTI1D, traps=traps_x1, ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection), ) masked_line_ci = dataset_line_7.apply_mask(mask=mask_1d_7_unmasked) post_cti_data = clocker_1d.add_cti(data=masked_line_ci.pre_cti_data, trap_list=traps_x1, ccd=ccd) analysis = ac.AnalysisDatasetLine(dataset_line=masked_line_ci, clocker=clocker_1d) instance = model.instance_from_unit_vector([]) fit_analysis = analysis.fit_from_instance(instance=instance) fit = ac.FitDatasetLine(dataset_line=masked_line_ci, post_cti_data=post_cti_data) assert fit.dataset.data.shape == (7, ) assert fit_analysis.log_likelihood == pytest.approx(fit.log_likelihood)
def test__log_likelihood_via_analysis__matches_manual_fit( self, imaging_ci_7x7, pre_cti_data_7x7, traps_x1, ccd, parallel_clocker_2d): model = af.CollectionPriorModel( cti=af.Model(ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection), ) analysis = ac.AnalysisImagingCI(dataset_ci=imaging_ci_7x7, clocker=parallel_clocker_2d) instance = model.instance_from_unit_vector([]) log_likelihood_via_analysis = analysis.log_likelihood_function( instance=instance) post_cti_data = parallel_clocker_2d.add_cti( data=pre_cti_data_7x7.native, parallel_trap_list=traps_x1, parallel_ccd=ccd) fit = ac.ci.FitImagingCI(dataset=analysis.dataset_ci, post_cti_data=post_cti_data) assert fit.log_likelihood == log_likelihood_via_analysis
def test_list_prior_model(self, prior_model): list_prior_model = af.CollectionPriorModel([prior_model]) list_prior_model_copy = deepcopy(list_prior_model) assert list_prior_model == list_prior_model_copy list_prior_model[0].centre_0 = af.UniformPrior() assert list_prior_model != list_prior_model_copy
def test_prior_model_override(self): mock_components_1 = af.PriorModel( af.m.MockComponents, components_0=af.CollectionPriorModel( light=af.m.MockChildTuplex2()), components_1=af.CollectionPriorModel(mass=af.m.MockChildTuplex3), ) mock_components_2 = af.PriorModel( af.m.MockComponents, components_0=af.CollectionPriorModel(light=af.m.MockChildTuplex2), components_1=af.CollectionPriorModel(mass=af.m.MockChildTuplex3()), ) result = mock_components_1 + mock_components_2 assert result.components_1.mass == mock_components_1.components_1.mass assert result.components_0.light == mock_components_2.components_0.light
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)
def test_is_only_model(): collection = af.CollectionPriorModel(gaussian=af.PriorModel(af.Gaussian), gaussian_2=af.PriorModel(af.Gaussian)) assert collection.is_only_model(af.Gaussian) is True collection.other = af.PriorModel(af.m.MockClassx2) assert collection.is_only_model(af.Gaussian) is False
def test_no_free_parameters(): collection = af.CollectionPriorModel(gaussian=af.Model( af.Gaussian, centre=1.0, normalization=1.0, sigma=1.0, )) assert collection.prior_count == 0 assert collection.has_model(af.Gaussian) is False
def make_frozen_collection(): model = af.CollectionPriorModel( gaussian=af.PriorModel( af.Gaussian ) ) model.freeze() return model
def test_observables(): model = af.PriorModel(ts.Species, observables=af.CollectionPriorModel( one=ts.Observable, two=ts.Observable)) assert model.prior_count == 5 instance = model.instance_from_prior_medians() assert isinstance(instance.observables["one"], ts.Observable)
def test_prior_model_override(self): galaxy_1 = af.PriorModel( mock.MockComponents, components_0=af.CollectionPriorModel(light=mock_real.EllProfile()), components_1=af.CollectionPriorModel( mass=mock_real.EllMassProfile), ) galaxy_2 = af.PriorModel( mock.MockComponents, components_0=af.CollectionPriorModel(light=mock_real.EllProfile), components_1=af.CollectionPriorModel( mass=mock_real.EllMassProfile()), ) result = galaxy_1 + galaxy_2 assert result.components_1.mass == galaxy_1.components_1.mass assert result.components_0.light == galaxy_2.components_0.light
def test_missing_from_source( target_gaussian, prior ): target_gaussian.centre = prior target_gaussian.take_attributes( af.CollectionPriorModel() ) assert target_gaussian.centre == prior
def test_non_trivial_equality(self): mock_components = af.PriorModel( af.m.MockComponents, components_0=af.CollectionPriorModel( mock_cls_0=af.m.MockChildTuplex2), components_1=af.CollectionPriorModel( mock_cls_2=af.m.MockChildTuplex3), ) model_mapper = af.ModelMapper() model_mapper.mock_components = mock_components model_mapper_copy = deepcopy(model_mapper) assert model_mapper == model_mapper_copy model_mapper.mock_components.components_0.tup_0 = af.UniformPrior() assert model_mapper != model_mapper_copy
def test__parallel_and_serial_checks_raise_exception(self, imaging_ci_7x7): model = af.CollectionPriorModel(cti=af.Model( ac.CTI2D, parallel_traps=[ ac.TrapInstantCapture(density=1.1), ac.TrapInstantCapture(density=1.1), ], parallel_ccd=ac.CCDPhase(), )) analysis = ac.AnalysisImagingCI( dataset_ci=imaging_ci_7x7, clocker=None, settings_cti=ac.SettingsCTI2D(parallel_total_density_range=(1.0, 2.0)), ) instance = model.instance_from_prior_medians() with pytest.raises(exc.PriorException): analysis.log_likelihood_function(instance=instance) model = af.CollectionPriorModel(cti=af.Model( ac.CTI2D, serial_traps=[ ac.TrapInstantCapture(density=1.1), ac.TrapInstantCapture(density=1.1), ], serial_ccd=ac.CCDPhase(), )) analysis = ac.AnalysisImagingCI( dataset_ci=[imaging_ci_7x7], clocker=None, settings_cti=ac.SettingsCTI2D(serial_total_density_range=(1.0, 2.0)), ) instance = model.instance_from_prior_medians() with pytest.raises(exc.PriorException): analysis.log_likelihood_function(instance=instance)
def test_add_children(self): galaxy_1 = af.PriorModel( mock.MockComponents, components_0=af.CollectionPriorModel(light_1=mock_real.EllProfile), components_1=af.CollectionPriorModel( mass_1=mock_real.EllMassProfile), ) galaxy_2 = af.PriorModel( mock.MockComponents, components_0=af.CollectionPriorModel(light_2=mock_real.EllProfile), components_1=af.CollectionPriorModel( mass_2=mock_real.EllMassProfile), ) result = galaxy_1 + galaxy_2 assert result.components_0.light_1 == galaxy_1.components_0.light_1 assert result.components_0.light_2 == galaxy_2.components_0.light_2 assert result.components_1.mass_1 == galaxy_1.components_1.mass_1 assert result.components_1.mass_2 == galaxy_2.components_1.mass_2
def __init__(self, cls=Species, **kwargs): """ Prior model for a species in a matrix that has defined relationships with other species. Parameters ---------- cls The class of the species kwargs """ super().__init__(cls, **kwargs) self.interactions = af.CollectionPriorModel()
def test_parameter_name_distinction(self): mm = af.ModelMapper() mm.ls = af.CollectionPriorModel([ af.PriorModel(af.m.MockClassRelativeWidth), af.PriorModel(af.m.MockClassRelativeWidth), ]) assert mm.model_component_and_parameter_names == [ "ls_0_one", "ls_0_two", "ls_0_three", "ls_1_one", "ls_1_two", "ls_1_three", ]
def test_add_children(self): mock_components_1 = af.PriorModel( af.m.MockComponents, components_0=af.CollectionPriorModel( mock_cls_0=af.m.MockChildTuplex2), components_1=af.CollectionPriorModel( mock_cls_2=af.m.MockChildTuplex3), ) mock_components_2 = af.PriorModel( af.m.MockComponents, components_0=af.CollectionPriorModel( mock_cls_1=af.m.MockChildTuplex2), components_1=af.CollectionPriorModel( mock_cls_3=af.m.MockChildTuplex3), ) result = mock_components_1 + mock_components_2 assert result.components_0.mock_cls_0 == mock_components_1.components_0.mock_cls_0 assert result.components_0.mock_cls_1 == mock_components_2.components_0.mock_cls_1 assert result.components_1.mock_cls_2 == mock_components_1.components_1.mock_cls_2 assert result.components_1.mock_cls_3 == mock_components_2.components_1.mock_cls_3
def test__make_result__result_line_is_returned(self, dataset_line_7, pre_cti_data_7, traps_x1, ccd, clocker_1d): model = af.CollectionPriorModel( cti=af.Model(ac.CTI1D, traps=traps_x1, ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection), ) analysis = ac.AnalysisDatasetLine(dataset_line=dataset_line_7, clocker=clocker_1d) search = mock.MockSearch(name="test_search") result = search.fit(model=model, analysis=analysis) assert isinstance(result, ResultDatasetLine)
def make_pipeline(slam, settings): 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 = slam.path_prefix_from(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=af.CollectionPriorModel( 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_hyper_phase(setup_hyper=slam.setup_hyper, include_hyper_image_sky=True) return al.PipelineDataset(pipeline_name, path_prefix, None, phase1)
def test_keyword_arguments(self): prior_model = af.CollectionPriorModel(one=af.m.MockClassx2, two=af.m.MockClassx2(1, 2)) assert len(prior_model.direct_prior_model_tuples) == 1 assert len(prior_model) == 2 instance = prior_model.instance_for_arguments({ prior_model.one.one: 0.1, prior_model.one.two: 0.2 }) assert instance.one.one == 0.1 assert instance.one.two == 0.2 assert instance.two.one == 1 assert instance.two.two == 2
def test__make_result__result_imaging_is_returned(self, imaging_ci_7x7, pre_cti_data_7x7, traps_x1, ccd, parallel_clocker_2d): model = af.CollectionPriorModel( cti=af.Model(ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection), ) analysis = ac.AnalysisImagingCI(dataset_ci=imaging_ci_7x7, clocker=parallel_clocker_2d) search = mock.MockSearch(name="test_search") result = search.fit(model=model, analysis=analysis) assert isinstance(result, ResultImagingCI)
def test__masks_available_as_property( self, analysis_imaging_ci_7x7, samples_with_result, parallel_clocker_2d, traps_x1, ccd, ): model = af.CollectionPriorModel( cti=af.Model(ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd) ) result = res.ResultDataset( samples=samples_with_result, analysis=analysis_imaging_ci_7x7, model=model, search=None, ) assert (result.mask == np.full(fill_value=False, shape=(7, 7))).all()
def test__full_and_extracted_fits_from_instance_and_imaging_ci( self, imaging_ci_7x7, mask_2d_7x7_unmasked, traps_x1, ccd, parallel_clocker_2d): model = af.CollectionPriorModel( cti=af.Model(ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection), ) masked_imaging_ci = imaging_ci_7x7.apply_mask( mask=mask_2d_7x7_unmasked) masked_imaging_ci = masked_imaging_ci.apply_settings( settings=ac.ci.SettingsImagingCI(parallel_pixels=(0, 1))) post_cti_data = parallel_clocker_2d.add_cti( data=masked_imaging_ci.pre_cti_data, parallel_trap_list=traps_x1, parallel_ccd=ccd, ) analysis = ac.AnalysisImagingCI(dataset_ci=masked_imaging_ci, clocker=parallel_clocker_2d) instance = model.instance_from_unit_vector([]) fit_analysis = analysis.fit_from_instance(instance=instance) fit = ac.ci.FitImagingCI(dataset=masked_imaging_ci, post_cti_data=post_cti_data) assert fit.image.shape == (7, 1) assert fit_analysis.log_likelihood == pytest.approx(fit.log_likelihood) fit_full_analysis = analysis.fit_full_dataset_from_instance( instance=instance) fit = ac.ci.FitImagingCI(dataset=imaging_ci_7x7, post_cti_data=post_cti_data) assert fit.image.shape == (7, 7) assert fit_full_analysis.log_likelihood == pytest.approx( fit.log_likelihood)