AberrationsFit#

class pyrost.AberrationsFit(parent, defocus, distance, pixels, pixel_aberrations, pixel_size, wavelength, phase=None, roi=None)#

Least squares optimizer for the lens aberrations’ profiles. AberrationsFit is capable of fitting lens’ pixel aberrations, deviation angles, and phase profile with polynomial function. Based on scipy.optimise.least_squares().

Parameters
  • parent (ReferenceType) – The Speckle tracking data container, from which the object is derived.

  • kwargs – Necessary and optional attributes specified in the notes section.

Raises

ValueError – If any of the necessary attributes has not been provided.

Notes

Necessary attributes:

  • defocus : Defocus distance [m].

  • distance : Sample-to-detector distance [m].

  • pixels : Pixel coordinates [pixels].

  • pixel_aberrations : Pixel aberrations profile [pixels].

  • pixel_size : Pixel’s size [m].

  • wavelength : Incoming beam’s wavelength [m].

Optional attributes:

  • roi : Region of interest.

  • thetas : Scattering angles [rad].

  • theta_ab : angular displacement profile [rad].

  • phase : aberrations phase profile [rad].

See also

scipy.optimize.least_squares() : Full nonlinear least-squares algorithm description.

contents()#

Return a list of the attributes stored in the container that are initialised.

Return type

List[str]

Returns

List of the attributes stored in the container.

crop_data(roi)#

Return a new AberrationsFit object with the updated roi.

Parameters

roi (Iterable) – Region of interest in the detector plane.

Return type

AberrationsFit

Returns

New AberrationsFit object with the updated roi.

fit(max_order=2, xtol=1e-14, ftol=1e-14, loss='cauchy')#

Fit lens’ pixel aberrations with polynomial function using scipy.optimise.least_squares().

Parameters
  • max_order (int) – Maximum order of the polynomial model function.

  • xtol (float) – Tolerance for termination by the change of the independent variables.

  • ftol (float) – Tolerance for termination by the change of the cost function.

  • loss (str) –

    Determines the loss function. The following keyword values are allowed:

    • linear : rho(z) = z. Gives a standard least-squares problem.

    • soft_l1 : rho(z) = 2 * ((1 + z)**0.5 - 1). The smooth approximation of l1 (absolute value) loss. Usually a good choice for robust least squares.

    • huber : rho(z) = z if z <= 1 else 2*z**0.5 - 1. Works similarly to ‘soft_l1’.

    • cauchy (default) : rho(z) = ln(1 + z). Severely weakens outliers influence, but may cause difficulties in optimization process.

    • arctan : rho(z) = arctan(z). Limits a maximum loss on a single residual, has properties similar to ‘cauchy’.

Return type

Dict[str, Union[float, ndarray]]

Returns

A dictionary with the fitting information. The following elements are defined inside:

  • c_3 : Third order aberrations coefficient [rad / mrad^3].

  • c_4 : Fourth order aberrations coefficient [rad / mrad^4].

  • fit : Array of the polynomial function coefficients of the pixel aberrations fit.

  • ph_fit : Array of the polynomial function coefficients of the phase aberrations fit.

  • rel_err : Vector of relative errors of the fit coefficients.

  • r_sq : R**2 goodness of fit.

See also

scipy.optimize.least_squares() : Full nonlinear least-squares algorithm description.

fit_phase(max_order=3, xtol=1e-14, ftol=1e-14, loss='linear')#

Fit lens’ phase aberrations with polynomial function using scipy.optimise.least_squares().

Parameters
  • max_order (int) – Maximum order of the polynomial model function.

  • xtol (float) – Tolerance for termination by the change of the independent variables.

  • ftol (float) – Tolerance for termination by the change of the cost function.

  • loss (str) –

    Determines the loss function. The following keyword values are allowed:

    • linear : rho(z) = z. Gives a standard least-squares problem.

    • soft_l1 : rho(z) = 2 * ((1 + z)**0.5 - 1). The smooth approximation of l1 (absolute value) loss. Usually a good choice for robust least squares.

    • huber : rho(z) = z if z <= 1 else 2*z**0.5 - 1. Works similarly to ‘soft_l1’.

    • cauchy (default) : rho(z) = ln(1 + z). Severely weakens outliers influence, but may cause difficulties in optimization process.

    • arctan : rho(z) = arctan(z). Limits a maximum loss on a single residual, has properties similar to ‘cauchy’.

