utils

Contents

utils#

astrophot.utils.decorators.ignore_numpy_warnings#

signature: ignore_numpy_warnings(func)

This decorator is used to turn off numpy warnings. This should only be used in initialize scripts which often run heuristic code to determine initial parameter values. These heuristics may encounter log(0) or sqrt(-1) or other numerical artifacts and should handle them before returning. This decorator simply cleans up that processes to minimize clutter in the output.

astrophot.utils.interpolate.interp2d#

signature: interp2d(im: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], i: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], j: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], padding_mode: str = ‘zeros’) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Interpolates a 2D image at specified coordinates. Similar to torch.nn.functional.grid_sample with align_corners=False.

Args: im (Tensor): A 2D tensor representing the image. i (Tensor): A tensor of i coordinates (in pixel space) at which to interpolate. j (Tensor): A tensor of j coordinates (in pixel space) at which to interpolate.

Returns: Tensor: Tensor with the same shape as i and j containing the interpolated values.

astrophot.utils.integration.quad_table#

signature: quad_table(order, dtype, device)

Generate a meshgrid for quadrature points using Legendre-Gauss quadrature.

Parameters#

n : int The number of quadrature points in each dimension. dtype : torch.dtype The desired data type of the tensor. device : torch.device The device on which to create the tensor.

Returns#

Tuple[torch.Tensor, torch.Tensor, torch.Tensor] The generated meshgrid as a tuple of Tensors.

astrophot.utils.parametric_profiles.sersic_np#

signature: sersic_np(R: numpy.ndarray, n: numpy.ndarray, Re: numpy.ndarray, Ie: numpy.ndarray) -> numpy.ndarray

Sersic 1d profile function, works more generally with numpy operations. In the event that impossible values are passed to the function it returns large values to guide optimizers away from such values.

Args:

  • R: Radii array at which to evaluate the sersic function

  • n: sersic index restricted to n > 0.36

  • Re: Effective radius in the same units as R

  • Ie: Effective surface density

astrophot.utils.parametric_profiles.gaussian_np#

signature: gaussian_np(R: numpy.ndarray, sigma: numpy.ndarray, I0: numpy.ndarray) -> numpy.ndarray

Gaussian 1d profile function, works more generally with numpy operations.

Args:

  • R: Radii array at which to evaluate the gaussian function

  • sigma: standard deviation of the gaussian in the same units as R

  • I0: central surface density

astrophot.utils.parametric_profiles.exponential_np#

signature: exponential_np(R: numpy.ndarray, Ie: numpy.ndarray, Re: numpy.ndarray) -> numpy.ndarray

Exponential 1d profile function, works more generally with numpy operations.

Args:

  • R: Radii array at which to evaluate the exponential function

  • Ie: Effective surface density

  • Re: Effective radius in the same units as R

astrophot.utils.parametric_profiles.moffat_np#

signature: moffat_np(R: numpy.ndarray, n: numpy.ndarray, Rd: numpy.ndarray, I0: numpy.ndarray) -> numpy.ndarray

Moffat 1d profile function, works with numpy operations.

Args:

  • R: Radii array at which to evaluate the moffat function

  • n: concentration index

  • Rd: scale length in the same units as R

  • I0: central surface density

astrophot.utils.parametric_profiles.nuker_np#

signature: nuker_np(R: numpy.ndarray, Rb: numpy.ndarray, Ib: numpy.ndarray, alpha: numpy.ndarray, beta: numpy.ndarray, gamma: numpy.ndarray) -> numpy.ndarray

Nuker 1d profile function, works with numpy functions

Args:

  • R: Radii tensor at which to evaluate the nuker function

  • Ib: brightness at the scale length, represented as the log of the brightness divided by pixel scale squared.

  • Rb: scale length radius

  • alpha: sharpness of transition between power law slopes

  • beta: outer power law slope

  • gamma: inner power law slope

astrophot.utils.parametric_profiles.ferrer_np#

signature: ferrer_np(R: numpy.ndarray, rout: numpy.ndarray, alpha: numpy.ndarray, beta: numpy.ndarray, I0: numpy.ndarray) -> numpy.ndarray

Modified Ferrer profile.

Args:

  • R: Radial distance from the center.

  • rout: Outer radius of the profile.

  • alpha: Power-law index.

  • beta: Exponent for the modified Ferrer function.

  • I0: Central intensity.

astrophot.utils.parametric_profiles.king_np#

signature: king_np(R: numpy.ndarray, Rc: numpy.ndarray, Rt: numpy.ndarray, alpha: numpy.ndarray, I0: numpy.ndarray) -> numpy.ndarray

Empirical King profile.

