Util functions

Utils: processing

Asterix.utils.processing_functions.resizing(image, new)

Resample the focal plane image to create a 2D array with new dimensions.

  • v1.0 2020 A. Potier

  • v2.0 19/03/21 J Mazoyer clean names + if image is real, result is real.

  • v3.0 05/2021 J Mazoyer Replacing currently with standard scipy function zoom

  • v4.0 06/2022 J Mazoyer Replacing with the rebin and crop function following discussion with L. Mugnier

  • v5.0 08/2022 J Mazoyer Rename to resizing

Parameters:
image2D array

Input image.

newint

Size of the output image after resizing, in pixels.

Returns:
Gvector2D array

Image resampled into new dimensions.

Asterix.utils.processing_functions.cropimage(img, ctr_x, ctr_y, newsizeimg)

Crop an image to create a 2D array with smaller dimensions.

AUTHOR: Axel Potier

Parameters:
img2D array

Input image, can be non squared.

ctr_xfloat

Center of the input image in the x direction around which you make the cut.

ctr_yfloat

Center of the input image in the y direction around which you make the cut.

newsizeimgint

Size of the new image.

Returns:
Gvector2D array

Squared image cropped into new dimensions.

Asterix.utils.processing_functions.crop_or_pad_image(image, dimout)

Crop or padd with zero to a 2D image depending on:

  • if dimout < dim : cropped image around pixel (dim/2,dim/2)

  • if dimout > dim : image around pixel (dim/2,dim/2) surrounded by 0

AUTHOR: Raphael Galicher

Parameters:
image2D array (float, double or complex)

Dim x dim array to crop or pad.

dimoutint

Dimension of the output array.

Returns
——–
im_out2D array (float)

Resized image.

Asterix.utils.processing_functions.rebin(image, factor=4, center_on_pixel=False)

Bin the image by a give factor. The dimensions of the image MUST be divisible by ‘factor’ or teh function. will raise an error. It this is not the case, use function resize_crop_bin().

We add a ‘center_on_pixel’ option. If false, we shift the image to put the 0-frequency in the corner before binning and then shift it back to the center. The goal is if you have a PSF center in between 4 pixels, this property is conserved. If center_on_pixel=True there is no way to conserve this property unless we are binning by an odd number.

AUTHOR: Johan Mazoyer

Parameters:
image2D array (float, double or complex)

dim1 x dim2 array with dim1 and dim2 are divisible by ‘factor’.

factorint

Factor of bin init_image size / final_image size.

center_on_pixelbool, default: False

If False the PSF is shifted before binning.

Returns:
im_out2D array (float)

Resized image of size dim1 // 4 x dim2//4.

Asterix.utils.processing_functions.resize_crop_bin(image, new_dim, center_on_pixel=False)
Resize the image by :
  1. Cropping the entrance image to the nearest multiplicative number of new_dim,

  2. binning the image to size (new_dim, new_dim).

We add a ‘center_on_pixel’ option. If false, we shift the image to put the 0-frequency in the corner before binning and then shift it back to the center. The goal is if you have a PSF center in between 4 pixels, this property is conserved. If center_on_pixel=True there is no way to conserve this property unless we are binning by an odd number.

AUTHOR: Johan Mazoyer

Parameters:
image2D array (float, double or complex)

dim1 x dim2 array with dim1 and dim2 are divisible by factor.

new_dimint

Dimension of output image. new_dim must be smaller than dim of the entrance image.

center_on_pixelbool, default: False

If False the PSf is shifted before binning.

Returns:
return_image2D array (float)

Resized image of size new_dim x new_dim.

Asterix.utils.processing_functions.ft_subpixel_shift(image, xshift, yshift, fourier=False, complex_image=False, norm='backward')

This function returns an image shifted by a non-integer amount via a Fourier-domain computation.

(Based on subpixel_shift.pro from ONERA’s IDL library by Laurent Mugnier) Renamed into ft_subpixel_shift to be clear on its purpose

