autolens.Kernel2D#

class Kernel2D[source]#

Bases: AbstractArray2D

An array of values, which are paired to a uniform 2D mask of pixels. Each entry on the array corresponds to a value at the centre of a pixel in an unmasked pixel. See the Array2D class for a full description of how Arrays work.

The Kernel2D class is an Array2D but with additioonal methods that allow it to be convolved with data.

Parameters:
  • values – The values of the array.

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

Methods

all

apply_mask

astype

convolved_array_from

Convolve an array with this Kernel2D

convolved_array_with_mask_from

Convolve an array with this Kernel2D

copy

extent_of_zoomed_array

For an extracted zoomed array computed from the method zoomed_around_mask compute its extent in scaled coordinates.

flip_hdu_for_ds9

from_as_gaussian_via_alma_fits_header_parameters

from_fits

Loads the Kernel2D from a .fits file.

from_gaussian

Setup the Kernel2D as a 2D symmetric elliptical Gaussian profile, according to the equation:

from_primary_hdu

Returns an Kernel2D by from a PrimaryHDU object which has been loaded via astropy.fits

full

Create a Kernel2D (see Kernel2D.__new__) where all values are filled with an input fill value, analogous to the method numpy ndarray.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_blur

Setup the Kernel2D as a kernel which does not convolve any signal, which is simply an array of shape (1, 1) with value 1.

no_mask

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

ones

Create an Kernel2D (see Kernel2D.__new__) where all values are filled with ones, analogous to the method numpy ndarray.ones.

output_to_fits

Output the array to a .fits file.

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.

rescaled_with_odd_dimensions_from

If the PSF kernel has one or two even-sized dimensions, return a PSF object where the kernel has odd-sized dimensions (odd-sized dimensions are required by a Convolver).

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

Create an Kernel2D (see Kernel2D.__new__) where all values are filled with zeros, analogous to the method numpy ndarray.ones.

zoomed_around_mask

Extract the 2D region of an array corresponding to the rectangle encompassing all unmasked values.

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

derive_indexes

derive_mask

dtype

geometry

hdu_for_output

The array as an HDU object, which can be output to a .fits file.

imag

in_counts

in_counts_per_second

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

normalized

Normalize the Kernel2D such that its data_vector values sum to unity.

origin

original_orientation

pixel_area

pixel_scale

pixel_scale_header

pixel_scales

readout_offsets

real

shape

shape_native

shape_slim

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

total_pixels

unmasked_grid

values

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

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

See the manual_slim and manual_native methods for examples. :type values: Union[ndarray, List] :param values: The values of the array input as an ndarray of shape [total_unmasked_pixels] or a list of

lists.

Parameters:
  • shape_native (Tuple[int, int]) – The 2D shape of the mask the array 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).

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

classmethod full(fill_value, shape_native, pixel_scales, origin=(0.0, 0.0), normalize=False)[source]#

Create a Kernel2D (see Kernel2D.__new__) where all values are filled with an input fill value, analogous to the method numpy ndarray.full.

From 1D input the method cannot determine the 2D shape of the array 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:
  • fill_value (float) – The value all array elements are filled with.

  • shape_native (Tuple[int, int]) – The 2D shape of the mask the array 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).

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

Return type:

Kernel2D

classmethod ones(shape_native, pixel_scales, origin=(0.0, 0.0), normalize=False)[source]#

Create an Kernel2D (see Kernel2D.__new__) where all values are filled with ones, analogous to the method numpy ndarray.ones.

From 1D input the method cannot determine the 2D shape of the array 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:
  • shape_native (Tuple[int, int]) – The 2D shape of the mask the array 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).

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

classmethod zeros(shape_native, pixel_scales, origin=(0.0, 0.0), normalize=False)[source]#

Create an Kernel2D (see Kernel2D.__new__) where all values are filled with zeros, analogous to the method numpy ndarray.ones.

From 1D input the method cannot determine the 2D shape of the array 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:
  • shape_native (Tuple[int, int]) – The 2D shape of the mask the array 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).

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

