Math utils

class surfalize.mathutils.Sinusoid(amplitude, period, x0, y0)

Bases: object

Generic sinusoid representation.

Parameters:
amplitudefloat

amplitude.

periodfloat

period.

x0float

offset in x.

y0float

offset in y.

Methods

__call__(x)

Computes the value of a generic sinusoid at position x.

first_extremum()

Compute the position of the first extremum (peak or valley) for x >= 0.

first_peak()

Compute the position of the first peak for x >= 0.

from_fit(xdata, ydata[, p0, infer_p0])

Fit a general sinusoid to x and y data using scipy.optimize.curve_fit.

first_extremum()

Compute the position of the first extremum (peak or valley) for x >= 0.

Returns:
xfefloat
first_peak()

Compute the position of the first peak for x >= 0.

Returns:
xfpfloat
classmethod from_fit(xdata, ydata, p0=None, infer_p0=False)

Fit a general sinusoid to x and y data using scipy.optimize.curve_fit. Optionally, an initial guess for the sinusoid parameters a, p, x0, y0 can be specified using the p0 keyword argument, where a is the amplitude, p is the period, x0 is the lateral and y0 the vertical offset.

This function uses unbounded fitting, which can result in negative amplitude, because bounded fitting using scipy.optimize.curve_fit invokes a different algorithm which seems to perform worse on this specific fitting problem. Instead, a result with negative amplitude is converted into the positive amplitude equivalent by phase shifting.

Parameters:
xdatalist-like

array of x-data

ydatalist-like

array of y-data

p0list-like[float, float, float, float] | None, defaults to None

Optional initial guess for the parameters a, p, x0, y0.

infer_p0bool, defaults to False

If True, automatically infers starting guesses of the parameters. Any values provided to the p0 keyword argument will be overwritten with the automatically inferred values.

Returns:
Sinusoid
surfalize.mathutils.argclosest(x, xdata)

Returns the index of the value in an array that is closest to the value x.

Parameters:
xfloat

value to which closest array value index should be computed

xdataarray_like

array of x-values

Returns:
indexint

Index of the value in xdata that is closest to x.

surfalize.mathutils.argmax_all(arr)

Returns all indices where the array reaches its maximum value.

Parameters:
arrarray-like

Input array

Returns:
numpy.ndarray

Array of indices where the maximum value occurs

surfalize.mathutils.argmin_all(arr)

Returns all indices where the array reaches its minimum value.

Parameters:
arrarray-like

Input array

Returns:
numpy.ndarray

Array of indices where the minimum value occurs

surfalize.mathutils.closest(x, data)

Returns the value in an array that is closest to the value x.

Parameters:
xfloat

value to which closest array value index should be computed

xdataarray_like

array of x-values

Returns:
value

Value in xdata that is closest to x.

surfalize.mathutils.get_period_fft_1d(xdata, ydata)

Estimates the dominant period from a 1d periodic profile with uniformly spaced points.

Parameters:
xdatalist-like

array of uniformly spaced xdata.

ydatalist-like

array of ydata with the same size as xdata.

Returns:
periodfloat
surfalize.mathutils.interp1d(xdata, ydata, assume_sorted=False)

Creates a function that linearly interpolates the given x- and y-data at any value of x.

Mimics scipy.interpolate.interp1d, since the scipy version is no longer supported and might be removed in future versions. Contrary to the scipy implementation, assume_sorted is False by default, since a default True value would lead to more errors. If the xdata is sorted in ascending order, then assume_sorted can be set to True to gain a performance increase.

Parameters:
xdataarray_like

array of x-values

ydataarray_like

array of y-values

assume_sortedbool, default False

If True, they xdata array must be supplied with ascendingly ordered values and sorting is skipped. If False, the array xdata will be sorted in ascending order and the array ydata will be sorted accordingly.

Returns:
function

Linear interpolation function y(x).

surfalize.mathutils.interpolate_line_on_2d_array(array, start, end, order=3, num_points=100)

Interpolates a line between two points on a 2d array using spline interpolation.

Parameters:
array2d array-like
starttuple[int, int]

Index of the start point.

endtuple[int, int]

Index of the end point.

orderint

Order of spline interpolation. Defaults to 3.

num_pointsint

Number of points of the interpolated line. Defaults to 100.

Returns:
np.ndarray