spcal.calc¶
Misc and helper calculation functions.
- spcal.calc.expand_mask(mask: ndarray, size: int) ndarray¶
Grows mask values in
maskbysizeon either side.Used to expand the regions where peaks are detected, to exclude nearby data. This is equivalent to
scipy.ndimage.binary_dilationexcept faster.- Parameters:
mask – a 1d or 2d-array of boolean type of shape (samples, features)
size – size to expand on both sides
- Returns:
mask dilated by
size, same shape asmask
- spcal.calc.interpolate_3d(x: ndarray | float, y: ndarray | float, z: ndarray | float, xs: ndarray, ys: ndarray, zs: ndarray, data: ndarray) ndarray | float¶
Cubic interpolation of (x, y, z) for data.
- Parameters:
x – x position of values to interpolate
y – y position of values to interpolate
z – z position of values to interpolate
xs – x values of
datays – y values of
datazs – z values of
datadata – known values, shape (xs, ys, zs)
- Returns:
interpolated values, shape (x)
- spcal.calc.is_integer_or_near(x: ndarray | float, max_deviation: float = 0.001) ndarray | float¶
Test if float data is ‘near’ integer. Near integers values are those less than max_deviation from a whole number.
- Parameters:
x – float array
max_deviation – max distance from whole number
- Returns:
array of bool
- spcal.calc.mode(x: ndarray, bins: int | ndarray | str = 'auto') float¶
Calculates the mode of ‘x’.
- Parameters:
x – array
- Returns:
mode
- spcal.calc.otsu(x: ndarray, remove_nan: bool = False, nbins: str | int = 'fd') float¶
Calculates the otsu threshold.
The Otsu threshold minimises intra-class variance for a two class system. If remove_nan then all nans are removed before computation.
- Parameters:
x – array
remove_nan – remove nan values
nbins – number of bins to use
See also
skimage.filters.threshold_otsu()
- spcal.calc.pca(x: ndarray, trim_to_components: int = 2) tuple[ndarray, ndarray, ndarray]¶
Perform a PCA on ‘x’, standard scales data.
- Parameters:
x – input array of shape (samples, features)
trim_to_components – trim to this many dims
- Returns:
pca data points component vectors explained variance per dim
- spcal.calc.search_sorted_closest(x: ndarray, v: ndarray, check_max_diff: float | None = None)¶
Get the idx of the closest values in
xforv.If
check_max_diffis a value, the maximum distance must be lower.- Parameters:
x – sorted array
y – values to find closest idx of in x
check_max_diff – if not None, check maximum diff is less than this
- Returns:
idx of closest
xvalues forv- Raises:
ValueError if check_max_diff is not None and max diff greater. –
- spcal.calc.weighted_linreg(x: ndarray, y: ndarray, w: ndarray | None = None) tuple[float, float, float, float]¶
Weighted linear regression.
Uses polyfit with sqrt(weights) for intercept and gradient.
- Parameters:
x – 1d-array
y – array, same size as x
w – weights, same size as x
- Returns:
gradient intercept r² error, S(y,x) the (unweighted) residual standard deviation
See also
pewlib.calibration.weighted_rsq()
- spcal.calc.weighted_rsq(x: ndarray, y: ndarray, w: ndarray | None = None) float¶
Calculate r² for weighted linear regression.
- Parameters:
x – 1d-array
y – array, same size as x
w – weights, same size as x
- spcal.calc.weights_from_weighting(x: ndarray, weighting: str, safe: bool = True) ndarray¶
Get weighting for x.
Conveience function for calculating simple weightings. If safe then any zeros in x are replace with the minimum non-zero value.
- Parameters:
x – 1d-array
weighting – weighting string {‘equal’, ‘x’, ‘1/x’, ‘1/(x^2)’}
safe – replace zeros with minimum
- Returns:
weights, same size as x