AbberationsFit

class pyrost.AbberationsFit(**kwargs)

Lens’ abberations profile model regression using nonlinear least-squares algorithm. AbberationsFit is capable of fitting lens’ pixel abberations, deviation angles, and phase profile with polynomial function. Based on scipy.optimise.least_squares().

Parameters

**kwargs (dict) – Dictionary of the attributes’ data specified in attr_set and init_set.

Variables

attr_set (set) – Set of attributes in the container which are necessary to initialize in the constructor.

Raises

ValueError – If an attribute specified in attr_set has not been provided.

Notes

Necessary attributes:

  • defocus : Defocus distance [m].

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

  • phase : Abberations phase profile [rad].

  • pixel_abberations : Pixel abberations profile [pixels].

  • pixel_size : Pixel’s size [m].

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

See also

scipy.optimize.least_squares()

Full nonlinear least-squares algorithm description.

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

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

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

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

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

  • loss ({'linear', 'soft_l1', 'huber', 'cauchy', 'arctan'}, optional) –

    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’.

  • roi (iterable, optional) – Region of interest. Full region if roi is None.

Returns

dict with the following fields defined:

  • alpha : Third order abberations ceofficient [rad/mrad^3].

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

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

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

  • r_sq : R**2 goodness of fit.

Return type

dict

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', roi=None)

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

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

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

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

  • loss ({'linear', 'soft_l1', 'huber', 'cauchy', 'arctan'}, optional) –

    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’.

  • roi (iterable, optional) – Region of interest. Full region if roi is None.

Returns

dict with the following fields defined:

  • alpha : Third order abberations ceofficient [rad/mrad^3].

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

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

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

  • r_sq : R**2 goodness of fit.

Return type

dict

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 (object, optional) – Data which is returned if the attribute is not found.

Returns

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

Return type

object

classmethod import_data(st_data, center=0, axis=1)

Return a new AbberationsFit object with all the necessary data attributes imported from the STData container object st_data.

Parameters
  • st_data (STData) – STData container object.

  • axis (int, optional) – Detector’s axis (0 - slow axis, 1 - fast axis).

Returns

A new AbberationsFit object.

Return type

AbberationsFit

items()

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

Returns

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

Return type

dict_items

keys()

Return the list of attributes stored in the container.

Returns

List of attributes stored in the container.

Return type

dict_keys

model(fit, roi=None)

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

Parameters
  • fit (numpy.ndarray) – Lens` pixel abberations fit coefficients.

  • roi (iterable, optional) – Region of interest. Full region if roi is None.

Returns

Array of polynomial function values.

Return type

numpy.ndarray

phase_to_pix(ph_fit)

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

Parameters

ph_fit (numpy.ndarray) – Lens` phase abberations fit coefficients.

Returns

Lens’ pixel abberations fit coefficients.

Return type

numpy.ndarray

pix_to_phase(fit, roi=None)

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

Parameters
  • fit (numpy.ndarray) – Lens’ pixel abberations fit coefficients.

  • roi (iterable, optional) – Region of interest. Full region if roi is None.

Returns

Lens` phase abberations fit coefficients.

Return type

numpy.ndarray

values()

Return the attributes’ data stored in the container.

Returns

List of data stored in the container.

Return type

dict_values