Skip to content

Commit

Permalink
final test fixdd
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Dec 21, 2023
1 parent de49b30 commit c7c0441
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 80 deletions.
5 changes: 1 addition & 4 deletions autolens/analysis/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ def should_plot(name):
if should_plot("image_with_positions"):
image_plotter.figure_2d()

def visualize_adapt_images(
self,
adapt_images : ag.AdaptImages
):
def visualize_adapt_images(self, adapt_images: ag.AdaptImages):
"""
Visualizes the adapt-images and adapt image inferred by a model-fit.
Expand Down
5 changes: 2 additions & 3 deletions autolens/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,13 @@ def make_analysis_imaging_7x7():
return al.AnalysisImaging(
dataset=make_masked_imaging_7x7(),
settings_inversion=aa.SettingsInversion(use_w_tilde=False),
adapt_images=make_adapt_images_7x7()
adapt_images=make_adapt_images_7x7(),
)


def make_analysis_interferometer_7():
return al.AnalysisInterferometer(
dataset=make_interferometer_7(),
adapt_images=make_adapt_images_7x7()
dataset=make_interferometer_7(), adapt_images=make_adapt_images_7x7()
)


Expand Down
10 changes: 3 additions & 7 deletions autolens/interferometer/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ def log_likelihood_function(self, instance):
raise e

try:
return self.fit_from(
instance=instance
).figure_of_merit
return self.fit_from(instance=instance).figure_of_merit
except (
PixelizationException,
exc.PixelizationException,
Expand Down Expand Up @@ -212,7 +210,7 @@ def fit_from(
FitInterferometer
The fit of the plane to the interferometer dataset, which includes the log likelihood.
"""

tracer = self.tracer_via_instance_from(
instance=instance, run_time_dict=run_time_dict
)
Expand Down Expand Up @@ -257,9 +255,7 @@ def visualize_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
)

if self.adapt_images is not None:
visualizer.visualize_adapt_images(
adapt_images=self.adapt_images
)
visualizer.visualize_adapt_images(adapt_images=self.adapt_images)

def visualize(self, paths: af.DirectoryPaths, instance, during_analysis):
"""
Expand Down
4 changes: 1 addition & 3 deletions autolens/interferometer/model/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def max_log_likelihood_fit(self) -> FitInterferometer:
An instance of a `FitInterferometer` corresponding to the maximum log likelihood model inferred by the
non-linear search.
"""
return self.analysis.fit_from(
instance=self.instance
)
return self.analysis.fit_from(instance=self.instance)

@property
def max_log_likelihood_tracer(self) -> Tracer:
Expand Down
20 changes: 8 additions & 12 deletions autolens/lens/to_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def lp_linear_func_list_galaxy_dict(
blurring_grid=traced_blurring_grids_of_planes_list[plane_index],
noise_map=self.noise_map,
settings_inversion=self.settings_inversion,
adapt_images=self.adapt_images
adapt_images=self.adapt_images,
)

lp_linear_galaxy_dict_of_plane = (
Expand All @@ -94,19 +94,17 @@ def cls_pg_list_from(self, cls: Type) -> List:

@cached_property
def adapt_galaxy_image_pg_list(self) -> List:

adapt_galaxy_image_pg_list = []

for plane in self.planes:

if plane.has(cls=aa.Pixelization):

plane_image_list = []

galaxies_with_pixelization_list = plane.galaxies_with_cls_list_from(cls=aa.Pixelization)
galaxies_with_pixelization_list = plane.galaxies_with_cls_list_from(
cls=aa.Pixelization
)

for galaxy in galaxies_with_pixelization_list:

try:
image = self.adapt_images.galaxy_image_dict[galaxy]
except (AttributeError, KeyError):
Expand All @@ -117,7 +115,6 @@ def adapt_galaxy_image_pg_list(self) -> List:
adapt_galaxy_image_pg_list.append(plane_image_list)

else:

adapt_galaxy_image_pg_list.append([])

return adapt_galaxy_image_pg_list
Expand Down Expand Up @@ -226,10 +223,9 @@ def mapper_galaxy_dict(self) -> Dict[aa.AbstractMapper, ag.Galaxy]:
pixelization_list = self.cls_pg_list_from(cls=aa.Pixelization)

try:

adapt_galaxy_image = self.adapt_galaxy_image_pg_list[plane_index][
mapper_index
]
adapt_galaxy_image = self.adapt_galaxy_image_pg_list[
plane_index
][mapper_index]
except AttributeError:
adapt_galaxy_image = None

Expand All @@ -244,7 +240,7 @@ def mapper_galaxy_dict(self) -> Dict[aa.AbstractMapper, ag.Galaxy]:
image_plane_mesh_grid=image_plane_mesh_grid_list[plane_index][
mapper_index
],
adapt_galaxy_image=adapt_galaxy_image
adapt_galaxy_image=adapt_galaxy_image,
)

galaxy = galaxies_with_pixelization_list[mapper_index]
Expand Down
4 changes: 1 addition & 3 deletions autolens/point/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def log_likelihood_function(self, instance):
except (AttributeError, ValueError, TypeError, NumbaException) as e:
raise exc.FitException from e