Args:

  • R: The radial distance from the center.

  • Rc: The core radius of the profile.

  • Rt: The truncation radius of the profile.

  • alpha: The power-law index of the profile.

  • I0: The central intensity of the profile.

astrophot.utils.initialize.center_of_mass#

signature: center_of_mass(image)

Determines the light weighted center of mass

astrophot.utils.initialize.recursive_center_of_mass#

signature: recursive_center_of_mass(image, max_iter=10, tol=0.1)

Determines the light weighted center of mass in a progressively smaller window each time centered on the previous center.

astrophot.utils.initialize.gaussian_psf#

signature: gaussian_psf(sigma, img_width, pixelscale, upsample=4, normalize=True)

create a gaussian point spread function (PSF) image.

Args:

  • sigma: Standard deviation of the Gaussian in arcseconds.

  • img_width: Width of the PSF image in pixels.

  • pixelscale: Pixel scale in arcseconds per pixel.

  • upsample: Upsampling factor to more accurately create the PSF (the outputted PSF is not upsampled).

  • normalize: Whether to normalize the PSF so that the sum of all pixels equals 1. If False, the PSF will not be normalized.

astrophot.utils.initialize.moffat_psf#

signature: moffat_psf(n, Rd, img_width, pixelscale, upsample=4, normalize=True)

Create a Moffat point spread function (PSF) image.

Args:

  • n: Moffat index (power-law index).

  • Rd: Scale radius of the Moffat profile in arcseconds.

  • img_width: Width of the PSF image in pixels.

  • pixelscale: Pixel scale in arcseconds per pixel.

  • upsample: Upsampling factor to more accurately create the PSF (the outputted PSF is not upsampled).

  • normalize: Whether to normalize the PSF so that the sum of all pixels equals 1. If False, the PSF will not be normalized.

astrophot.utils.initialize.centroids_from_segmentation_map#

signature: centroids_from_segmentation_map(seg_map: Union[numpy.ndarray, str], image: ‘Image’, sky_level: Optional[float] = None, hdul_index_seg: int = 0, skip_index: tuple = (0,))

identify centroid centers for all segments in a segmentation map

For each segment in the map, computes a flux weighted centroid in pixel space. A dictionary of pixel centers is produced where the keys of the dictionary correspond to the segment id’s.

Args:

  • seg_map (Union[np.ndarray, str]): A segmentation map which gives the object identity for each pixel

  • image (Union[np.ndarray, str]): An Image which will be used in the light weighted center of mass calculation

  • sky_level (float): The sky level to subtract from the image data before calculating centroids. Default: None, which uses the median of the image data.

  • hdul_index_seg (int): If reading from a fits file this is the hdu list index at which the map is found. Default: 0

  • skip_index (tuple): Lists which identities (if any) in the segmentation map should be ignored. Default (0,)

astrophot.utils.initialize.windows_from_segmentation_map#

signature: windows_from_segmentation_map(seg_map, hdul_index=0, skip_index=(0,))

Convert a segmentation map into boinding boxes

Takes a segmentation map as input and uses the segmentation ids to determine bounding boxes for every object. Scales the bounding boxes according to given factors and returns the coordinates.

each window is formatted as a list of lists with: window = [[xmin,ymin],[xmax,ymax]]

expand_scale changes the base window by the given factor. expand_border is added afterwards on all sides (so an expand border of 1 will add 2 to the total width of the window.

astrophot.utils.initialize.filter_windows#

signature: filter_windows(windows, min_size: Optional[float] = None, max_size: Optional[float] = None, min_area: Optional[float] = None, max_area: Optional[float] = None, min_flux: Optional[float] = None, max_flux: Optional[float] = None, image: ‘Image’ = None)

Filter a set of windows based on a set of criteria.

Args:

  • windows: A dictionary of windows to filter. Each window is formatted as a list of lists with: window = [[xmin,ymin],[xmax,ymax]]

  • min_size: minimum size of the window in pixels

  • max_size: maximum size of the window in pixels

  • min_area: minimum area of the window in pixels

  • max_area: maximum area of the window in pixels

  • min_flux: minimum flux of the window in ADU

  • max_flux: maximum flux of the window in ADU

  • image: the image from which the flux is calculated for min_flux and max_flux

astrophot.utils.initialize.transfer_windows#

signature: transfer_windows(windows, base_image, new_image)

Convert a set of windows from one image object to another. This will account for the relative adjustments in origin, pixelscale, and rotation between the two images.

Args:

  • windows: A dictionary of windows to be transferred. Each window is formatted as a list of lists with: window = [[xmin,ymin],[xmax,ymax]]

  • base_image: The image object from which the windows are being transferred.

  • new_image: The image object to which the windows are being transferred.

astrophot.utils.conversions.sersic_n_to_b#

signature: sersic_n_to_b(n: Union[float, numpy.ndarray, Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]]) -> Union[float, numpy.ndarray, Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]]

