autolens.Array2D#

class Array2D[source]#

Bases: AbstractArray2D

A uniform 2D array of values, which are paired with a 2D mask of pixels.

The Array2D`, like all data structures (e.g. ``Grid2D, VectorYX2D) has in-built functionality which:

  • Applies a 2D mask (a Mask2D object) to the da_ta structure’s values.

  • Maps the data structure between two data representations: slim` (all unmasked values in a 1D ndarray) and native (all unmasked values in a 2D ndarray).

  • Associates Cartesian Grid2D objects of (y,x) coordinates with the data structure (e.g. a (y,x) grid of all unmasked pixels).

  • Associates grids with the data structure, which perform calculations higher resolutions which are then binned up.

Each entry of an Array2D corresponds to a value at the centre of a pixel in its corresponding Mask2D. It is ordered such that pixels begin from the top-row of the corresponding mask and go right and down. The positive y-axis is upwards and positive x-axis to the right.

A detailed description of the data structure API is provided below.

__slim__

Below is a visual illustration of an Array2D’s 2D mask, where a total of 10 pixels are unmasked and are included in the array.

x x x x x x x x x x
x x x x x x x x x x     This is an example ``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 array)
x x x O O O O x x x     O = `False` (Pixel is not masked and included in the array)
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 are as follows (the positive / negative direction of the Grid2D objects associated with the array are also shown on the y and x axes).

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

 x x x x x x x x x x  ^   array_2d[0] = 10
 x x x x x x x x x x  I   array_2d[1] = 20
 x x x x x x x x x x  I   array_2d[2] = 30
 x x x x 0 1 x x x x +ve  array_2d[3] = 40
 x x x 2 3 4 5 x x x  y   array_2d[4] = 50
 x x x 6 7 8 9 x x x -ve  array_2d[5] = 60
 x x x x x x x x x x  I   array_2d[6] = 70
 x x x x x x x x x x  I   array_2d[7] = 80
 x x x x x x x x x x \/   array_2d[8] = 90
 x x x x x x x x x x      array_2d[9] = 100

The Array2D in its slim data representation is an ndarray of shape [total_unmasked_pixels].

For the Mask2D above the slim representation therefore contains 10 entries and two examples of these entries are:

array[3] = the 4th unmasked pixel's value, given by value 40 above.
array[6] = the 7th unmasked pixel's value, given by value 80 above.

A Cartesian grid of (y,x) coordinates, corresponding to all slim values (e.g. unmasked pixels) is given by array_2d.derive_grid.masked.slim.

__native__

The Array2D above, but represented as an an ndarray of shape [total_y_values, total_x_values], where all masked entries have values of 0.0.

For the following 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 ``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 array)
x x x O O O O x x x     O = `False` (Pixel is not masked and included in the array)
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

Where the array has the following indexes (left figure) and values (right):

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

 x x x x x x x x x x  ^   array_2d[0] = 10
 x x x x x x x x x x  I   array_2d[1] = 20
 x x x x x x x x x x  I   array_2d[2] = 30
 x x x x 0 1 x x x x +ve  array_2d[3] = 40
 x x x 2 3 4 5 x x x  y   array_2d[4] = 50
 x x x 6 7 8 9 x x x -ve  array_2d[5] = 60
 x x x x x x x x x x  I   array_2d[6] = 70
 x x x x x x x x x x  I   array_2d[7] = 80
 x x x x x x x x x x \/   array_2d[8] = 90
 x x x x x x x x x x      array_2d[9] = 100

In the above array:

- array[0,0] = 0.0 (it is masked, thus zero)
- array[0,0] = 0.0 (it is masked, thus zero)
- array[3,3] = 0.0 (it is masked, thus zero)
- array[3,3] = 0.0 (it is masked, thus zero)
- array[3,4] = 10
- array[3,5] = 20
- array[4,5] = 50

SLIM TO NATIVE MAPPING

The Array2D has functionality which maps data between the slim and native data representations.

For the example mask above, the 1D ndarray given by mask.derive_indexes.slim_to_native is:

slim_to_native[0] = [3,4]
slim_to_native[1] = [3,5]
slim_to_native[2] = [4,3]
slim_to_native[3] = [4,4]
slim_to_native[4] = [4,5]
slim_to_native[5] = [4,6]
slim_to_native[6] = [5,3]
slim_to_native[7] = [5,4]
slim_to_native[8] = [5,5]
slim_to_native[9] = [5,6]

In PyAutoCTI all Array2D objects are used in their native representation. Significant memory can be saved by only store this format, thus the native_only config override can force this behaviour. It is recommended users do not use this option to avoid unexpected behaviour.

Parameters:
  • values (Union[ndarray, List, AbstractArray2D]) – The values of the array, which can be input in the slim or native format.

  • mask (Mask2D) – The 2D mask associated with the array, defining the pixels each array value in its slim representation is paired with.

  • header (Header) – Optional metadata header (e.g. from a FITS file) associated with the array.

  • store_native (bool) – If True, the ndarray is stored in its native format [total_y_pixels, total_x_pixels]. This avoids mapping large data arrays to and from the slim / native formats, which can be a computational bottleneck.

  • skip_mask (bool) – If True, masking is skipped and values are stored as-is without zeroing masked entries.

  • xp – The array module to use (default numpy; pass jax.numpy for JAX support).

