psf.base#
Base PSF Module
This module provides a base class for Point Spread Function (PSF) calculations.
Kramer Harrison, 2025
Functions
|
Replace values <= 0 in the image with the smallest positive value in the image. |
Classes
|
Base class for Point Spread Function (PSF) calculations. |
- class BasePSF(optic: Optic, field: Field, wavelength: str | float, num_rays=128, strategy='chief_ray', remove_tilt=True, **kwargs)[source]#
Base class for Point Spread Function (PSF) calculations.
- Parameters:
optic (Optic) – The optical system.
field (tuple) – The field as (x, y) at which to compute the PSF.
wavelength (str | float) – The wavelength of light. Can be ‘primary’ or a float value.
num_rays (int, optional) – The number of rays used for wavefront computation. Defaults to 128.
strategy (str) – The calculation strategy to use. Supported options are “chief_ray”, “centroid_sphere”, and “best_fit_sphere”. Defaults to “chief_ray”.
remove_tilt (bool) – If True, removes tilt and piston from the OPD data. Defaults to True.
**kwargs – Additional keyword arguments passed to the strategy.
- Variables:
psf (ndarray) – The computed PSF. This should be set by subclasses.
- Methods:
view (projection=’2d’, log=False, figsize=(7, 5.5) – num_points=128): Visualizes the PSF.
cross_section (axis=’y’, through=’peak’) – Returns a 1D slice of the PSF.
line_spread_function (axis=’y’) – Returns the line spread function.
view_cross_section (axis=’both’, log=False) – Plots PSF cross-sections.
- cross_section(axis: Literal['x', 'y'] = 'y', through: Literal['peak', 'center'] = 'peak') tuple[BEArray, BEArray][source]#
Returns a 1D cross-section of the PSF.
Rows of the PSF array run along y and columns along x, as in view.
- Parameters:
- Returns:
A tuple
(coords, values).coordsare the pixel centre positions along axis, with pixeln // 2at 0, in µm if the subclass implements _get_psf_units and in pixels otherwise.valuesare the PSF values as stored (not renormalized).- Return type:
- Raises:
RuntimeError – If the PSF has not been computed.
ValueError – If axis is not ‘x’ or ‘y’, or through is not ‘peak’ or ‘center’.
- static fit_and_remove_tilt(data: WavefrontData, remove_piston: bool = False, ridge: float = 1e-12) be.ndarray#
Removes piston and tilt from OPD data using weighted least squares.
- Parameters:
data (WavefrontData) – The wavefront data containing pupil coordinates and OPD.
remove_piston (bool, optional) – If True, removes piston term as well as tilt. Defaults to False.
ridge (float, optional) – Small diagonal regularization for stability. Defaults to 1e-12.
- Returns:
OPD with piston and tilt removed, shape (N,).
- Return type:
opd_detrended (be.ndarray)
- get_data(field: tuple[float, float], wl: float) WavefrontData#
Retrieves precomputed wavefront data for a field and wavelength.
- Parameters:
- Returns:
A data container with the computed wavefront results.
- Return type:
- line_spread_function(axis: Literal['x', 'y'] = 'y') tuple[BEArray, BEArray][source]#
Returns the line spread function (LSF) of the PSF.
The LSF along axis is the PSF integrated along the other axis (sum times pixel pitch), so the LSF integrates to the same total as the PSF.
- Parameters:
axis (str, optional) – The axis along which the LSF is returned, ‘x’ or ‘y’. Defaults to ‘y’.
- Returns:
A tuple
(coords, lsf).coordsare the pixel centre positions along axis, in the same units as cross_section.- Return type:
- Raises:
RuntimeError – If the PSF has not been computed.
ValueError – If axis is not ‘x’ or ‘y’.
- strehl_ratio()[source]#
Computes the Strehl ratio of the PSF.
The Strehl ratio is the ratio of the peak intensity of the aberrated PSF to the peak intensity of the diffraction-limited PSF. Assumes self.psf is normalized such that its peak would be 1.0 (or 100%) for a diffraction-limited system.
- Returns:
The Strehl ratio.
- Return type:
- Raises:
RuntimeError – If the PSF has not been computed.
- view(fig_to_plot_on: Figure | None = None, projection: str = '2d', log: bool = False, figsize: tuple = (7, 5.5), threshold: float = 0.05, num_points: int = 128) tuple[Figure, Axes][source]#
Visualizes the PSF.
- Parameters:
projection (str, optional) – The projection type. Can be ‘2d’ or ‘3d’. Defaults to ‘2d’.
log (bool, optional) – Whether to use a logarithmic scale for the intensity. Defaults to False.
figsize (tuple, optional) – The figure size. Defaults to (7, 5.5).
threshold (float, optional) – The threshold for determining the bounds of the PSF for zoomed view. Defaults to 0.05.
num_points (int, optional) – The number of points used for interpolating the PSF for smoother visualization. Defaults to 128.
- Returns:
A tuple containing the figure and axes objects.
- Return type:
- Raises:
RunentimeError – If the PSF has not been computed.
ValueError – If the projection is not ‘2d’ or ‘3d’.
RuntimeError – If the PSF has not been computed by the subclass.
- view_cross_section(axis: Literal['x', 'y', 'both'] = 'both', log: bool = False, fig_to_plot_on: Figure | None = None, figsize: tuple[float, float] = (7, 4.5), *, show: bool = True) tuple[Figure, Axes][source]#
Plots cross-sections of the PSF through its peak.
- Parameters:
axis (str, optional) – The cross-section(s) to plot: ‘x’, ‘y’ or ‘both’. Defaults to ‘both’.
log (bool, optional) – Whether to use a logarithmic scale for the intensity. Defaults to False.
fig_to_plot_on (Figure, optional) – The figure to plot on. If None, a new figure is created. Defaults to None.
figsize (tuple[float, float], optional) – The figure size. Defaults to (7, 4.5).
show (bool) – If True (default), calls plt.show(). Set False for headless use.
- Returns:
A tuple containing the figure and axes objects.
- Return type:
- Raises:
RuntimeError – If the PSF has not been computed.
ValueError – If axis is not ‘x’, ‘y’ or ‘both’.
- replace_nonpositive(image, min_value=1e-09)[source]#
Replace values <= 0 in the image with the smallest positive value in the image. If no positive value exists, use min_value.
- Parameters:
image – Array (backend or numpy) to process.
min_value – Value to use if no positive values exist (default: 1e-9).
- Returns:
Array with non-positive values replaced.