Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/image mesh refactor #242

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion autolens/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from autoarray.mask.mask_1d import Mask1D
from autoarray.mask.mask_2d import Mask2D
from autoarray.operators.convolver import Convolver
from autoarray.inversion.pixelization import image_mesh
from autoarray.inversion.pixelization import mesh
from autoarray.inversion import regularization as reg
from autoarray.inversion.pixelization.image_mesh.abstract import AbstractImageMesh
from autoarray.inversion.pixelization.mesh.abstract import AbstractMesh
from autoarray.inversion.regularization.abstract import AbstractRegularization
from autoarray.inversion.pixelization.pixelization import Pixelization
Expand All @@ -33,7 +35,6 @@
from autoarray.structures.arrays.irregular import ArrayIrregular
from autoarray.structures.grids.uniform_1d import Grid1D
from autoarray.structures.grids.uniform_2d import Grid2D
from autoarray.structures.grids.sparse_2d import Grid2DSparse
from autoarray.structures.grids.iterate_2d import Grid2DIterate
from autoarray.structures.grids.irregular_2d import Grid2DIrregular
from autoarray.structures.grids.irregular_2d import Grid2DIrregularUniform
Expand Down
8 changes: 4 additions & 4 deletions autolens/aggregator/fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ def _fit_imaging_from(
)
settings_inversion = settings_inversion or fit.value(name="settings_inversion")