AUTHORS: L.Mugnier, M.Kourdourli, J. Mazoyer

05/09/2022 : Introduction in asterix. Kourdourli version 05/09/2022 : add complex_array param Mazoyer 05/09/2022 : we invert xshift and yshift to be in agreement with np.roll (integer shift in numpy) Mazoyer 06/09/2022 : added integer shift if we can Mazoyer 06/09/2022 : works for non-square array / non even dimensions array Mazoyer

Parameters:
image2D numpy array

Initial image to be shifted.

xshiftfloat

Amount of desired shift in X direction.

yshiftfloat

Amount of desired shift in Y direction.

fourierbool (optional, default False)

If True, then the input image is assumed to be already Fourier transformed, i.e. the input is FFT^-1(image).

complex_imagebool (optional, default False)

If “False”, then the output array will be assumed to be real. If you want to shift a complex array, use complex_image = True.

normstring default ‘backward’

‘backward’, ‘forward’ or ‘ortho’. this is the same paramter as in numpy.fft functions https://numpy.org/doc/stable/reference/routines.fft.html#module-numpy.fft If ‘backward’ no normalisation is done on MFT(inverse = False) and normalisation 1/N is done in MFT(inverse=True). If ‘forward’ 1/N normalisation is done on MFT(inverse = False) and no normalisation is done in MFT(inverse=True). If ‘ortho’ 1/sqrt(N) normalisation is done in both directions. Note that norm = ‘ortho’ allows you to conserve energy between a focal plane and pupil plane. The default is ‘backward’ to be consistent with numpy.fft.fft2() and numpy.fft.ifft2().

Returns:
shifted_image2D numpy array

Shifted array with respect to the xshift and yshift used as input.

Asterix.utils.processing_functions.find_sizes_closest2factor(init_size_large, factor_zoomout, max_allowed_fft_size)

This function returns the best sizes (best_size_large, best_size_small) so that best_size_small / init_size_large are closest to factor_zoomout with best_size_small and init_size_large even integers.

AUTHORS: J Mazoyer

05/09/2022 : Introduction in Asterix

Parameters:
init_size_largeint

initial size

factor_zoomoutfloat

factor to be zoomed out (final_image size / init_image size). factor_zoomout < 1

max_allowed_fft_sizeint

the maximum size to check

Returns:
dimensionstuple of 2 floats

best_size_large, best_size_small

Asterix.utils.processing_functions.ft_zoom_out(image, factor_zoomout, complex_image=False, max_allowed_fft_size=2000)

This function returns an image zoomed out with Fourier-domain computation. The array is padded until max_allowed_fft_size and takes the best size so that factor_zoomout*size_padded is the closest to an integer.

BE CAREFUL WITH THE CENTERING, IT IS HARD TO FIND A GOOD RULE FOR ALL CASES (ODD OR EVEN DIMENSION IN OUTPUT AND INPUT) SO IT IS WHAT IT IS AND USERS ARE ENCOURAGED TO CHECK IF THIS IS WHAT THEY WANT.

AUTHORS: J Mazoyer

05/09/2022 : Introduction in asterix

Parameters:
image2D numpy array

Initial array, must be square.

factor_zoomoutfloat

Factor to be zoomed out by (final_image size / init_image size). factor_zoomout < 1

complex_imagebool(optional input, default False)

If this keyword is “False”, then the output array will be assumed to be real. If you want to shift a complex array, use complex_image=True.

max_allowed_fft_sizeint (optional input, default 2000)

The maximum size of the first fft. If you increase, you might find a better match, but it might take longer.

Returns:
smaller_image_cropped2D numpy array

zoomed out array

Utils: Save and read

Asterix.utils.save_and_read.save_plane_in_fits(dir_save_fits, name_plane, image)

Function to quickly save a real or complex file in fits.

