def test__figure_of_merit__includes_hyper_image_and_noise__matches_fit( self, interferometer_7): hyper_background_noise = al.hyper_data.HyperBackgroundNoise( noise_scale=1.0) lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllSersic(intensity=0.1)) model = af.Collection( galaxies=af.Collection(lens=lens_galaxy), hyper_background_noise=hyper_background_noise, ) analysis = al.AnalysisInterferometer(dataset=interferometer_7) instance = model.instance_from_unit_vector([]) analysis_log_likelihood = analysis.log_likelihood_function( instance=instance) tracer = analysis.tracer_for_instance(instance=instance) fit = al.FitInterferometer( interferometer=interferometer_7, tracer=tracer, hyper_background_noise=hyper_background_noise, ) assert fit.log_likelihood == analysis_log_likelihood
def test__fit_interferometer_all_above_weight_gen(self, interferometer_7, samples, model): path_prefix = "aggregator_fit_interferometer_gen" database_file = path.join(conf.instance.output_path, "fit_interferometer.sqlite") result_path = path.join(conf.instance.output_path, path_prefix) clean(database_file=database_file, result_path=result_path) search = mock.MockSearch(samples=samples, result=mock.MockResult(model=model, samples=samples)) search.paths = af.DirectoryPaths(path_prefix=path_prefix) analysis = al.AnalysisInterferometer(dataset=interferometer_7) search.fit(model=model, analysis=analysis) agg = af.Aggregator.from_database(filename=database_file) agg.add_directory(directory=result_path) fit_interferometer_agg = al.agg.FitInterferometerAgg(aggregator=agg) fit_interferometer_pdf_gen = fit_interferometer_agg.all_above_weight_gen( minimum_weight=-1.0) i = 0 for fit_interferometer_gen in fit_interferometer_pdf_gen: for fit_interferometer in fit_interferometer_gen: i += 1 if i == 1: assert fit_interferometer.tracer.galaxies[ 0].redshift == 0.5 assert fit_interferometer.tracer.galaxies[ 0].light.centre == ( 1.0, 1.0, ) assert fit_interferometer.tracer.galaxies[ 1].redshift == 1.0 if i == 2: assert fit_interferometer.tracer.galaxies[ 0].redshift == 0.5 assert fit_interferometer.tracer.galaxies[ 0].light.centre == ( 10.0, 10.0, ) assert fit_interferometer.tracer.galaxies[ 1].redshift == 1.0 assert i == 2 clean(database_file=database_file, result_path=result_path)
def test__stochastic_log_likelihoods_for_instance(self, interferometer_7): lens_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) lens_hyper_image[4] = 10.0 source_hyper_image = al.Array2D.ones(shape_native=(3, 3), pixel_scales=0.1) source_hyper_image[4] = 10.0 hyper_model_image = al.Array2D.full(fill_value=0.5, shape_native=(3, 3), pixel_scales=0.1) hyper_galaxy_image_path_dict = { ("galaxies", "lens"): lens_hyper_image, ("galaxies", "source"): source_hyper_image, } result = mock.MockResult( hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict, hyper_model_image=hyper_model_image, ) analysis = al.AnalysisInterferometer( dataset=interferometer_7, settings_lens=al.SettingsLens(stochastic_samples=2), hyper_dataset_result=result, ) galaxies = af.ModelInstance() galaxies.source = al.Galaxy( redshift=1.0, pixelization=al.pix.VoronoiBrightnessImage(pixels=5), regularization=al.reg.Constant(), ) instance = af.ModelInstance() instance.galaxies = galaxies log_evidences = analysis.stochastic_log_likelihoods_for_instance( instance=instance) assert len(log_evidences) == 2 assert log_evidences[0] != log_evidences[1] galaxies.source = al.Galaxy( redshift=1.0, pixelization=al.pix.DelaunayBrightnessImage(pixels=5), regularization=al.reg.Constant(), ) instance = af.ModelInstance() instance.galaxies = galaxies log_evidences = analysis.stochastic_log_likelihoods_for_instance( instance=instance) assert len(log_evidences) == 2 assert log_evidences[0] != log_evidences[1]
def test__positions__likelihood_overwrite__changes_likelihood( interferometer_7, mask_2d_7x7): lens = al.Galaxy(redshift=0.5, mass=al.mp.SphIsothermal()) source = al.Galaxy(redshift=1.0, light=al.lp.SphSersic()) model = af.Collection(galaxies=af.Collection(lens=lens, source=source)) analysis = al.AnalysisInterferometer(dataset=interferometer_7) instance = model.instance_from_unit_vector([]) analysis_log_likelihood = analysis.log_likelihood_function( instance=instance) tracer = analysis.tracer_via_instance_from(instance=instance) fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) assert fit.log_likelihood == analysis_log_likelihood assert analysis_log_likelihood == pytest.approx(-127914.36273, 1.0e-4) positions_likelihood = al.PositionsLHPenalty(positions=al.Grid2DIrregular([ (1.0, 100.0), (200.0, 2.0) ]), threshold=0.01) analysis = al.AnalysisInterferometer( dataset=interferometer_7, positions_likelihood=positions_likelihood) analysis_log_likelihood = analysis.log_likelihood_function( instance=instance) log_likelihood_penalty_base = positions_likelihood.log_likelihood_penalty_base_from( dataset=interferometer_7) log_likelihood_penalty = positions_likelihood.log_likelihood_penalty_from( tracer=tracer) assert analysis_log_likelihood == pytest.approx( log_likelihood_penalty_base - log_likelihood_penalty, 1.0e-4) assert analysis_log_likelihood == pytest.approx(-22048700567.590656, 1.0e-4)
def test__sets_up_hyper_galaxy_visibiltiies__from_results( self, interferometer_7): hyper_galaxy_image_path_dict = { ("galaxies", "lens"): al.Array2D.ones(shape_native=(3, 3), pixel_scales=1.0), ("galaxies", "source"): al.Array2D.full(fill_value=2.0, shape_native=(3, 3), pixel_scales=1.0), } hyper_galaxy_visibilities_path_dict = { ("galaxies", "lens"): al.Visibilities.full(fill_value=4.0, shape_slim=(7, )), ("galaxies", "source"): al.Visibilities.full(fill_value=5.0, shape_slim=(7, )), } result = mock.MockResult( hyper_galaxy_image_path_dict=hyper_galaxy_image_path_dict, hyper_model_image=al.Array2D.full(fill_value=3.0, shape_native=(3, 3), pixel_scales=1.0), hyper_galaxy_visibilities_path_dict= hyper_galaxy_visibilities_path_dict, hyper_model_visibilities=al.Visibilities.full(fill_value=6.0, shape_slim=(7, )), ) analysis = al.AnalysisInterferometer(dataset=interferometer_7, hyper_result=result) analysis.set_hyper_dataset(result=result) assert (analysis.hyper_galaxy_image_path_dict[( "galaxies", "lens")].native == np.ones((3, 3))).all() assert (analysis.hyper_galaxy_image_path_dict[( "galaxies", "source")].native == 2.0 * np.ones((3, 3))).all() assert (analysis.hyper_model_image.native == 3.0 * np.ones( (3, 3))).all() assert (analysis.hyper_galaxy_visibilities_path_dict[( "galaxies", "lens")] == (4.0 + 4.0j) * np.ones((7, ))).all() assert (analysis.hyper_galaxy_visibilities_path_dict[( "galaxies", "source")] == (5.0 + 5.0j) * np.ones((7, ))).all() assert (analysis.hyper_model_visibilities == (6.0 + 6.0j) * np.ones( (7, ))).all()
def test__make_result__result_interferometer_is_returned( self, interferometer_7): model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy( redshift=0.5))) search = mock.MockSearch(name="test_search") analysis = al.AnalysisInterferometer(dataset=interferometer_7) result = search.fit(model=model, analysis=analysis) assert isinstance(result, res.ResultInterferometer)
def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles( interferometer_7): lens_galaxy = al.Galaxy(redshift=0.5, light=al.lp.EllSersic(intensity=0.1)) model = af.Collection(galaxies=af.Collection(lens=lens_galaxy)) analysis = al.AnalysisInterferometer(dataset=interferometer_7) instance = model.instance_from_unit_vector([]) analysis_log_likelihood = analysis.log_likelihood_function( instance=instance) tracer = analysis.tracer_via_instance_from(instance=instance) fit = al.FitInterferometer(dataset=interferometer_7, tracer=tracer) assert fit.log_likelihood == analysis_log_likelihood
def test__positions_do_not_trace_within_threshold__raises_exception( self, interferometer_7, mask_2d_7x7): model = af.Collection(galaxies=af.Collection( lens=al.Galaxy(redshift=0.5, mass=al.mp.SphIsothermal()), source=al.Galaxy(redshift=1.0), )) analysis = al.AnalysisInterferometer( dataset=interferometer_7, positions=al.Grid2DIrregular([(1.0, 100.0), (200.0, 2.0)]), settings_lens=al.SettingsLens(positions_threshold=0.01), ) instance = model.instance_from_unit_vector([]) with pytest.raises(exc.RayTracingException): analysis.log_likelihood_function(instance=instance)
def test__fit_interferometer_randomly_drawn_via_pdf_gen_from( interferometer_7, samples, model ): path_prefix = "aggregator_fit_interferometer_gen" database_file = path.join(conf.instance.output_path, "fit_interferometer.sqlite") result_path = path.join(conf.instance.output_path, path_prefix) clean(database_file=database_file, result_path=result_path) result = al.m.MockResult(model=model, samples=samples) search = al.m.MockSearch(samples=samples, result=result) search.paths = af.DirectoryPaths(path_prefix=path_prefix) analysis = al.AnalysisInterferometer(dataset=interferometer_7) search.fit(model=model, analysis=analysis) agg = af.Aggregator.from_database(filename=database_file) agg.add_directory(directory=result_path) fit_interferometer_agg = al.agg.FitInterferometerAgg(aggregator=agg) fit_interferometer_pdf_gen = fit_interferometer_agg.randomly_drawn_via_pdf_gen_from( total_samples=2 ) i = 0 for fit_interferometer_gen in fit_interferometer_pdf_gen: for fit_interferometer in fit_interferometer_gen: i += 1 assert fit_interferometer.tracer.galaxies[0].redshift == 0.5 assert fit_interferometer.tracer.galaxies[0].light.centre == (10.0, 10.0) assert fit_interferometer.tracer.galaxies[1].redshift == 1.0 assert i == 2 clean(database_file=database_file, result_path=result_path)
def test__make_result__result_interferometer_is_returned(interferometer_7): model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy( redshift=0.5))) instance = model.instance_from_prior_medians() samples = al.m.MockSamples(max_log_likelihood_instance=instance) search = al.m.MockSearch(name="test_search", samples=samples) analysis = al.AnalysisInterferometer(dataset=interferometer_7) def modify_after_fit(paths: af.DirectoryPaths, model: af.AbstractPriorModel, result: af.Result): pass analysis.modify_after_fit = modify_after_fit result = search.fit(model=model, analysis=analysis) assert isinstance(result, ResultInterferometer)
""" __SOURCE PARAMETRIC PIPELINE (no lens light)__ The SOURCE PARAMETRIC PIPELINE (no lens light) uses one search to initialize a robust model for the source galaxy's light, which in this example: - Uses a parametric `EllSersic` bulge for the source's light (omitting a disk / envelope). - Uses an `EllIsothermal` model for the lens's total mass distribution with an `ExternalShear`. __Settings__: - Mass Centre: Fix the mass profile centre to (0.0, 0.0) (this assumption will be relaxed in the SOURCE INVERSION PIPELINE). """ analysis = al.AnalysisInterferometer(dataset=interferometer) source_parametric_results = slam.source_parametric.no_lens_light( settings_autofit=settings_autofit, setup_hyper=setup_hyper, analysis=analysis, mass=af.Model(al.mp.EllIsothermal), shear=af.Model(al.mp.ExternalShear), source_bulge=af.Model(al.lp.EllSersic), mass_centre=(0.0, 0.0), redshift_lens=0.5, redshift_source=1.0, ) """ __SOURCE INVERSION PIPELINE (no lens light)__
) """ __SOURCE PARAMETRIC PIPELINE (no lens light)__ The SOURCE PARAMETRIC PIPELINE (no lens light) uses one search to initialize a robust model for the source galaxy's light, which in this example: - Uses a parametric `EllSersic` bulge for the source's light (omitting a disk / envelope). - Uses an `EllIsothermal` model for the lens's total mass distribution with an `ExternalShear`. __Settings__: - Mass Centre: Fix the mass profile centre to (0.0, 0.0) (this assumption will be relaxed in the MASS TOTAL PIPELINE). """ analysis = al.AnalysisInterferometer(dataset=interferometer) source_parametric_results = slam.source_parametric.no_lens_light( settings_autofit=settings_autofit, analysis=analysis, setup_hyper=setup_hyper, mass=af.Model(al.mp.EllIsothermal), shear=af.Model(al.mp.ExternalShear), source_bulge=af.Model(al.lp.EllSersic), mass_centre=(0.0, 0.0), redshift_lens=0.5, redshift_source=1.0, ) """ __MASS TOTAL PIPELINE (no lens light)__
For interferometer model-fits, we include a `SettingsInversion` object which describes how the linear algebra calculations required to use an `Inversion` are performed. One of two different approaches can be used: - **Matrices:** Use a numerically more accurate matrix formalism to perform the linear algebra. For datasets of < 100 0000 visibilities this approach is computationally feasible, and if your dataset is this small we we recommend that you use this option (by setting `use_linear_operators=False`. However, larger visibility datasets these matrices require excessive amounts of memory (> 16 GB) to store, making this approach unfeasible. - **Linear Operators (default)**: These are slightly less accurate, but do not require excessive amounts of memory to store the linear algebra calculations. For any dataset with > 1 million visibilities this is the only viable approach to perform lens modeling efficiently. """ settings_inversion = al.SettingsInversion(use_linear_operators=True) analysis = al.AnalysisInterferometer(dataset=interferometer, settings_inversion=settings_inversion) """ __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 output folder for live outputs of the results of the fit, including on-the-fly visualization of the best fit model! """ result = search.fit(model=model, analysis=analysis) """ __Result__ The search returns a result object, which includes:
""" __Search + Analysis + Model Fit (Search 1)__ We now create the non-linear search, analysis and perform the model-fit using this model. You may wish to inspect the results of the search 1 model-fit to ensure a fast non-linear search has been provided that provides a reasonably accurate lens model. """ search = af.DynestyStatic( path_prefix=path.join("interferometer", "chaining", "parametric_to_inversion"), name="search[1]__parametric", unique_tag=dataset_name, nlive=50, ) analysis = al.AnalysisInterferometer(dataset=interferometer) 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, where: - The lens galaxy's total mass distribution is again an `EllIsothermal` and `ExternalShear` [7 parameters]. - The source-galaxy's light uses a `DelaunayMagnification` pixelization [2 parameters]. - This pixelization is regularized using a `Constant` scheme which smooths every source pixel equally [1 parameter]. The number of free parameters and therefore the dimensionality of non-linear parameter space is N=10. The priors of the majority of the `EllIsothermal` and `ExternalShear` initialized using the results of search 1.
def make_analysis_interferometer_7(): return al.AnalysisInterferometer(dataset=make_interferometer_7())