def fit_from(
self, instance, run_time_dict: Optional[Dict] = None
) -> FitPointDict:
def fit_from(self, instance, run_time_dict: Optional[Dict] = None) -> FitPointDict:
tracer = self.tracer_via_instance_from(
instance=instance, run_time_dict=run_time_dict
)
Expand Down
4 changes: 1 addition & 3 deletions autolens/quantity/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ def __init__(
"""
super().__init__(dataset=dataset, func_str=func_str, cosmology=cosmology)

AnalysisLensing.__init__(
self=self, cosmology=cosmology
)
AnalysisLensing.__init__(self=self, cosmology=cosmology)

def fit_quantity_for_instance(self, instance: af.ModelInstance) -> FitQuantity:
"""
Expand Down
3 changes: 1 addition & 2 deletions test_autolens/aggregator/test_aggregator_fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ def test__fit_imaging__adapt_images(

for fit_gen in fit_pdf_gen:
for fit_list in fit_gen:

i += 1

assert (
Expand All @@ -127,4 +126,4 @@ def test__fit_imaging__adapt_images(

assert i == 2

clean(database_file=database_file)
clean(database_file=database_file)
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ def test__fit_interferometer__adapt_images(

assert i == 2

clean(database_file=database_file)
clean(database_file=database_file)
8 changes: 4 additions & 4 deletions test_autolens/analysis/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def test__tracer_for_instance__subhalo_redshift_rescale_used(analysis_imaging_7x
assert tracer.galaxies[1].mass.centre == pytest.approx((-0.19959, -0.39919), 1.0e-4)


def test__relocate_pix_border__determines_if_border_pixel_relocation_is_used(masked_imaging_7x7):
def test__relocate_pix_border__determines_if_border_pixel_relocation_is_used(
masked_imaging_7x7,
):
pixelization = al.Pixelization(
mesh=al.mesh.Rectangular(shape=(3, 3)),
regularization=al.reg.Constant(coefficient=1.0),
Expand All @@ -105,9 +107,7 @@ def test__relocate_pix_border__determines_if_border_pixel_relocation_is_used(mas
analysis.dataset.grid_pixelization[4] = np.array([[500.0, 0.0]])

instance = model.instance_from_unit_vector([])
fit = analysis.fit_from(
instance=instance
)
fit = analysis.fit_from(instance=instance)

assert fit.inversion.linear_obj_list[0].source_plane_data_grid[4][
0
Expand Down
1 change: 1 addition & 0 deletions test_autolens/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def make_adapt_model_image_7x7():
def make_adapt_galaxy_name_image_dict_7x7():
return fixtures.make_adapt_galaxy_name_image_dict_7x7()


@pytest.fixture(name="adapt_images_7x7")
def make_adapt_images_7x7():
return fixtures.make_adapt_images_7x7()
Expand Down
32 changes: 0 additions & 32 deletions test_autolens/interferometer/model/test_analysis_interferometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,38 +115,6 @@ def test__positions__likelihood_overwrite__changes_likelihood(
assert analysis_log_likelihood == pytest.approx(-22048700567.590656, 1.0e-4)


def test__sets_up_adapt_galaxy_images(interferometer_7):
adapt_galaxy_name_image_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
),
}

result = al.m.MockResult(
adapt_galaxy_name_image_dict=adapt_galaxy_name_image_dict,
adapt_model_image=al.Array2D.full(
fill_value=3.0, shape_native=(3, 3), pixel_scales=1.0
),
)

analysis = al.AnalysisInterferometer(dataset=interferometer_7, adapt_result=result)

analysis.set_adapt_dataset(result=result)

assert (
analysis.adapt_galaxy_name_image_dict[("galaxies", "lens")].native
== np.ones((3, 3))
).all()

assert (
analysis.adapt_galaxy_name_image_dict[("galaxies", "source")].native
== 2.0 * np.ones((3, 3))
).all()

assert (analysis.adapt_model_image.native == 3.0 * np.ones((3, 3))).all()


def test__profile_log_likelihood_function(interferometer_7):
pixelization = al.Pixelization(
mesh=al.mesh.Rectangular(shape=(3, 3)),
Expand Down
8 changes: 2 additions & 6 deletions test_autolens/lens/test_to_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ def test__adapt_galaxy_image_pg_list(sub_grid_2d_7x7):

gal_pix = al.Galaxy(redshift=0.5, pixelization=pixelization)

adapt_images = al.AdaptImages(
model_image=1,
galaxy_image_dict={gal_pix : 1}
)
adapt_images = al.AdaptImages(model_image=1, galaxy_image_dict={gal_pix: 1})

tracer = al.Tracer.from_galaxies(galaxies=[gal_pix, gal])

Expand All @@ -204,8 +201,7 @@ def test__adapt_galaxy_image_pg_list(sub_grid_2d_7x7):
gal_pix2 = al.Galaxy(redshift=2.0, pixelization=pixelization)

adapt_images = al.AdaptImages(
model_image=1,
galaxy_image_dict={gal_pix0 : 1, gal_pix1: 2, gal_pix2 : 3}
model_image=1, galaxy_image_dict={gal_pix0: 1, gal_pix1: 2, gal_pix2: 3}
)

tracer = al.Tracer.from_galaxies(
Expand Down

0 comments on commit c7c0441

Please sign in to comment.