Пример #1
0
    def test_fill_constant(self):
        seriesA: TimeSeries = TimeSeries.from_times_and_values(
            self.time,
            np.array([np.nan] * 5 + [2.0] * 5 + [np.nan] * 5 + [2.0] * 10 +
                     [np.nan] * 5),
        )

        # Check that no changes are made if there are no missing values
        self.assertEqual(self.series1,
                         fill_missing_values(self.series1, "auto"))

        # Check that a constant function is filled to a constant function
        self.assertEqual(self.series1, fill_missing_values(seriesA, "auto"))
Пример #2
0
    def test_linear(self):
        seriesB: TimeSeries = TimeSeries.from_times_and_values(
            self.time,
            np.array(self.lin[:10] + [np.nan] * 10 + self.lin[-10:]))

        # Check for linear interpolation part
        self.assertEqual(self.series2, fill_missing_values(seriesB, "auto"))
Пример #3
0
 def test_fill_quad(self):
     seriesE: TimeSeries = TimeSeries.from_times_and_values(
         self.time,
         np.array(self.cub[:10] + [np.nan] * 10 + self.cub[-10:]))
     self.assertEqual(
         self.series5,
         round(fill_missing_values(seriesE, "auto", method="quadratic"), 7),
     )
Пример #4
0
    def fit(self, series: TimeSeries):
        series = fill_missing_values(series)
        super().fit(series)
        series = self.training_series

        # determine trend
        if self.trend == "poly":
            trend_coefficients = np.polyfit(range(len(series)),
                                            series.univariate_values(),
                                            self.trend_poly_degree)
            self.trend_function = np.poly1d(trend_coefficients)
        elif self.trend == "exp":
            trend_coefficients = np.polyfit(range(len(series)),
                                            np.log(series.univariate_values()),
                                            1)
            self.trend_function = lambda x: np.exp(trend_coefficients[
                1]) * np.exp(trend_coefficients[0] * x)
        else:
            self.trend_function = lambda x: 0

        # subtract trend
        detrended_values = series.univariate_values() - self.trend_function(
            range(len(series)))
        detrended_series = TimeSeries.from_times_and_values(
            series.time_index, detrended_values)

        # crop training set to match the seasonality of the first prediction point
        if self.required_matches is None:
            curr_required_matches = _find_relevant_timestamp_attributes(
                detrended_series)
        else:
            curr_required_matches = self.required_matches
        cropped_series = _crop_to_match_seasons(
            detrended_series, required_matches=curr_required_matches)

        # perform dft
        self.fft_values = np.fft.fft(cropped_series.univariate_values())

        # get indices of `nr_freqs_to_keep` (if a correct value was provied) frequencies with the highest amplitudes
        # by partitioning around the element with sorted index -nr_freqs_to_keep instead of sorting the whole array
        first_n = self.nr_freqs_to_keep
        if first_n is None or first_n < 1 or first_n > len(self.fft_values):
            first_n = len(self.fft_values)
        self.filtered_indices = np.argpartition(abs(self.fft_values),
                                                -first_n)[-first_n:]

        # set all other values in the frequency domain to 0
        self.fft_values_filtered = np.zeros(len(self.fft_values),
                                            dtype=np.complex_)
        self.fft_values_filtered[self.filtered_indices] = self.fft_values[
            self.filtered_indices]

        # precompute all possible predicted values using inverse dft
        self.predicted_values = np.fft.ifft(self.fft_values_filtered).real

        return self
Пример #5
0
 def test_multivariate_fill(self):
     seriesA: TimeSeries = TimeSeries.from_times_and_values(
         self.time,
         np.array([np.nan] * 5 + [2.0] * 5 + [np.nan] * 5 + [2.0] * 10 +
                  [np.nan] * 5),
     )
     seriesB: TimeSeries = TimeSeries.from_times_and_values(
         self.time,
         np.array(self.lin[:10] + [np.nan] * 10 + self.lin[-10:]))
     self.assertEqual(
         self.series1.stack(self.series2),
         fill_missing_values(seriesA.stack(seriesB), "auto"),
     )
Пример #6
0
 def ts_transform(series: TimeSeries, fill: Union[str, float],
                  **kwargs) -> TimeSeries:
     return fill_missing_values(series, fill, **kwargs)
Пример #7
0
    def test_ffil(self):
        seriesD: TimeSeries = TimeSeries.from_times_and_values(
            self.time, np.array(self.lin[:20] + [np.nan] * 10))

        self.assertEqual(self.series4, fill_missing_values(seriesD, "auto"))
Пример #8
0
    def test_bfill(self):
        seriesC: TimeSeries = TimeSeries.from_times_and_values(
            self.time, np.array([np.nan] * 10 + self.lin[-20:]))

        # Check that auto-backfill works properly
        self.assertEqual(self.series3, fill_missing_values(seriesC, "auto"))
Пример #9
0
 def transform(self, data: TimeSeries, **interpolate_kwargs) -> TimeSeries:
     super().transform(data)
     return fill_missing_values(data, self._fill, **interpolate_kwargs)