autolens.Grid2DIterate#
- class Grid2DIterate[source]#
Bases:
Grid2D
Represents a grid of coordinates as described for the
Grid2D
class, but using an iterative sub-grid that adapts its resolution when it is input into a function that performs an analytic calculation.A
Grid2D
represents (y,x) coordinates using a sub-grid, where functions are evaluated once at every coordinate on the sub-grid and averaged to give a more precise evaluation an analytic function. AGrid2DIterate
does not have a specified sub-grid size, but instead iteratively recomputes the analytic function at increasing sub-grid sizes until an input fractional accuracy is reached.Iteration is performed on a per (y,x) coordinate basis, such that the sub-grid size will adopt low values wherever doing so can meet the fractional accuracy and high values only where it is required to meet the fractional accuracy. For functions where a wide range of sub-grid sizes allow fractional accuracy to be met this ensures the function can be evaluated accurate in a computaionally efficient manner.
This overcomes a limitation of the
Grid2D
class whereby if a small subset of pixels require high levels of sub-gridding to be evaluated accuracy, the entire grid would require this sub-grid size thus leading to unecessary expensive function evaluations.- Parameters:
values (
ndarray
) – The (y,x) coordinates of the grid.mask (
Mask2D
) – The 2D mask associated with the grid, defining the pixels each grid coordinate is paired with and originates from.fractional_accuracy (
float
) – The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio of the value at a higher sub size to the value computed using the previous sub_size. The fractional accuracy does not depend on the units or magnitude of the function being evaluated.relative_accuracy (
Optional
[float
]) – The relative accuracy the function evaluted must meet to be accepted, where this value is the absolute difference of the values computed using the higher sub size and lower sub size grids. The relative accuracy depends on the units / magnitude of the function being evaluated.sub_steps (
Optional
[List
[int
]]) – The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None, they are setup as the default values [2, 4, 8, 16].store_native (
bool
) – If True, the ndarray is stored in its native format [total_y_pixels, total_x_pixels, 2]. This avoids mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck.
Methods
all
array_at_sub_size_from
- rtype:
astype
- rtype:
AbstractNDArray
Setup a blurring-grid from a mask, where a blurring grid consists of all pixels that are masked (and therefore have their values set to (0.0, 0.0)), but are close enough to the unmasked pixels that their values will be convolved into the unmasked those pixels.
Returns the blurring grid from a grid and create it as a Grid2DIterate, via an input 2D kernel shape.
bounding_box
Create a Grid2D (see Grid2D.__new__) from an input bounding box with coordinates [y_min, y_max, x_min, x_max], where the shape_native is used to compute the (y,x) grid values within this bounding box.
copy
distances_to_coordinate_from
Returns the distance of every coordinate on the grid from an input (y,x) coordinate.
extent_with_buffer_from
The extent of the grid in scaled units returned as a list [x_min, x_max, y_min, y_max], where all values are buffed such that their extent is further than the grid's extent..
flip_hdu_for_ds9
from_extent
Create a Grid2D (see Grid2D.__new__) by inputting the extent of the (y,x) grid coordinates as an input (x0, x1, y0, y1) tuple.
from_fits
Create a Grid2D (see Grid2D.__new__) from a mask, where only unmasked pixels are included in the grid (if the grid is represented in its native 2D masked values are (0.0, 0.0)).
Create a Grid2DIterate (see Grid2DIterate.__new__) from a mask, where only unmasked pixels are included in the grid (if the grid is represented in 2D masked values are (0.0, 0.0)).
from_yx_1d
Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates as 1D y and x values.
from_yx_2d
Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates as 2D y and x values.
grid_2d_radial_projected_from
Determine a projected radial grid of points from a 2D region of coordinates defined by an extent [xmin, xmax, ymin, ymax] and with a (y,x) centre.
grid_2d_radial_projected_shape_slim_from
The function grid_scaled_2d_slim_radial_projected_from() determines a projected radial grid of points from a 2D region of coordinates defined by an extent [xmin, xmax, ymin, ymax] and with a (y,x) centre.
Returns a new Grid2DIterate from this grid, where the (y,x) coordinates of this grid have a grid of (y,x) values, termed the deflection grid, subtracted from them to determine the new grid of (y,x) values.
grid_at_sub_size_from
- rtype:
grid_with_coordinates_within_distance_removed_from
Remove all coordinates from this Grid2D which are within a certain distance of an input list of coordinates.
instance_flatten
Flatten an instance of an autoarray class into a tuple of its attributes (i.e.
instance_unflatten
Unflatten a tuple of attributes (i.e.
invert
Iterate over a function that returns an array of values until the it meets a specified fractional accuracy.
Create the iterated array from a result array that is computed at a higher sub size leel than the previous grid.
Iterate over a function that returns a grid of values until the it meets a specified fractional accuracy.
Create the iterated grid from a result grid that is computed at a higher sub size level than the previous grid.
Iterate over a function that returns an array or grid of values until the it meets a specified fractional accuracy.
max
min
Create a Grid2DIterate (see Grid2DIterate.__new__) by inputting the grid coordinates in 1D, for example:
output_to_fits
Output the grid to a .fits file.
When the edge pixels of a mask are unmasked and a convolution is to occur, the signal of edge pixels will be 'missing' if the grid is used to evaluate the signal via an analytic function.
relocated_grid_from
Relocate the coordinates of a grid to the border of this grid if they are outside the border, where the border is defined as all pixels at the edge of the grid's mask (see mask._border_1d_indexes).
relocated_mesh_grid_from
Relocate the coordinates of a pixelization grid to the border of this grid.
reshape
- rtype:
AbstractNDArray
Returns the resulting iterated array, by mapping it to 1D and then passing it back as an
Array2D
structure.sqrt
- rtype:
AbstractNDArray
squared_distances_to_coordinate_from
Returns the squared distance of every coordinate on the grid from an input coordinate.
structure_2d_from
Convert a result from an ndarray to an aa.Array2D or aa.Grid2D structure, where the conversion depends on type(result) as follows:
structure_2d_list_from
Convert a result from a list of ndarrays to a list of aa.Array2D or aa.Grid2D structure, where the conversion depends on type(result) as follows:
sum
Returns a fractional mask from a result array, where the fractional mask describes whether the evaluated value in the result array is within the
Grid2DIterate
's specified fractional accuracy.Jitted function to determine the fractional mask, which is a mask where:
Returns a fractional mask from a result array, where the fractional mask describes whether the evaluated value in the result array is within the
Grid2DIterate
's specified fractional accuracy.Jitted function to determine the fractional mask, which is a mask where:
trimmed_after_convolution_from
- rtype:
Structure
Create a Grid2DIterate (see Grid2DIterate.__new__) as a uniform grid of (y,x) values given an input shape_native and pixel scale of the grid:
values_from
Create a ArrayIrregular object from a 1D NumPy array of values of shape [total_coordinates].
with_new_array
Copy this object but give it a new array.
Attributes
array
Return a
Grid2D
of the binned-up grid in its 1D representation, which is stored with shape [total_unmasked_pixels, 2].derive_grid
- rtype:
DeriveGrid2D
derive_indexes
- rtype:
DeriveIndexes2D
derive_mask
- rtype:
DeriveMask2D
dtype
flipped
Return the grid as an ndarray of shape [total_unmasked_pixels, 2] with flipped values such that coordinates are given as (x,y) values.
geometry
hdu_for_output
imag
- rtype:
AbstractNDArray
in_radians
Return the grid as an ndarray where all (y,x) values are converted to Radians.
Return a
Grid2D
where the data is stored in itsnative
representation, which has shape [sub_size*total_y_pixels, sub_size*total_x_pixels, 2].ndim
origin
pixel_area
pixel_scale
- rtype:
pixel_scale_header
- rtype:
pixel_scales
real
- rtype:
AbstractNDArray
scaled_maxima
The (y,x) maximum values of the grid in scaled units, buffed such that their extent is further than the grid's extent.
scaled_minima
The (y,x) minimum values of the grid in scaled units, buffed such that their extent is further than the grid's extent.
shape
shape_native
shape_native_scaled_interior
The (y,x) interior 2D shape of the grid in scaled units, computed from the minimum and maximum y and x values of the grid.
shape_slim
- rtype:
size
Return a
Grid2D
where the data is stored itsslim
representation, which is an ndarray of shape [total_unmasked_pixels * sub_size**2, 2].sub_border_grid
A property that is only computed once per instance and then replaces itself with an ordinary attribute.
sub_shape_native
sub_shape_slim
- rtype:
sub_size
- rtype:
total_area
total_pixels
- rtype:
unmasked_grid
- classmethod no_mask(values, shape_native, pixel_scales, origin=(0.0, 0.0), fractional_accuracy=0.9999, relative_accuracy=None, sub_steps=None)[source]#
Create a Grid2DIterate (see Grid2DIterate.__new__) by inputting the grid coordinates in 1D, for example:
grid=np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]])
grid=[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]
From 1D input the method cannot determine the 2D shape of the grid and its mask, thus the shape_native must be input into this method. The mask is setup as a unmasked
Mask2D
of shape_native.- Parameters:
values (
Union
[ndarray
,List
]) – The (y,x) coordinates of the grid input as an ndarray of shape [total_unmasked_pixells*(sub_size**2), 2] or a list of lists.shape_native (
Tuple
[int
,int
]) – The 2D shape of the mask the grid is paired with.pixel_scales (
Union
[Tuple
[float
],Tuple
[float
,float
],float
]) – The (y,x) arcsecond-to-pixel units conversion factor of every pixel. If this is input as a float, it is converted to a (float, float).fractional_accuracy (
float
) – The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio of the value at a higher sub size to the value computed using the previous sub_size. The fractional accuracy does not depend on the units or magnitude of the function being evaluated.relative_accuracy (
Optional
[float
]) – The relative accuracy the function evaluted must meet to be accepted, where this value is the absolute difference of the values computed using the higher sub size and lower sub size grids. The relative accuracy depends on the units / magnitude of the function being evaluated.sub_steps ([int] or None) – The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None, they are setup as the default values [2, 4, 8, 16].
origin (
Tuple
[float
,float
]) – The origin of the grid’s mask.
- Return type:
- classmethod uniform(shape_native, pixel_scales, origin=(0.0, 0.0), fractional_accuracy=0.9999, relative_accuracy=None, sub_steps=None)[source]#
Create a Grid2DIterate (see Grid2DIterate.__new__) as a uniform grid of (y,x) values given an input shape_native and pixel scale of the grid:
- Parameters:
shape_native (
Tuple
[int
,int
]) – The 2D shape of the uniform grid and the mask that it is paired with.pixel_scales (
Union
[Tuple
[float
],Tuple
[float
,float
],float
]) – The (y,x) arcsecond-to-pixel units conversion factor of every pixel. If this is input as a float, it is converted to a (float, float).fractional_accuracy (
float
) – The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio of the value at a higher sub size to the value computed using the previous sub_size. The fractional accuracy does not depend on the units or magnitude of the function being evaluated.relative_accuracy (
Optional
[float
]) – The relative accuracy the function evaluted must meet to be accepted, where this value is the absolute difference of the values computed using the higher sub size and lower sub size grids. The relative accuracy depends on the units / magnitude of the function being evaluated.sub_steps ([int] or None) – The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None, they are setup as the default values [2, 4, 8, 16].
origin (
Tuple
[float
,float
]) – The origin of the grid’s mask.
- Return type:
- classmethod from_mask(mask, fractional_accuracy=0.9999, relative_accuracy=None, sub_steps=None)[source]#
Create a Grid2DIterate (see Grid2DIterate.__new__) from a mask, where only unmasked pixels are included in the grid (if the grid is represented in 2D masked values are (0.0, 0.0)).
The mask’s pixel_scales and origin properties are used to compute the grid (y,x) coordinates.
- Parameters:
mask (
Mask2D
) – The mask whose masked pixels are used to setup the sub-pixel grid.fractional_accuracy (
float
) – The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio of the value at a higher sub size to the value computed using the previous sub_size. The fractional accuracy does not depend on the units or magnitude of the function being evaluated.relative_accuracy (
Optional
[float
]) – The relative accuracy the function evaluted must meet to be accepted, where this value is the absolute difference of the values computed using the higher sub size and lower sub size grids. The relative accuracy depends on the units / magnitude of the function being evaluated.sub_steps ([int] or None) – The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None, they are setup as the default values [2, 4, 8, 16].
- Return type:
- classmethod blurring_grid_from(mask, kernel_shape_native, fractional_accuracy=0.9999, relative_accuracy=None, sub_steps=None)[source]#
Setup a blurring-grid from a mask, where a blurring grid consists of all pixels that are masked (and therefore have their values set to (0.0, 0.0)), but are close enough to the unmasked pixels that their values will be convolved into the unmasked those pixels. This when computing images from light profile objects.
See Grid2D.blurring_grid_from for a full description of a blurring grid. This method creates the blurring grid as a Grid2DIterate.
- Parameters:
mask (
Mask2D
) – The mask whose masked pixels are used to setup the blurring grid.kernel_shape_native (
Tuple
[int
,int
]) – The 2D shape of the kernel which convolves signal from masked pixels to unmasked pixels.fractional_accuracy (
float
) – The fractional accuracy the function evaluated must meet to be accepted, where this accuracy is the ratio of the value at a higher sub size to the value computed using the previous sub_size. The fractional accuracy does not depend on the units or magnitude of the function being evaluated.relative_accuracy (
Optional
[float
]) – The relative accuracy the function evaluted must meet to be accepted, where this value is the absolute difference of the values computed using the higher sub size and lower sub size grids. The relative accuracy depends on the units / magnitude of the function being evaluated.sub_steps ([int] or None) – The sub-size values used to iteratively evaluated the function at high levels of sub-gridding. If None, they are setup as the default values [2, 4, 8, 16].
- Return type:
- property slim: Grid2DIterate#
Return a
Grid2D
where the data is stored itsslim
representation, which is an ndarray of shape [total_unmasked_pixels * sub_size**2, 2].If it is already stored in its
slim
representation it is returned as it is. If not, it is mapped fromnative
toslim
and returned as a newGrid2D
.- Return type:
- property native: Grid2DIterate#
Return a
Grid2D
where the data is stored in itsnative
representation, which has shape [sub_size*total_y_pixels, sub_size*total_x_pixels, 2].If it is already stored in its
native
representation it is return as it is. If not, it is mapped fromslim
tonative
and returned as a newGrid2D
.This method is used in the child
Grid2D
classes to create theirnative
properties.- Return type:
- property binned: Grid2DIterate#
Return a
Grid2D
of the binned-up grid in its 1D representation, which is stored with shape [total_unmasked_pixels, 2].The binning up process converts a grid from (y,x) values where each value is a coordinate on the sub-grid to (y,x) values where each coordinate is at the centre of its mask (e.g. a grid with a sub_size of 1). This is performed by taking the mean of all (y,x) values in each sub pixel.
If the grid is stored in 1D it is return as is. If it is stored in 2D, it must first be mapped from 2D to 1D.
- Return type:
- grid_2d_via_deflection_grid_from(deflection_grid)[source]#
Returns a new Grid2DIterate from this grid, where the (y,x) coordinates of this grid have a grid of (y,x) values, termed the deflection grid, subtracted from them to determine the new grid of (y,x) values.
This is used by PyAutoLens to perform grid ray-tracing.
- Parameters:
deflection_grid (
ndarray
) – The grid of (y,x) coordinates which is subtracted from this grid.- Return type:
- blurring_grid_via_kernel_shape_from(kernel_shape_native)[source]#
Returns the blurring grid from a grid and create it as a Grid2DIterate, via an input 2D kernel shape.
For a full description of blurring grids, checkout blurring_grid_from.
- padded_grid_from(kernel_shape_native)[source]#
When the edge pixels of a mask are unmasked and a convolution is to occur, the signal of edge pixels will be ‘missing’ if the grid is used to evaluate the signal via an analytic function.
To ensure this signal is included the padded grid is used, which is ‘buffed’ such that it includes all pixels whose signal will be convolved into the unmasked pixels given the 2D kernel shape.
- threshold_mask_via_arrays_from(array_lower_sub_2d, array_higher_sub_2d)[source]#
Returns a fractional mask from a result array, where the fractional mask describes whether the evaluated value in the result array is within the
Grid2DIterate
’s specified fractional accuracy. The fractional mask thus determines whether a pixel on the grid needs to be reevaluated at a higher level of sub-gridding to meet the specified fractional accuracy. If it must be re-evaluated, the fractional masks’s entry isFalse
.The fractional mask is computed by comparing the results evaluated at one level of sub-gridding to another at a higher level of sub-griding. Thus, the sub-grid size in chosen on a per-pixel basis until the function is evaluated at the specified fractional accuracy.
- static threshold_mask_via_arrays_jit_from(fractional_accuracy_threshold, relative_accuracy_threshold, threshold_mask, array_higher_sub_2d, array_lower_sub_2d, array_higher_mask)[source]#
Jitted function to determine the fractional mask, which is a mask where:
True
entries signify the function has been evaluated in that pixel to desired accuracy and therefore does not need to be iteratively computed at higher levels of sub-gridding.False
entries signify the function has not been evaluated in that pixel to desired fractional accuracy and therefore must be iterative computed at higher levels of sub-gridding to meet this accuracy.
- Return type:
ndarray
- iterated_array_from(func, cls, array_lower_sub_2d)[source]#
Iterate over a function that returns an array of values until the it meets a specified fractional accuracy. The function returns a result on a pixel-grid where evaluating it on more points on a higher resolution sub-grid followed by binning lead to a more precise evaluation of the function. The function is assumed to belong to a class, which is input into tthe method.
The function is first called for a sub-grid size of 1 and a higher resolution grid. The ratio of values give the fractional accuracy of each function evaluation. Pixels which do not meet the fractional accuracy are iteratively revaluated on higher resolution sub-grids. This is repeated until all pixels meet the fractional accuracy or the highest sub-size specified in the sub_steps attribute is computed.
If the function return all zeros, the iteration is terminated early given that all levels of sub-gridding will return zeros. This occurs when a function is missing optional objects that contribute to the calculation.
An example use case of this function is when a “image_2d_from” methods in PyAutoGalaxy’s
LightProfile
module is comomputed, which by evaluating the function on a higher resolution sub-grids sample the analytic light profile at more points and thus more precisely.- Parameters:
func (func) – The function which is iterated over to compute a more precise evaluation.
cls (cls) – The class the function belongs to.
grid_lower_sub_2d – The results computed by the function using a lower sub-grid size
- Return type:
- return_iterated_array_result(iterated_array)[source]#
Returns the resulting iterated array, by mapping it to 1D and then passing it back as an
Array2D
structure.- Parameters:
iterated_array (
Array2D
) –- Returns:
The resulting array computed via iteration.
- Return type:
iterated_array
- static iterated_array_jit_from(iterated_array, threshold_mask_higher_sub, threshold_mask_lower_sub, array_higher_sub_2d)[source]#
Create the iterated array from a result array that is computed at a higher sub size leel than the previous grid.
The iterated array is only updated for pixels where the fractional accuracy is met.
- Return type:
ndarray
- threshold_mask_via_grids_from(grid_lower_sub_2d, grid_higher_sub_2d)[source]#
Returns a fractional mask from a result array, where the fractional mask describes whether the evaluated value in the result array is within the
Grid2DIterate
’s specified fractional accuracy. The fractional mask thus determines whether a pixel on the grid needs to be reevaluated at a higher level of sub-gridding to meet the specified fractional accuracy. If it must be re-evaluated, the fractional masks’s entry isFalse
.The fractional mask is computed by comparing the results evaluated at one level of sub-gridding to another at a higher level of sub-griding. Thus, the sub-grid size in chosen on a per-pixel basis until the function is evaluated at the specified fractional accuracy.
- static threshold_mask_via_grids_jit_from(fractional_accuracy_threshold, relative_accuracy_threshold, threshold_mask, grid_higher_sub_2d, grid_lower_sub_2d, grid_higher_mask)[source]#
Jitted function to determine the fractional mask, which is a mask where:
True
entries signify the function has been evaluated in that pixel to desired fractional accuracy and therefore does not need to be iteratively computed at higher levels of sub-gridding.False
entries signify the function has not been evaluated in that pixel to desired fractional accuracy and therefore must be iterative computed at higher levels of sub-gridding to meet this accuracy.
- Return type:
ndarray
- iterated_grid_from(func, cls, grid_lower_sub_2d)[source]#
Iterate over a function that returns a grid of values until the it meets a specified fractional accuracy. The function returns a result on a pixel-grid where evaluating it on more points on a higher resolution sub-grid followed by binning lead to a more precise evaluation of the function. For the fractional accuracy of the grid to be met, both the y and x values must meet it.
The function is first called for a sub-grid size of 1 and a higher resolution grid. The ratio of values give the fractional accuracy of each function evaluation. Pixels which do not meet the fractional accuracy are iteratively revaulated on higher resolution sub-grids. This is repeated until all pixels meet the fractional accuracy or the highest sub-size specified in the sub_steps attribute is computed.
If the function return all zeros, the iteration is terminated early given that all levels of sub-gridding will return zeros. This occurs when a function is missing optional objects that contribute to the calculation.
An example use case of this function is when a “deflections_yx_2d_from” methods in PyAutoLens’s
MassProfile
module is computed, which by evaluating the function on a higher resolution sub-grid samples the analytic mass profile at more points and thus more precisely.
- static iterated_grid_jit_from(iterated_grid, threshold_mask_higher_sub, threshold_mask_lower_sub, grid_higher_sub_2d)[source]#
Create the iterated grid from a result grid that is computed at a higher sub size level than the previous grid.
The iterated grid is only updated for pixels where the fractional accuracy is met in both the (y,x) coodinates.
- Return type:
- iterated_result_from(func, cls)[source]#
Iterate over a function that returns an array or grid of values until the it meets a specified fractional accuracy. The function returns a result on a pixel-grid where evaluating it on more points on a higher resolution sub-grid followed by binning lead to a more precise evaluation of the function.
A full description of the iteration method can be found in the functions iterated_array_from and iterated_grid_from. This function computes the result on a grid with a sub-size of 1, and uses its shape to call the correct function.