class TestHorizonShift:
    @given(
        giotto_time_series(min_length=10,
                           allow_infinity=False,
                           allow_nan=False),
        st.integers(1, 8),
    )
    def test_horizon_int(self, time_series, horizon):
        y_shifted = horizon_shift(time_series, horizon)
        assert y_shifted.shape[1] == horizon

        # Check first line of y_shifted
        for i in range(1, horizon + 1):
            assert time_series.iloc[i, 0] == y_shifted.iloc[0, i - 1]

    @given(
        giotto_time_series(min_length=10,
                           allow_infinity=False,
                           allow_nan=False),
        st.sets(elements=st.integers(1, 8), min_size=1, max_size=8),
    )
    def test_horizon_list(self, time_series, horizon):
        horizon = list(sorted(horizon))
        y_shifted = horizon_shift(time_series, horizon)
        assert y_shifted.shape[1] == len(horizon)

        # Check first line of y_shifted
        for i, elem in enumerate(horizon):
            assert time_series.iloc[elem, 0] == y_shifted.iloc[0, i]
Exemple #2
0
class TestAcf:
    @given(x=st.lists(st.floats(allow_nan=False), min_size=1))
    def test_autocorrelation(self, x):
        autocorr = _autocorrelation(np.array(x))
        expected = np.correlate(x, x, mode="full")[-len(x) :] / len(x)
        np.testing.assert_array_equal(autocorr, expected)

    @given(
        x=st.lists(
            st.floats(
                allow_nan=False, allow_infinity=False, max_value=1e20, min_value=1e20
            ),
            min_size=1,
        )
    )
    def test_scale(self, x):
        scaled_x = _normalize(np.array(x))
        assert scaled_x.mean() == pytest.approx(0.0)
        assert scaled_x.std() == pytest.approx(1.0) or scaled_x.std() == pytest.approx(
            0.0
        )

    @given(x=st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=2))
    def test_solve_yw(self, x):
        rho = _solve_yw_equation(np.array(x))
        if not np.isnan(np.sum(rho)):
            assert len(rho) == len(x) - 1

    @given(
        x=st.lists(st.floats(allow_nan=False, allow_infinity=False), min_size=2),
        order=st.integers(min_value=1),
    )
    def test_yule_walker_abs(self, x, order):
        pacf = yule_walker(np.array(x), order)
        if not (np.isnan(np.sum(pacf)) or len(pacf) == 0):
            assert all(abs(pacf) <= 1)

    @given(
        df=giotto_time_series(min_length=1, allow_nan=False, allow_infinity=False),
        max_lag=st.one_of(st.integers(min_value=1, max_value=100), st.none()),
    )
    def test_acf_len(self, df, max_lag):
        df_array = np.ravel(df.values)
        res = acf(df_array, max_lag)
        if max_lag is None:
            max_lag = len(df)
        assert len(res) == min(max_lag, len(df))

    @given(
        df=giotto_time_series(
            min_length=1, allow_nan=False, allow_infinity=False, max_length=50
        ),
        max_lag=st.one_of(st.integers(min_value=1, max_value=100), st.none()),
    )
    def test_pacf_len(self, df, max_lag):
        df_array = np.ravel(df.values)
        res = pacf(df_array, max_lag)
        if max_lag is None:
            max_lag = len(df)
        assert len(res) == min(max_lag, len(df))
Exemple #3
0
class TestSplits:
    @given(t=period_indexes(min_length=1, max_length=1))
    @example(t=pd.PeriodIndex(["1974-12-31"], freq="W"))
    @example(t=pd.PeriodIndex(["1972-01-01"], freq="W"))
    @settings(deadline=None)
    def test_week_of_year(self, t):
        period = t[0]
        week = _week_of_year(period)
        assert re.match(r"\d{4}_\d\d?$", week)

    @given(
        df=giotto_time_series(min_length=3, max_length=500),
        cycle=st.one_of(
            st.sampled_from(["year", "quarter", "month", "week"]),
            st.from_regex(r"[1-9][DWMQY]", fullmatch=True),
        ),
    )
    @settings(deadline=None)
    def test__get_cycle_names_size(self, df, cycle):
        cycle = _get_cycle_names(df, cycle)
        assert len(cycle) == len(df)

    @given(
        df=giotto_time_series(min_length=3, max_length=500),
        cycle=st.one_of(
            st.sampled_from(["year", "quarter", "month", "week"]),
            st.from_regex(r"[1-9][DWMQY]", fullmatch=True),
        ),
        freq=st.from_regex(r"[1-9]?[DWMQ]", fullmatch=True),
    )
    @settings(deadline=None)
    def test__get_season_names_size(self, df, cycle, freq):
        seasons = _get_season_names(df, cycle, freq)
        assert len(seasons) == len(df)

    @given(
        df=giotto_time_series(min_length=3, max_length=500),
        cycle=st.one_of(
            st.sampled_from(["year", "quarter", "month", "week"]),
            st.from_regex(r"[1-9][DWMQY]", fullmatch=True),
        ),
        freq=st.one_of(st.from_regex(r"[1-9]?[DWMQ]", fullmatch=True), st.none()),
        agg=st.sampled_from(["mean", "sum", "last"]),
    )
    @settings(deadline=None)
    def test_seasonal_split_shape_named(self, df, cycle, freq, agg):
        split = seasonal_split(df, cycle=cycle, freq=freq, agg=agg)
        if freq is None:
            freq = df.index.freqstr
        assert split.stack().shape == df.resample(freq).agg(agg).dropna().shape
