Utility Functions#

oversample(image_low_res, factor_y, factor_x, method='nearest')#

Oversample an image by a given factor in the x and y direction

Parameters:
image_low_res: the 2D array to be oversampled
factor_y,_x factor_z: oversampling factor for the y-axis, x-axis
Returns:
image_high_res: the oversampled array with shape (image_low_res.shape[0]*factor_y, image_low_res.shape[1]*factor_x)
resample(image_high_res, factor_y, factor_x)#

Downsample an image by a given factor in the x and y direction.

This is done by summing pixels to reduce the resolution.

Parameters:
image_high_res: the 2D array to be downsampled
factor_y,_x factor_z: downsample factor for the y-axis, x-axis –> must be able to divide the shape of that axis
Returns:
image_low_res: the downsampled array with shape (image_low_res.shape[0]/factor_y, image_low_res.shape[1]/factor_x)
scale_distribution(distribution, mu, sigma, max, min)#

Rescale a base distribution to its target distribution. This is used for parameters that are rescaled to their target parameter space by simply applying a function, and not using numpyro.deterministic. Based on the non-None parameters given to this function, the target distribution is inferred.

Parameters:
distribution: base distribution (2D array containing posterior/prior data)
mu: mean of distribution, None if mean not needed to define the target distribution
sigma: std of the distribution, None if std not needed to define the target distribution
max: upper bound of the distribution, None if max not needed to define the target distribution
min: lower bound of the distribution, None if min not needed to define the target distribution
Returns:
rescaled_distribution: the new target distribution
find_best_sample(inference_data, variable, mu, sigma, max, min, MLS)#

Find the best sample (either median or highest likelihood) from input posterior, for each parameter in the parameter list. Before finding the best sample, each posterior is rescaled to its target distribution.

Parameters:
inference_data: arviz inference data type – results from the numpyro fitting
mu: mean of distribution
sigma: std of the distribution
max: upper bound of the distribution (None if no upper bound)
min: lower bound of the distribution (None if no lower bound)
Returns:
rescaled_distribution: data rescaled to a truncated normal distribution N(mu,sigma), min<x<max
compute_gal_props(image, n=1, threshold_sigma=3)#

compute PA of galaxy in the image using skimage measure regionprops

load_psf(filter, y_factor, size=9, psf_folder='mpsf_gds/')#

Load and process a PSF file for a given filter.

Loads a PSF FITS file, crops it to the desired size, and optionally downsamples it based on the pixel scale factor.

Parameters:
  • filter (str) – Filter name (e.g., ‘F444W’)

  • y_factor (int) – Spatial oversampling factor. If 1, PSF is downsampled to 0.063” resolution

  • size (int, optional) – Size of output PSF array (default: 9)

  • psf_folder (str, optional) – Path to folder containing PSF files (default: ‘mpsf_gds/’)

Returns:

Normalized PSF array of shape (size, size)

Return type:

numpy.ndarray

Notes

The PSF is always renormalized to sum to 1 after processing. Filename format expected: ‘mpsf_{filter}.fits’ in psf_folder.

compute_inclination(axis_ratio=None, ellip=None, q0=0.0)#

Compute the inclination of a galaxy given its axis ratio or ellipticity

compute_axis_ratio(inc, q0=0.0)#

Compute the axis ratio of a galaxy given its inclination

add_v_re(inf_data, kin_model, grism_object, num_samples, re_manual=None)#
sersic_profile(x, y, amplitude, r_eff, n, x_0, y_0, ellip, theta, c=0)#
compute_adaptive_sersic_profile(x, y, intensity, r_eff, n, x0, y0, ellip, PA_sersic)#

Notes about inputs: -x and y have to be oversampled to the right amount that you want to obtain at the end -r_eff is given in the original pixel size -if x and y are centered on zero then x0=y0 = 0

flux_to_Ie(flux, n, r_e, ellip)#
Ie_to_flux(Ie, n, r_e, ellip)#
rotate_coords(x, y, xc, yc, theta)#

Rotate (x, y) around (xc, yc) by angle theta (in radians). Rotation is CLOCKWISE.

downsample_error(error_array)#

Downsample a 2D error array by a factor of 2 with proper error propagation.

Uses RMS (root-mean-square) averaging of 2x2 blocks to properly propagate uncertainties when downsampling error maps.

Parameters:

error_array (numpy.ndarray) – 2D array of error/uncertainty values to downsample

Returns:

Downsampled error array with shape (h//2, w//2)

Return type:

numpy.ndarray

Notes

If dimensions are odd, the array is cropped to even dimensions before downsampling. Error propagation: sigma_downsampled = sqrt(mean(sigma_i^2)) for 2x2 blocks.

downsample_psf_centered(psf_full, size)#

Crop and downsample a PSF around its center.

Extracts a centered region from the full PSF and downsamples it by a factor of 2.

Parameters:
  • psf_full (numpy.ndarray) – Full PSF array (typically high-resolution)

  • size (int) – Half-width of the region to extract (output will be (2*size+1, 2*size+1) before downsampling)

Returns:

Downsampled PSF with shape approximately (size, size)

Return type:

numpy.ndarray

Notes

The PSF is cropped symmetrically around its center before downsampling.