primap2.csg.LocalTrendsStrategy#
- class primap2.csg.LocalTrendsStrategy(fit_params: FitParameters = NOTHING, allow_negative: bool = NOTHING)[source]#
Fill missing data using local trends or single overlap values.
The NaNs in the first timeseries \(\textrm{ts}(t)\) are filled using harmonized data from the lower priority timeseries \(\textrm{fill_ts}(t)\).
For gaps in the data and missing data at the boundaries of time series different treatments are used.
For boundaries the strategy uses
\[\textrm{fill_ts}_h(t) = \textrm{fill_ts}(t) \times a,\]where \(\textrm{fill_ts}_h(t)\) is the harmonized dataset and \(a\) is determined by
\[a = \textrm{fill_ts}_t(t_b) / \textrm{ts}_t(t_b),\]where \(\textrm{fill_ts}_t(t_b)\) is the trend value calculated for \(\textrm{fill_ts}(t_b)\) and equally for \(\textrm{ts}_t(t_b)\). \(t_b\) is the last (in case of a right boundary) or first (in case of a left boundary) non-NaN data point in \(\textrm{ts}\). The trend value is calculated using a linear trend of length trend_length or less data points if a time-series does not cover the full period. By setting min_trend_points a minimal number of points necessary for the trend calculation can be set. If less points are available a
StrategyUnableToProcesserror will be raised. This enables the user to define a fallback strategy, e.g. single point matching.For the case of gaps this leads to the situation that we can’t use trends on one side of the gap and single year matching as fallback on the other. Left and right scaling factors are always calculated using the same method.
By setting trend_length to 1 single year matching is used.
For gaps the left (\(t_{bl}\)) and right (\(t_{br}\)) end have to be considered. The data is harmonized using
\[\textrm{fill_ts}_h(t) = \textrm{fill_ts}(t) \times \frac{a_l(t_{br}-t)+a_r(t-t_{bl})}{t_{br}-t_{bl}},\]where
\[a_l = \textrm{fill_ts}_t(t_{bl}) / \textrm{ts}_t(t_{bl}),\]and
\[a_r = \textrm{fill_ts}_t(t_b) / \textrm{ts}_t(t_b).\]If only one of the ends of the gap has an overlap with \(\textrm{fill_ts}(t)\), we use the harmonization factor of this side for the whole gap (we treat the gap like a boundary).
If there is no overlap in non-NaN data between \(\textrm{ts}(t)\) and \(\textrm{fill_ts}(t)\) a
StrategyUnableToProcesserror will be raised.If
allow_negative = Falseand the harmonized time-series \(\textrm{fill_ts}_h(t)\) contains negative data aStrategyUnableToProcesserror will be raised.Filling multiple gaps and boundaries with this function is scientifically questionable as they will all use different scaling factors and thus don’t use a consistent model to harmonize one time-series \(\textrm{fill_ts}(t)\) to \(\textrm{ts}(t)\). Use with care.
- Attributes:
- fit_params
Instance of the FitParameters class defining the parameters for the fits on the boundaries of the time-series. The default values are
trend_length=10, # ten years if default unit for trend length is used) min_trend_points=5, # minimal data points necessary for trend calculation trend_length_unit=”YS”, # year start datapoint fit_degree=1, # linear trend by default fallback_degree=0, # constant
- allow_negative
Allow the filling time series to contain negative data initially.
Methods
fill(*, ts, fill_ts, fill_ts_repr)Fill missing data using matching of local trends on the boundaries.
- __init__(fit_params: FitParameters = NOTHING, allow_negative: bool = NOTHING) None#
Method generated by attrs for class LocalTrendsStrategy.
Methods
__init__([fit_params, allow_negative])Method generated by attrs for class LocalTrendsStrategy.
fill(*, ts, fill_ts, fill_ts_repr)Fill missing data using matching of local trends on the boundaries.
Attributes
fit_paramsallow_negativetype