Examples

This example uses the Array2D.no_mask method to create the Array2D.

Different methods using different inputs are available and documented throughout this webpage.

import autoarray as aa

array_2d = aa.Array2D.no_mask(
    values=np.array([1.0, 2.0, 3.0, 4.0]),
    shape_native=(2, 2),
    pixel_scales=1.0,
)
import autoarray as aa

array_2d = aa.Array2D.no_mask(
    values=[1.0, 2.0, 3.0, 4.0],
    shape_native=(2, 1),
    pixel_scales=1.0,
)

mask = aa.Mask2D(
    mask=[[False, False], [True, True]],
    pixel_scales=2.0,
)

array_2d = array_2d.apply_mask(mask=mask)

# Print certain array attributes.

print(array_2d.slim) # masked 1D data representation.
print(array_2d.native) # masked 2D data representation.

Methods

all

apply_mask

astype

brightest_coordinate_in_region_from

Returns the brightest pixel in the array inside an input region of form (y0, y1, x0, x1) where the region is in scaled coordinates.

brightest_sub_pixel_coordinate_in_region_from

Returns the brightest sub-pixel in the array inside an input region of form (y0, y1, x0, x1) where the region is in scaled coordinates.

copy

from_fits

Returns an Array2D by loading the array values from a .fits file.

from_yx_and_values

Returns an Array2D by inputting the y and x pixel values where the array is filled and the values that fill it.

full

Returns an Array2D where all values are filled with an input fill value, analogous to np.full().

instance_flatten

Flatten an instance of an autoarray class into a tuple of its attributes (i.e.. a pytree).

instance_unflatten

Unflatten a tuple of attributes (i.e. a pytree) into an instance of an autoarray class.

invert

max

min

no_mask

Returns an Array2D from an array via inputs in its slim or native data representation.

ones

Returns an Array2D where all values are filled with ones, analogous to np.ones().

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

reshape

resized_from

Resize the array around its centre to a new input shape.

sqrt

sum

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

with_new_array

Copy this object but give it a new array.

zeros

Returns an Array2D where all values are filled with zeros, analogous to np.zeros().

Attributes

array

binned_across_columns

Bins the 2D array up to a 1D array, where each value is the mean of all unmasked values in each column.

binned_across_rows

Bins the 2D array up to a 1D array, where each value is the mean of all unmasked values in each row.

derive_grid

The DeriveGrid2D object of the mask, used to compute derived grids of (y,x) coordinates such as the edge grid, border grid, and full unmasked grid.

derive_indexes

The DeriveIndexes2D object of the mask, used to compute index arrays that map data between the slim (1D unmasked) and native (2D full-shape) representations.

derive_mask

The DeriveMask2D object of the mask, used to compute derived masks such as the edge mask, border mask, and blurring mask.

dtype

geometry

The geometry object of the mask associated with this structure, which defines coordinate conversions between pixel units and scaled units.

header_dict

The FITS header dictionary of the mask associated with this structure, containing pixel scale and origin entries.

imag

in_counts

The array converted from electrons-per-second (eps) to total counts, using the exposure time stored in the associated Header.

in_counts_per_second

The array converted from electrons-per-second (eps) to counts-per-second, via an intermediate conversion to total counts using the Header exposure time.

is_transformed

native

Return a Array2D where the data is stored in its native representation, which is an ndarray of shape [total_y_pixels, total_x_pixels].

native_for_fits

Return a Array2D for output to a .fits file, where the data is stored in its native representation, which is an ndarray of shape [total_y_pixels, total_x_pixels].

native_skip_mask

Return a Array2D where the data is stored in its native representation, which is an ndarray of shape [total_y_pixels, total_x_pixels].

ndim

origin

The (y,x) scaled units origin of the mask's coordinate system.

original_orientation

The array rotated back to its original CCD read-out orientation, using the read-out-electronics (ROE) corner stored in the associated Header.

pixel_area

The area of a single pixel in scaled units squared (pixel_scales[0] * pixel_scales[1]).

pixel_scale

The pixel scale as a single float value.

pixel_scales

The (y,x) scaled units to pixel units conversion factors of every pixel, as a tuple of floats.

readout_offsets

The (y,x) pixel offsets of the read-out electronics corner, taken from the associated Header.

real

shape

shape_native

The shape of the data structure in its native representation (e.g. (total_y_pixels, total_x_pixels) for a 2D structure).

shape_slim

The 1D shape of the data structure in its slim representation, equal to the number of unmasked pixels.

size

slim

Return an Array2D where the data is stored its slim representation, which is an ndarray of shape [total_unmasked_pixels].

store_native

total_area

The total area of all unmasked pixels in scaled units squared (total_pixels * pixel_area).

total_pixels

The total number of unmasked pixels in the data structure (its slim length).

unmasked_grid

A grid of (y,x) coordinates of every pixel in the full mask shape (including masked pixels), using the mask's geometry to compute each pixel's scaled coordinate.

