Skip to content
Merged
2 changes: 1 addition & 1 deletion autoastro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
from autoastro.hyper import hyper_data
from autoastro import plot

__version__ = '0.7.2'
__version__ = "0.7.2"
145 changes: 96 additions & 49 deletions autoastro/galaxy/galaxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from autoastro import dimensions as dim
from autoastro import lensing
from autofit.tools import text_util
from autoarray.structures import grids
from autoarray.structures import arrays, grids
from autoarray.operators.inversion import pixelizations as pix
from autoastro.profiles import light_profiles as lp
from autoastro.profiles import mass_profiles as mp
Expand Down Expand Up @@ -122,30 +122,99 @@ def has_light_profile(self):
def has_mass_profile(self):
return len(self.mass_profiles) > 0

@property
def has_only_mass_sheets(self):

if not self.has_mass_profile:
return False

mass_sheet_bools = [
mass_profile.is_mass_sheet for mass_profile in self.mass_profiles
]
total_mass_sheets = sum(mass_sheet_bools)

return len(self.mass_profiles) == total_mass_sheets

@property
def has_profile(self):
return len(self.mass_profiles) + len(self.light_profiles) > 0

@property
def light_profile_centres(self):
return [light_profile.centre for light_profile in self.light_profiles]
"""Returns the light profile centres of the galaxy as a *Coordinates* object, which structures the centres
in lists according to which light profile they come from.

Fo example, if a galaxy has two light profiles, the first with one centre and second with two centres this
returns:

[[(y0, x0)], [(y0, x0), (y1, x1)]]

This is used for visualization, for example plotting the centres of all light profiles colored by their profile.

NOTE: Currently, no light profiles can have more than one centre (it unlikely one ever will). The structure of
the output follows this convention to follow other methods in the *Galaxy* class that return profile
attributes."""
return grids.Coordinates(
[[light_profile.centre] for light_profile in self.light_profiles]
)

@property
def mass_profile_centres(self):
"""Returns the mass profile centres of the galaxy as a *Coordinates* object, which structures the centres
in lists according to which mass profile they come from.

Fo example, if a galaxy has two mass profiles, the first with one centre and second with two centres this
returns:

[[(y0, x0)], [(y0, x0), (y1, x1)]]

This is used for visualization, for example plotting the centres of all mass profiles colored by their profile.

NOTE: Currently, no mass profiles can have more than one centre (it unlikely one ever will). The structure of
the output follows this convention to follow other methods in the *Galaxy* class that return profile
attributes.

The centres of mass-sheets are omitted, as their centres are not relevant to lensing calculations."""
centres = [
mass_profile.centre
[mass_profile.centre]
for mass_profile in self.mass_profiles
if not mass_profile.is_mass_sheet
]
return list(filter(None, centres))
return grids.Coordinates(list(filter(None, centres)))

@property
def mass_profile_axis_ratios(self):
return [mass_profile.axis_ratio for mass_profile in self.mass_profiles]
"""Returns the mass profile axis-ratios of the galaxy as a *Values* object, which structures the axis-ratios
in lists according to which mass profile they come from.

Fo example, if a galaxy has two mass profiles, the first with one axis-ratio and second with two axis-ratios
this returns:

[[axis_ratio_0], [axis_ratio_0, axis_ratio_1]]

This is used for visualization, for example plotting the axis-ratios of all mass profiles colored by their
profile.

"""
return arrays.Values(
[[mass_profile.axis_ratio] for mass_profile in self.mass_profiles]
)

@property
def mass_profile_phis(self):
return [mass_profile.phi for mass_profile in self.mass_profiles]
"""Returns the mass profile phis of the galaxy as a *Values* object, which structures the phis in lists
according to which mass profile they come from.

Fo example, if a galaxy has two mass profiles, the first with one phi and second with two phis this returns:

[[phi_0], [phi_0, phi_1]]

This is used for visualization, for example plotting the phis of all mass profiles colored by their profile.

"""
return arrays.Values(
[[mass_profile.phi] for mass_profile in self.mass_profiles]
)