sparse_grids_of_planes_list = agg_util.sparse_grids_of_planes_list_from(
mesh_grids_of_planes_list = agg_util.mesh_grids_of_planes_list_from(
fit=fit, total_fits=len(dataset_list), use_preloaded_grid=use_preloaded_grid
)

fit_dataset_list = []

for dataset, tracer, sparse_grids_of_planes in zip(
dataset_list, tracer_list, sparse_grids_of_planes_list
for dataset, tracer, mesh_grids_of_planes in zip(
dataset_list, tracer_list, mesh_grids_of_planes_list
):
preloads = agg_util.preloads_from(
preloads_cls=Preloads,
use_preloaded_grid=use_preloaded_grid,
sparse_grids_of_planes=sparse_grids_of_planes,
mesh_grids_of_planes=mesh_grids_of_planes,
use_w_tilde=False,
)

Expand Down
8 changes: 4 additions & 4 deletions autolens/aggregator/fit_interferometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,19 @@ def _fit_interferometer_from(
)
settings_inversion = settings_inversion or fit.value(name="settings_inversion")

sparse_grids_of_planes_list = agg_util.sparse_grids_of_planes_list_from(
mesh_grids_of_planes_list = agg_util.mesh_grids_of_planes_list_from(
fit=fit, total_fits=len(dataset_list), use_preloaded_grid=use_preloaded_grid
)

fit_dataset_list = []

for dataset, tracer, sparse_grids_of_planes in zip(
dataset_list, tracer_list, sparse_grids_of_planes_list
for dataset, tracer, mesh_grids_of_planes in zip(
dataset_list, tracer_list, mesh_grids_of_planes_list
):
preloads = agg_util.preloads_from(
preloads_cls=Preloads,
use_preloaded_grid=use_preloaded_grid,
sparse_grids_of_planes=sparse_grids_of_planes,
mesh_grids_of_planes=mesh_grids_of_planes,
use_w_tilde=False,
)

Expand Down
29 changes: 18 additions & 11 deletions autolens/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
self.raise_exceptions(model=model)

def raise_exceptions(self, model):
if ag.util.model.has_pixelization_from(model=model):
has_pix = model.has_model(cls=(aa.Pixelization,)) or model.has_instance(
cls=(aa.Pixelization,)
)

if has_pix:
if (
self.positions_likelihood is None
and self.raise_inversion_positions_likelihood_exception
Expand Down Expand Up @@ -312,24 +316,27 @@ def save_results(self, paths: af.DirectoryPaths, result: ResultDataset):
except AttributeError:
pass

if conf.instance["general"]["output"]["fit_dill"]:
with open(paths._files_path / "fit.dill", "wb") as f:
dill.dump(result.max_log_likelihood_fit, f)
image_mesh_list = []

for galaxy in result.instance.galaxies:
pixelization_list = galaxy.cls_list_from(cls=aa.Pixelization)

mesh_list = ag.util.model.mesh_list_from(model=result.model)
for pixelization in pixelization_list:
if pixelization is not None:
image_mesh_list.append(pixelization.image_mesh)

if len(mesh_list) > 0:
if len(image_mesh_list) > 0:
paths.save_json(
name="preload_sparse_grids_of_planes",
name="preload_mesh_grids_of_planes",
object_dict=to_dict(
result.max_log_likelihood_fit.tracer_to_inversion.sparse_image_plane_grid_pg_list
result.max_log_likelihood_fit.tracer_to_inversion.image_plane_mesh_grid_pg_list
),
)

if conf.instance["general"]["adapt"]["stochastic_outputs"]:
if len(mesh_list) > 0:
for mesh in mesh_list:
if mesh.is_stochastic:
if len(image_mesh_list) > 0:
for image_mesh in image_mesh_list:
if image_mesh.is_stochastic:
self.save_stochastic_outputs(
paths=paths, samples=result.samples
)
Expand Down
46 changes: 23 additions & 23 deletions autolens/analysis/preloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
use_w_tilde: Optional[bool] = None,
blurred_image: Optional[aa.Array2D] = None,
traced_grids_of_planes_for_inversion: Optional[aa.Grid2D] = None,
sparse_image_plane_grid_pg_list: Optional[List[List[aa.Grid2D]]] = None,
image_plane_mesh_grid_pg_list: Optional[List[List[aa.Grid2D]]] = None,
relocated_grid: Optional[aa.Grid2D] = None,
mapper_list: Optional[aa.AbstractMapper] = None,
mapper_galaxy_dict: Optional[Dict[aa.AbstractMapper, ag.Galaxy]] = None,
Expand All @@ -27,8 +27,8 @@ def __init__(
mapper_operated_mapping_matrix_dict=None,
regularization_matrix: Optional[np.ndarray] = None,
log_det_regularization_matrix_term: Optional[float] = None,
traced_sparse_grids_list_of_planes=None,
sparse_image_plane_grid_list=None,
traced_mesh_grids_list_of_planes=None,
image_plane_mesh_grid_list=None,
failed=False,
):
"""
Expand Down Expand Up @@ -56,7 +56,7 @@ def __init__(
traced_grids_of_planes_for_inversion
The two dimensional grids corresponding to the traced grids in a lens fit. This can be preloaded when no
mass profiles in the model vary.
sparse_image_plane_grid_pg_list
image_plane_mesh_grid_pg_list
The two dimensional grids corresponding to the sparse image plane grids in a lens fit, that is ray-traced to
the source plane to form the source pixelization. This can be preloaded when no pixelizations in the model
vary.
Expand All @@ -80,7 +80,7 @@ def __init__(
use_w_tilde=use_w_tilde,
blurred_image=blurred_image,
relocated_grid=relocated_grid,
sparse_image_plane_grid_pg_list=sparse_image_plane_grid_pg_list,
image_plane_mesh_grid_pg_list=image_plane_mesh_grid_pg_list,
mapper_list=mapper_list,
mapper_galaxy_dict=mapper_galaxy_dict,
operated_mapping_matrix=operated_mapping_matrix,
Expand All @@ -89,8 +89,8 @@ def __init__(
mapper_operated_mapping_matrix_dict=mapper_operated_mapping_matrix_dict,
regularization_matrix=regularization_matrix,
log_det_regularization_matrix_term=log_det_regularization_matrix_term,
traced_sparse_grids_list_of_planes=traced_sparse_grids_list_of_planes,
sparse_image_plane_grid_list=sparse_image_plane_grid_list,
traced_mesh_grids_list_of_planes=traced_mesh_grids_list_of_planes,
image_plane_mesh_grid_list=image_plane_mesh_grid_list,
)

self.traced_grids_of_planes_for_inversion = traced_grids_of_planes_for_inversion
Expand Down Expand Up @@ -122,7 +122,7 @@ def setup_all_via_fits(cls, fit_0, fit_1) -> "Preloads":
preloads.set_blurred_image(fit_0=fit_0, fit_1=fit_1)

preloads.set_traced_grids_of_planes_for_inversion(fit_0=fit_0, fit_1=fit_1)
preloads.set_sparse_image_plane_grid_pg_list(fit_0=fit_0, fit_1=fit_1)
preloads.set_image_plane_mesh_grid_pg_list(fit_0=fit_0, fit_1=fit_1)
preloads.set_relocated_grid(fit_0=fit_0, fit_1=fit_1)
preloads.set_mapper_list(fit_0=fit_0, fit_1=fit_1)

Expand Down Expand Up @@ -183,7 +183,7 @@ def set_traced_grids_of_planes_for_inversion(self, fit_0, fit_1):
"PRELOADS - Traced grid of planes (for inversion) preloaded for this model-fit."
)

def set_sparse_image_plane_grid_pg_list(self, fit_0, fit_1):
def set_image_plane_mesh_grid_pg_list(self, fit_0, fit_1):
"""
If the `Pixelization`'s in a model are fixed their image-plane sparse grid (which defines the set of pixels
that are ray-traced to construct the source-plane pixelization) do not change during the model=fit and
Expand All @@ -203,33 +203,33 @@ def set_sparse_image_plane_grid_pg_list(self, fit_0, fit_1):
The second fit corresponding to a model with a different set of unit-values.
"""

self.sparse_image_plane_grid_pg_list = None
self.image_plane_mesh_grid_pg_list = None

sparse_image_plane_grid_pg_list_0 = (
fit_0.tracer_to_inversion.sparse_image_plane_grid_pg_list
image_plane_mesh_grid_pg_list_0 = (
fit_0.tracer_to_inversion.image_plane_mesh_grid_pg_list
)

sparse_image_plane_grid_pg_list_1 = (
fit_1.tracer_to_inversion.sparse_image_plane_grid_pg_list
image_plane_mesh_grid_pg_list_1 = (
fit_1.tracer_to_inversion.image_plane_mesh_grid_pg_list
)

if sparse_image_plane_grid_pg_list_0[-1] is not None:
if sparse_image_plane_grid_pg_list_0[-1][0] is not None:
if image_plane_mesh_grid_pg_list_0[-1] is not None:
if image_plane_mesh_grid_pg_list_0[-1][0] is not None:
if (
sparse_image_plane_grid_pg_list_0[-1][0].shape[0]
== sparse_image_plane_grid_pg_list_1[-1][0].shape[0]
image_plane_mesh_grid_pg_list_0[-1][0].shape[0]
== image_plane_mesh_grid_pg_list_1[-1][0].shape[0]
):
if (
np.max(
abs(
sparse_image_plane_grid_pg_list_0[-1][0]
- sparse_image_plane_grid_pg_list_1[-1][0]
image_plane_mesh_grid_pg_list_0[-1][0]
- image_plane_mesh_grid_pg_list_1[-1][0]
)
)
< 1e-8
):
self.sparse_image_plane_grid_pg_list = (
sparse_image_plane_grid_pg_list_0
self.image_plane_mesh_grid_pg_list = (
image_plane_mesh_grid_pg_list_0
)

logger.info(
Expand All @@ -252,7 +252,7 @@ def info(self) -> List[str]:
f"Traced Grids of Planes (For LEq) = {self.traced_grids_of_planes_for_inversion is not None}\n"
]
line += [
f"Sparse Image-Plane Grids of Planes = {self.sparse_image_plane_grid_pg_list is not None}\n"
f"Sparse Image-Plane Grids of Planes = {self.image_plane_mesh_grid_pg_list is not None}\n"
]
line += [f"Relocated Grid = {self.relocated_grid is not None}\n"]
line += [f"Mapper = {self.mapper_list is not None}\n"]
Expand Down
12 changes: 0 additions & 12 deletions autolens/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,6 @@ def make_tracer_x2_plane_inversion_7x7():
)


def make_tracer_x2_plane_voronoi_7x7():
pixelization = al.Pixelization(
mesh=al.mesh.VoronoiMagnification(), regularization=al.reg.Constant()
)

source_gal_inversion = al.Galaxy(redshift=1.0, pixelization=pixelization)

return al.Tracer.from_galaxies(
galaxies=[make_gal_x1_mp(), make_gal_x1_lp(), source_gal_inversion]
)


def make_tracer_x2_plane_point():
source_gal_x1_lp = al.Galaxy(redshift=1.0, point_0=al.ps.PointFlux())

Expand Down
2 changes: 1 addition & 1 deletion autolens/imaging/mock/mock_fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ def tracer_to_inversion(self) -> MockTracerToInversion:

return MockTracerToInversion(
tracer=self.tracer,
sparse_image_plane_grid_pg_list=self.tracer.sparse_image_plane_grid_pg_list,
image_plane_mesh_grid_pg_list=self.tracer.image_plane_mesh_grid_pg_list,
)
10 changes: 4 additions & 6 deletions autolens/imaging/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,22 @@ def stochastic_log_likelihoods_via_instance_from(self, instance: af.ModelInstanc
return

if not any(
pix.mesh.is_stochastic for pix in tracer.cls_list_from(cls=ag.Pixelization)
pix.image_mesh.is_stochastic for pix in tracer.cls_list_from(cls=ag.Pixelization)
):
return

settings_pixelization = (
self.settings_pixelization.settings_with_is_stochastic_true()
)

log_evidences = []

for i in range(self.settings_lens.stochastic_samples):

try:

tracer.galaxies[-1].pixelization.image_mesh.seed = i

log_evidence = FitImaging(
dataset=self.dataset,
tracer=tracer,
settings_pixelization=settings_pixelization,
settings_pixelization=self.settings_pixelization,
settings_inversion=self.settings_inversion,
preloads=self.preloads,
).log_evidence
Expand Down
10 changes: 4 additions & 6 deletions autolens/interferometer/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,22 @@ def stochastic_log_likelihoods_via_instance_from(self, instance):

if not any(
[
pix.mesh.is_stochastic
pix.image_mesh.is_stochastic
for pix in tracer.cls_list_from(cls=ag.Pixelization)
]
):
return

settings_pixelization = (
self.settings_pixelization.settings_with_is_stochastic_true()
)

log_evidences = []

for i in range(self.settings_lens.stochastic_samples):
try:
tracer.galaxies[-1].pixelization.image_mesh.seed = i

log_evidence = FitInterferometer(
dataset=self.dataset,
tracer=tracer,
settings_pixelization=settings_pixelization,
settings_pixelization=self.settings_pixelization,
settings_inversion=self.settings_inversion,
preloads=self.preloads,
).log_evidence
Expand Down
8 changes: 4 additions & 4 deletions autolens/lens/mock/mock_to_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class MockTracerToInversion:
def __init__(
self,
tracer,
sparse_image_plane_grid_pg_list=None,
image_plane_mesh_grid_pg_list=None,
run_time_dict: Optional[Dict] = None,
):
self.tracer = tracer

self.sparse_image_plane_grid_pg_list = sparse_image_plane_grid_pg_list
self.image_plane_mesh_grid_pg_list = image_plane_mesh_grid_pg_list

self.run_time_dict = run_time_dict

def sparse_image_plane_grid_pg_list(self):
return self.sparse_image_plane_grid_pg_list
def image_plane_mesh_grid_pg_list(self):
return self.image_plane_mesh_grid_pg_list
10 changes: 4 additions & 6 deletions autolens/lens/mock/mock_tracer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class MockTracer:
def __init__(
self, traced_grid_2d_list_from=None, sparse_image_plane_grid_pg_list=None
self, traced_grid_2d_list_from=None, image_plane_mesh_grid_pg_list=None
):
self.sparse_image_plane_grid_pg_list = sparse_image_plane_grid_pg_list
self.image_plane_mesh_grid_pg_list = image_plane_mesh_grid_pg_list
self._traced_grid_2d_list_from = traced_grid_2d_list_from

def traced_grid_2d_list_from(self, grid):
Expand All @@ -12,17 +12,15 @@ def traced_grid_2d_list_from(self, grid):
class MockTracerPoint(MockTracer):
def __init__(
self,
sparse_image_plane_grid_pg_list=None,
image_plane_mesh_grid_pg_list=None,
traced_grid=None,
attribute=None,
profile=None,
magnification=None,
einstein_radius=None,
einstein_mass=None,
):
super().__init__(
sparse_image_plane_grid_pg_list=sparse_image_plane_grid_pg_list
)
super().__init__(image_plane_mesh_grid_pg_list=image_plane_mesh_grid_pg_list)

self.positions = traced_grid

Expand Down
Loading
Loading