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__fits_to_extracted_and_full_datasets_available( self, imaging_ci_7x7, mask_2d_7x7_unmasked, parallel_clocker_2d, samples_with_result, ): 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))) analysis = ac.AnalysisImagingCI(dataset_ci=masked_imaging_ci, clocker=parallel_clocker_2d) result = ResultImagingCI(samples=samples_with_result, analysis=analysis, model=None, search=None) assert (result.max_log_likelihood_fit.mask == np.full( fill_value=False, shape=(7, 1))).all() assert (result.max_log_likelihood_full_fit.mask == np.full( fill_value=False, shape=(7, 7))).all()
def test__noise_scaling_map_list_is_setup_correctly( self, imaging_ci_7x7, mask_2d_7x7_unmasked, layout_ci_7x7, parallel_clocker_2d, samples_with_result, ): imaging_ci_7x7.cosmic_ray_map = None masked_imaging_ci_7x7 = imaging_ci_7x7.apply_mask( mask=mask_2d_7x7_unmasked) analysis = ac.AnalysisImagingCI(dataset_ci=masked_imaging_ci_7x7, clocker=parallel_clocker_2d) result = ResultImagingCI(samples=samples_with_result, analysis=analysis, model=None, search=None) assert (result.noise_scaling_map_list[0] == result.noise_scaling_map_of_regions_ci).all() assert (result.noise_scaling_map_list[1] == result.noise_scaling_map_of_parallel_epers).all() assert (result.noise_scaling_map_list[2] == result.noise_scaling_map_of_serial_trails).all() assert (result.noise_scaling_map_list[3] == result.noise_scaling_map_of_serial_overscan_no_trails).all()
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__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__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)
def test__extracted_fits_from_instance_and_imaging_ci__include_noise_scaling( self, imaging_ci_7x7, mask_2d_7x7_unmasked, traps_x1, ccd, parallel_clocker_2d, layout_ci_7x7, ): model = af.CollectionPriorModel( cti=af.Model(ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd), hyper_noise=af.Model(ac.ci.HyperCINoiseCollection, regions_ci=ac.ci.HyperCINoiseScalar), ) noise_scaling_map_list_list_of_regions_ci = [ ac.Array2D.ones(shape_native=(7, 7), pixel_scales=1.0) ] imaging_ci_7x7.noise_scaling_map_list = [ noise_scaling_map_list_list_of_regions_ci[0] ] 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))) analysis = ac.AnalysisImagingCI(dataset_ci=masked_imaging_ci, clocker=parallel_clocker_2d) instance = model.instance_from_prior_medians() fit_analysis = analysis.fit_from_instance(instance=instance, hyper_noise_scale=True) post_cti_data = parallel_clocker_2d.add_cti( data=masked_imaging_ci.pre_cti_data, parallel_trap_list=traps_x1, parallel_ccd=ccd, ) fit = ac.ci.FitImagingCI( dataset=masked_imaging_ci, post_cti_data=post_cti_data, hyper_noise_scalars=[instance.hyper_noise.regions_ci], ) assert fit.image.shape == (7, 1) assert fit.log_likelihood == pytest.approx(fit_analysis.log_likelihood, 1.0e-4) fit = ac.ci.FitImagingCI( dataset=masked_imaging_ci, post_cti_data=post_cti_data, hyper_noise_scalars=[ac.ci.HyperCINoiseScalar(scale_factor=0.0)], ) assert fit.log_likelihood != pytest.approx(fit_analysis.log_likelihood, 1.0e-4) fit_full_analysis = analysis.fit_full_dataset_from_instance( instance=instance, hyper_noise_scale=True) masked_imaging_ci = imaging_ci_7x7.apply_mask( mask=mask_2d_7x7_unmasked) fit = ac.ci.FitImagingCI( dataset=masked_imaging_ci, post_cti_data=post_cti_data, hyper_noise_scalars=[instance.hyper_noise.regions_ci], ) assert fit.image.shape == (7, 7) assert fit.log_likelihood == pytest.approx( fit_full_analysis.log_likelihood, 1.0e-4) fit = ac.ci.FitImagingCI( dataset=masked_imaging_ci, post_cti_data=post_cti_data, hyper_noise_scalars=[ac.ci.HyperCINoiseScalar(scale_factor=0.0)], ) assert fit.log_likelihood != pytest.approx( fit_full_analysis.log_likelihood, 1.0e-4)
imaging_ci_trimmed_list = [ imaging_ci.apply_mask(mask=mask_ci) for imaging_ci in imaging_ci_list ] imaging_ci_trimmed_list = [ imaging_ci.apply_settings(settings=ac.ci.SettingsImagingCI(parallel_columns=(0, 1))) for imaging_ci in imaging_ci_trimmed_list ] """ __Analysis__ The `AnalysisImagingCI` object defines the `log_likelihood_function` used by the non-linear search to fit the model to the `ImagingCI`dataset. """ analysis = ac.AnalysisImagingCI(dataset_ci=imaging_ci_trimmed_list, clocker=clocker) analysis_list = [ ac.AnalysisImagingCI(dataset_ci=imaging_ci, clocker=clocker) for imaging_ci in imaging_ci_trimmed_list ] analysis = analysis_list[0] + analysis_list[1] + analysis_list[2] + analysis_list[3] """ __Model-Fit__ We can now begin the model-fit by passing the model and analysis object to the search, which performs a non-linear search to find which models fit the data with the highest likelihood. Checkout the folder `autocti_workspace/output/imaging_ci/parallel[x2]` for live outputs of the results of the fit, including on-the-fly visualization of the best fit model!
def test__noise_scaling_map_list_is_list_of_result__are_correct( self, imaging_ci_7x7, mask_2d_7x7_unmasked, parallel_clocker_2d, layout_ci_7x7, samples_with_result, traps_x1, ccd, ): noise_scaling_map_list_list_of_regions_ci = [ ac.Array2D.ones(shape_native=(7, 7), pixel_scales=1.0) ] noise_scaling_map_list_list_of_parallel_epers = [ ac.Array2D.full(fill_value=2.0, shape_native=(7, 7), pixel_scales=1.0) ] noise_scaling_map_list_list_of_serial_trails = [ ac.Array2D.full(fill_value=3.0, shape_native=(7, 7), pixel_scales=1.0) ] noise_scaling_map_list_list_of_serial_overscan_no_trails = [ ac.Array2D.full(fill_value=4.0, shape_native=(7, 7), pixel_scales=1.0) ] imaging_ci_7x7.noise_scaling_map_list = [ noise_scaling_map_list_list_of_regions_ci[0], noise_scaling_map_list_list_of_parallel_epers[0], noise_scaling_map_list_list_of_serial_trails[0], noise_scaling_map_list_list_of_serial_overscan_no_trails[0], ] masked_imaging_ci_7x7 = imaging_ci_7x7.apply_mask( mask=mask_2d_7x7_unmasked) analysis = ac.AnalysisImagingCI(dataset_ci=masked_imaging_ci_7x7, clocker=parallel_clocker_2d) fit_analysis = analysis.fit_from_instance( instance=samples_with_result.max_log_likelihood_instance) result = ResultImagingCI(samples=samples_with_result, analysis=analysis, model=None, search=None) assert result.noise_scaling_map_of_regions_ci == pytest.approx( fit_analysis.chi_squared_map_of_regions_ci, 1.0e-2) assert result.noise_scaling_map_of_parallel_epers == pytest.approx( fit_analysis.chi_squared_map_of_parallel_epers, 1.0e-2) assert result.noise_scaling_map_of_serial_trails == pytest.approx( fit_analysis.chi_squared_map_of_serial_trails, 1.0e-2) assert result.noise_scaling_map_of_serial_overscan_no_trails == pytest.approx( fit_analysis.chi_squared_map_of_serial_overscan_no_trails, 1.0e-2) assert result.noise_scaling_map_of_regions_ci[1, 1] == pytest.approx( 18.16, 1.0e-1) assert result.noise_scaling_map_of_parallel_epers[1, 1] == pytest.approx( 0.0, 1.0e-4) assert result.noise_scaling_map_of_serial_trails[1, 1] == pytest.approx( 0.0, 1.0e-4) assert result.noise_scaling_map_of_serial_overscan_no_trails[ 1, 1] == pytest.approx(0.0, 1.0e-4) model = af.CollectionPriorModel( cti=af.Model( ac.CTI2D, parallel_traps=traps_x1, parallel_ccd=ccd, serial_traps=traps_x1, serial_ccd=ccd, ), hyper_noise=af.Model( ac.ci.HyperCINoiseCollection, regions_ci=ac.ci.HyperCINoiseScalar(scale_factor=1.0), parallel_epers=ac.ci.HyperCINoiseScalar(scale_factor=1.0), serial_trails=ac.ci.HyperCINoiseScalar(scale_factor=1.0), serial_overscan_no_trails=ac.ci.HyperCINoiseScalar( scale_factor=1.0), ), ) instance = model.instance_from_prior_medians() fit_analysis = analysis.fit_from_instance(instance=instance) assert result.noise_scaling_map_of_regions_ci != pytest.approx( fit_analysis.chi_squared_map_of_regions_ci, 1.0e-2) assert result.noise_scaling_map_of_parallel_epers != pytest.approx( fit_analysis.chi_squared_map_of_parallel_epers, 1.0e-2) assert result.noise_scaling_map_of_serial_trails != pytest.approx( fit_analysis.chi_squared_map_of_serial_trails, 1.0e-2) assert result.noise_scaling_map_of_serial_overscan_no_trails != pytest.approx( fit_analysis.chi_squared_map_of_serial_overscan_no_trails, 1.0e-2)
""" __Search + Analysis + Model-Fit (Search 1)__ To reduce run-times, we trim the `ImagingCI` data from the high resolution data (e.g. 2000 columns) to just 50 columns to speed up the model-fit at the expense of inferring larger errors on the CTI model. """ search = af.DynestyStatic(path_prefix=path_prefix, name="search[1]_parallel[x1]", nlive=50) imaging_ci_trimmed_list = [ imaging_ci.apply_settings(settings=ac.ci.SettingsImagingCI( parallel_columns=(0, 50))) for imaging_ci in imaging_ci_list ] analysis = ac.AnalysisImagingCI(dataset_ci=imaging_ci_trimmed_list, clocker=parallel_clocker) result_1 = search.fit(model=model, analysis=analysis) """ __Model (Search 2)__ We use the results of search 1 to create the lens model fitted in search 2, with: - Two or more parallel `TrapInstantCapture`'s species [4+ parameters: prior on density initialized from search 1]. - A simple `CCD` volume filling parametrization with fixed notch depth and capacity [1 parameter: priors initialized from search 1]. The number of free parameters and therefore the dimensionality of non-linear parameter space is N=5 or more. """ parallel_trap_0 = af.Model(ac.TrapInstantCapture)