Utility Functions

Utility functions for SynIM simulations, including mask extrapolation

synim.utils.apply_extrapolation(data, edge_pixels, reference_indices, coefficients, in_place=False)

Applies linear extrapolation to edge pixels using precalculated indices and coefficients.

Parameters:
  • data (ndarray) – Input array to extrapolate.

  • edge_pixels (ndarray) – Linear indices of edge pixels to extrapolate.

  • reference_indices (ndarray) – Indices of reference pixels.

  • coefficients (ndarray) – Coefficients for linear extrapolation.

  • in_place (bool) – If True, modifies the input data array directly.

Returns:

Array with extrapolated pixels.

Return type:

ndarray

synim.utils.apply_mask(array, mask, norm=False, fill_value=None, in_place=False)

Apply a 2D or 3D mask to a 2D or 3D array.

Parameters:
  • array – Input array

  • mask – Mask array

  • norm – If True, normalize by mask

  • fill_value – Value to fill masked regions

  • in_place – If True, modify array in-place (only works if no dtype conversion needed)

synim.utils.calculate_extrapolation_indices_coeffs(mask, debug=False, debug_pixels=None)

Calculates indices and coefficients for extrapolating edge pixels of a mask.

Parameters:
  • mask (ndarray) – Binary mask (True/1 inside, False/0 outside).

  • debug (bool) – If True, displays debug information and plots.

  • debug_pixels (list) – List of [y, x] coordinates for detailed debug output.

Returns:

(edge_pixels, reference_indices, coefficients)
  • edge_pixels: Linear indices of the edge pixels to extrapolate.

  • reference_indices: Array of reference pixel indices for extrapolation.

  • coefficients: Coefficients for linear extrapolation.

Return type:

tuple

synim.utils.dm2d_to_3d(dm_array, mask, normalize=True, xp_local=<module 'numpy' from '/home/docs/checkouts/readthedocs.org/user_builds/synim/envs/latest/lib/python3.11/site-packages/numpy/__init__.py'>, float_dtype_local=<class 'numpy.float32'>)

Convert a 2D DM influence function to a 3D array using a mask.

synim.utils.dm3d_to_2d(dm_array, mask, xp_local=<module 'numpy' from '/home/docs/checkouts/readthedocs.org/user_builds/synim/envs/latest/lib/python3.11/site-packages/numpy/__init__.py'>, float_dtype_local=<class 'numpy.float32'>)

Convert a 3D DM influence function to a 2D array using a mask.

synim.utils.has_transformations(rotation, translation, magnification)

Helper function to check if there are any non-trivial transformations.

Returns: - bool: True if there are transformations, False otherwise

synim.utils.make_orto_modes(array)
synim.utils.rebin(array, new_shape, method='average')

Resize array to new dimensions.

synim.utils.rotshiftzoom_array(input_array, dm_translation=(0.0, 0.0), dm_rotation=0.0, dm_magnification=(1.0, 1.0), wfs_translation=(0.0, 0.0), wfs_rotation=0.0, wfs_magnification=(1.0, 1.0), wfs_anamorphosis_45=1.0, output_size=None)

This function applies magnification, rotation, shift and resize of a 2D or 3D numpy/cupy array using affine transformation. Rotation is applied in the same direction as the first function.

Parameters: - input_array: numpy/cupy array, input data to be transformed - dm_translation: tuple, translation for DM (x, y) - dm_rotation: float, rotation angle for DM in degrees - dm_magnification: tuple, magnification factors for DM (x, y) - wfs_translation: tuple, translation for WFS (x, y) - wfs_rotation: float, rotation angle for WFS in degrees - wfs_magnification: tuple, magnification factors for WFS (x, y) - wfs_anamorphosis_45 (float): Diagonal anamorphosis factor.

Values > 1 stretch along +45° diagonal and compress along -45° diagonal. Implemented as a shear transformation.

  • output_size: tuple, desired output size (height, width)

Returns: - output: numpy/cupy array, transformed data

synim.utils.shiftzoom_from_source_dm_params(source_pol_coo, source_height, dm_height, pixel_pitch)

Compute the shift and zoom parameters for a DM based on the source coordinates and heights.

Parameters: - source_pol_coo: tuple, (radius, angle) in polar coordinates - source_height: float, height of the source - dm_height: float, height of the DM - pixel_pitch: float, pixel pitch in meters

Returns: - shift: tuple, (x_shift, y_shift) in pixels - zoom: tuple, (x_zoom, y_zoom) magnification factors