autolens.Grid2D#

class Grid2D[source]#

Bases: Structure

A grid of 2D (y,x) coordinates, which are paired to a uniform 2D mask of pixels and sub-pixels. Each entry on the grid corresponds to the (y,x) coordinates at the centre of a sub-pixel of an unmasked pixel.

A Grid2D is ordered such that pixels begin from the top-row (e.g. index [0, 0]) of the corresponding mask and go right and down. The positive y-axis is upwards and positive x-axis to the right.

The grid can be stored in two formats:

  • slimmed: all masked entries are removed so the ndarray is shape [total_unmasked_coordinates*sub_size**2, 2]

  • native: it retains the original shape of the grid so the ndarray is shape [total_y_coordinates*sub_size, total_x_coordinates*sub_size, 2].

Case 1 (sub-size=1, slim)

The Grid2D is an ndarray of shape [total_unmasked_coordinates, 2], therefore when slim the shape of the grid is 2, not 1.

The first element of the ndarray corresponds to the pixel index and second element the y or x coordinate value.

For example:

  • grid[3,0] = the 4th unmasked pixel’s y-coordinate.

  • grid[6,1] = the 7th unmasked pixel’s x-coordinate.

Below is a visual illustration of a grid, where a total of 10 pixels are unmasked and are included in the grid.

x x x x x x x x x x
x x x x x x x x x x     This is an example mask.Mask2D, where:
x x x x x x x x x x
x x x x O O x x x x     x = `True` (Pixel is masked and excluded from the grid)
x x x O O O O x x x     O = `False` (Pixel is not masked and included in the grid)
x x x O O O O x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x