Return type:

Kernel2D

classmethod no_blur(pixel_scales)[source]#

Setup the Kernel2D as a kernel which does not convolve any signal, which is simply an array of shape (1, 1) with value 1.

Parameters:

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

classmethod from_gaussian(shape_native, pixel_scales, sigma, centre=(0.0, 0.0), axis_ratio=1.0, angle=0.0, normalize=False)[source]#

Setup the Kernel2D as a 2D symmetric elliptical Gaussian profile, according to the equation:

(1.0 / (sigma * sqrt(2.0*pi))) * exp(-0.5 * (r/sigma)**2)

Parameters:
  • shape_native (Tuple[int, int]) – The 2D shape of the mask the array 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).

  • sigma (float) – The value of sigma in the equation, describing the size and full-width half maximum of the Gaussian.

  • centre (Tuple[float, float]) – The (y,x) central coordinates of the Gaussian.

  • axis_ratio (float) – The axis-ratio of the elliptical Gaussian.

  • angle (float) – The rotational angle of the Gaussian’s ellipse defined counter clockwise from the positive x-axis.

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

Return type:

Kernel2D

classmethod from_fits(file_path, hdu, pixel_scales, origin=(0.0, 0.0), normalize=False)[source]#

Loads the Kernel2D from a .fits file.

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’

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

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

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

  • normalize (bool) – If True, the Kernel2D’s array values are normalized such that they sum to 1.0.

Return type:

Kernel2D

classmethod from_primary_hdu(primary_hdu, origin=(0.0, 0.0))[source]#

Returns an Kernel2D by from a PrimaryHDU object which has been loaded via astropy.fits

This assumes that the header of the PrimaryHDU contains an entry named PIXSCALE which gives the pixel-scale of the array.

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

Parameters:
  • primary_hdu (PrimaryHDU) – The PrimaryHDU object which has already been loaded from a .fits file via astropy.fits and contains the array data and the pixel-scale in the header with an entry named PIXSCALE.

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

Return type:

Kernel2D

Examples

from astropy.io import fits
import autoarray as aa

primary_hdu = fits.open("path/to/file.fits")

array_2d = aa.Kernel2D.from_primary_hdu(
    primary_hdu=primary_hdu,
)
rescaled_with_odd_dimensions_from(rescale_factor, normalize=False)[source]#

If the PSF kernel has one or two even-sized dimensions, return a PSF object where the kernel has odd-sized dimensions (odd-sized dimensions are required by a Convolver).

The PSF can be scaled to larger / smaller sizes than the input size, if the rescale factor uses values that deviate furher from 1.0.

Kernels are rescald using the scikit-image routine rescale, which performs rescaling via an interpolation routine. This may lead to loss of accuracy in the PSF kernel and it is advised that users, where possible, create their PSF on an odd-sized array using their data reduction pipelines that remove this approximation.

Parameters:
  • rescale_factor (float) – The factor by which the kernel is rescaled. If this has a value of 1.0, the kernel is rescaled to the closest odd-sized dimensions (e.g. 20 -> 19). Higher / lower values scale to higher / lower dimensions.

  • normalize (bool) – Whether the PSF should be normalized after being rescaled.

Return type:

Kernel2D

property normalized: Kernel2D#

Normalize the Kernel2D such that its data_vector values sum to unity.

convolved_array_from(array)[source]#

Convolve an array with this Kernel2D

Parameters:

image – An array representing the image the Kernel2D is convolved with.

Returns:

An array representing the image after convolution.

Return type:

convolved_image

Raises:

KernelException if either Kernel2D psf dimension is odd

convolved_array_with_mask_from(array, mask)[source]#

Convolve an array with this Kernel2D

Parameters:

image – An array representing the image the Kernel2D is convolved with.

Returns:

An array representing the image after convolution.

Return type:

convolved_image

Raises:

KernelException if either Kernel2D psf dimension is odd