post_treaters module

Define various smoothing functions for measured data.

CONVOLUTION_MODES_T

Available modes for the running mean routine.

alias of Literal[‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’]

DEPRECATED_CONVOLUTION_MODES = ('full', 'same', 'valid')

Deprecated modes for the running mean routine.

running_mean(input_data, n_mean, mode='nearest', **kwargs)[source]

Compute the running mean.

Deprecated since version 1.9.0: The modes listed in DEPRECATED_CONVOLUTION_MODES will call the numpy.convolve() function. Prefer the CONVOLUTION_MODES_T, which will call scipy.ndimage.uniform_filter1d().

See also

numpy.convolve()

Parameters:
  • input_data (ndarray[tuple[Any, ...], dtype[double]]) – Data to smooth of shape N.

  • n_mean (int) – Number of points on which running mean is ran.

  • mode (Literal['reflect', 'constant', 'nearest', 'mirror', 'wrap'] | Literal['full', 'same', 'valid'], default: 'nearest') – Convolution modes. Refer to scipy.ndimage.uniform_filter1d() documentation, which is the actual function that is called.

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Returns:

Smoothed data.

lower_envelope(input_data, envelope_window, mode='nearest')[source]

Compute the lower envelope, i.e. the signal minimum without the bumps.

Todo

Illustration in documentation.

Todo

May also use scipy.ndimage.percentile_filter(). May be more robust with noisy data or occasional downward spikes. To test.

Parameters:
  • input_data (ndarray[tuple[Any, ...], dtype[double]]) – Data to smooth of shape N.

  • envelope_window (int) – Number of points on which lower envelope is taken.

  • mode (Literal['reflect', 'constant', 'nearest', 'mirror', 'wrap'], default: 'nearest') – Convolution modes. Refer to scipy.ndimage.minimum_filter1d() documentation, which is the actual function that is called.

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Returns:

Smoothed data.

average_y_for_nearby_x_within_distance(y_values, x_values, tol=1e-06, max_index_distance=1, keep_shape=True)[source]

Average y_values measured at nearly identical x_values

This function groups values in y_values that correspond to x_values which are numerically close (within tol) and occur within max_index_distance of each other. These grouped y_values are averaged, and the result is returned either in a shape-preserving format or as a compact array, depending on keep_shape.

Parameters:
  • y_values (ndarray[tuple[Any, ...], dtype[double]]) – The dependent variable values to average.

  • x_values (ndarray[tuple[Any, ...], dtype[double]]) – The independent variable values used to group corresponding y_values.

  • tol (float, default: 1e-06) – Maximum absolute difference under which x_values are considered equal.

  • max_index_distance (int, default: 1) – Maximum index separation allowed when grouping similar x_values. Prevents averaging across distant, unrelated measurements.

  • keep_shape (bool, default: True) – If True, the returned array has the same shape as the input, with only the first element of each group containing the average and others filled with np.nan. If False (not recommended), returns a compact array with only the averaged values.

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Returns:

The averaged y_values, either shape-preserving or compact.

Raises:

ValueError – If x_values and y_values do not have the same shape.

Examples

>>> x = np.array([100.0, 100.0, 200.0, 200.0])
>>> y = np.array([1.0, 3.0, 10.0, 14.0])
>>> average_y_for_nearby_x_within_distance(y, x)
array([2.0, nan, 12.0, nan])
>>> average_y_for_nearby_x_within_distance(y, x, keep_shape=False)
array([2.0, 12.0])
drop_x_where_y_is_nan(x_values, y_values)[source]

Return x_values without indexes where y_values is np.nan.

This can be used in for RPA when some current data is dropped (keep_shape = False) but we still want the same shape for potential.

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[double]]

replace_data_under_threshold(input_data, threshold, replace_value, min_consecutive=1)[source]

Replace data where min_consecutive values are below threshold.

Data is replaced by replace_value.

Parameters:
  • input_data (ndarray[tuple[Any, ...], dtype[double]]) – Data to filter.

  • threshold (float) – Threshold under which data is considered noise.

  • replace_value (float) – Value to replace the data with.

  • min_consecutive (int, default: 1) – Minimum number of consecutive values below the threshold required for replacement.

Return type:

ndarray[tuple[Any, ...], dtype[double]]

Returns:

Modified data array.

return_constant(input_data, constant)[source]

Always return same value.

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[double]]

get_data_above_noise(data, noise_level=None, level=None)[source]

Detect signal above noise, return it.

Parameters:
  • data (ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]) – Array of data in pulsed shape, such as power pulse or synch.

  • noise_level (float | None, default: None) – Absolute level of noise. If not provided, we use level to compute it.

  • level (float | None, default: None) – Noise percentile wrt data, in \(\mathrm{%}\).

Returns:

Only the widest pulse detected in the signal.

Return type:

ndarray[tuple[Any, ...], dtype[TypeVar(_ScalarT, bound= generic)]]