irradiapy.utils.math module

This module contains math utilities for the irradiapy package.

irradiapy.utils.math.apply_boundary_conditions(data_atoms, x, y, z)[source]

Apply boundary conditions to atoms.

Parameters:
  • data_atoms (dict[str, Any]) – Atoms data containing atom positions and boundaries.

  • x (bool) – Whether to apply periodic boundary conditions in the x direction.

  • y (bool) – Whether to apply periodic boundary conditions in the y direction.

  • z (bool) – Whether to apply periodic boundary conditions in the z direction.

Returns:

Updated atoms data with applied boundary conditions.

Return type:

dict[str, Any]

irradiapy.utils.math.fit_gaussian(xs, ys, p0=None, asymmetry=1.0)[source]

Fit data to a Gaussian function.

Parameters:
  • xs (GenericAlias[double]) – X values where the function is evaluated.

  • ys (GenericAlias[double]) – Y values at the given xs.

  • p0 (GenericAlias[double] | None) – Initial guess of fit parameters. If None, a guess is generated.

  • asymmetry (float) – Bound for the asymmetry fit parameter. Fit will be done in (-asymmetry, asymmetry).

Return type:

tuple[GenericAlias[double], GenericAlias[double], Callable[[GenericAlias[double]], GenericAlias[double]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted Gaussian.

irradiapy.utils.math.fit_linear(xs, ys, yerrs=None)[source]

Fit a linear function to the given data: y = a * x + b.

Parameters:
  • xs (GenericAlias[double]) – X values where the function is evaluated.

  • ys (GenericAlias[double]) – Y values at the given xs.

  • yerrs (GenericAlias[double] | None) – Y errors.

Return type:

tuple[GenericAlias[double], GenericAlias[double], Callable[[GenericAlias[double]], GenericAlias[double]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted linear function.

irradiapy.utils.math.fit_lorentzian(xs, ys, p0=None, asymmetry=1.0)[source]

Fit data to a Lorentzian function.

Parameters:
  • xs (GenericAlias[double]) – X values where the function is evaluated.

  • ys (GenericAlias[double]) – Y values at the given xs.

  • p0 (GenericAlias[double] | None) – Initial guess of fit parameters. If None, a guess is generated.

  • asymmetry (float) – Bound for the asymmetry fit parameter. Fit will be done in (-asymmetry, asymmetry).

Return type:

tuple[GenericAlias[double], GenericAlias[double], Callable[[GenericAlias[double]], GenericAlias[double]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted Lorentzian.

irradiapy.utils.math.fit_offset_power_law(xs, ys, yerrs=None)[source]

Fit a power law with an offset to the given histogram data: y = a * x**k + b.

Note

The presence of the offset b prevents a log–log linearization. We therefore fit the model directly in linear space via nonlinear least squares.

Parameters:
  • xs (GenericAlias[double]) – X values where the function is evaluated.

  • ys (GenericAlias[double]) – Y values at the given xs.

  • yerrs (GenericAlias[double] | None) – Y errors (standard deviations) in linear space.

Return type:

tuple[GenericAlias[double], GenericAlias[double], Callable[[GenericAlias[double]], GenericAlias[double]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted power law with offset.

irradiapy.utils.math.fit_power_law(xs, ys, yerrs=None)[source]

Fit a power law to the given histogram data: y = a * x**k.

Fitted in log-log space: log(y) = log(a) + k * log(x)

Parameters:
  • xs (GenericAlias[double]) – x-values where the function is evaluated.

  • ys (GenericAlias[double]) – y-values at the given xs.

  • yerrs (GenericAlias[double] | None) – y-errors.

Return type:

tuple[GenericAlias[double], GenericAlias[double], Callable[[GenericAlias[double]], GenericAlias[double]]]

Returns:

  • popt (npt.NDArray[np.float64]) – Optimal values for the parameters.

  • pcov (npt.NDArray[np.float64]) – Covariance of popt.

  • fit_function (Callable[[npt.NDArray[np.float64]], npt.NDArray[np.float64]]) – Function that evaluates the fitted power law.

irradiapy.utils.math.gaussian(xs, x_peak, linewidth, amplitude, asymmetry)[source]

Evaluate a Gaussian function.

Parameters:
  • xs (GenericAlias[double]) – Where to evaluate the function.

  • x_peak (float) – Position with maximum value.

  • linewidth (float) – Linewidth.

  • amplitude (float) – Maximum amplitude.

  • asymmetry (float) – Asymmetry.

Returns:

Evaluated Gaussian function.

Return type:

float | GenericAlias[double]

References

See https://doi.org/10.1016/j.nimb.2021.05.014

irradiapy.utils.math.linear(x, a, b)[source]

Evaluate a linear function: y = a * x + b.

Parameters:
  • x (GenericAlias[double]) – Input values.

  • a (float) – Slope of the line.

  • b (float) – Intercept of the line.

Returns:

Evaluated linear function.

Return type:

GenericAlias[double]

irradiapy.utils.math.lorentzian(xs, x_peak, linewidth, amplitude, asymmetry)[source]

Evaluate a Lorentzian function.

Parameters:
  • xs (GenericAlias[double]) – Where to evaluate the function.

  • x_peak (float) – Position with maximum value.

  • linewidth (float) – Linewidth.

  • amplitude (float) – Maximum amplitude.

  • asymmetry (float) – Asymmetry.

Returns:

Evaluated Lorentzian function.

Return type:

float | GenericAlias[double]

References

See https://doi.org/10.1016/j.nimb.2021.05.014

irradiapy.utils.math.offset_power_law(x, a, k, b)[source]

Evaluate a power law: y = a * x**k + b

Parameters:
  • x (GenericAlias[double]) – Input values.

  • a (float) – Prefactor.

  • k (float) – Exponent.

  • b (float) – Offset.

Returns:

Evaluated power law with offset.

Return type:

GenericAlias[double]

irradiapy.utils.math.power_law(x, a, k)[source]

Evaluate a power law: y = a * x**k

Parameters:
  • x (GenericAlias[double]) – Input values.

  • a (float) – Prefactor.

  • k (float) – Exponent.

Returns:

Evaluated power law.

Return type:

GenericAlias[double]

irradiapy.utils.math.recombine_in_radius(data_defects, radius)[source]

Recombine defects (interstitials and vacancies) within a given radius.

Takes into account periodic boundary conditions.

Parameters:
  • data_defects (dict[str, Any]) – Defects data containing defect positions and boundaries.

  • radius (float) – Radius within which to recombine defects.

Returns:

Updated defects data with recombined defects.

Return type:

dict[str, Any]

irradiapy.utils.math.repeated_prime_factors(n)[source]

Return the prime factors of n (with repetition).

Parameters:

n (int) – The number to factorize.

Returns:

List of prime factors of n, including repetitions.

Return type:

list[int]