Parameters:
dir_save_fits: string

path to directory to save the fits

name_planestring

name of the plane. final name is - current_time_str + ‘_’ + name_plane + ‘_RE_and_IM.fits’ if complex - current_time_str + ‘_’ + name_plane + ‘_RE.fits’ if real

imagenumpy array

to save. Can be of any dimension

Asterix.utils.save_and_read.quickfits(tab, folder='', name='tmp')

Quickly save an image to a fits file.

By default, it will save on the desktop with a random name to avoid overwriting. Not sure if the default saving on Desktop works for Windows, but it works fine on MacOS and Linux.

AUTHOR: Johan Mazoyer

Parameters:
tabndarray

Image to be saved to disk.

folderstring

Path to save file location.

namestring

File name of images to save to disk.

Asterix.utils.save_and_read.progress(count, total, status='')

print a progress bar for a for loop.

Parameters:
count: int

Counter in the for loop.

total: int

Number of iterations in the for loop.

Asterix.utils.save_and_read.read_parameter_file(parameter_file, NewMODELconfig={}, NewDMconfig={}, NewCoronaconfig={}, NewEstimationconfig={}, NewCorrectionconfig={}, NewLoopconfig={}, NewSIMUconfig={})

Check existence of the given parameter file, read it and check validity.

AUTHOR: Johan Mazoyer

Parameters:
parameter_file: string

Absolute path to a .ini parameter file.

NewMODELconfigdict, optional

Can be used to directly change a parameter in the MODELconfig section of the input parameter file.

NewDMconfigdict, optional

Can be used to directly change a parameter in the DMconfig section of the input parameter file.

NewCoronaconfigdict, optional

Can be used to directly change a parameter in the Coronaconfig section of the input parameter file.

NewEstimationconfigdict, optional

Can be used to directly change a parameter in the Estimationconfig section of the input parameter file.

NewCorrectionconfigdict, optional

Can be used to directly change a parameter in the Correctionconfig section of the input parameter file.

NewSIMUconfigdict, optional

Can be used to directly change a parameter in the SIMUconfig section of the input parameter file.

Returns:
configdict

Parameter dictionary.

Asterix.utils.save_and_read.from_param_to_header(config)

Convert ConfigObj parameters to fits header type list AUTHOR: Axel Potier

Parameters:
configdict

Config object.

Returns:
headerdict

List of parameters.

Asterix.utils.save_and_read.get_data_dir(env_var_name='ASTERIX_DATA_PATH', config_in=None, datadir='asterix_data')

Create a path to the local data directory.

If the environment variable env_var_name exists, this is returned as the full data path and the input ‘datadir’

is ignored. You can set this individually on your OS.

If the environment variable does not exist (default for all new users) but the user adapted the ini file and

accesses the ‘Data_dir’ entry, the configfile entry is returned and ‘datadir’ is ignored.

If the environment variable does not exist and the Data_dir entry in the ini file is not passed or set to ‘.’,

the directory ‘datadir’ is appended to the user’s home directory and returned as an absolute path. On MacOS of user ‘myuser’ for example, this would return: ‘/Users/myuser/asterix_data’

Parameters:
env_var_namestring, optional

Environment variable for optional override, default ‘ASTERIX_DATA_PATH’.

config_instring, optional

Directory name passed through from configuration file.

datadirstring, optional

Name of the top-level data directory, default “asterix_data”.

Returns:
data_dirstring

Absolute path to top-level data directory.

Asterix.utils.save_and_read.create_experiment_dir(append='')

Create the name for an experiment directory.

Create a timestamp including current year, month and day, as well as hour, minute and second. Add the passed suffix ‘append’ before returning it.

Parameters:
appendstring

Filename suffix to add to timestamp, default is ‘’.

Returns:
experiment_dir: string

Absolute path to top-level data directory.

Asterix.utils.save_and_read.get_git_description()

Return git description of current branch and commit.