Exemple #4
0
class TestShift:
    def _correct_shift(self, df: pd.DataFrame, shift: int) -> pd.DataFrame:
        return df.shift(shift)

    @pytest.mark.parametrize(
        ("shift", "expected"), [(1, df_shift_1), (-2, df_shift_m2), (0, df_shift_0)]
    )
    def test_shift_transform(self, shift, expected):
        shift = Shift(shift=shift)
        testing.assert_frame_equal(shift.fit_transform(df), expected)

    def test_multi_columns_time_shift_feature(self):
        shift = Shift(shift=-2)
        df_multi = pd.DataFrame({"x0": [0, 1, 2, 3, 4, 5], "x1": [7, 8, 9, 10, 11, 12]})

        expected_df = pd.DataFrame.from_dict(
            {
                f"x0__{shift_class_name}": [2, 3, 4, 5, np.nan, np.nan],
                f"x1__{shift_class_name}": [9, 10, 11, 12, np.nan, np.nan],
            }
        )

        testing.assert_frame_equal(shift.fit_transform(df_multi), expected_df)

    @given(
        giotto_time_series(
            start_date=pd.Timestamp(2000, 1, 1), end_date=pd.Timestamp(2010, 1, 1)
        ),
        st.integers(0, 200),
    )
    def test_random_ts_and_shifts(self, df: pd.DataFrame, shift: int):
        shift_feature = Shift(shift=shift)

        df_shifted = shift_feature.fit_transform(df)
        correct_df_shifted = self._correct_shift(df, shift)
class TestConstantFeature:
    def test_correct_constant_feature(self):
        constant = 12
        df = pd.DataFrame.from_dict({"old_name": [0, 1, 2, 3, 4, 5]})

        constant_feature = Constant(constant=constant)

        df_constant = constant_feature.fit_transform(df)
        expected_df_constant = pd.DataFrame.from_dict({
            f"0__{constant_feature.__class__.__name__}": [
                constant,
                constant,
                constant,
                constant,
                constant,
                constant,
            ]
        })

        testing.assert_frame_equal(expected_df_constant,
                                   df_constant,
                                   check_dtype=False)

    @given(
        giotto_time_series(
            min_length=1,
            start_date=pd.Timestamp(2000, 1, 1),
            end_date=pd.Timestamp(2010, 1, 1),
        ),
        st.integers(0, 100),
    )
    def test_random_ts_and_constant(self, df: pd.DataFrame, constant: int):

        constant_feature = Constant(constant=constant)
        df_constant = constant_feature.fit_transform(df)
Exemple #6
0
class TestMovingAverage:
    def _correct_ma(self, df: pd.DataFrame, window_size: int) -> pd.DataFrame:
        return (df.rolling(window_size).mean().add_suffix(
            "__" + MovingAverage().__class__.__name__))

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

        ma_feature = MovingAverage(window_size=window_size)

        with pytest.raises(ValueError):
            ma_feature.fit_transform(df)

    def test_positive_window_size(self):
        window_size = 2
        df = pd.DataFrame.from_dict({"x": [0, 1, 2, 3, 4, 5]})

        ma_feature = MovingAverage(window_size=window_size)
        df_ma = ma_feature.fit_transform(df)
        output_name = "x__" + ma_feature.__class__.__name__
        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, check_names=False)

    def test_multi_columns_window_size(self):
        window_size = 2
        df = pd.DataFrame.from_dict({
            "x0": [0, 1, 2, 3, 4, 5],
            "x1": [7, 8, 9, 10, 11, 12]
        })

        ma_feature = MovingAverage(window_size=window_size)
        feature_name = ma_feature.__class__.__name__

        df_ma = ma_feature.fit_transform(df)
        expected_df_ma = pd.DataFrame({
            f"x0__{feature_name}": [np.nan, 0.5, 1.5, 2.5, 3.5, 4.5],
            f"x1__{feature_name}": [np.nan, 7.5, 8.5, 9.5, 10.5, 11.5],
        })

        testing.assert_frame_equal(expected_df_ma, df_ma, check_names=False)

    @given(
        giotto_time_series(start_date=pd.Timestamp(2000, 1, 1),
                           end_date=pd.Timestamp(2010, 1, 1)),
        st.integers(0, 100),
    )
    def test_random_ts_and_window_size(self, df: pd.DataFrame,
                                       window_size: int):
        ma_feature = MovingAverage(window_size=window_size)
        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)
