Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Mar 26, 2024
2 parents 284a672 + 2f7c135 commit e7e5732
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 99 deletions.
5 changes: 0 additions & 5 deletions test_autolens/aggregator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ def make_model():

@pytest.fixture(name="samples")
def make_samples(model):
galaxy_0 = al.Galaxy(redshift=0.5, light=al.lp.Sersic(centre=(0.0, 1.0)))
galaxy_1 = al.Galaxy(redshift=1.0, light=al.lp.Sersic())

tracer = al.Tracer(galaxies=[galaxy_0, galaxy_1])

parameters = [model.prior_count * [1.0], model.prior_count * [10.0]]

Expand All @@ -84,6 +80,5 @@ def make_samples(model):
return al.m.MockSamples(
model=model,
sample_list=sample_list,
max_log_likelihood_instance=tracer,
prior_means=[1.0] * model.prior_count,
)
68 changes: 31 additions & 37 deletions test_autolens/analysis/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ def test__max_log_likelihood_tracer(
)
)

samples = al.m.MockSamples(
model=model,
max_log_likelihood_instance=tracer_x2_plane_7x7,
prior_means=[1.0] * model.prior_count,
)

search = al.m.MockSearch(name="test_search_2", samples=samples)
search = al.m.MockSearch(name="test_search_2")

result = search.fit(model=model, analysis=analysis_imaging_7x7)

Expand Down Expand Up @@ -58,9 +52,9 @@ def test__max_log_likelihood_positions_threshold(masked_imaging_7x7):
]
)

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis)
result = res.Result(samples_summary=samples_summary, analysis=analysis)

assert result.max_log_likelihood_positions_threshold == pytest.approx(
0.8309561230, 1.0e-4
Expand All @@ -76,9 +70,9 @@ def test__source_plane_light_profile_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_light_profile_centre.in_list == [(1.0, 2.0)]

Expand All @@ -94,9 +88,9 @@ def test__source_plane_light_profile_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source_0, source_1])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_light_profile_centre.in_list == [(1.0, 2.0)]

Expand All @@ -110,17 +104,17 @@ def test__source_plane_light_profile_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source_0, source_1])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_light_profile_centre.in_list == [(5.0, 6.0)]

tracer = al.Tracer(galaxies=[al.Galaxy(redshift=0.5)])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_light_profile_centre == None

Expand All @@ -137,9 +131,9 @@ def test__source_plane_inversion_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert (
result.source_plane_inversion_centre.in_list[0]
Expand All @@ -155,9 +149,9 @@ def test__source_plane_inversion_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_inversion_centre == None

Expand All @@ -166,9 +160,9 @@ def test__source_plane_inversion_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_inversion_centre == None

Expand All @@ -189,9 +183,9 @@ def test__source_plane_centre(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.source_plane_centre.in_list[0] == pytest.approx(
(-0.916666, -0.916666), 1.0e-4
Expand Down Expand Up @@ -220,9 +214,9 @@ def test__image_plane_multiple_image_positions(analysis_imaging_7x7):

tracer = al.Tracer(galaxies=[lens, source])

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

multiple_images = result.image_plane_multiple_image_positions

Expand All @@ -243,9 +237,9 @@ def test__image_plane_multiple_image_positions(analysis_imaging_7x7):
]
)

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.image_plane_multiple_image_positions.in_list[0][0] == pytest.approx(
1.0004, 1.0e-2
Expand All @@ -268,9 +262,9 @@ def test__positions_threshold_from(analysis_imaging_7x7):
]
)

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert result.positions_threshold_from() == pytest.approx(0.000973519, 1.0e-4)
assert result.positions_threshold_from(factor=5.0) == pytest.approx(
Expand All @@ -297,9 +291,9 @@ def test__positions_likelihood_from(analysis_imaging_7x7):
]
)

samples = al.m.MockSamples(max_log_likelihood_instance=tracer)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=tracer)

result = res.Result(samples=samples, analysis=analysis_imaging_7x7)
result = res.Result(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

positions_likelihood = result.positions_likelihood_from(
factor=0.1, minimum_threshold=0.2
Expand All @@ -320,7 +314,7 @@ def test__results_include_mask__available_as_property(
analysis_imaging_7x7, masked_imaging_7x7, samples_summary_with_result
):
result = res.ResultDataset(
samples=samples_summary_with_result,
samples_summary=samples_summary_with_result,
analysis=analysis_imaging_7x7,
)

Expand All @@ -331,7 +325,7 @@ def test__results_include_positions__available_as_property(
analysis_imaging_7x7, masked_imaging_7x7, samples_summary_with_result
):
result = res.ResultDataset(
samples=samples_summary_with_result,
samples_summary=samples_summary_with_result,
analysis=analysis_imaging_7x7,
)

Expand All @@ -346,7 +340,7 @@ def test__results_include_positions__available_as_property(
)

result = res.ResultDataset(
samples=samples_summary_with_result,
samples_summary=samples_summary_with_result,
analysis=analysis,
)

Expand All @@ -362,7 +356,7 @@ def test___image_dict(analysis_imaging_7x7):
instance.galaxies = galaxies

result = ResultImaging(
samples=al.m.MockSamples(max_log_likelihood_instance=instance),
samples_summary=al.m.MockSamplesSummary(max_log_likelihood_instance=instance),
analysis=analysis_imaging_7x7,
)

Expand Down
2 changes: 1 addition & 1 deletion test_autolens/config/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ profiling:
perform: true
repeats: 1
test:
check_likelihood_function: true # if True, when a search is resumed the likelihood of a previous sample is recalculated to ensure it is consistent with the previous run.
check_likelihood_function: false # if True, when a search is resumed the likelihood of a previous sample is recalculated to ensure it is consistent with the previous run.
check_preloads: false
exception_override: false
preloads_check_threshold: 1.0 # If the figure of merit of a fit with and without preloads is greater than this threshold, the check preload test fails and an exception raised for a model-fit.
Expand Down
14 changes: 3 additions & 11 deletions test_autolens/imaging/model/test_analysis_imaging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
from os import path
import pytest

Expand All @@ -15,17 +14,8 @@


def test__make_result__result_imaging_is_returned(masked_imaging_7x7):
model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(redshift=0.5)))

