pyacs.gts.lib.l1trend.optimal_refinement module

Optimal piecewise linear function refinement algorithms.

pyacs.gts.lib.l1trend.optimal_refinement.optimal_pwlf_refinement(t, y, y0, yn, nbp, weights=None)[source]

Find the optimal piecewise linear function with nbp breakpoints fitting y.

The function finds the optimal piecewise linear function (opwlf) fitting y with nbp breakpoints according to the weighted L2 norm. The obtained optimal piecewise linear function verifies opwlf[0]=y0 and opwlf[-1]=yn, that is end and starting point of y are kept fixed.

The problem is applied to small dimension (n<=6), so that a systematic search can be performed and does not require complex optimization approaches.

For large time series (>100 points), preprocessing is applied to decimate the data while preserving essential structure.

Parameters:
  • t (numpy.ndarray) – Time array expressed in integer seconds

  • y (numpy.ndarray) – Values of the function to fit

  • y0 (float) – Value of the function at t[0] (fixed)

  • yn (float) – Value of the function at t[-1] (fixed)

  • nbp (int) – Number of breakpoints (excluding start and end points)

  • weights (numpy.ndarray, optional) – Weights for each data point. If None, all weights are set to 1.0.

Returns:

(optimal_breakpoints, optimal_values, min_error) - optimal_breakpoints: array of breakpoint times - optimal_values: array of function values at breakpoints - min_error: minimum weighted L2 error achieved

Return type:

tuple

pyacs.gts.lib.l1trend.optimal_refinement.optimal_pwlf_refinement_fast(t, y, y0, yn, weights=None)[source]

Fast version of optimal_pwlf_refinement using l1trendi with criterion AICc.

Parameters:
  • t (numpy.ndarray) – Time array expressed in integer seconds

  • y (numpy.ndarray) – Values of the function to fit

  • y0 (float) – Value of the function at t[0] (fixed)

  • yn (float) – Value of the function at t[-1] (fixed)

  • weights (numpy.ndarray, optional) – Weights for each data point. If None, all weights are set to 1.0.

Returns:

(optimal_breakpoints, optimal_values, min_error)

Return type:

tuple

pyacs.gts.lib.l1trend.optimal_refinement.preprocess_timeseries_for_optimization(t, y, target_length=50)[source]

Preprocess time series for optimization by applying median filter and decimation.

This function reduces the number of points while preserving the essential structure of the time series, making optimization faster.

Parameters:
  • t (numpy.ndarray) – Time array

  • y (numpy.ndarray) – Values array

  • target_length (int) – Target length for the decimated series (default: 50)

Returns:

(t_decimated, y_decimated, decimation_factor) - t_decimated: decimated time array - y_decimated: decimated values array - decimation_factor: factor used for decimation

Return type:

tuple