Return type

Dict[str, Union[float, ndarray]]

Returns

A dictionary with the fitting information. The following elements are defined inside:

  • c_3 : Third order aberrations coefficient [rad / mrad^3].

  • c_4 : Fourth order aberrations coefficient [rad / mrad^4].

  • fit : Array of the polynomial function coefficients of the pixel aberrations fit.

  • ph_fit : Array of the polynomial function coefficients of the phase aberrations fit.

  • rel_err : Vector of relative errors of the fit coefficients.

  • r_sq : R**2 goodness of fit.

See also

scipy.optimize.least_squares() : Full nonlinear least-squares algorithm description.

get(attr, value=None)#

Retrieve a dataset, return value if the attribute is not found.

Parameters
  • attr (str) – Data attribute.

  • value (Optional[Any]) – Data which is returned if the attribute is not found.

Return type

Any

Returns

Attribute’s data stored in the container, value if attr is not found.

items()#

Return (key, value) pairs of the datasets stored in the container.

Return type

ItemsView

Returns

(key, value) pairs of the datasets stored in the container.

keys()#

Return a list of the attributes available in the container.

Return type

List[str]

Returns

List of the attributes available in the container.

model(fit)#

Return the polynomial function values of lens’ deviation angles fit.

Parameters

fit (ndarray) – Lens` pixel aberrations fit coefficients.

Return type

ndarray

Returns

Array of polynomial function values.

phase_to_pix(ph_fit)#

Convert fit coefficients from pixel aberrations fit to aberrations phase fit.

Parameters

ph_fit (ndarray) – Lens` phase aberrations fit coefficients.

Return type

ndarray

Returns

Lens’ pixel aberrations fit coefficients.

pix_to_phase(fit)#

Convert fit coefficients from pixel aberrations fit to aberrations phase fit.

Parameters

fit (ndarray) – Lens’ pixel aberrations fit coefficients.

Return type

ndarray

Returns

Lens` phase aberrations fit coefficients.

remove_linear_term(fit=None, xtol=1e-14, ftol=1e-14, loss='cauchy')#

Return a new AberrationsFit object with the linear term removed from pixel_aberrations profile.

Parameters
  • fit (Optional[ndarray]) – Fit coefficients of a first order polynomial. Inferred from pixel_aberrations by fitting a line if None.

  • xtol (float) – Tolerance for termination by the change of the independent variables.

  • ftol (float) – Tolerance for termination by the change of the cost function.

  • loss (str) –

    Determines the loss function. The following keyword values are allowed:

    • linear : rho(z) = z. Gives a standard least-squares problem.

    • soft_l1 : rho(z) = 2 * ((1 + z)**0.5 - 1). The smooth approximation of l1 (absolute value) loss. Usually a good choice for robust least squares.

    • huber : rho(z) = z if z <= 1 else 2*z**0.5 - 1. Works similarly to ‘soft_l1’.

    • cauchy (default) : rho(z) = ln(1 + z). Severely weakens outliers influence, but may cause difficulties in optimization process.

    • arctan : rho(z) = arctan(z). Limits a maximum loss on a single residual, has properties similar to ‘cauchy’.

Return type

AberrationsFit

Returns

New AberrationsFit object with the updated pixel_aberrations and phase.

replace(**kwargs)#

Return a new container object with a set of attributes replaced.

Parameters

kwargs (Any) – A set of attributes and the values to to replace.

Return type

~D

Returns

A new container object with updated attributes.

update_center(center)#

Return a new AberrationsFit object with the pixels centered around center.

Parameters

center (float) – Index of the zerro scattering angle or direct beam pixel.

Return type

AberrationsFit

Returns

New AberrationsFit object with the updated pixels, phase, and pixel_aberrations.

update_phase()#

Return a new AberrationsFit object with the updated phase.

Return type

AberrationsFit

Returns

New AberrationsFit object with the updated phase.

values()#

Return the attributes’ data stored in the container.

Return type

ValuesView

Returns

List of data stored in the container.