Compute the b(n) for a sersic model. This factor ensures that the \(R_e\) and \(I_e\) parameters do in fact correspond to the half light values and not some other scale radius/intensity.

astrophot.utils.conversions.sersic_I0_to_flux_np#

signature: sersic_I0_to_flux_np(I0: numpy.ndarray, n: numpy.ndarray, R: numpy.ndarray, q: numpy.ndarray) -> numpy.ndarray

Compute the total flux integrated to infinity for a 2D elliptical sersic given the \(I_0,n,R_s,q\) parameters which uniquely define the profile (\(I_0\) is the central intensity in flux/arcsec^2). Note that \(R_s\) is not the effective radius, but in fact the scale radius in the more straightforward sersic representation:

\[I(R) = I_0e^{-(R/R_s)^{1/n}}\]

Args:

  • I0: central intensity (flux/arcsec^2)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_flux_to_I0_np#

signature: sersic_flux_to_I0_np(flux: numpy.ndarray, n: numpy.ndarray, R: numpy.ndarray, q: numpy.ndarray) -> numpy.ndarray

Compute the central intensity (flux/arcsec^2) for a 2D elliptical sersic given the \(F,n,R_s,q\) parameters which uniquely define the profile (\(F\) is the total flux integrated to infinity). Note that \(R_s\) is not the effective radius, but in fact the scale radius in the more straightforward sersic representation:

\[I(R) = I_0e^{-(R/R_s)^{1/n}}\]

Args:

  • flux: total flux integrated to infinity (flux)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_Ie_to_flux_np#

signature: sersic_Ie_to_flux_np(Ie: numpy.ndarray, n: numpy.ndarray, R: numpy.ndarray, q: numpy.ndarray) -> numpy.ndarray

Compute the total flux integrated to infinity for a 2D elliptical sersic given the \(I_e,n,R_e,q\) parameters which uniquely define the profile (\(I_e\) is the intensity at \(R_e\) in flux/arcsec^2). Note that \(R_e\) is the effective radius in the sersic representation:

\[I(R) = I_ee^{-b_n[(R/R_e)^{1/n}-1]}\]

Args:

  • Ie: intensity at the effective radius (flux/arcsec^2)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_flux_to_Ie_np#

signature: sersic_flux_to_Ie_np(flux: numpy.ndarray, n: numpy.ndarray, R: numpy.ndarray, q: numpy.ndarray) -> numpy.ndarray

Compute the intensity at \(R_e\) (flux/arcsec^2) for a 2D elliptical sersic given the \(F,n,R_e,q\) parameters which uniquely define the profile (\(F\) is the total flux integrated to infinity). Note that \(R_e\) is the effective radius in the sersic representation:

\[I(R) = I_ee^{-b_n[(R/R_e)^{1/n}-1]}\]

Args:

  • flux: flux integrated to infinity (flux)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_I0_to_flux_torch#

signature: sersic_I0_to_flux_torch(I0: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], n: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], R: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], q: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Compute the total flux integrated to infinity for a 2D elliptical sersic given the \(I_0,n,R_s,q\) parameters which uniquely define the profile (\(I_0\) is the central intensity in flux/arcsec^2). Note that \(R_s\) is not the effective radius, but in fact the scale radius in the more straightforward sersic representation:

\[I(R) = I_0e^{-(R/R_s)^{1/n}}\]

Args:

  • I0: central intensity (flux/arcsec^2)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_flux_to_I0_torch#

signature: sersic_flux_to_I0_torch(flux: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], n: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], R: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], q: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Compute the central intensity (flux/arcsec^2) for a 2D elliptical sersic given the \(F,n,R_s,q\) parameters which uniquely define the profile (\(F\) is the total flux integrated to infinity). Note that \(R_s\) is not the effective radius, but in fact the scale radius in the more straightforward sersic representation:

\[I(R) = I_0e^{-(R/R_s)^{1/n}}\]

Args:

  • flux: total flux integrated to infinity (flux)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_Ie_to_flux_torch#

signature: sersic_Ie_to_flux_torch(Ie: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], n: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], R: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], q: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Compute the total flux integrated to infinity for a 2D elliptical sersic given the \(I_e,n,R_e,q\) parameters which uniquely define the profile (\(I_e\) is the intensity at \(R_e\) in flux/arcsec^2). Note that \(R_e\) is the effective radius in the sersic representation:

\[I(R) = I_ee^{-b_n[(R/R_e)^{1/n}-1]}\]

Args:

  • Ie: intensity at the effective radius (flux/arcsec^2)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_flux_to_Ie_torch#

