swectral.denoiser.MovingAvg#

class swectral.denoiser.MovingAvg(window_size, axis=1, mode='center', padding='extrapolation', numtype='float32', outlier_replacer=None, *, window_weights=None, gaussian_sigma=-1.0, axis_double_definition_warning=True)[source]#

Apply moving average data smoothing to 2-dimensional array of 1-dimensional data series. The averaging calculation is applied to the rolling window.

Attributes:
window_sizeint

Size of rolling window.

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.

The default is “center”.

paddingstr, optional

Padding approach for the output. Available options:

  • “none” - No padding applied, the resulting data series will be window_size - 1 smaller than the original data series.

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

  • “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 “extrapolation”.

numtypestr or type, optional

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

outlier_replacerArrayOutlier instance or None, optional

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

window_weights1D array_like, optional

Weights array for weighted average. The default is None.

gaussian_sigmafloat, optional

Sigma for Gaussian kernel. By default it is calculated as (window size - 1) / 6.

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

simple_moving_average(data_array)

Implemente simple moving average of input 2d data array.

moving_median(data_array)

Implemente moving median of input 2d data array.

weighted_moving_average(data_array)

Implemente weighted moving average of input 2d data array.

gaussian_filter(data_array)

Implemente Gaussian smoothing of input 2d data array.

Examples

Basic usage with window_size:

>>> ma = MovingAvg(3)

Use different window_size:

>>> ma = MovingAvg(4)

Pad with numpy.nan:

>>> ma = MovingAvg(3, padding='nan')

Specify rolling mode:

>>> ma = MovingAvg(3, mode='knn')

Compute along a different axis:

>>> ma = MovingAvg(3, axis=0)
__init__(window_size, axis=1, mode='center', padding='extrapolation', numtype='float32', outlier_replacer=None, *, window_weights=None, gaussian_sigma=-1.0, axis_double_definition_warning=True)[source]#

Methods

__init__(window_size[, axis, mode, padding, ...])

gaussian_filter(data_array)

Implemente Gaussian smoothing of input 2d data array.

moving_median(data_array)

Implemente moving median of input 2d data array.

simple_moving_average(data_array)

Implemente simple moving average of input 2d data array.

weighted_moving_average(data_array)

Implemente weighted moving average of input 2d data array.

simple_moving_average(data_array)[source]#

Implemente simple moving average of input 2d data array.

Parameters:
data_arraynumpy.ndarray

input data series in 2d data array.

Returns:
numpy.ndarray

Smoothing result.

Return type:

ndarray

Examples

>>> ma = MovingAvg(5)
>>> ma.simple_moving_average([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> ma.simple_moving_average([[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, MovingAvg(5).simple_moving_average)

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

>>> pipe.add_process(7, 7, 0, MovingAvg(5).simple_moving_average)
moving_median(data_array)[source]#

Implemente moving median of input 2d data array.

Parameters:
data_arraynumpy.ndarray.

input data series in 2d data array.

Returns:
numpy.ndarray

Smoothing result.

Return type:

ndarray

Examples

>>> ma = MovingAvg(5)
>>> ma.moving_median([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> ma.moving_median([[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, MovingAvg(5).moving_median)

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

>>> pipe.add_process(7, 7, 0, MovingAvg(5).moving_median)
weighted_moving_average(data_array)[source]#

Implemente weighted moving average 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

Smoothing result.

Return type:

ndarray

Examples

>>> ma = MovingAvg(5)
>>> ma.weighted_moving_average([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> ma.weighted_moving_average([[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, MovingAvg(5).weighted_moving_average)

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

>>> pipe.add_process(7, 7, 0, MovingAvg(5).weighted_moving_average)
gaussian_filter(data_array)[source]#

Implemente Gaussian 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

Smoothing result.

Return type:

ndarray

Examples

>>> ma = MovingAvg(5)
>>> ma.gaussian_filter([1, 2, 3, 4, 5, 6, 77, 88, 9, 10])
>>> ma.gaussian_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, MovingAvg(5).gaussian_filter)

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

>>> pipe.add_process(7, 7, 0, MovingAvg(5).gaussian_filter)