The mask pixel index’s will come out like this (and the direction of scaled coordinates is highlighted around the mask.

pixel_scales = 1.0"

<--- -ve  x  +ve -->
                                                y      x
 x x x x x x x x x x  ^   grid[0] = [ 1.5, -0.5]
 x x x x x x x x x x  I   grid[1] = [ 1.5,  0.5]
 x x x x x x x x x x  I   grid[2] = [ 0.5, -1.5]
 x x x x 0 1 x x x x +ve  grid[3] = [ 0.5, -0.5]
 x x x 2 3 4 5 x x x  y   grid[4] = [ 0.5,  0.5]
 x x x 6 7 8 9 x x x -ve  grid[5] = [ 0.5,  1.5]
 x x x x x x x x x x  I   grid[6] = [-0.5, -1.5]
 x x x x x x x x x x  I   grid[7] = [-0.5, -0.5]
 x x x x x x x x x x \/   grid[8] = [-0.5,  0.5]
 x x x x x x x x x x      grid[9] = [-0.5,  1.5]

Case 2 (sub-size>1, slim)

If the mask’s sub_size is > 1, the grid is defined as a sub-grid where each entry corresponds to the (y,x) coordinates at the centre of each sub-pixel of an unmasked pixel. The Grid2D is therefore stored as an ndarray of shape [total_unmasked_coordinates*sub_size**2, 2]

The sub-grid indexes are ordered such that pixels begin from the first (top-left) sub-pixel in the first unmasked pixel. Indexes then go over the sub-pixels in each unmasked pixel, for every unmasked pixel. Therefore, the sub-grid is an ndarray of shape [total_unmasked_coordinates*(sub_grid_shape)**2, 2].

For example:

  • grid[9, 1] - using a 2x2 sub-grid, gives the 3rd unmasked pixel’s 2nd sub-pixel x-coordinate.

  • grid[9, 1] - using a 3x3 sub-grid, gives the 2nd unmasked pixel’s 1st sub-pixel x-coordinate.

  • grid[27, 0] - using a 3x3 sub-grid, gives the 4th unmasked pixel’s 1st sub-pixel y-coordinate.

Below is a visual illustration of a sub grid. Indexing of each sub-pixel goes from the top-left corner. In contrast to the grid above, our illustration below restricts the mask to just 2 pixels, to keep the illustration brief.

x x x x x x x x x x
x x x x x x x x x x     This is an example mask.Mask2D, where:
x x x x x x x x x x
x x x x x x x x x x     x = `True` (Pixel is masked and excluded from lens)
x x x x O O x x x x     O = `False` (Pixel is not masked and included in lens)
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x

Our grid with a sub-size looks like it did before:

pixel_scales = 1.0"

<--- -ve  x  +ve -->

 x x x x x x x x x x  ^
 x x x x x x x x x x  I
 x x x x x x x x x x  I                        y     x
 x x x x x x x x x x +ve  grid[0] = [0.5,  -1.5]
 x x x x 0 1 x x x x  y   grid[1] = [0.5,  -0.5]
 x x x x x x x x x x -ve
 x x x x x x x x x x  I
 x x x x x x x x x x  I
 x x x x x x x x x x \/
 x x x x x x x x x x

However, if the sub-size is 2, we go to each unmasked pixel and allocate sub-pixel coordinates for it. For example, for pixel 0, if sub_size=2, we use a 2x2 sub-grid:

Pixel 0 - (2x2):
                    y      x
       grid[0] = [0.66, -1.66]
I0I1I  grid[1] = [0.66, -1.33]
I2I3I  grid[2] = [0.33, -1.66]
       grid[3] = [0.33, -1.33]

If we used a sub_size of 3, for the pixel we we would create a 3x3 sub-grid:

                      y      x
         grid[0] = [0.75, -0.75]
         grid[1] = [0.75, -0.5]
         grid[2] = [0.75, -0.25]
I0I1I2I  grid[3] = [0.5,  -0.75]
I3I4I5I  grid[4] = [0.5,  -0.5]
I6I7I8I  grid[5] = [0.5,  -0.25]
         grid[6] = [0.25, -0.75]
         grid[7] = [0.25, -0.5]
         grid[8] = [0.25, -0.25]

Case 3 (sub_size=1, native)

The Grid2D has the same properties as Case 1, but is stored as an an ndarray of shape [total_y_coordinates, total_x_coordinates, 2]. Therefore when native the shape of the grid is 3, not 2.

All masked entries on the grid has (y,x) values of (0.0, 0.0).

For the following example mask:

x x x x x x x x x x
x x x x x x x x x x     This is an example mask.Mask2D, where:
x x x x x x x x x x
x x x x O O x x x x     x = `True` (Pixel is masked and excluded from the grid)
x x x O O O O x x x     O = `False` (Pixel is not masked and included in the grid)
x x x O O O O x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x
x x x x x x x x x x

In the above grid:

  • grid[0,0,0] = 0.0 (it is masked, thus zero)

  • grid[0,0,1] = 0.0 (it is masked, thus zero)

  • grid[3,3,0] = 0.0 (it is masked, thus zero)

  • grid[3,3,1] = 0.0 (it is masked, thus zero)

  • grid[3,4,0] = 1.5

  • grid[3,4,1] = -0.5

Case 4 (sub_size>1 native)

The properties of this grid can be derived by combining Case’s 2 and 3 above, whereby the grid is stored as an ndarray of shape [total_y_coordinates*sub_size, total_x_coordinates*sub_size, 2].

All sub-pixels in masked pixels have values (0.0, 0.0).

Grid2D Mapping:

Every set of (y,x) coordinates in a pixel of the sub-grid maps to an unmasked pixel in the mask. For a uniform grid, every (y,x) coordinate directly corresponds to the location of its paired unmasked pixel.

It is not a requirement that grid is uniform and that their coordinates align with the mask. The input grid could be an irregular set of (y,x) coordinates where the indexing signifies that the (y,x) coordinate originates or is paired with the mask’s pixels but has had its value change by some aspect of the calculation.

This is important for the child project PyAutoLens, where grids in the image-plane are ray-traced and deflected to perform lensing calculations. The grid indexing is used to map pixels between the image-plane and source-plane.

Parameters
  • values – The (y,x) coordinates of the grid.

  • mask – The 2D mask associated with the grid, defining the pixels each grid coordinate is paired with and originates from.

  • store_native – 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

Returns True if all elements evaluate to True.

any

Returns True if any of the elements of a evaluate to True.

argmax

Return indices of the maximum values along the given axis.

argmin

Return indices of the minimum values along the given axis.

argpartition

Returns the indices that would partition this array.

argsort

Returns the indices that would sort this array.

astype

Copy of the array, cast to a specified type.

blurring_grid_from

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.

blurring_grid_via_kernel_shape_from

Returns the blurring grid from a grid, 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.

byteswap

Swap the bytes of the array elements

choose

Use an index array to construct a new array from a set of choices.

clip

Return an array whose values are limited to [min, max].

compress

Return selected slices of this array along given axis.

conj

Complex-conjugate all elements.

conjugate

Return the complex conjugate, element-wise.

copy

Return a copy of the array.

cumprod

Return the cumulative product of the elements along the given axis.

cumsum

Return the cumulative sum of the elements along the given axis.

diagonal

Return specified diagonals.

distances_to_coordinate_from

Returns the distance of every coordinate on the grid from an input (y,x) coordinate.

dot

dump

Dump a pickle of the array to the specified file.

dumps

Returns the pickle of the array as a string.

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

fill

Fill the array with a scalar value.

flatten

Return a copy of the array collapsed into one dimension.

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

from_mask

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

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.

getfield

Returns a field of the given array as a certain type.

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.

grid_2d_via_deflection_grid_from

Returns a new Grid2D 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_with_coordinates_within_distance_removed_from

Remove all coordinates from this Grid2D which are within a certain distance of an input list of coordinates.

item

Copy an element of an array to a standard Python scalar and return it.

itemset

Insert scalar into an array (scalar is cast to array's dtype, if possible)

max

Return the maximum along a given axis.

mean

Returns the average of the array elements along given axis.

min

Return the minimum along a given axis.

newbyteorder

Return the array with the same data viewed with a different byte order.

no_mask

Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates in 1D or 2D, automatically determining whether to use the 'manual_slim' or 'manual_native' methods.

nonzero

Return the indices of the elements that are non-zero.

output_to_fits

Output the grid to a .fits file.

padded_grid_from

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.

partition

Rearranges the elements in the array in such a way that the value of the element in kth position is in the position it would be in a sorted array.

prod

Return the product of the array elements over the given axis

ptp

Peak to peak (maximum - minimum) value along a given axis.

put

Set a.flat[n] = values[n] for all n in indices.

ravel

Return a flattened array.

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.

repeat

Repeat elements of an array.

reshape

Returns an array containing the same data with a new shape.

resize

Change shape and size of array in-place.

round

Return a with each element rounded to the given number of decimals.

searchsorted

Find indices where elements of v should be inserted in a to maintain order.

setfield

Put a value into a specified place in a field defined by a data-type.

setflags

Set array flags WRITEABLE, ALIGNED, WRITEBACKIFCOPY, respectively.

sort

Sort an array in-place.

squared_distances_to_coordinate_from

Returns the squared distance of every coordinate on the grid from an input coordinate.

squeeze

Remove axes of length one from a.

std

Returns the standard deviation of the array elements along given axis.

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

Return the sum of the array elements over the given axis.

swapaxes

Return a view of the array with axis1 and axis2 interchanged.

take

Return an array formed from the elements of a at the given indices.

tobytes

Construct Python bytes containing the raw data bytes in the array.

tofile

Write array to a file as text or binary (default).

tolist

Return the array as an a.ndim-levels deep nested list of Python scalars.

tostring

A compatibility alias for tobytes, with exactly the same behavior.

trace

Return the sum along diagonals of the array.

transpose

Returns a view of the array with axes transposed.

trimmed_after_convolution_from

rtype

Structure

uniform

Create a Grid2D (see Grid2D.__new__) as a uniform grid of (y,x) values given an input shape_native and pixel_scales of the grid:

values_from

Create a ArrayIrregular object from a 1D NumPy array of values of shape [total_coordinates].

var

Returns the variance of the array elements, along given axis.

view

New view of array with the same data.

Attributes

T

The transposed array.

base

Base object if memory is from some other object.

binned

Return a Grid2D of the binned-up grid in its 1D representation, which is stored with shape [total_unmasked_pixels, 2].

ctypes

An object to simplify the interaction of the array with the ctypes module.

data

Python buffer object pointing to the start of the array's data.

derive_grid

rtype

DeriveGrid2D

derive_indexes

rtype

DeriveIndexes2D

derive_mask

rtype

DeriveMask2D

dtype

Data-type of the array's elements.

flags

Information about the memory layout of the array.

flat

A 1-D iterator over the array.

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

The imaginary part of the array.

in_radians

Return the grid as an ndarray where all (y,x) values are converted to Radians.

itemsize

Length of one array element in bytes.

native

Return a Grid2D where the data is stored in its native representation, which has shape [sub_size*total_y_pixels, sub_size*total_x_pixels, 2].

nbytes

Total bytes consumed by the elements of the array.

ndim

Number of array dimensions.

origin

rtype

Tuple[int, ...]

pixel_area

pixel_scale

rtype

float

pixel_scale_header

rtype

Dict

pixel_scales

rtype

Tuple[float, ...]

real

The real part of the array.

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

Tuple of array dimensions.

shape_native

rtype

Tuple[int, ...]

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

int

size

Number of elements in the array.

slim

Return a Grid2D where the data is stored its slim representation, which is an ndarray of shape [total_unmasked_pixels * sub_size**2, 2].

strides

Tuple of bytes to step in each dimension when traversing an array.

sub_border_grid

A property that is only computed once per instance and then replaces itself with an ordinary attribute.

sub_shape_native

rtype

Tuple[int, ...]

sub_shape_slim

rtype

int

sub_size

rtype

int

total_area

total_pixels

rtype

int

unmasked_grid

rtype

Union[Grid1D, Grid2D]

classmethod no_mask(values, pixel_scales, shape_native=None, sub_size=1, origin=(0.0, 0.0))[source]#

Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates in 1D or 2D, automatically determining whether to use the ‘manual_slim’ or ‘manual_native’ methods.

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.

The 2D shape of the grid and its mask are determined from the input grid and the mask is setup as an 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 (Optional[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).

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin (Tuple[float, float]) – The origin of the grid’s mask.

Return type

Grid2D

classmethod from_yx_1d(y, x, shape_native, pixel_scales, sub_size=1, origin=(0.0, 0.0))[source]#

Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates as 1D y and x values.

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
  • list (x or) – The y coordinates of the grid input as an ndarray of shape [total_coordinates] or list.

  • list – The x coordinates of the grid input as an ndarray of shape [total_coordinates] or list.

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

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin (Tuple[float, float]) – The origin of the grid’s mask.

Examples

 import autoarray as aa

 # Make Grid2D from input np.ndarray.

 grid_2d = aa.Grid2D.from_yx_1d(
     y=np.array([1.0, 3.0, 5.0, 7.0]),
     x=np.array([2.0, 4.0, 6.0, 8.0]),
     shape_native=(2, 2),
     pixel_scales=1.0,
 )

 # Make Grid2D from input list.

grid_2d = aa.Grid2D.from_yx_1d(
     y=[1.0, 3.0, 5.0, 7.0],
     x=[2.0, 4.0, 6.0, 8.0],
     shape_native=(2, 2),
     pixel_scales=1.0,
 )

 # Print grid's slim (masked 1D data representation) and
 # native (masked 2D data representation)

 print(grid_2d.slim)
 print(grid_2d.native)
Return type

Grid2D

classmethod from_yx_2d(y, x, pixel_scales, sub_size=1, origin=(0.0, 0.0))[source]#

Create a Grid2D (see Grid2D.__new__) by inputting the grid coordinates as 2D y and x values.

The 2D shape of the grid and its mask are determined from the input grid and the mask is setup as an unmasked Mask2D of shape_native.

Parameters
  • list (x or) – The y coordinates of the grid input as an ndarray of shape [total_coordinates] or list.

  • list – The x coordinates of the grid input as an ndarray of shape [total_coordinates] or list.

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

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin (Tuple[float, float]) – The origin of the grid’s mask.

Examples

import autoarray as aa

# Make Grid2D from input list(s).

grid_2d = aa.Grid2D.from_yx_2d(
    y=[[1.0], [3.0]],
    x=[[2.0], [4.0]],
    pixel_scales=1.0
)
Return type

Grid2D

classmethod from_extent(extent, shape_native, sub_size=1)[source]#

Create a Grid2D (see Grid2D.__new__) by inputting the extent of the (y,x) grid coordinates as an input (x0, x1, y0, y1) tuple.

The (y,x) shape_native in pixels is also input which determines the resolution of the Grid2D.

(The PyAutoArray API typically uses a (y,x) notation, however extent variables begin with x currently. This will be updated in a future release):

extent = (x0, x1, y0, y1) = (2.0, 4.0, -2.0, 6.0) shape_native = (y,x) = (10, 20)

Parameters
  • extent (Tuple[float, float, float, float]) – The (x0, x1, y0, y1) extent of the grid in scaled coordinates over which the grid is created.

  • shape_native (Tuple[int, int]) – The 2D shape of the grid that is created within this extent.

  • pixel_scales – 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).

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin – The origin of the grid’s mask.

Return type

Grid2D

classmethod uniform(shape_native, pixel_scales, sub_size=1, origin=(0.0, 0.0))[source]#

Create a Grid2D (see Grid2D.__new__) as a uniform grid of (y,x) values given an input shape_native and pixel_scales 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) scaled units to pixel units conversion factors of every pixel. If this is input as a float, it is converted to a (float, float) tuple.

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin (Tuple[float, float]) – The origin of the grid’s mask.

Return type

Grid2D

classmethod bounding_box(bounding_box, shape_native, sub_size=1, buffer_around_corners=False)[source]#

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.

If buffer_around_corners=True, the grid’s (y,x) values fully align with the input bounding box values. This means the mask’s edge pixels extend beyond the bounding box by pixel_scale/2.0. If buffer_around_corners=False, the grid (y,x) coordinates are defined within the bounding box such that the mask’s edge pixels align with the bouning box.

Parameters
  • shape_native (Tuple[int, int]) – The 2D shape of the uniform grid and the mask that it is paired with.

  • pixel_scales – 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).

  • sub_size (int) – The size (sub_size x sub_size) of each unmasked pixels sub-grid.

  • origin – The origin of the grid’s mask.

  • buffer_around_corners (bool) – Whether the grid is buffered such that the (y,x) values in the centre of its masks’ edge pixels align with the input bounding box values.

