swectral.denoiser.LocalPolynomial#

class swectral.denoiser.LocalPolynomial(window_size, polynomial_order, axis=1, mode='center', padding='ext_edge', numtype='float32', outlier_replacer=None, *, wfunc_x='tricubic', wdist_param_x=-1.0, wfunc_y='uniform', wdist_param_y=-1.0, axis_double_definition_warning=True)[source]#

Apply local polynomial data smoothing to 2D array of 1D series data. Local polynomial regression is applied to the rolling window to smooth the data series.

Attributes:
window_sizeint

Size of rolling window, must be at least 2.

polynomial_orderint

Order of used polynomial regression.

axisint, optional

Window is rolling along the assigned axis. The default is 1.

modestr, optional

Rolling mode for kernel application. Available options:

  • “center” - window is created centered on current data point, better applicable to static data series.

  • “end” - for time series, window is created end with current data point, better applicable to time series.

  • “knn” - k-nearst neighbor mode, k is the window size.

For “knn”, the window is constructed with available data centered on current data point, except the edge windows. The edge windows are constructed on the k nearst neighbor values.

Note: “knn” cannot be applied to “savitzky_golay_filter”.

The default is “center”.

paddingstr, optional

Padding approach for rolling mode “center” and “end”. Available options:

  • “none” - No padding applied. For rolling mode “center” and “end”, the resulting data series will be window_size - 1 smaller than the original data series without padding.

  • “nan” - Missing values are set to numpy nan after applying the function. “nan” cannot be applied to “savitzky_golay_filter”.

  • “ext_edge” - Edge value of the first and last window are applied to pad before applying the smoothing function.

  • “constant_edge” - Missing values are filled with edge value after applying function.

  • “extrapolation” - Missing values are linearly extrapolated from edge values with length of window_size/2 (center mode), window_size-1 (end mode) after function application.

The default is “ext_edge”.

numtypestr, optional

Number type of given data, supported number type of Numpy. Default is float32.

outlier_replacerArrayOutlier object, optional

Outlier removing object with defined attributes. The default is None.

wfunc_xstr or function

Weight distribution function for LOWESS filter at x axis.

The weight function can be a distribution name or a custom function.

Available weight distribution names:

“tricubic” / “triangular” / “cosine” / “gaussian” / “epanechnikov” / “exponential” / “uniform”

The default is “tricubic”. Configure distribution feature using shape parameter wdist_param_x.

wdist_param_xfloat

Shape parameter of weight distribution at x axis for LOWESS filter.

Default is -1.0, which applies the default shape parameter for the selected weight distribution.

The shape parameter affects the weight computation as follows:

For "triangular" weight distribution:

sample_weights = 1 - abs(distance_to_focal_point) / b

b is the shape parameter. Default is the distance of window boundary to focal point.

For "cosine" weight distribution:

sample_weights = cos(Pi * distance_to_focal_point / (2 * s))

s is the shape parameter. Default is the distance of window boundary to focal point.

For "gaussian" weight distribution:

sample_weights = exp(-(distance_to_focal_point ** 2) / (2 * (sigma ** 2)))

sigma is the shape parameter. Default is 1.0.

For "epanechnikov" weight distribution:

sample_weights = 1 - (distance_to_focal_point / h) ** 2

h is the shape parameter. Default is the distance of window boundary to focal point.

For "exponential" weight distribution:

sample_weights = exp(-abs(distance_to_focal_point / h))

h is the shape parameter. Default is the distance of window boundary to focal point.

wfunc_ystr or function

Weight distribution function for LOWESS filter at y axis. The weight function can be a distribution name or a custom function.

Available weight distribution names:

“tricubic” / “triangular” / “cosine” / “gaussian” / “epanechnikov” / “exponential” / “uniform”

The default is “uniform”. Configure distribution feature using shape parameter wdist_param_y.

wdist_param_yfloat

Shape parameter of weight distribution at y axis for LOWESS filter.

Default is -1.0, which applies the default shape parameter for the selected weight distribution. See wdist_param_x for details.

axis_double_definition_warningbool, optional

If True, the duplicate definition warning will prompt when function has “axis” argument.

The default is True. Set false for known application.

Methods

savitzky_golay_filter(data_array)

Implemente Savitzky-Golay smoothing of input 2D data array.

simple_polynomial_filter(data_array)