signature: sersic_flux_to_Ie_torch(flux: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], n: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], R: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], q: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Compute the intensity at \(R_e\) (flux/arcsec^2) for a 2D elliptical sersic given the \(F,n,R_e,q\) parameters which uniquely define the profile (\(F\) is the total flux integrated to infinity). Note that \(R_e\) is the effective radius in the sersic representation:

\[I(R) = I_ee^{-b_n[(R/R_e)^{1/n}-1]}\]

Args:

  • flux: flux integrated to infinity (flux)

  • n: sersic index

  • R: Scale radius

  • q: axis ratio (b/a)

astrophot.utils.conversions.sersic_inv_np#

signature: sersic_inv_np(I: numpy.ndarray, n: numpy.ndarray, Re: numpy.ndarray, Ie: numpy.ndarray) -> numpy.ndarray

Invert the sersic profile. Compute the radius corresponding to a given intensity for a pure sersic profile.

astrophot.utils.conversions.sersic_inv_torch#

signature: sersic_inv_torch(I: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], n: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], Re: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’], Ie: Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]) -> Annotated[torch.Tensor, ‘One of: torch.Tensor or jax.numpy.ndarray depending on the chosen backend.’]

Invert the sersic profile. Compute the radius corresponding to a given intensity for a pure sersic profile.

astrophot.utils.conversions.moffat_I0_to_flux#

signature: moffat_I0_to_flux(I0: float, n: float, rd: float, q: float) -> float

Compute the total flux integrated to infinity for a moffat profile.

Args:

  • I0: central intensity (flux/arcsec^2)

  • n: moffat curvature parameter (unitless)

  • rd: scale radius

  • q: axis ratio

astrophot.utils.conversions.flux_to_sb#

signature: flux_to_sb(flux: float, pixel_area: float, zeropoint: float) -> float

Conversion from flux units to logarithmic surface brightness units.

\[\mu = -2.5\log_{10}(flux) + z.p. + 2.5\log_{10}(A)\]

where \(z.p.\) is the zeropoint and \(A\) is the area of a pixel.

astrophot.utils.conversions.flux_to_mag#

signature: flux_to_mag(flux: float, zeropoint: float, fluxe: Optional[float] = None) -> float

Converts a flux total into logarithmic magnitude units.

\[m = -2.5\log_{10}(flux) + z.p.\]

where \(z.p.\) is the zeropoint.

astrophot.utils.conversions.sb_to_flux#

signature: sb_to_flux(sb: float, pixel_area: float, zeropoint: float) -> float

Converts logarithmic surface brightness units into flux units.

\[flux = A 10^{-(\mu - z.p.)/2.5}\]

where \(z.p.\) is the zeropoint and \(A\) is the area of a pixel.

astrophot.utils.conversions.mag_to_flux#

signature: mag_to_flux(mag: float, zeropoint: float, mage: Optional[float] = None) -> float

converts logarithmic magnitude units into a flux total.

\[flux = 10^{-(m - z.p.)/2.5}\]

where \(z.p.\) is the zeropoint.

astrophot.utils.conversions.magperarcsec2_to_mag#

signature: magperarcsec2_to_mag(mu: float, a: Optional[float] = None, b: Optional[float] = None, A: Optional[float] = None) -> float

Converts mag/arcsec^2 to mag

Args:

  • mu: mag/arcsec^2

  • a: semi major axis radius (arcsec)

  • b: semi minor axis radius (arcsec)

  • A: pre-calculated area (arcsec^2)

\[m = \mu -2.5\log_{10}(A)\]

where \(A\) is an area in arcsec^2.

astrophot.utils.conversions.mag_to_magperarcsec2#

signature: mag_to_magperarcsec2(m: float, a: Optional[float] = None, b: Optional[float] = None, R: Optional[float] = None, A: Optional[float] = None) -> float

Converts mag to mag/arcsec^2

Args:

  • m: mag

  • a: semi major axis radius (arcsec)

  • b: semi minor axis radius (arcsec)

  • A: pre-calculated area (arcsec^2)

\[\mu = m + 2.5\log_{10}(A)\]

where \(A\) is an area in arcsec^2.

astrophot.utils.ls_open#

signature: ls_open(ra, dec, size_arcsec, band=’r’, release=’ls_dr9’)

Retrieves and opens a FITS cutout from the deepest stacked image in the specified Legacy Survey data release using the Astro Data Lab SIA service.

Args: ra (float): Right Ascension in decimal degrees. dec (float): Declination in decimal degrees. size_arcsec (float): Size of the square cutout (side length) in arcseconds. band (str): The filter band (e.g., ‘g’, ‘r’, ‘z’). Case-insensitive. release (str): The Legacy Survey Data Release (e.g., ‘DR9’).

Returns: astropy.io.fits.HDUList: The opened FITS file object.