autolens.Grid1D#

class Grid1D[source]#

Bases: Structure

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

A Grid1D is ordered such that pixels begin from the left (e.g. index [0]) of the corresponding mask and go right. The positive x-axis is 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]

  • 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 Grid1D is an ndarray of shape [total_unmasked_coordinates].

The first element of the ndarray corresponds to the pixel index. For example:

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

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

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

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

x x x O o x O x x x

This is an example mask.Mask1D, where:

x = `True` (Pixel is masked and excluded from the grid)
O = `False` (Pixel is not masked and included in the grid)

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

x x x 0 1 x 2 x x x

grid[0] = [-1.5]
grid[1] = [-0.5]
grid[2] = [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 (x) coordinates at the centre of each sub-pixel of an unmasked pixel. The Grid1D is therefore stored as an ndarray of shape [total_unmasked_coordinates*sub_size].

The sub-grid indexes are ordered such that pixels begin from the first (leftmost) 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]. For example:

  • grid[5] - using sub_size=2, gives the 3rd unmasked pixel’s 2nd sub-pixel x-coordinate.

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

  • grid[10] - using sub_size=3, 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 O x O x x x

This is an example mask.Mask1D, where:

x = `True` (Pixel is masked and excluded from the grid)
O = `False` (Pixel is not masked and included in the grid)

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

pixel_scales = 1.0"

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

 x x x 0 x 1 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:

grid[0] = [-0.75]
grid[1] = [-0.25]

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

grid[0] = [-0.833]
grid[1] = [-0.5]
grid[2] = [-0.166]

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

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

For the following example mask:

x x x O O x O x x x

- grid[0] = 0.0 (it is masked, thus zero)
- grid[1] = 0.0 (it is masked, thus zero)
- grid[2] = 0.0 (it is masked, thus zero)
- grid[3] = -1.5
- grid[4] = -0.5
- grid[5] = 0.0 (it is masked, thus zero)
- grid[6] = 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_x_coordinates*sub_size,].

All sub-pixels in masked pixels have value 0.0.

Grid1D Mapping

Every set of (x) coordinates in a pixel of the sub-grid maps to an unmasked pixel in the mask. For a uniform grid, every 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 x coordinates where the indexing signifies that the 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.

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.

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.

dot

dump

Dump a pickle of the array to the specified file.

dumps

Returns the pickle of the array as a string.

fill

Fill the array with a scalar value.

flatten

Return a copy of the array collapsed into one dimension.

flip_hdu_for_ds9

from_mask

Create a Grid1D (see Grid1D.__new__) from a mask, where only unmasked pixels are included in the grid (if the grid is represented in its native 1D masked values are 0.0).

getfield

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

grid_2d_radial_projected_from

Project the 1D grid of (y,x) coordinates to an irregular 2d grid of (y,x) 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 Grid1D (see Grid1D.__new__) by inputting the grid coordinates in 1D.

nonzero

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

output_to_fits

Output the grid to a .fits file.

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.

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.

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 Grid1D (see Grid`D.__new__) as a uniform grid of (x) values given an input shape_native and pixel_scales of the grid.

uniform_from_zero

Create a Grid1D (see Grid`D.__new__) as a uniform grid of (x) values given an input shape_native and pixel_scales of the grid, where the first (x) coordinate of the grid is 0.0 and all other values ascend positively.

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

Convenience method to access the binned-up grid in its 1D representation, which is a Grid2D stored as an ndarray of 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.

geometry

hdu_for_output

imag

The imaginary part of the array.

itemsize

Length of one array element in bytes.

native

Return a Grid1D where the data is stored in its native representation, which is an ndarray of shape [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.

shape

Tuple of array dimensions.

shape_native

rtype

Tuple[int, ...]

shape_slim

rtype

int

size

Number of elements in the array.

slim

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

strides

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

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, sub_size=1, origin=(0.0,))[source]#

Create a Grid1D (see Grid1D.__new__) by inputting the grid coordinates in 1D.

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.

  • 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]) – The origin of the grid’s mask.

Examples

import autogrid as aa

# Make Grid1D from input np.ndgrid.

grid_1d = aa.Grid1D.no_mask(grid=np.grid([1.0, 2.0, 3.0, 4.0]), pixel_scales=1.0)

# Make Grid2D from input list.

grid_1d = aa.Grid1D.no_mask(grid=[1.0, 2.0, 3.0, 4.0], pixel_scales=1.0)

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

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

Grid1D

classmethod from_mask(mask)[source]#

Create a Grid1D (see Grid1D.__new__) from a mask, where only unmasked pixels are included in the grid (if the grid is represented in its native 1D masked values are 0.0).

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

Parameters

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

Return type

Grid1D

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

Create a Grid1D (see Grid`D.__new__) as a uniform grid of (x) values given an input shape_native and pixel_scales of the grid.

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

  • pixel_scales (Union[Tuple[float], Tuple[float, float], float]) – The (x) scaled units to pixel units conversion factor of every pixel. If this is input as a float, it is converted to a (float,) tuple.

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

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

Return type

Grid1D

classmethod uniform_from_zero(shape_native, pixel_scales, sub_size=1)[source]#

Create a Grid1D (see Grid`D.__new__) as a uniform grid of (x) values given an input shape_native and pixel_scales of the grid, where the first (x) coordinate of the grid is 0.0 and all other values ascend positively.

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

  • pixel_scales (Union[Tuple[float], Tuple[float, float], float]) – The (x) scaled units to pixel units conversion factor of every pixel. If this is input as a float, it is converted to a (float,) tuple. it is converted to a (float,) tuple.

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

Return type

Grid1D

property slim: Grid1D#

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

If it is already stored in its slim representation the Grid1D is returned as it is. If not, it is mapped from native to slim and returned as a new Grid1D.

Return type

Grid1D

property native: Grid1D#

Return a Grid1D where the data is stored in its native representation, which is an ndarray of shape [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 Grid1D.

Return type

Grid1D

property binned: Grid1D#

Convenience method to access the binned-up grid in its 1D representation, which is a Grid2D stored as an ndarray of 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

Grid1D

grid_2d_radial_projected_from(angle=0.0)[source]#

Project the 1D grid of (y,x) coordinates to an irregular 2d grid of (y,x) coordinates. The projection works as follows:

1) Map the 1D (x) coordinates to 2D along the x-axis, such that the x value of every 2D coordinate is the corresponding (x) value in the 1D grid, and every y value is 0.0.

  1. Rotate this projected 2D grid clockwise by the input angle.

Parameters

angle (float) – The angle with which the project 2D grid of coordinates is rotated clockwise.

Returns

The projected and rotated 2D grid of (y,x) coordinates.

Return type

Grid2DIrregular

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

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

Return type

Union[Array1D, Grid2D, Grid2DTransformed, Grid2DTransformedNumpy]

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

result_list (List) – The input result (e.g. of a decorated function) that is converted to a PyAutoArray structure.

Return type

List[Union[Array1D, Grid2D, Grid2DTransformed, Grid2DTransformedNumpy]]