From 7815dd7fc69171b89d39a49bf5308efb8fe7876a Mon Sep 17 00:00:00 2001 From: Oleksandr Shchur Date: Tue, 15 Nov 2022 19:57:06 +0100 Subject: [PATCH] Fix leaderboard with static features (#2398) --- .../src/autogluon/timeseries/learner.py | 2 +- timeseries/tests/unittests/test_learner.py | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/timeseries/src/autogluon/timeseries/learner.py b/timeseries/src/autogluon/timeseries/learner.py index 197f7929c27..3ff49079663 100644 --- a/timeseries/src/autogluon/timeseries/learner.py +++ b/timeseries/src/autogluon/timeseries/learner.py @@ -375,7 +375,7 @@ def score( return self.load_trainer().score(data=data, model=model, metric=metric) def leaderboard(self, data: Optional[TimeSeriesDataFrame] = None) -> pd.DataFrame: - if self.static_feature_pipeline.is_fit(): + if data is not None and self.static_feature_pipeline.is_fit(): fix_message = ( "Please make sure that data has static_features with columns and dtypes exactly matching " "train_data.static_features. " diff --git a/timeseries/tests/unittests/test_learner.py b/timeseries/tests/unittests/test_learner.py index 33fe013f520..8e704466a1f 100644 --- a/timeseries/tests/unittests/test_learner.py +++ b/timeseries/tests/unittests/test_learner.py @@ -394,3 +394,37 @@ def test_when_ignore_index_is_true_and_known_covariates_available_then_learner_c known_covariates = get_data_frame_with_variable_lengths(ITEM_ID_TO_LENGTH, known_covariates_names=["X", "Y"]) preds = learner.predict(train_data, known_covariates=known_covariates) assert preds.item_ids.equals(train_data.item_ids) + + +@pytest.mark.parametrize("pred_data_present", [True, False]) +@pytest.mark.parametrize("static_features_present", [True, False]) +@pytest.mark.parametrize("known_covariates_present", [True, False]) +def test_when_train_data_has_static_or_dynamic_feat_then_leaderboard_works( + temp_model_path, pred_data_present, static_features_present, known_covariates_present +): + if static_features_present: + static_features = get_static_features(["B", "A"], feature_names=["f1", "f2"]) + else: + static_features = None + + if known_covariates_present: + known_covariates_names = ["X", "Y"] + else: + known_covariates_names = None + + train_data = get_data_frame_with_variable_lengths( + {"B": 20, "A": 15}, static_features=static_features, known_covariates_names=known_covariates_names + ) + + if pred_data_present: + pred_data = get_data_frame_with_variable_lengths( + {"B": 20, "A": 15}, static_features=static_features, known_covariates_names=known_covariates_names + ) + else: + pred_data = None + + learner = TimeSeriesLearner(path_context=temp_model_path) + learner.fit(train_data=train_data, hyperparameters=HYPERPARAMETERS_DUMMY) + leaderboard = learner.leaderboard(data=pred_data) + assert len(leaderboard) > 0 + assert ("score_test" in leaderboard.columns) == pred_data_present