Return type

Grid2D

classmethod from_mask(mask)[source]#

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

The mask’s pixel_scales, sub_size 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.

Return type

Grid2D

classmethod from_fits(file_path, pixel_scales, sub_size=1, origin=(0.0, 0.0))[source]#

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

The mask’s pixel_scales, sub_size and origin properties are used to compute the grid (y,x) coordinates.

Parameters

mask – The mask whose masked pixels are used to setup the sub-pixel grid.

Return type

Grid2D

classmethod blurring_grid_from(mask, kernel_shape_native)[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.

The mask’s pixel_scales, sub_size and origin properties are used to compute the blurring grid’s (y,x) coordinates.

For example, if our mask is as follows:

x x x x x x x x x xI
x x x x x x x x x xI     This is an imaging.Mask2D, where
x x x x x x x x x xI
x x x x x x x x x xI     x = `True` (Pixel is masked and excluded from lens)
x x x O O O x x x xI     O = `False` (Pixel is not masked and included in lens)
x x x O O O x x x xI
x x x O O O x x x xI
x x x x x x x x x xI
x x x x x x x x x xI
x x x x x x x x x xI

For a PSF of shape (3,3), the following blurring mask is computed (noting that only pixels that are direct neighbors of the unmasked pixels above will blur light into an unmasked pixel)

x x x x x x x x xI     This is an example grid.Mask2D, where
x x x x x x x x xI
x x O O O O O x xI     x = `True` (Pixel is masked and excluded from lens)
x x O x x x O x xI     O = `False` (Pixel is not masked and included in lens)
x x O x x x O x xI
x x O x x x O x xI
x x O O O O O x xI
x x x x x x x x xI
x x x x x x x x xI

Thus, the blurring grid coordinates and indexes will be as follows

pixel_scales = 1.0"

positive    negative
                                                    y     x                          y     x
 x x x  x  x  x  x  x xI  I   blurring_grid[0] = [2.0, -2.0]  blurring_grid[9] =  [-1.0, -2.0]
 x x x  x  x  x  x  x xI  I   blurring_grid[1] = [2.0, -1.0]  blurring_grid[10] = [-1.0,  2.0]
 x xI0 I1 I2 I3 I4  x xI pos  blurring_grid[2] = [2.0,  0.0]  blurring_grid[11] = [-2.0, -2.0]
 x xI5  x  x  x I6  x xI  y   blurring_grid[3] = [2.0,  1.0]  blurring_grid[12] = [-2.0, -1.0]
 x xI7  x  x  x I8  x xI  I   blurring_grid[4] = [2.0,  2.0]  blurring_grid[13] = [-2.0,  0.0]
 x xI9  x  x  x I10 x xI neg  blurring_grid[5] = [1.0, -2.0]  blurring_grid[14] = [-2.0,  1.0]
 x xI11I12I13I14I15 x xI  I   blurring_grid[6] = [1.0,  2.0]  blurring_grid[15] = [-2.0,  2.0]
 x x x  x  x  x  x  x xI  I   blurring_grid[7] = [0.0, -2.0]
 x x x  x  x  x  x  x xI  I   blurring_grid[8] = [0.0,  2.0]

For a PSF of shape (5,5), the following blurring mask is computed (noting that pixels are 2 pixels from a direct unmasked pixels now blur light into an unmasked pixel)

x x x x x x x x xI     This is an example grid.Mask2D, where
xIoIoIoIoIoIoIo xI
xIoIoIoIoIoIoIo xI     x = `True` (Pixel is masked and excluded from lens)
xIoIo x x xIoIo xI     O = `False` (Pixel is not masked and included in lens)
xIoIo x x xIoIo xI
xIoIo x x xIoIo xI
xIoIoIoIoIoIoIo xI
xIoIoIoIoIoIoIo xI
x x x x x x x x xI
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.

Return type

Grid2D

property slim: Grid2D#

Return a Grid2D where the data is stored its slim 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 from native to slim and returned as a new Grid2D.

Return type

Grid2D

property native: Grid2D#

Return a Grid2D where the data is stored in its native 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 from slim to native and returned as a new Grid2D.

This method is used in the child Grid2D classes to create their native properties.

Return type

Grid2D

property binned: Grid2D#

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

Grid2D

property flipped: Grid2D#

Return the grid as an ndarray of shape [total_unmasked_pixels, 2] with flipped values such that coordinates are given as (x,y) values.

This is used to interface with Python libraries that require the grid in (x,y) format.

Return type

Grid2D

property in_radians: Grid2D#

Return the grid as an ndarray where all (y,x) values are converted to Radians.

This grid is used by the interferometer module.

Return type

Grid2D

grid_2d_via_deflection_grid_from(deflection_grid)[source]#

Returns a new Grid2D 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 (Grid2D) – The grid of (y,x) coordinates which is subtracted from this grid.

Return type

Grid2D

blurring_grid_via_kernel_shape_from(kernel_shape_native)[source]#

Returns the blurring grid from a grid, via an input 2D kernel shape.

For a full description of blurring grids, checkout blurring_grid_from.

Parameters
kernel_shape_native

The 2D shape of the kernel which convolves signal from masked pixels to unmasked pixels.

:rtype: :py:class:`~autoarray.structures.grids.uniform_2d.Grid2D`
grid_with_coordinates_within_distance_removed_from(coordinates, distance)[source]#

Remove all coordinates from this Grid2D which are within a certain distance of an input list of coordinates.

For example, if the grid has the coordinate (0.0, 0.0) and coordinates=[(0.0, 0.0)], distance=0.1 is input into this function, a new Grid2D will be created which removes the coordinate (0.0, 0.0).

Parameters
  • coordinates ([(float, float)]) – The list of coordinates which are removed from the grid if they are within the distance threshold.

  • distance (float) – The distance threshold that coordinates are removed if they are within that of the input coordinates.

Return type

Grid2D

structure_2d_from(result)[source]#

Convert a result from an ndarray to an aa.Array2D or aa.Grid2D structure, where the conversion depends on type(result) as follows:

  • 1D np.ndarray -> aa.Array2D

  • 2D np.ndarray -> aa.Grid2D

This function is used by the grid_2d_to_structure decorator to convert the output result of a function to an autoarray structure when a Grid2D instance is passed to the decorated function.

Parameters

[np.ndarray] (result or) – The input result (e.g. of a decorated function) that is converted to a PyAutoArray structure.

Return type

Union[Array2D, Grid2D]

structure_2d_list_from(result_list)[source]#

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:

  • [1D np.ndarray] -> [aa.Array2D]

  • [2D np.ndarray] -> [aa.Grid2D]

This function is used by the grid_like_list_to_structure-list decorator to convert the output result of a function to a list of autoarray structure when a Grid2D instance is passed to the decorated function.

Parameters

[np.ndarray] (result_list or) – The input result (e.g. of a decorated function) that is converted to a PyAutoArray structure.

Return type

List[Union[Array2D, Grid2D]]

values_from(array_slim)[source]#

Create a ArrayIrregular object from a 1D NumPy array of values of shape [total_coordinates]. The ArrayIrregular are structured following this Grid2DIrregular instance.

Return type

ArrayIrregular

squared_distances_to_coordinate_from(coordinate=(0.0, 0.0))[source]#

Returns the squared distance of every coordinate on the grid from an input coordinate.

Parameters

coordinate (Tuple[float, float]) – The (y,x) coordinate from which the squared distance of every grid (y,x) coordinate is computed.

Return type

Array2D

distances_to_coordinate_from(coordinate=(0.0, 0.0))[source]#

Returns the distance of every coordinate on the grid from an input (y,x) coordinate.

Parameters

coordinate (Tuple[float, float]) – The (y,x) coordinate from which the distance of every grid (y,x) coordinate is computed.

Return type

Array2D

grid_2d_radial_projected_shape_slim_from(centre=(0.0, 0.0))[source]#

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.

To do this, the function first performs these 3 steps:

  1. Given the region defined by the extent [xmin, xmax, ymin, ymax], the algorithm finds the longest 1D distance of the 4 paths from the (y,x) centre to the edge of the region (e.g. following the positive / negative y and x axes).

  2. Use the pixel-scale corresponding to the direction chosen (e.g. if the positive x-axis was the longest, the pixel_scale in the x dimension is used).

  3. Determine the number of pixels between the centre and the edge of the region using the longest path between the two chosen above.

A schematic is shown below:

-------------------
|                 |
|<- - -  - ->x    | x = centre
|                 | <-> = longest radial path from centre to extent edge
|                 |
-------------------

Using the centre x above, this function finds the longest radial path to the edge of the extent window.

This function returns the integer number of pixels given by this radial grid, which is then used to create the radial grid.

Parameters
  • extent – The extent of the grid the radii grid is computed using, with format [xmin, xmax, ymin, ymax]

  • centre ((float, flloat)) – The (y,x) central coordinate which the radial grid is traced outwards from.

  • pixel_scales – The (y,x) scaled units to pixel units conversion factor of the 2D mask array.

  • sub_size – The size of the sub-grid that each pixel of the 2D mask array is divided into.

Returns

The 1D integer shape of a radial set of points sampling the longest distance from the centre to the edge of the extent in along the positive x-axis.

Return type

int

grid_2d_radial_projected_from(centre=(0.0, 0.0), angle=0.0, shape_slim=0)[source]#

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.

This functions operates as follows:

  1. Given the region defined by the extent [xmin, xmax, ymin, ymax], the algorithm finds the longest 1D distance of the 4 paths from the (y,x) centre to the edge of the region e.g. following the positive / negative y and x axes.

  2. Use the pixel-scale corresponding to the direction chosen e.g. if the positive x-axis was the longest, the pixel_scale in the x dimension is used.

  3. Determine the number of pixels between the centre and the edge of the region using the longest path between the two chosen above.

  4. Create a (y,x) grid of radial points where all points are at the centre’s y value = 0.0 and the x values iterate from the centre in increasing steps of the pixel-scale.

  5. Rotate these radial coordinates by the input angle clockwise.

A schematic is shown below:

-------------------
|                 |
|<- - -  - ->x    | x = centre
|                 | <-> = longest radial path from centre to extent edge
|                 |
-------------------
Parameters
  • centre (Tuple[float, float]) – The (y,x) central coordinate which the radial grid is traced outwards from.

  • angle (float) – The angle with which the radial coordinates are rotated clockwise.

Returns

A radial set of points sampling the longest distance from the centre to the edge of the extent in along the positive x-axis.

Return type

Grid2DIrregular

property shape_native_scaled_interior: Tuple[float, float]#

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.

This differs from the shape_native_scaled because the edges of the shape are at the maxima and minima of the grid’s (y,x) values, whereas the shape_native_scaled uses the uniform geometry of the grid and its pixel_scales, which means it has a buffer at each edge of half a pixel_scale.

Return type

Tuple[float, float]

property scaled_minima: Tuple#

The (y,x) minimum values of the grid in scaled units, buffed such that their extent is further than the grid’s extent.

Return type

Tuple

property scaled_maxima: Tuple#

The (y,x) maximum values of the grid in scaled units, buffed such that their extent is further than the grid’s extent.

Return type

Tuple

extent_with_buffer_from(buffer=1e-08)[source]#

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

This follows the format of the extent input parameter in the matplotlib method imshow (and other methods) and is used for visualization in the plot module.

Return type

List[float]

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.

Parameters

kernel_shape_native (Tuple[int, int]) – The 2D shape of the kernel which convolves signal from masked pixels to unmasked pixels.

Return type

Grid2D

relocated_grid_from(grid)[source]#

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

This is performed as follows:

1: Use the mean value of the grid’s y and x coordinates to determine the origin of the grid. 2: Compute the radial distance of every grid coordinate from the origin. 3: For every coordinate, find its nearest pixel in the border. 4: Determine if it is outside the border, by comparing its radial distance from the origin to its paired border pixel’s radial distance. 5: If its radial distance is larger, use the ratio of radial distances to move the coordinate to the border (if its inside the border, do nothing).

The method can be used on uniform or irregular grids, however for irregular grids the border of the ‘image-plane’ mask is used to define border pixels.

Parameters

grid (Grid2D) – The grid (uniform or irregular) whose pixels are to be relocated to the border edge if outside it.

Return type

Grid2D

relocated_mesh_grid_from(mesh_grid)[source]#

Relocate the coordinates of a pixelization grid to the border of this grid. See the method relocated_grid_from() for a full description of how this grid relocation works.

This function operates the same as other grid relocation functions but instead returns the grid as a Grid2DSparse instance, which contains information pairing the grid to a pixelization.

Parameters

grid – The pixelization grid whose pixels are relocated to the border edge if outside it.

Return type

Grid2DSparse