Skip to content

Commit

Permalink
Merge pull request #262 from Jammy2211/feature/keep_the_noise_down
Browse files Browse the repository at this point in the history
Feature/keep the noise down
  • Loading branch information
Jammy2211 authored Apr 8, 2024
2 parents d6df5d7 + 4f7eda2 commit 3d3cd6f
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 33 deletions.
3 changes: 2 additions & 1 deletion autolens/analysis/analysis/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

logger.setLevel(level="INFO")


class AnalysisDataset(AgAnalysisDataset, AnalysisLens):
def __init__(
self,
Expand Down Expand Up @@ -192,4 +193,4 @@ def save_results(self, paths: af.DirectoryPaths, result: ResultDataset):
object_dict=to_dict(
result.max_log_likelihood_fit.tracer_to_inversion.image_plane_mesh_grid_pg_list
),
)
)
1 change: 1 addition & 0 deletions autolens/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def make_analysis_interferometer_7():
analysis._adapt_images = make_adapt_images_7x7()
return analysis


def make_analysis_point_x2():
return al.AnalysisPoint(
point_dict=make_point_dict(),
Expand Down
2 changes: 1 addition & 1 deletion autolens/interferometer/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def make_result(
paths=paths,
samples=samples,
analysis=self,
search_internal=search_internal
search_internal=search_internal,
)

def save_attributes(self, paths: af.DirectoryPaths):
Expand Down
4 changes: 2 additions & 2 deletions autolens/lens/tracer_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ def grid_2d_at_redshift_from(
return traced_grid_list[plane_index_with_redshift[0]]

for plane_index, plane_redshift in enumerate(plane_redshifts):
if redshift < plane_redshift:
plane_index_insert = plane_index
if redshift > plane_redshift:
plane_index_insert = plane_index + 1

planes.insert(plane_index_insert, [ag.Galaxy(redshift=redshift)])

Expand Down
2 changes: 1 addition & 1 deletion autolens/quantity/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ def make_result(
paths=paths,
samples=samples,
search_internal=search_internal,
analysis=self
analysis=self,
)
1 change: 0 additions & 1 deletion test_autolens/aggregator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def make_model():

@pytest.fixture(name="samples")
def make_samples(model):

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

sample_list = Sample.from_lists(
Expand Down
2 changes: 1 addition & 1 deletion test_autolens/analysis/analysis/test_analysis_lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ def test__tracer_for_instance__subhalo_redshift_rescale_used(analysis_imaging_7x
instance = model.instance_from_unit_vector([])
tracer = analysis_imaging_7x7.tracer_via_instance_from(instance=instance)

assert tracer.galaxies[1].mass.centre == pytest.approx((-0.19959, -0.39919), 1.0e-4)
assert tracer.galaxies[1].mass.centre == pytest.approx((-0.19959, -0.39919), 1.0e-4)
20 changes: 15 additions & 5 deletions test_autolens/analysis/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ def test__source_plane_inversion_centre(analysis_imaging_7x7):

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

result = ResultImaging(samples_summary=samples_summary, 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 @@ -151,7 +153,9 @@ def test__source_plane_inversion_centre(analysis_imaging_7x7):

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

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

assert result.source_plane_inversion_centre == None

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

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

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

assert result.source_plane_inversion_centre == None

Expand All @@ -185,7 +191,9 @@ def test__source_plane_centre(analysis_imaging_7x7):

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

result = ResultImaging(samples_summary=samples_summary, 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 @@ -216,7 +224,9 @@ def test__image_plane_multiple_image_positions(analysis_imaging_7x7):

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

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

multiple_images = result.image_plane_multiple_image_positions

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
63 changes: 63 additions & 0 deletions test_autolens/config/output.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Determines whether files saved by the search are output to the hard-disk. This is true both when saving to the
# directory structure and when saving to database.

# Files can be listed name: bool where the name is the name of the file without a suffix (e.g. model not model.json)
# and bool is true or false.

# If a given file is not listed then the default value is used.

default: true # If true then files which are not explicitly listed here are output anyway. If false then they are not.

### Samples ###

# The `samples.csv`file contains every sampled value of every free parameter with its log likelihood and weight.

# This file is often large, therefore disabling it can significantly reduce hard-disk space use.

# `samples.csv` is used to perform marginalization, infer model parameter errors and do other analysis of the search
# chains. Even if output of `samples.csv` is disabled, these tasks are still performed by the fit and output to
# the `samples_summary.json` file. However, without a `samples.csv` file these types of tasks cannot be performed
# after the fit is complete, for example via the database.

samples: true

# The `samples.csv` file contains every accepted sampled value of every free parameter with its log likelihood and
# weight. For certain searches, the majority of samples have a very low weight, which has no numerical impact on the
# results of the model-fit. However, these samples are still output to the `samples.csv` file, taking up hard-disk space
# and slowing down analysis of the samples (e.g. via the database).

# The `samples_weight_threshold` below specifies the threshold value of the weight such that samples with a weight
# below this value are not output to the `samples.csv` file. This can be used to reduce the size of the `samples.csv`
# file and speed up analysis of the samples.

# Note that for many searches (e.g. MCMC) all samples have equal weight, and thus this threshold has no impact and
# there is no simple way to save hard-disk space. However, for nested sampling, the majority of samples have a very
# low weight and this threshold can be used to save hard-disk space.

# Set value to empty (e.g. delete 1.0e-10 below) to disable this feature.

samples_weight_threshold: 1.0e-10

### Search Internal ###

# The search internal folder which contains a saved state of the non-linear search, as a .pickle or .dill file.

# If the entry below is false, the folder is still output during the model-fit, as it is required to resume the fit
# from where it left off. Therefore, settings `false` below does not impact model-fitting checkpointing and resumption.
# Instead, the search internal folder is deleted once the fit is completed.

# The search internal folder file is often large, therefore deleting it after a fit is complete can significantly
# reduce hard-disk space use.

# The search internal representation (e.g. what you can load from the output .pickle file) may have additional
# quantities specific to the non-linear search that you are interested in inspecting. Deleting the folder means this
# information is list.

search_internal: false

# Other Files:

covariance: false # `covariance.csv`: The [free parameters x free parameters] covariance matrix.
data: true # `data.json`: The value of every data point in the data.
noise_map: true # `noise_map.json`: The value of every RMS noise map value.

7 changes: 0 additions & 7 deletions test_autolens/imaging/model/test_analysis_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ def test__make_result__result_imaging_is_returned(masked_imaging_7x7):

analysis = al.AnalysisImaging(dataset=masked_imaging_7x7)

def modify_after_fit(
paths: af.DirectoryPaths, model: af.AbstractPriorModel, result: af.Result
):
pass

analysis.modify_after_fit = modify_after_fit

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

result = search.fit(model=model, analysis=analysis)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ def test__make_result__result_interferometer_is_returned(interferometer_7):

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

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

result = search.fit(model=model, analysis=analysis)
Expand Down
29 changes: 27 additions & 2 deletions test_autolens/lens/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,38 @@ def test__grid_2d_at_redshift_from(sub_grid_2d_7x7):

tracer = al.Tracer(galaxies=galaxies, cosmology=al.cosmo.Planck15())

galaxies_plus_extra = [g0, g1, g2, g3, g4, g5, al.Galaxy(redshift=0.5)]
tracer_plus_extra = al.Tracer(
galaxies=galaxies_plus_extra, cosmology=al.cosmo.Planck15()
)
grid_2d_list_from = tracer_plus_extra.traced_grid_2d_list_from(grid=grid_simple)

grid_at_redshift = tracer.grid_2d_at_redshift_from(grid=grid_simple, redshift=0.5)

assert grid_at_redshift[0] == pytest.approx((9.73109691, 19.46219382), 1.0e-4)
assert grid_2d_list_from[1] == pytest.approx(grid_at_redshift, 1.0e-4)
assert grid_at_redshift[0] == pytest.approx((0.6273814, 1.2547628), 1.0e-4)

galaxies_plus_extra = [g0, g1, g2, g3, g4, g5, al.Galaxy(redshift=1.75)]
tracer_plus_extra = al.Tracer(
galaxies=galaxies_plus_extra, cosmology=al.cosmo.Planck15()
)
grid_2d_list_from = tracer_plus_extra.traced_grid_2d_list_from(grid=grid_simple)

grid_at_redshift = tracer.grid_2d_at_redshift_from(grid=grid_simple, redshift=1.75)

assert grid_at_redshift[0] == pytest.approx((0.65903649, 1.31807298), 1.0e-4)
assert grid_2d_list_from[2] == pytest.approx(grid_at_redshift, 1.0e-4)
assert grid_at_redshift[0] == pytest.approx((0.27331481161, 0.5466296232), 1.0e-4)

galaxies_plus_extra = [g0, g1, g2, g3, g4, g5, al.Galaxy(redshift=2.0)]
tracer_plus_extra = al.Tracer(
galaxies=galaxies_plus_extra, cosmology=al.cosmo.Planck15()
)
grid_2d_list_from = tracer_plus_extra.traced_grid_2d_list_from(grid=grid_simple)

grid_at_redshift = tracer.grid_2d_at_redshift_from(grid=grid_simple, redshift=2.0)

assert grid_2d_list_from[2] == pytest.approx(grid_at_redshift, 1.0e-4)
assert grid_at_redshift[0] == pytest.approx((0.222772465, 0.445544931), 1.0e-4)


def test__image_2d_list_from():
Expand Down
8 changes: 4 additions & 4 deletions test_autolens/quantity/model/test_analysis_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

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

def test__make_result__result_quantity_is_returned(
dataset_quantity_7x7_array_2d
):

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)))

analysis = al.AnalysisQuantity(
Expand All @@ -22,8 +21,9 @@ def test__make_result__result_quantity_is_returned(

assert isinstance(result, ResultQuantity)


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

Expand Down

0 comments on commit 3d3cd6f

Please sign in to comment.