-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fill time steps for single time-series in DeepAR predict (#152)
* Fill missing time steps in DeepAR predict * Add single series test * Fix multi series missing time steps and add test * nit * Support multiple id_cols and add test case * Bump version * docstring * Address comments and add test
- Loading branch information
Showing
5 changed files
with
237 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
runtime/databricks/automl_runtime/forecast/deepar/utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# | ||
# Copyright (C) 2024 Databricks, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from typing import List, Optional | ||
|
||
import pandas as pd | ||
|
||
|
||
def set_index_and_fill_missing_time_steps(df: pd.DataFrame, time_col: str, | ||
frequency: str, | ||
id_cols: Optional[List[str]] = None): | ||
""" | ||
Transform the input dataframe to an acceptable format for the GluonTS library. | ||
- Set the time column as the index | ||
- Impute missing time steps between the min and max time steps | ||
:param df: the input dataframe that contains time_col | ||
:param time_col: time column name | ||
:param frequency: the frequency of the time series | ||
:param id_cols: the column names of the identity columns for multi-series time series; None for single series | ||
:return: single-series - transformed dataframe; | ||
multi-series - dictionary of transformed dataframes, each key is the (concatenated) id of the time series | ||
""" | ||
total_min, total_max = df[time_col].min(), df[time_col].max() | ||
new_index_full = pd.date_range(total_min, total_max, freq=frequency) | ||
|
||
if id_cols is not None: | ||
df_dict = {} | ||
for grouped_id, grouped_df in df.groupby(id_cols): | ||
if isinstance(grouped_id, tuple): | ||
ts_id = "-".join([str(x) for x in grouped_id]) | ||
else: | ||
ts_id = str(grouped_id) | ||
df_dict[ts_id] = (grouped_df.set_index(time_col).sort_index() | ||
.reindex(new_index_full).drop(id_cols, axis=1)) | ||
|
||
return df_dict | ||
|
||
df = df.set_index(time_col).sort_index() | ||
|
||
# Fill in missing time steps between the min and max time steps | ||
return df.reindex(new_index_full) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
runtime/tests/automl_runtime/forecast/deepar/utils_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# | ||
# Copyright (C) 2024 Databricks, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
import unittest | ||
|
||
import pandas as pd | ||
|
||
from databricks.automl_runtime.forecast.deepar.utils import set_index_and_fill_missing_time_steps | ||
|
||
|
||
class TestDeepARUtils(unittest.TestCase): | ||
def test_single_series_filled(self): | ||
target_col = "sales" | ||
time_col = "date" | ||
num_rows = 10 | ||
|
||
base_df = pd.concat( | ||
[ | ||
pd.to_datetime( | ||
pd.Series(range(num_rows), name=time_col).apply( | ||
lambda i: f"2020-10-{i + 1}" | ||
) | ||
), | ||
pd.Series(range(num_rows), name=target_col), | ||
], | ||
axis=1, | ||
) | ||
dropped_df = base_df.drop([4, 5]).reset_index(drop=True) | ||
|
||
transformed_df = set_index_and_fill_missing_time_steps(dropped_df, time_col, "D") | ||
|
||
expected_df = base_df.copy() | ||
expected_df.loc[[4, 5], target_col] = float('nan') | ||
expected_df = expected_df.set_index(time_col).rename_axis(None).asfreq("D") | ||
|
||
pd.testing.assert_frame_equal(transformed_df, expected_df) | ||
|
||
def test_multi_series_filled(self): | ||
target_col = "sales" | ||
time_col = "date" | ||
id_col = "store" | ||
|
||
num_rows_per_ts = 10 | ||
base_df = pd.concat( | ||
[ | ||
pd.to_datetime( | ||
pd.Series(range(num_rows_per_ts), name=time_col).apply( | ||
lambda i: f"2020-10-{i + 1}" | ||
) | ||
), | ||
pd.Series(range(num_rows_per_ts), name=target_col), | ||
], | ||
axis=1, | ||
) | ||
dropped_base_df = base_df.drop([4, 5]).reset_index(drop=True) | ||
dropped_df = pd.concat([dropped_base_df.copy(), dropped_base_df.copy()], ignore_index=True) | ||
dropped_df[id_col] = [1] * (num_rows_per_ts - 2) + [2] * (num_rows_per_ts - 2) | ||
|
||
transformed_df_dict = set_index_and_fill_missing_time_steps(dropped_df, time_col, "D", id_cols=[id_col]) | ||
self.assertEqual(transformed_df_dict.keys(), {"1", "2"}) | ||
|
||
expected_first_df = base_df.copy() | ||
expected_first_df.loc[[4, 5], target_col] = float('nan') | ||
expected_first_df = expected_first_df.set_index(time_col).rename_axis(None).asfreq("D") | ||
|
||
pd.testing.assert_frame_equal(transformed_df_dict["1"], expected_first_df) | ||
|
||
def test_multi_series_multi_id_cols_filled(self): | ||
target_col = "sales" | ||
time_col = "date" | ||
id_cols = ["store", "dept"] | ||
|
||
num_rows_per_ts = 10 | ||
base_df = pd.concat( | ||
[ | ||
pd.to_datetime( | ||
pd.Series(range(num_rows_per_ts), name=time_col).apply( | ||
lambda i: f"2020-10-{i + 1}" | ||
) | ||
), | ||
pd.Series(range(num_rows_per_ts), name=target_col), | ||
], | ||
axis=1, | ||
) | ||
dropped_base_df = base_df.drop([4, 5]).reset_index(drop=True) | ||
dropped_df = pd.concat([dropped_base_df.copy(), dropped_base_df.copy(), | ||
dropped_base_df.copy(), dropped_base_df.copy()], ignore_index=True) | ||
dropped_df[id_cols[0]] = ([1] * (num_rows_per_ts - 2) + [2] * (num_rows_per_ts - 2)) * 2 | ||
dropped_df[id_cols[1]] = [1] * (2 * (num_rows_per_ts - 2)) + [2] * (2 * (num_rows_per_ts - 2)) | ||
|
||
transformed_df_dict = set_index_and_fill_missing_time_steps(dropped_df, time_col, "D", id_cols=id_cols) | ||
self.assertEqual(transformed_df_dict.keys(), {"1-1", "1-2", "2-1", "2-2"}) | ||
|
||
expected_first_df = base_df.copy() | ||
expected_first_df.loc[[4, 5], target_col] = float('nan') | ||
expected_first_df = expected_first_df.set_index(time_col).rename_axis(None).asfreq("D") | ||
|
||
pd.testing.assert_frame_equal(transformed_df_dict["1-1"], expected_first_df) |