@property
def uses_cluster_inversion(self):
Expand Down Expand Up @@ -330,7 +399,7 @@ def new_object_with_units_converted(

return self.__class__(**new_dict)

@grids.convert_coordinates_to_grid
@grids.grid_like_to_numpy
def profile_image_from_grid(self, grid):
"""Calculate the summed image of all of the galaxy's light profiles using a grid of Cartesian (y,x) \
coordinates.
Expand All @@ -341,21 +410,15 @@ def profile_image_from_grid(self, grid):

Parameters
----------
grid : ndarray
grid : grid_like
The (y, x) coordinates in the original reference frame of the grid.

"""
if self.has_light_profile:
profile_image = sum(
return sum(
map(lambda p: p.profile_image_from_grid(grid=grid), self.light_profiles)
)
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=profile_image
)
else:
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=np.zeros((grid.sub_shape_1d,))
)
return np.zeros((grid.shape[0],))

def blurred_profile_image_from_grid_and_psf(self, grid, psf, blurring_grid=None):

Expand Down Expand Up @@ -423,37 +486,30 @@ def luminosity_within_circle_in_units(
)
return None

@grids.convert_coordinates_to_grid
@grids.grid_like_to_numpy
def convergence_from_grid(self, grid):
"""Compute the summed convergence of the galaxy's mass profiles using a grid \
of Cartesian (y,x) coordinates.
"""Compute the summed convergence of the galaxy's mass profiles using a grid of Cartesian (y,x) coordinates.

If the galaxy has no mass profiles, a grid of zeros is returned.

See *profiles.mass_profiles* module for details of how this is performed.

The *reshape_returned_array* decorator reshapes the NumPy arrays the convergence is outputted on. See \
*aa.reshape_returned_array* for a description of the output.
The *grid_like_to_numpy* decorator reshapes the NumPy arrays the convergence is outputted on. See \
*aa.grid_like_to_numpy* for a description of the output.

Parameters
----------
grid : ndarray
grid : grid_like
The (y, x) coordinates in the original reference frame of the grid.

"""
if self.has_mass_profile:
convergence = sum(
return sum(
map(lambda p: p.convergence_from_grid(grid=grid), self.mass_profiles)
)
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=convergence
)
else:
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=np.zeros((grid.sub_shape_1d,))
)
return np.zeros((grid.shape[0],))

@grids.convert_coordinates_to_grid
@grids.grid_like_to_numpy
def potential_from_grid(self, grid):
"""Compute the summed gravitational potential of the galaxy's mass profiles \
using a grid of Cartesian (y,x) coordinates.
Expand All @@ -462,28 +518,22 @@ def potential_from_grid(self, grid):

See *profiles.mass_profiles* module for details of how this is performed.

The *reshape_returned_array* decorator reshapes the NumPy arrays the convergence is outputted on. See \
*aa.reshape_returned_array* for a description of the output.
The *grid_like_to_numpy* decorator reshapes the NumPy arrays the convergence is outputted on. See \
*aa.grid_like_to_numpy* for a description of the output.

Parameters
----------
grid : ndarray
grid : grid_like
The (y, x) coordinates in the original reference frame of the grid.

"""
if self.has_mass_profile:
potential = sum(
return sum(
map(lambda p: p.potential_from_grid(grid=grid), self.mass_profiles)
)
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=potential
)
else:
return grid.mapping.array_stored_1d_from_sub_array_1d(
sub_array_1d=np.zeros((grid.sub_shape_1d,))
)
return np.zeros((grid.shape[0],))

@grids.convert_coordinates_to_grid
@grids.grid_like_to_numpy
def deflections_from_grid(self, grid):
"""Compute the summed (y,x) deflection angles of the galaxy's mass profiles \
using a grid of Cartesian (y,x) coordinates.
Expand All @@ -494,17 +544,14 @@ def deflections_from_grid(self, grid):