Exemple #7
0
class TestAR:
    @pytest.mark.parametrize("explainer_type", [None, "lime", "shap"])
    @given(
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_constructor(self, p, horizon, explainer_type):
        ar = AR(p, horizon, explainer_type=explainer_type)
        assert len(ar.features) == p
        assert ar.horizon == horizon
        assert ar.explainer_type == explainer_type
        assert ar.model.explainer_type == explainer_type

    @given(
        time_series=giotto_time_series(allow_nan=False,
                                       allow_infinity=False,
                                       min_length=7,
                                       max_length=200),
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_features_are_correct(self, time_series, p, horizon):
        ar = AR(p, horizon)
        for i, feature in enumerate(ar.features):
            assert feature[0] == f"s{i}"
            assert isinstance(feature[1], Shift)
            assert feature[1].shift == i

    @given(
        time_series=giotto_time_series(allow_nan=False,
                                       allow_infinity=False,
                                       min_length=7,
                                       max_length=200),
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_results(self, time_series, p, horizon):
        ar = AR(p, horizon)
        predictions = ar.fit(time_series).predict()
        assert predictions.shape[0] == horizon
        assert predictions.shape[1] == horizon
def forecast_input(draw, max_lenth):
    length = draw(st.integers(min_value=2, max_value=max_lenth))
    horizon = draw(st.integers(min_value=1, max_value=length - 1))
    X = draw(
        giotto_time_series(
            min_length=length,
            max_length=max_lenth,
            allow_nan=False,
            allow_infinity=False,
        ))
    y = horizon_shift(X, horizon=horizon)
    X_train, y_train, X_test, y_test = FeatureSplitter().transform(X, y)
    return X_train, y_train, X_test
def forecast_input(draw, max_lenth):
    length = draw(st.integers(min_value=4, max_value=max_lenth))
    horizon = draw(st.integers(min_value=1, max_value=length - 1))
    window = draw(st.integers(min_value=1, max_value=length - horizon))
    df = draw(
        giotto_time_series(
            min_length=horizon + window,
            max_length=max_lenth,
            allow_nan=False,
            allow_infinity=False,
        )
    )
    return df, horizon, window
Exemple #10
0
class TestAR:
    @given(
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_constructor(self, p, horizon):
        ar = AR(p, horizon)
        assert len(ar.features) == p
        assert ar.horizon == horizon

    @given(
        time_series=giotto_time_series(allow_nan=False,
                                       allow_infinity=False,
                                       min_length=7,
                                       max_length=200),
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_features_are_correct(self, time_series, p, horizon):
        ar = AR(p, horizon)
        for i, feature in enumerate(ar.features):
            assert feature[0] == f"s{i}"
            assert isinstance(feature[1], Shift)
            assert feature[1].shift == i

    @given(
        time_series=giotto_time_series(allow_nan=False,
                                       allow_infinity=False,
                                       min_length=7,
                                       max_length=200),
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_results(self, time_series, p, horizon):
        ar = AR(p, horizon)
        predictions = ar.fit(time_series).predict()
        assert predictions.shape[0] == horizon
        assert predictions.shape[1] == horizon
Exemple #11
0
class TestExogenous:
    @given(giotto_time_series(min_length=2))
    def test_exogenous_single_column(self, time_series: pd.DataFrame):
        exogenous = Exogenous()
        transformed_time_series = exogenous.fit_transform(time_series)
        transformed_time_series.columns = ["time_series"]
        assert_frame_equal(transformed_time_series, time_series, check_names=False)

    @given(data_frames([column("A", dtype=int), column("B", dtype=float)]))
    def test_multiple_columns(self, time_series: pd.DataFrame):
        exogenous = Exogenous()
        transformed_time_series = exogenous.fit_transform(time_series)
        transformed_time_series.columns = ["A", "B"]
        assert_frame_equal(transformed_time_series, time_series, check_names=False)

    @given(giotto_time_series(min_length=2))
    def test_naming(self, time_series: pd.DataFrame):
        exogenous = Exogenous()
        transformed_time_series = exogenous.fit_transform(time_series)
        expected_columns = [
            f"{column_name}__Exogenous" for column_name in time_series.columns
        ]
        assert expected_columns == list(transformed_time_series.columns)
Exemple #12
0
class TestAR:
    @given(
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_constructor(self, p, horizon):
        ar = AR(p, horizon)
        assert len(ar.features) == p
        assert ar.horizon == horizon

    @given(
        time_series=giotto_time_series(allow_nan=False,
                                       allow_infinity=False,
                                       min_length=7,
                                       max_length=200),
        p=st.integers(min_value=1, max_value=5),
        horizon=st.integers(min_value=1, max_value=3),
    )
    def test_results(self, time_series, p, horizon):
        ar = AR(p, horizon)
        predictions = ar.fit(time_series).predict()
        assert predictions.shape[0] == horizon
        assert predictions.shape[1] == horizon
Exemple #13
0
class TestHierarchicalBottomUp:
    def test_basic_constructor(self, time_series_forecasting_model1_no_cache):
        HierarchicalBottomUp(model=time_series_forecasting_model1_no_cache,
                             hierarchy_tree='infer')

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_bottom_up_on_different_data(
            self, dataframes, hierarchical_basic_bottom_up_model):
        hierarchical_basic_bottom_up_model.fit(dataframes).predict(dataframes)

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_bottom_up(self, dataframes,
                                         hierarchical_basic_bottom_up_model):
        hierarchical_basic_bottom_up_model.fit(dataframes).predict()

    @given(dataframes=n_time_series_with_same_index())
    def test_constructor(self, time_series_forecasting_model1_no_cache,
                         dataframes):
        tree = tree_construction(dataframes)
        HierarchicalBottomUp(time_series_forecasting_model1_no_cache, tree)

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_bottom_up(self, data, dataframes,
                                   time_series_forecasting_model1_no_cache):
        model = data.draw(
            hierarchical_bottom_up_model(
                time_series_forecasting_model1_no_cache))
        prediction = model.fit(dataframes).predict()
        for key in dataframes.keys():
            if key not in prediction.keys():
                raise ValueError

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_on_subset_of_time_series(
            self, dataframes, hierarchical_basic_bottom_up_model):
        key = np.random.choice(list(dataframes.keys()), 1)[0]
        hierarchical_basic_bottom_up_model.fit(dataframes)
        hierarchical_basic_bottom_up_model.predict({key: dataframes[key]})

    def test_error_predict_not_fitted(self,
                                      hierarchical_basic_bottom_up_model):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            hierarchical_basic_bottom_up_model.predict()

    @given(dataframes=n_time_series_with_same_index())
    def test_error_with_bad_predict_key(self, dataframes,
                                        hierarchical_basic_bottom_up_model):
        correct_key = np.random.choice(list(dataframes.keys()), 1)[0]
        bad_key = "".join(dataframes.keys()) + "bad_key"
        hierarchical_basic_bottom_up_model.fit(dataframes)
        with pytest.raises(KeyError):
            hierarchical_basic_bottom_up_model.predict(
                {bad_key: dataframes[correct_key]})

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_dataframe(self, time_series,
                                 hierarchical_basic_bottom_up_model):
        with pytest.raises(ValueError):
            hierarchical_basic_bottom_up_model.fit(time_series)

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_key_not_string(self, time_series,
                                      hierarchical_basic_bottom_up_model):
        with pytest.raises(ValueError):
            hierarchical_basic_bottom_up_model.fit({1: time_series})

    def test_error_fit_value_not_dataframe(self,
                                           hierarchical_basic_bottom_up_model):
        with pytest.raises(ValueError):
            hierarchical_basic_bottom_up_model.fit({"wrong_field": 12})
Exemple #14
0

def test_unevenly_spaced_time_series():
    unevenly_spaced_ts = pd.DataFrame(index=[
        pd.Period("2012-01-01"),
        pd.Period("2012-01-03"),
        pd.Period("2012-01-10"),
    ])
    cal_feature = Calendar(
        start_date="ignored",
        end_date="ignored",
        country="Brazil",
        kernel=np.array([0, 1]),
    )

    with pytest.raises(ValueError):
        cal_feature.fit_transform(unevenly_spaced_ts)


@settings(deadline=pd.Timedelta(milliseconds=5000), max_examples=7)
@given(giotto_time_series(min_length=2, max_length=30))
def test_correct_index_random_ts(ts):
    cal_feature = Calendar(
        start_date="ignored",
        end_date="ignored",
        country="Brazil",
        kernel=np.array([1, 2]),
    )
    Xt = cal_feature.fit_transform(ts)
    np.testing.assert_array_equal(Xt.index, ts.index)
Exemple #15
0
class TestHierarchicalNaive:
    def test_constructor(self, time_series_forecasting_model1_no_cache):
        HierarchicalNaive(model=time_series_forecasting_model1_no_cache)

    def test_constructor_no_hierarchy_tree(
            self, time_series_forecasting_model1_no_cache):
        hierarchy_tree = {}
        with pytest.raises(TypeError):
            HierarchicalNaive(
                model=time_series_forecasting_model1_no_cache,
                hierarchy_tree=hierarchy_tree,
            )

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_dataframe(self, time_series, hierarchical_naive_model):
        with pytest.raises(ValueError):
            hierarchical_naive_model.fit(time_series)

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_key_not_string(self, time_series,
                                      hierarchical_naive_model):
        with pytest.raises(ValueError):
            hierarchical_naive_model.fit({1: time_series})

    def test_error_fit_value_not_dataframe(self, hierarchical_naive_model):
        with pytest.raises(ValueError):
            hierarchical_naive_model.fit({"wrong_field": 12})

    @given(dataframes=n_time_series_with_same_index())
    def test_fit_n_dataframes(self, dataframes, hierarchical_naive_model):
        hierarchical_naive_model.fit(dataframes)

    @given(dataframes=n_time_series_with_same_index())
    def test_fit_predict_n_dataframes_on_different_data(
            self, dataframes, hierarchical_naive_model):
        hierarchical_naive_model.fit(dataframes).predict(dataframes)

    @given(dataframes=n_time_series_with_same_index())
    def test_fit_predict_n_dataframes(self, dataframes,
                                      hierarchical_naive_model):
        hierarchical_naive_model.fit(dataframes).predict()

    @given(dataframes=n_time_series_with_same_index())
    def test_fit_predict_on_subset_of_time_series(self, dataframes,
                                                  hierarchical_naive_model):
        key = np.random.choice(list(dataframes.keys()), 1)[0]
        hierarchical_naive_model.fit(dataframes)
        hierarchical_naive_model.predict({key: dataframes[key]})

    def test_error_predict_not_fitted(self, hierarchical_naive_model):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            hierarchical_naive_model.predict()

    @given(dataframes=n_time_series_with_same_index())
    def test_error_with_bad_predict_key(self, dataframes,
                                        hierarchical_naive_model):
        correct_key = np.random.choice(list(dataframes.keys()), 1)[0]
        bad_key = "".join(dataframes.keys()) + "bad_key"
        hierarchical_naive_model.fit(dataframes)
        with pytest.raises(KeyError):
            hierarchical_naive_model.predict(
                {bad_key: dataframes[correct_key]})
Exemple #16
0
class TestHierarchicalMiddleOut:
    def test_basic_constructor(self, time_series_forecasting_model1_no_cache):
        HierarchicalMiddleOut(model=time_series_forecasting_model1_no_cache, hierarchy_tree='infer')

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out_on_different_data(self, dataframes, hierarchical_basic_middle_out_model):
        hierarchical_basic_middle_out_model.fit(dataframes).predict(dataframes)

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out_fp_method(self, dataframes, hierarchical_basic_middle_out_model_fp_method):
        hierarchical_basic_middle_out_model_fp_method.fit(dataframes).predict(dataframes)

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out(self, dataframes, hierarchical_basic_middle_out_model):
        hierarchical_basic_middle_out_model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_sga(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_sga_level0(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sgf(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_sgf(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga_tree_by_hands(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_sga_tree_by_hand(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sgf_tree_by_hands(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_sgf_tree_by_hand(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_fp_tree_by_hands(self, data, dataframes, time_series_forecasting_model1_no_cache):
        model = data.draw(hierarchical_middle_out_model_fp_tree_by_hand(time_series_forecasting_model1_no_cache))
        model.fit(dataframes).predict()

    def test_error_predict_not_fitted(self, hierarchical_basic_middle_out_model):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            hierarchical_basic_middle_out_model.predict()

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_dataframe(self, time_series, hierarchical_basic_middle_out_model):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit(time_series)

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_key_not_string(self, time_series, hierarchical_basic_middle_out_model):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit({1: time_series})

    def test_error_fit_value_not_dataframe(self, hierarchical_basic_middle_out_model):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit({"wrong_field": 12})

    def test_prediction_values_middle_out_middle_level(self):
        index = ['2000-01-01 00:00:00', '2000-01-01 00:00:01',
               '2000-01-01 00:00:02', '2000-01-01 00:00:03',
               '2000-01-01 00:00:04', '2000-01-01 00:00:05',
               '2000-01-01 00:00:06', '2000-01-01 00:00:07',
               '2000-01-01 00:00:08', '2000-01-01 00:00:09',
               '2000-01-01 00:00:10', '2000-01-01 00:00:11',
               '2000-01-01 00:00:12', '2000-01-01 00:00:13',
               '2000-01-01 00:00:14', '2000-01-01 00:00:15',
               '2000-01-01 00:00:16', '2000-01-01 00:00:17',
               '2000-01-01 00:00:18', '2000-01-01 00:00:19']
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)]+[0 for i in range(10)]
        data3_list = [0 for i in range(10)]+[1 for i in range(10)]
        data4_list = [0 for i in range(20)]
        data5_list = [0.5 for i in range(20)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data4 = pd.DataFrame(index=index, data=data4_list)
        data5 = pd.DataFrame(index=index, data=data5_list)

        data = {'data1': data1, 'data2': data2, 'data3': data3, 'data4': data4, 'data5': data5}
        tree_adj = {'data1': ['data2', 'data3'], 'data2': ['data4', 'data5'], 'data3': []}
        prediction = HierarchicalMiddleOut(model=time_series_model, hierarchy_tree=tree_adj, root='data1', method='tdsga')
        prediction.fit(data).predict(data)

    def test_prediction_values_middle_out_tdfp(self):
        index = ['2000-01-01 00:00:00', '2000-01-01 00:00:01',
               '2000-01-01 00:00:02', '2000-01-01 00:00:03',
               '2000-01-01 00:00:04', '2000-01-01 00:00:05',
               '2000-01-01 00:00:06', '2000-01-01 00:00:07',
               '2000-01-01 00:00:08', '2000-01-01 00:00:09',
               '2000-01-01 00:00:10', '2000-01-01 00:00:11',
               '2000-01-01 00:00:12', '2000-01-01 00:00:13',
               '2000-01-01 00:00:14', '2000-01-01 00:00:15',
               '2000-01-01 00:00:16', '2000-01-01 00:00:17',
               '2000-01-01 00:00:18', '2000-01-01 00:00:19']
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)]+[0 for i in range(10)]
        data3_list = [0 for i in range(10)]+[1 for i in range(10)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data = {'data1': data1, 'data2': data2, 'data3': data3}
        tree_adj = {'data1': ['data2', 'data3'], 'data2': [], 'data3': []}
        values_prediction_data1 = [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]
        values_prediction_child = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
        test_prediction_data1 = pd.DataFrame(data=values_prediction_data1, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        test_prediction_data2 = pd.DataFrame(data=values_prediction_child, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        test_prediction_data3 = pd.DataFrame(data=values_prediction_data1, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        prediction = HierarchicalMiddleOut(model=time_series_model, hierarchy_tree=tree_adj, root='data1', method='tdfp')
        test_prediction_data = {'data1': test_prediction_data1, 'data2': test_prediction_data2, 'data3': test_prediction_data3}
        prediction = prediction.fit(data).predict()

    def test_prediction_values_middle_out_tdfp(self):
        index = ['2000-01-01 00:00:00', '2000-01-01 00:00:01',
               '2000-01-01 00:00:02', '2000-01-01 00:00:03',
               '2000-01-01 00:00:04', '2000-01-01 00:00:05',
               '2000-01-01 00:00:06', '2000-01-01 00:00:07',
               '2000-01-01 00:00:08', '2000-01-01 00:00:09',
               '2000-01-01 00:00:10', '2000-01-01 00:00:11',
               '2000-01-01 00:00:12', '2000-01-01 00:00:13',
               '2000-01-01 00:00:14', '2000-01-01 00:00:15',
               '2000-01-01 00:00:16', '2000-01-01 00:00:17',
               '2000-01-01 00:00:18', '2000-01-01 00:00:19']
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)]+[0 for i in range(10)]
        data3_list = [0 for i in range(10)]+[1 for i in range(10)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data = {'data1': data1, 'data2': data2, 'data3': data3}
        tree_adj = {'data1': ['data2', 'data3'], 'data2': [], 'data3': []}
        values_prediction_data1 = [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]
        values_prediction_child = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
        test_prediction_data1 = pd.DataFrame(data=values_prediction_data1, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        test_prediction_data2 = pd.DataFrame(data=values_prediction_child, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        test_prediction_data3 = pd.DataFrame(data=values_prediction_data1, columns=['y_1', 'y_2', 'y_3'], index=['2000-01-01 00:00:17', '2000-01-01 00:00:18', '2000-01-01 00:00:19'])
        prediction = HierarchicalMiddleOut(model=time_series_model, hierarchy_tree=tree_adj, root='data1', method='tdfp')
        test_prediction_data = {'data1': test_prediction_data1, 'data2': test_prediction_data2, 'data3': test_prediction_data3}
        prediction = prediction.fit(data).predict()
        for key in test_prediction_data.keys():
            assert_frame_equal(test_prediction_data[key], prediction[key])
class TestHierarchicalMiddleOut:
    def test_basic_constructor(self, time_series_forecasting_model1_no_cache):
        HierarchicalMiddleOut(
            model=time_series_forecasting_model1_no_cache, hierarchy_tree="infer"
        )

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out_on_different_data(
        self, dataframes, hierarchical_basic_middle_out_model
    ):
        hierarchical_basic_middle_out_model.fit(dataframes).predict(dataframes)

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out_fp_method(
        self, dataframes, hierarchical_basic_middle_out_model_fp_method
    ):
        hierarchical_basic_middle_out_model_fp_method.fit(dataframes).predict(
            dataframes
        )

    @given(dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_basic_middle_out(
        self, dataframes, hierarchical_basic_middle_out_model
    ):
        hierarchical_basic_middle_out_model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_sga(time_series_forecasting_model1_no_cache)
        )
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_sga_level0(
                time_series_forecasting_model1_no_cache
            )
        )
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sgf(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_sgf(time_series_forecasting_model1_no_cache)
        )
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sga_tree_by_hands(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_sga_tree_by_hand(
                time_series_forecasting_model1_no_cache
            )
        )
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_sgf_tree_by_hands(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_sgf_tree_by_hand(
                time_series_forecasting_model1_no_cache
            )
        )
        model.fit(dataframes).predict()

    @given(data=st.data(), dataframes=n_time_series_with_same_index(min_n=5))
    def test_fit_predict_middle_out_fp_tree_by_hands(
        self, data, dataframes, time_series_forecasting_model1_no_cache
    ):
        model = data.draw(
            hierarchical_middle_out_model_fp_tree_by_hand(
                time_series_forecasting_model1_no_cache
            )
        )
        model.fit(dataframes).predict()

    def test_error_predict_not_fitted(self, hierarchical_basic_middle_out_model):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            hierarchical_basic_middle_out_model.predict()

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_dataframe(
        self, time_series, hierarchical_basic_middle_out_model
    ):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit(time_series)

    @given(time_series=giotto_time_series(min_length=5))
    def test_error_fit_key_not_string(
        self, time_series, hierarchical_basic_middle_out_model
    ):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit({1: time_series})

    def test_error_fit_value_not_dataframe(self, hierarchical_basic_middle_out_model):
        with pytest.raises(ValueError):
            hierarchical_basic_middle_out_model.fit({"wrong_field": 12})

    def test_prediction_values_middle_out_middle_level(self):
        index = [
            "2000-01-01 00:00:00",
            "2000-01-01 00:00:01",
            "2000-01-01 00:00:02",
            "2000-01-01 00:00:03",
            "2000-01-01 00:00:04",
            "2000-01-01 00:00:05",
            "2000-01-01 00:00:06",
            "2000-01-01 00:00:07",
            "2000-01-01 00:00:08",
            "2000-01-01 00:00:09",
            "2000-01-01 00:00:10",
            "2000-01-01 00:00:11",
            "2000-01-01 00:00:12",
            "2000-01-01 00:00:13",
            "2000-01-01 00:00:14",
            "2000-01-01 00:00:15",
            "2000-01-01 00:00:16",
            "2000-01-01 00:00:17",
            "2000-01-01 00:00:18",
            "2000-01-01 00:00:19",
        ]
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)] + [0 for i in range(10)]
        data3_list = [0 for i in range(10)] + [1 for i in range(10)]
        data4_list = [0 for i in range(20)]
        data5_list = [0.5 for i in range(20)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data4 = pd.DataFrame(index=index, data=data4_list)
        data5 = pd.DataFrame(index=index, data=data5_list)

        data = {
            "data1": data1,
            "data2": data2,
            "data3": data3,
            "data4": data4,
            "data5": data5,
        }
        tree_adj = {
            "data1": ["data2", "data3"],
            "data2": ["data4", "data5"],
            "data3": [],
        }
        prediction = HierarchicalMiddleOut(
            model=time_series_model,
            hierarchy_tree=tree_adj,
            root="data1",
            method="tdsga",
        )
        prediction.fit(data).predict(data)

    def test_prediction_values_middle_out_tdfp(self):
        index = [
            "2000-01-01 00:00:00",
            "2000-01-01 00:00:01",
            "2000-01-01 00:00:02",
            "2000-01-01 00:00:03",
            "2000-01-01 00:00:04",
            "2000-01-01 00:00:05",
            "2000-01-01 00:00:06",
            "2000-01-01 00:00:07",
            "2000-01-01 00:00:08",
            "2000-01-01 00:00:09",
            "2000-01-01 00:00:10",
            "2000-01-01 00:00:11",
            "2000-01-01 00:00:12",
            "2000-01-01 00:00:13",
            "2000-01-01 00:00:14",
            "2000-01-01 00:00:15",
            "2000-01-01 00:00:16",
            "2000-01-01 00:00:17",
            "2000-01-01 00:00:18",
            "2000-01-01 00:00:19",
        ]
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)] + [0 for i in range(10)]
        data3_list = [0 for i in range(10)] + [1 for i in range(10)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data = {"data1": data1, "data2": data2, "data3": data3}
        tree_adj = {"data1": ["data2", "data3"], "data2": [], "data3": []}
        values_prediction_data1 = [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
        values_prediction_child = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
        test_prediction_data1 = pd.DataFrame(
            data=values_prediction_data1,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        test_prediction_data2 = pd.DataFrame(
            data=values_prediction_child,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        test_prediction_data3 = pd.DataFrame(
            data=values_prediction_data1,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        prediction = HierarchicalMiddleOut(
            model=time_series_model,
            hierarchy_tree=tree_adj,
            root="data1",
            method="tdfp",
        )
        test_prediction_data = {
            "data1": test_prediction_data1,
            "data2": test_prediction_data2,
            "data3": test_prediction_data3,
        }
        prediction = prediction.fit(data).predict()

    def test_prediction_values_middle_out_tdfp(self):
        index = [
            "2000-01-01 00:00:00",
            "2000-01-01 00:00:01",
            "2000-01-01 00:00:02",
            "2000-01-01 00:00:03",
            "2000-01-01 00:00:04",
            "2000-01-01 00:00:05",
            "2000-01-01 00:00:06",
            "2000-01-01 00:00:07",
            "2000-01-01 00:00:08",
            "2000-01-01 00:00:09",
            "2000-01-01 00:00:10",
            "2000-01-01 00:00:11",
            "2000-01-01 00:00:12",
            "2000-01-01 00:00:13",
            "2000-01-01 00:00:14",
            "2000-01-01 00:00:15",
            "2000-01-01 00:00:16",
            "2000-01-01 00:00:17",
            "2000-01-01 00:00:18",
            "2000-01-01 00:00:19",
        ]
        time_series_model = AR(p=2, horizon=3)
        data1_list = [1 for i in range(20)]
        data2_list = [1 for i in range(10)] + [0 for i in range(10)]
        data3_list = [0 for i in range(10)] + [1 for i in range(10)]
        data1 = pd.DataFrame(index=index, data=data1_list)
        data2 = pd.DataFrame(index=index, data=data2_list)
        data3 = pd.DataFrame(index=index, data=data3_list)
        data = {"data1": data1, "data2": data2, "data3": data3}
        tree_adj = {"data1": ["data2", "data3"], "data2": [], "data3": []}
        values_prediction_data1 = [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
        values_prediction_child = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
        test_prediction_data1 = pd.DataFrame(
            data=values_prediction_data1,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        test_prediction_data2 = pd.DataFrame(
            data=values_prediction_child,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        test_prediction_data3 = pd.DataFrame(
            data=values_prediction_data1,
            columns=["y_1", "y_2", "y_3"],
            index=["2000-01-01 00:00:17", "2000-01-01 00:00:18", "2000-01-01 00:00:19"],
        )
        prediction = HierarchicalMiddleOut(
            model=time_series_model,
            hierarchy_tree=tree_adj,
            root="data1",
            method="tdfp",
        )
        test_prediction_data = {
            "data1": test_prediction_data1,
            "data2": test_prediction_data2,
            "data3": test_prediction_data3,
        }
        prediction = prediction.fit(data).predict()
        for key in test_prediction_data.keys():
            assert_frame_equal(test_prediction_data[key], prediction[key])
Exemple #18
0
class TestTimeSeriesForecastingModel:
    def test_constructor(self, features1, model1):
        horizon, cache_features = 2, True
        time_series_forecasting_model = TimeSeriesForecastingModel(
            features=features1,
            horizon=horizon,
            model=model1,
            cache_features=cache_features,
        )
        assert time_series_forecasting_model.features == features1
        assert time_series_forecasting_model.horizon == horizon
        assert time_series_forecasting_model.model == model1
        assert time_series_forecasting_model.cache_features == cache_features

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_no_cache_stores_X_test_and_model(
            self, time_series, time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series)
        assert hasattr(time_series_forecasting_model1_no_cache, "model_")
        assert hasattr(time_series_forecasting_model1_no_cache, "X_test_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_no_cache_does_not_store_X_train_y_train(
            self, time_series, time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series)
        assert not hasattr(time_series_forecasting_model1_no_cache, "X_train_")
        assert not hasattr(time_series_forecasting_model1_no_cache, "y_train_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_cache_stores_all_training_params(
            self, time_series, time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series)
        assert hasattr(time_series_forecasting_model1_cache, "model_")
        assert hasattr(time_series_forecasting_model1_cache, "X_test_")
        assert hasattr(time_series_forecasting_model1_cache, "X_train_")
        assert hasattr(time_series_forecasting_model1_cache, "y_train_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_no_cache_fail_model_not_fitted(
            self, time_series, time_series_forecasting_model1_no_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_no_cache.predict(time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_cache_fail_model_not_fitted(
            self, time_series, time_series_forecasting_model1_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.predict(time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_works_no_input(self, time_series,
                                    time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series).predict()

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_works_input(self, time_series,
                                 time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series).predict(
            time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_twice_no_cache_only_models(
            self, time_series, time_series_forecasting_model1_no_cache):
        with pytest.raises(AttributeError):
            time_series_forecasting_model1_no_cache.fit(time_series).fit(
                time_series, only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_once_only_models(self, time_series,
                                        time_series_forecasting_model1_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.fit(time_series,
                                                     only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_twice_only_models(self, time_series,
                                   time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series).fit(
            time_series, only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_twice_set_features_only_models(
            self, time_series, time_series_forecasting_model1_cache,
            features2):
        time_series_forecasting_model1_cache.fit(time_series)
        time_series_forecasting_model1_cache.set_params(features=features2)
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.fit(time_series,
                                                     only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_twice_set_model_only_models(
            self, time_series, time_series_forecasting_model1_cache, model2):
        time_series_forecasting_model1_cache.fit(time_series)
        time_series_forecasting_model1_cache.set_params(model=model2)
        time_series_forecasting_model1_cache.fit(time_series, only_model=True)
Exemple #19
0
class TestTimeSeriesForecastingModel:
    def test_constructor(self, features1, model1):
        horizon, cache_features = 2, True
        time_series_forecasting_model = TimeSeriesForecastingModel(
            features=features1,
            horizon=horizon,
            model=model1,
            cache_features=cache_features,
        )
        assert time_series_forecasting_model.features == features1
        assert time_series_forecasting_model.horizon == horizon
        assert time_series_forecasting_model.model == model1
        assert time_series_forecasting_model.cache_features == cache_features

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_no_cache_stores_X_test_and_model(
            self, time_series, time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series)
        assert hasattr(time_series_forecasting_model1_no_cache, "model_")
        assert hasattr(time_series_forecasting_model1_no_cache, "X_test_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_no_cache_does_not_store_X_train_y_train(
            self, time_series, time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series)
        assert not hasattr(time_series_forecasting_model1_no_cache, "X_train_")
        assert not hasattr(time_series_forecasting_model1_no_cache, "y_train_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_cache_stores_all_training_params(
            self, time_series, time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series)
        assert hasattr(time_series_forecasting_model1_cache, "model_")
        assert hasattr(time_series_forecasting_model1_cache, "X_test_")
        assert hasattr(time_series_forecasting_model1_cache, "X_train_")
        assert hasattr(time_series_forecasting_model1_cache, "y_train_")

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_no_cache_fail_model_not_fitted(
            self, time_series, time_series_forecasting_model1_no_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_no_cache.predict(time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_cache_fail_model_not_fitted(
            self, time_series, time_series_forecasting_model1_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.predict(time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_works_no_input(self, time_series,
                                    time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series).predict()

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_predict_works_input(self, time_series,
                                 time_series_forecasting_model1_no_cache):
        time_series_forecasting_model1_no_cache.fit(time_series).predict(
            time_series)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_twice_no_cache_only_models(
            self, time_series, time_series_forecasting_model1_no_cache):
        with pytest.raises(AttributeError):
            time_series_forecasting_model1_no_cache.fit(time_series).fit(
                time_series, only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_once_only_models(self, time_series,
                                        time_series_forecasting_model1_cache):
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.fit(time_series,
                                                     only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_twice_only_models(self, time_series,
                                   time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series).fit(
            time_series, only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_error_fit_twice_set_features_only_models(
            self, time_series, time_series_forecasting_model1_cache,
            features2):
        time_series_forecasting_model1_cache.fit(time_series)
        time_series_forecasting_model1_cache.set_params(features=features2)
        with pytest.raises(sklearn.exceptions.NotFittedError):
            time_series_forecasting_model1_cache.fit(time_series,
                                                     only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_fit_twice_set_model_only_models(
            self, time_series, time_series_forecasting_model1_cache, model2):
        time_series_forecasting_model1_cache.fit(time_series)
        time_series_forecasting_model1_cache.set_params(model=model2)
        time_series_forecasting_model1_cache.fit(time_series, only_model=True)

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    @pytest.mark.parametrize("metrics", [{"RMSE": rmse, "MAE": mae}])
    def test_score_custom(self, time_series,
                          time_series_forecasting_model1_cache, metrics):
        time_series_forecasting_model1_cache.fit(time_series)
        score = time_series_forecasting_model1_cache.score(metrics=metrics)
        assert score.shape == (2, 2)
        assert all(map(lambda x: x >= 0.0, score.iloc[0]))

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_score_default(self, time_series,
                           time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series)
        score = time_series_forecasting_model1_cache.score()
        assert score.shape == (1, 2)
        assert all(map(lambda x: x >= 0.0, score.iloc[0]))

    @given(time_series=giotto_time_series(allow_infinity=False,
                                          allow_nan=False,
                                          min_length=5))
    def test_score_y_test(self, time_series,
                          time_series_forecasting_model1_cache):
        time_series_forecasting_model1_cache.fit(time_series)
        len_test = time_series_forecasting_model1_cache.horizon + 2
        X_test = time_series.iloc[-len_test:]
        score = time_series_forecasting_model1_cache.score(X=X_test)
        assert score.shape == (1, 2)
        assert all(map(lambda x: x >= 0.0, score.iloc[0]))