lezargus.library.array module#

Collection of array or image manipulation functions.

If there are any functions which are done on arrays (or anything that is just an array under the hood), we usually group it here. Moreover, functions which would otherwise operate on images are also placed here. As images are just arrays under the hood (and to avoid conflict with /lezargus/container/image.py), image manipulation functions are kept here too.

Note that all of these functions follow the axes convention of indexing being (x, y, lambda). If a cube is not of this shape, then it will likely return erroneous results, but, the functions themselves cannot detect this.

lezargus.library.array.bin_cube_array_spatially(cube: ndarray, x_bin: int, y_bin: int, mode: str = 'add') ndarray[source]#

Bin a cube spatially into super pixels.

We only bin the cube in the spatial directions, the spectral direction is not touched.

Parameters:
  • cube (ndarray) – The data cube to binned.

  • x_bin (int) – The number of pixels in the x-direction to bin over per super pixel.

  • y_bin (int) – The number of pixels in the y-direction to bin over per super pixel.

  • mode (string, default = "add") –

    The mode to combine the data.

    • add : Add the pixels together.

    • mean : Use the mean of the pixels.

Returns:

binned_image – The data cube after binning.

Return type:

ndarray

lezargus.library.array.bin_image_array(image: ndarray, x_bin: int, y_bin: int, mode: str = 'add') ndarray[source]#

Bin an image by using integer super pixels.

A lot of inspiration for this function is from here: https://scipython.com/blog/binning-a-2d-array-in-numpy/

Parameters:
  • image (ndarray) – The input image/array to binned.

  • x_bin (int) – The number of pixels in the x-direction to bin over per super pixel.

  • y_bin (int) – The number of pixels in the y-direction to bin over per super pixel.

  • mode (string, default = "add") –

    The mode to combine the data.

    • add : Add the pixels together.

    • mean : Use the mean of the pixels.

Returns:

binned_image – The image/array after binning.

Return type:

ndarray

lezargus.library.array.clean_finite_arrays(*arrays: ndarray) tuple[ndarray][source]#

Return parallel arrays with any non-finite number removed from them.

We remove all parallel-aligned values (aligned with each other) which are not a finite number, such as NaN and infinity. Because we remove data, the shape of the output arrays will likely be very different to the input.

Parameters:

*arrays (ndarray) – The arrays, which are all parallel, to remove the non-finite numbers from.

Returns:

clean_arrays – The cleaned arrays, arranged in a tuple, in the exact order they were input in as arrays.

Return type:

tuple

lezargus.library.array.convolve_cube_by_image_array(cube: ndarray, kernel: ndarray) ndarray[source]#

Convolve the image slices of a 3D cube with a 2D image.

We loop over and convolve image slices of the cube with the provided kernel; we do not try and do an entire 3D convolution of the cube due to memory limitations.

Parameters:
  • cube (ndarray) – The data cube from which we will convolve.

  • kernel (ndarray) – The image kernel we are using to convolve.

Returns:

convolved_cube – The cube, with the image slices convolved by the provided kernel.

Return type:

ndarray

lezargus.library.array.rotate_image_array(input_array: ndarray, rotation: float) ndarray[source]#

Rotate a 2D image array array.

This function is a connivent wrapper around scipy’s function. The array is padded with NaNs so any data outside the original array after rotation is null.

Parameters:
  • input_array (ndarray) – The input array to be rotated.

  • rotation (float) – The rotation angle, in radians.

Returns:

rotated_array – The rotated array/image.

Return type:

ndarray

lezargus.library.array.translate_image_array(input_array: ndarray, x_shift: float, y_shift: float) ndarray[source]#

Translate a 2D image array array.

This function is a convient wrapper around scipy’s function. The array is padded with NaNs so any data outside the original array after translation is null.

Parameters:
  • input_array (ndarray) – The input array to be translated.

  • x_shift (float) – The number of pixels that the array is shifted in the x-axis.

  • y_shift (float) – The number of pixels that the array is shifted in the y-axis.

Returns:

shifted_array – The shifted array/image.

Return type:

ndarray

lezargus.library.array.verify_shape_compatibility(reference_array: ndarray, test_array: ndarray, return_broadcast: bool = False) bool | tuple[bool, ndarray][source]#

Verify if a test array is compatible with the reference array.

This function serves to see if two arrays are compatible in shape. If the “test” array is just a single number, we allow it to broadcast, and return it if needed.

Parameters:
  • reference_array (ndarray) – The reference array which we are testing against.

  • test_array (ndarray) – The test array that we are testing to.

  • return_broadcast (bool, default = False) – If True, we return a compatible array built from the test array information.

Returns:

  • verify (bool) – The verification.

  • broadcast (ndarray, optional) – The broadcast, we only return this if return_broadcast was provided. If verify is False, then this is None.