Parameters
----------
grid : ndarray
grid : grid_like
The (y, x) coordinates in the original reference frame of the grid.
"""
if self.has_mass_profile:
deflections = sum(
return sum(
map(lambda p: p.deflections_from_grid(grid=grid), self.mass_profiles)
)
return grid.mapping.grid_stored_1d_from_sub_grid_1d(sub_grid_1d=deflections)
return grid.mapping.grid_stored_1d_from_sub_grid_1d(
sub_grid_1d=np.full((grid.sub_shape_1d, 2), 0.0)
)
return np.zeros((grid.shape[0], 2))

def mass_within_circle_in_units(
self,
Expand Down
20 changes: 10 additions & 10 deletions autoastro/lensing.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,25 +189,25 @@ def convergence_bounding_box(self, convergence_threshold=0.02):
[y_min, y_max, x_min, x_max] = self.mass_profile_bounding_box

def func_for_y_min(y):
grid = grids.GridIrregular.manual_1d(grid=[[y, x_max], [y, x_min]])
grid = np.array([[y, x_max], [y, x_min]])
return np.max(self.convergence_from_grid(grid=grid) - convergence_threshold)

convergence_y_min = root_scalar(func_for_y_min, bracket=[y_min, -1000.0]).root

def func_for_y_max(y):
grid = grids.GridIrregular.manual_1d(grid=[[y, x_max], [y, x_min]])
grid = np.array([[y, x_max], [y, x_min]])
return np.min(self.convergence_from_grid(grid=grid) - convergence_threshold)

convergence_y_max = root_scalar(func_for_y_max, bracket=[y_max, 1000.0]).root

def func_for_x_min(x):
grid = grids.GridIrregular.manual_1d(grid=[[y_max, x], [y_min, x]])
grid = np.array([[y_max, x], [y_min, x]])
return np.max(self.convergence_from_grid(grid=grid) - convergence_threshold)

convergence_x_min = root_scalar(func_for_x_min, bracket=[x_min, -1000.0]).root

def func_for_x_max(x):
grid = grids.GridIrregular.manual_1d(grid=[[y_max, x], [y_min, x]])
grid = np.array([[y_max, x], [y_min, x]])
return np.min(self.convergence_from_grid(grid=grid) - convergence_threshold)

convergence_x_max = root_scalar(func_for_x_max, bracket=[x_max, 1000.0]).root
Expand Down Expand Up @@ -260,7 +260,7 @@ def tangential_critical_curve(self):
shape_2d=tangential_eigen_values.sub_shape_2d,
)

return grids.GridIrregular(grid=tangential_critical_curve)
return grids.Coordinates(tangential_critical_curve)

@property
def radial_critical_curve(self):
Expand All @@ -281,12 +281,13 @@ def radial_critical_curve(self):
shape_2d=radial_eigen_values.sub_shape_2d,
)

return grids.GridIrregular(grid=radial_critical_curve)
return grids.Coordinates(radial_critical_curve)

@property
@array_util.Memoizer()
def critical_curves(self):
return [self.tangential_critical_curve, self.radial_critical_curve]
return grids.Coordinates(
[self.tangential_critical_curve, self.radial_critical_curve]
)

@property
def tangential_caustic(self):
Expand Down Expand Up @@ -317,9 +318,8 @@ def radial_caustic(self):
return radial_critical_curve - deflections_critical_curve

@property
@array_util.Memoizer()
def caustics(self):
return [self.tangential_caustic, self.radial_caustic]
return grids.Coordinates([self.tangential_caustic, self.radial_caustic])

@property
@array_util.Memoizer()
Expand Down
12 changes: 6 additions & 6 deletions autoastro/plot/galaxy_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def profile_image(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose image are plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""
plotter.plot_array(
Expand All @@ -38,7 +38,7 @@ def convergence(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose convergence is plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""
plotter.plot_array(
Expand All @@ -63,7 +63,7 @@ def potential(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose potential is plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""
plotter.plot_array(
Expand All @@ -88,7 +88,7 @@ def deflections_y(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose y deflecton angles are plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""
deflections = galaxy.deflections_from_grid(grid=grid)
Expand Down Expand Up @@ -118,7 +118,7 @@ def deflections_x(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose x deflecton angles are plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""
deflections = galaxy.deflections_from_grid(grid=grid)
Expand Down Expand Up @@ -147,7 +147,7 @@ def magnification(galaxy, grid, positions=None, include=None, plotter=None):
-----------
galaxy : model.galaxy.aast.Galaxy
The galaxy whose magnification is plotted.
grid : ndarray or datas.arrays.grid_stacks.Grid
grid : grid_like or datas.arrays.grid_stacks.Grid
The (y,x) coordinates of the grid, in an arrays of shape (total_coordinates, 2)
"""

Expand Down
Loading