Implemente simple polynomial smoothing of input 2D data array.

lowess_filter(data_array)

Implemente LOWESS smoothing of input 2D data array.

Examples

Basic usage with window_size and polynomial_order:

>>> lp = LocalPolynomial(3, 1)

Use different window_size:

>>> lp = LocalPolynomial(4, 1)

Use different polynomial_order:

>>> lp = LocalPolynomial(3, 2)

Pad with numpy.nan:

>>> lp = LocalPolynomial(3, 1, padding='nan')

Specify rolling mode:

>>> lp = LocalPolynomial(3, 1, mode='knn')

Compute along a different axis:

>>> lp = LocalPolynomial(3, 1, axis=0)
__init__(window_size, polynomial_order, axis=1, mode='center', padding='ext_edge', numtype='float32', outlier_replacer=None, *, wfunc_x='tricubic', wdist_param_x=-1.0, wfunc_y='uniform', wdist_param_y=-1.0, axis_double_definition_warning=True)[source]#

Methods

__init__(window_size, polynomial_order[, ...])

lowess_filter(data_array)

Implemente LOWESS smoothing of input 2D data array.

savitzky_golay_filter(data_array)

Implemente Savitzky-Golay smoothing of input 2D data array.

simple_polynomial_filter(data_array)

Implemente simple polynomial smoothing of input 2D data array.

savitzky_golay_filter(data_array)[source]#

Implemente Savitzky-Golay smoothing of input 2D data array.

Parameters:
data_array1D array_like or 2D array_like

1D data array or 2D data array of 1D series data.

Returns:
numpy.ndarray

Resulting smoothed data array.

Return type:

ndarray

Examples

>>> lp = LocalPolynomial(5, polynomial_order=2)
>>> lp.savitzky_golay_filter([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> lp.savitzky_golay_filter([[1, 2, 3, 4, 5, 6, 77, 88, 9, 10], [1, 22, 33, 4, 5, 6, 7, 8, 9, 10]])

Add to prepared SpecPipe instance pipe for ROI pixel spectrum processing:

>>> pipe.add_process(6, 6, 0, LocalPolynomial(5, polynomial_order=2).savitzky_golay_filter)

Add to prepared SpecPipe instance pipe for the processing of 1D sample data:

>>> pipe.add_process(7, 7, 0, LocalPolynomial(5, polynomial_order=2).savitzky_golay_filter)
simple_polynomial_filter(data_array)[source]#

Implemente simple polynomial smoothing of input 2D data array.

Parameters:
data_array1D array_like or 2D array_like

1D data array or 2D data array of 1D series data.

Returns:
numpy.ndarray

Resulting smoothed data array.

Return type:

ndarray

Examples

>>> lp = LocalPolynomial(5, polynomial_order=2)
>>> lp.simple_polynomial_filter([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> lp.simple_polynomial_filter([[1, 2, 3, 4, 5, 6, 77, 88, 9, 10], [1, 22, 33, 4, 5, 6, 7, 8, 9, 10]])

Add to prepared SpecPipe instance pipe for ROI pixel spectrum processing:

>>> pipe.add_process(6, 6, 0, LocalPolynomial(5, polynomial_order=2).simple_polynomial_filter)

Add to prepared SpecPipe instance pipe for the processing of 1D sample data:

>>> pipe.add_process(7, 7, 0, LocalPolynomial(5, polynomial_order=2).simple_polynomial_filter)
lowess_filter(data_array)[source]#

Implemente LOWESS smoothing of input 2D data array.

Parameters:
data_array1D array_like or 2D array_like

1D data array or 2D data array of 1D series data.

Returns:
numpy.ndarray

Resulting smoothed data array.

Return type:

ndarray

Examples

>>> lp = LocalPolynomial(5, polynomial_order=2)
>>> lp.lowess_filter([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> lp.lowess_filter([[1, 2, 3, 4, 5, 6, 77, 88, 9, 10], [1, 22, 33, 4, 5, 6, 7, 8, 9, 10]])

Add to prepared SpecPipe instance pipe for ROI pixel spectrum processing:

>>> pipe.add_process(6, 6, 0, LocalPolynomial(5, polynomial_order=2).lowess_filter)

Add to prepared SpecPipe instance pipe for the processing of 1D sample data:

>>> pipe.add_process(7, 7, 0, LocalPolynomial(5, polynomial_order=2).lowess_filter)