instance = model.instance_from_prior_medians()

samples = al.m.MockSamples(
model=model,
max_log_likelihood_instance=instance,
prior_means=[1.0] * model.prior_count
)

search = al.m.MockSearch(name="test_search", samples=samples)
model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(redshift=0.5)))

analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)

Expand All @@ -36,6 +26,8 @@ def modify_after_fit(

analysis.modify_after_fit = modify_after_fit

search = al.m.MockSearch(name="test_search")

result = search.fit(model=model, analysis=analysis)

assert isinstance(result, ResultImaging)
Expand Down
5 changes: 2 additions & 3 deletions test_autolens/imaging/model/test_result_imaging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
import pytest

import autofit as af
Expand All @@ -15,9 +14,9 @@ def test___linear_light_profiles_in_result(analysis_imaging_7x7):
instance = af.ModelInstance()
instance.galaxies = galaxies

samples = al.m.MockSamples(max_log_likelihood_instance=instance)
samples_summary = al.m.MockSamplesSummary(max_log_likelihood_instance=instance)

result = ResultImaging(samples=samples, analysis=analysis_imaging_7x7)
result = ResultImaging(samples_summary=samples_summary, analysis=analysis_imaging_7x7)

assert not isinstance(
result.max_log_likelihood_tracer.galaxies[0].bulge,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from os import path
import numpy as np
import pytest

import autofit as af
Expand All @@ -14,12 +13,6 @@
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(
Expand All @@ -29,6 +22,8 @@ def modify_after_fit(

analysis.modify_after_fit = modify_after_fit

search = al.m.MockSearch(name="test_search")

result = search.fit(model=model, analysis=analysis)

assert isinstance(result, ResultInterferometer)
Expand Down
68 changes: 33 additions & 35 deletions test_autolens/quantity/model/test_analysis_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,49 @@

directory = path.dirname(path.realpath(__file__))

def test__make_result__result_quantity_is_returned(
dataset_quantity_7x7_array_2d
):
model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(redshift=0.5)))

class TestAnalysisQuantity:
def test__make_result__result_quantity_is_returned(
self, dataset_quantity_7x7_array_2d
):
model = af.Collection(galaxies=af.Collection(galaxy_0=al.Galaxy(redshift=0.5)))
analysis = al.AnalysisQuantity(
dataset=dataset_quantity_7x7_array_2d, func_str="convergence_2d_from"
)

analysis = al.AnalysisQuantity(
dataset=dataset_quantity_7x7_array_2d, func_str="convergence_2d_from"
)
search = al.m.MockSearch(name="test_search")

search = al.m.MockSearch(name="test_search")
result = search.fit(model=model, analysis=analysis)

result = search.fit(model=model, analysis=analysis)
assert isinstance(result, ResultQuantity)

assert isinstance(result, ResultQuantity)
def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
dataset_quantity_7x7_array_2d
):
galaxy = al.Galaxy(redshift=0.5, light=al.mp.Isothermal(einstein_radius=1.0))

def test__figure_of_merit__matches_correct_fit_given_galaxy_profiles(
self, dataset_quantity_7x7_array_2d
):
galaxy = al.Galaxy(redshift=0.5, light=al.mp.Isothermal(einstein_radius=1.0))
model = af.Collection(galaxies=af.Collection(galaxy=galaxy))

model = af.Collection(galaxies=af.Collection(galaxy=galaxy))
analysis = al.AnalysisQuantity(
dataset=dataset_quantity_7x7_array_2d, func_str="convergence_2d_from"
)

analysis = al.AnalysisQuantity(
dataset=dataset_quantity_7x7_array_2d, func_str="convergence_2d_from"
)
instance = model.instance_from_unit_vector([])
fit_figure_of_merit = analysis.log_likelihood_function(instance=instance)

instance = model.instance_from_unit_vector([])
fit_figure_of_merit = analysis.log_likelihood_function(instance=instance)
tracer = analysis.tracer_via_instance_from(instance=instance)

tracer = analysis.tracer_via_instance_from(instance=instance)
fit = al.FitQuantity(
dataset=dataset_quantity_7x7_array_2d,
tracer=tracer,
func_str="convergence_2d_from",
)

fit = al.FitQuantity(
dataset=dataset_quantity_7x7_array_2d,
tracer=tracer,
func_str="convergence_2d_from",
)
assert fit.log_likelihood == fit_figure_of_merit

assert fit.log_likelihood == fit_figure_of_merit
fit = al.FitQuantity(
dataset=dataset_quantity_7x7_array_2d,
tracer=tracer,
func_str="potential_2d_from",
)

fit = al.FitQuantity(
dataset=dataset_quantity_7x7_array_2d,
tracer=tracer,
func_str="potential_2d_from",
)

assert fit.log_likelihood != fit_figure_of_merit
assert fit.log_likelihood != fit_figure_of_merit

0 comments on commit e7e5732

Please sign in to comment.