def test_invalid_window_size(self):
        output_name = "moving_average"
        window_size = -1
        df = pd.DataFrame.from_dict({"old_name": [0, 1, 2, 3, 4, 5]})

        ma_feature = MovingAverageFeature(window_size=window_size,
                                          output_name=output_name)
        with pytest.raises(ValueError):
            ma_feature.fit_transform(df)
    def test_random_ts_and_window_size(self, df: pd.DataFrame,
                                       window_size: int):
        output_name = "time_series"

        ma_feature = MovingAverageFeature(window_size=window_size,
                                          output_name=output_name)
        df_ma = ma_feature.fit_transform(df)
        expected_df_ma = self._correct_ma(df, window_size)

        testing.assert_frame_equal(expected_df_ma, df_ma)
Пример #3
0
    def test_correct_x_shifts(self):
        horizon = 2
        ts = pd.DataFrame.from_dict({"ignored": range(10)})
        ts.index = np.random.random(len(ts))
        features = [
            MovingAverageFeature(window_size=2, output_name="mov_avg_2"),
            MovingAverageFeature(window_size=5, output_name="mov_avg_5"),
            ShiftFeature(shift=3, output_name="shift_3"),
        ]

        feature_creation = FeatureCreation(horizon=horizon,
                                           time_series_features=features)
        x_shifts = feature_creation._create_x_features(ts)
        expected_x_shifts = pd.DataFrame.from_dict({
            "mov_avg_2": [
                np.nan,
                0.5,
                1.5,
                2.5,
                3.5,
                4.5,
                5.5,
                6.5,
                7.5,
                8.5,
            ],
            "mov_avg_5": [
                np.nan,
                np.nan,
                np.nan,
                np.nan,
                2,
                3,
                4,
                5,
                6,
                7,
            ],
            "shift_3": [
                np.nan,
                np.nan,
                np.nan,
                0,
                1,
                2,
                3,
                4,
                5,
                6,
            ],
        })
        expected_x_shifts.index = ts.index

        pd.testing.assert_frame_equal(expected_x_shifts, x_shifts)
    def test_positive_window_size(self):
        output_name = "moving_average"
        window_size = 2
        df = pd.DataFrame.from_dict({"old_name": [0, 1, 2, 3, 4, 5]})

        ma_feature = MovingAverageFeature(window_size=window_size,
                                          output_name=output_name)
        df_ma = ma_feature.fit_transform(df)
        expected_df_ma = pd.DataFrame.from_dict(
            {output_name: [np.nan, 0.5, 1.5, 2.5, 3.5, 4.5]})

        testing.assert_frame_equal(expected_df_ma, df_ma)
Пример #5
0
def test_correct_fit_transform():
    n_cols = 10
    testing.N, testing.K = 500, n_cols
    df = testing.makeTimeDataFrame(freq="MS")

    output_name = "shift"
    ma_feature = MovingAverageFeature(window_size=2, output_name=output_name)

    fit_transform_res = ma_feature.fit_transform(df)

    transform_res = ma_feature.fit(df).transform(df)

    testing.assert_frame_equal(fit_transform_res, transform_res)
Пример #6
0
    def test_correct_y_shifts_random_ts(self, ts, horizon):
        features = [
            MovingAverageFeature(window_size=2, output_name="mov_avg_2"),
            MovingAverageFeature(window_size=5, output_name="mov_avg_5"),
            ShiftFeature(shift=3, output_name="shift_3"),
        ]

        feature_creation = FeatureCreation(horizon=horizon,
                                           time_series_features=features)
        x_shifts = feature_creation._create_x_features(ts)
        expected_x_shifts = self._correct_x(ts, features)

        pd.testing.assert_frame_equal(expected_x_shifts, x_shifts)
    def test_multi_columns_window_size(self):
        output_name = "moving_average"
        window_size = 2
        df = pd.DataFrame.from_dict({
            "old_name_0": [0, 1, 2, 3, 4, 5],
            "old_name_1": [7, 8, 9, 10, 11, 12]
        })

        ma_feature = MovingAverageFeature(window_size=window_size,
                                          output_name=output_name)
        df_ma = ma_feature.fit_transform(df)
        expected_df_ma = pd.DataFrame.from_dict({
            f"{output_name}_0": [np.nan, 0.5, 1.5, 2.5, 3.5, 4.5],
            f"{output_name}_1": [np.nan, 7.5, 8.5, 9.5, 10.5, 11.5],
        })

        testing.assert_frame_equal(expected_df_ma, df_ma)
Пример #8
0
import pytest

import hypothesis.strategies as st
from hypothesis import given, settings, HealthCheck

from giottotime.feature_creation import ShiftFeature, MovingAverageFeature
from giottotime.model_selection.feature_splitters import FeatureSplitter
from giottotime.utils.hypothesis.feature_matrices import X_y_matrices

FEATURES = [
    ShiftFeature(0, "0"),
    ShiftFeature(1, "1"),
    MovingAverageFeature(3, "3"),
]

HORIZON = 4


class TestFeatureSplitter:
    def test_constructor(self):
        FeatureSplitter()

    @given(st.text().filter(lambda x: x != "any"))
    def test_constructor_wrong_parameter(self, drop_na_mode: str):
        with pytest.raises(ValueError):
            FeatureSplitter(drop_na_mode)

    @settings(suppress_health_check=(HealthCheck.too_slow, ))
    @given(
        X_y_matrices(
            horizon=HORIZON,