values

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

Returns an Array2D from an array via inputs in its slim or native data representation.

From a slim 1D input the method cannot determine the 2D shape of the array and its mask. The shape_native must therefore also be input into this method. The mask is setup as a unmasked Mask2D of shape_native.

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

Parameters:
  • values (Union[ndarray, List, AbstractArray2D]) – The values of the array input with shape [total_unmasked_pixels] or shape [total_y_pixels, total_x_pixels].

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

  • shape_native (Tuple[int, int]) – The 2D shape of the array in its native format, and its 2D mask (only required if input shape is in slim format).

  • origin (Tuple[float, float]) – The (y,x) scaled units origin of the mask’s coordinate system.

Examples

import autoarray as aa

# Make Array2D from input list, native format
# (This array has shape_native=(2,2)).

array_2d = aa.Array2D.no_mask(
    values=np.array([[1.0, 2.0], [3.0, 4.0]]),
    pixel_scales=1.0,
)
classmethod full(fill_value, shape_native, pixel_scales, origin=(0.0, 0.0), header=None)[source]#

Returns an Array2D where all values are filled with an input fill value, analogous to np.full().

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

From this input the method cannot determine the 2D shape of the array and its mask. The shape_native must therefore also be input into this method. The mask is setup as a unmasked Mask2D of shape_native.

Parameters:
  • fill_value (float) – The value all array elements are filled with.

  • shape_native (Tuple[int, int]) – The 2D shape of the array in its native format, and its 2D mask.

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

  • origin (Tuple[float, float]) – The (y,x) scaled units origin of the mask’s coordinate system.

Examples

import autoarray as aa

array_2d = aa.Array2D.full(
    fill_value=2.0,
    shape_native=(2, 2),
    pixel_scales=1.0,
)
classmethod ones(shape_native, pixel_scales, origin=(0.0, 0.0), header=None)[source]#

Returns an Array2D where all values are filled with ones, analogous to np.ones().

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

From this input the method cannot determine the 2D shape of the array and its mask. The shape_native must therefore also be input into this method. The mask is setup as a unmasked Mask2D of shape_native.

Parameters:
  • shape_native (Tuple[int, int]) – The 2D shape of the array in its native format, and its 2D mask.

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

  • origin (Tuple[float, float]) – The (y,x) scaled units origin of the mask’s coordinate system.

Examples

import autoarray as aa

array_2d = aa.Array2D.ones(
    shape_native=(2, 2),
    pixel_scales=1.0,
)
classmethod zeros(shape_native, pixel_scales, origin=(0.0, 0.0), header=None)[source]#

Returns an Array2D where all values are filled with zeros, analogous to np.zeros().

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

From this input the method cannot determine the 2D shape of the array and its mask. The shape_native must therefore also be input into this method. The mask is setup as a unmasked Mask2D of shape_native.

Parameters:
  • shape_native (Tuple[int, int]) – The 2D shape of the array in its native format, and its 2D mask.

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

  • origin (Tuple[float, float]) – The (y,x) scaled units origin of the mask’s coordinate system.

Examples

import autoarray as aa

array_2d = aa.Array2D.zeros(
    shape_native=(2, 2),
    pixel_scales=1.0,
)
classmethod from_fits(file_path, pixel_scales, hdu=0, origin=(0.0, 0.0))[source]#

Returns an Array2D by loading the array values from a .fits file.

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

Parameters:
  • file_path (Union[Path, str]) – The path the file is loaded from, including the filename and the .fits extension, e.g. ‘/path/to/filename.fits’

  • pixel_scales (Union[Tuple[float], Tuple[float, float], float, None]) – 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) structure.

  • hdu (int) – The Header-Data Unit of the .fits file the array data is loaded from.

  • origin (Tuple[float, float]) – The (y,x) scaled units origin of the coordinate system.

Examples

import autoarray as aa

array_2d = aa.Array2D.from_fits(
    file_path="path/to/file.fits",
    hdu=0,
    pixel_scales=1.0,
)
classmethod from_yx_and_values(y, x, values, shape_native, pixel_scales, header=None)[source]#

Returns an Array2D by inputting the y and x pixel values where the array is filled and the values that fill it.

For a full description of Array2D objects, including a description of the slim and native attribute used by the API, see the Array2D class API documentation.

Parameters:
  • y (Union[ndarray, List]) – The y pixel indexes where value are input, with shape [total_unmasked_pixels].

  • x (Union[ndarray, List]) – The x pixel indexes where value are input, with shape [total_unmasked_pixels].

  • list (values or) – The values which are used to fill in the array, with shape [total_unmasked_pixel].

  • shape_native (Tuple[int, int]) – The 2D shape of the array in its native format, and its 2D mask.

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

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

Examples

import autoarray as aa

array_2d = aa.Array2D.from_yx_and_values(
    y=np.array([0.5, 0.5, -0.5, -0.5]),
    x=np.array([-0.5, 0.5, -0.5, 0.5]),
    values=np.array([1.0, 2.0, 3.0, 4.0]),
    shape_native=(2, 2),
    pixel_scales=1.0,
)