-
Notifications
You must be signed in to change notification settings - Fork 555
Auto-Create Timestamps in prettify_prediction() When test_data is None
#1508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aditisingh02
wants to merge
9
commits into
microsoft:main
Choose a base branch
from
aditisingh02:feature/auto-create-timestamps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9660c65
feat: auto-create timestamps in prettify_prediction when test_data is…
aditisingh02 4d0c8c6
Merge branch 'microsoft:main' into feature/auto-create-timestamps
aditisingh02 2e49bc0
Merge branch 'main' into feature/auto-create-timestamps
thinkall a52d3f3
Merge branch 'main' into feature/auto-create-timestamps
thinkall b2f118f
fix: timestamp generation in prettify_prediction for empty test_data
aditisingh02 14e33e3
test: simplify and relocate auto-timestamp tests per review feedback
aditisingh02 a7520a0
Merge branch 'main' into feature/auto-create-timestamps
thinkall b481f8a
style: fix formatting and respond to PR feedback
aditisingh02 a30b769
Merge branch 'microsoft:main' into feature/auto-create-timestamps
aditisingh02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
| # Auto-Create Timestamps in `prettify_prediction()` When `test_data` is None | ||
|
|
||
| ## Why are these changes needed? | ||
|
|
||
| Currently, the `TimeSeriesDataset.prettify_prediction()` method in `flaml/automl/time_series/ts_data.py` throws a `NotImplementedError` when `test_data` is `None`. | ||
| This is frustrating for users who want to make predictions without providing explicit test data timestamps. | ||
|
|
||
| **This PR implements automatic timestamp generation** by: | ||
|
|
||
| 1. Using the training data's end date as the starting point. | ||
| 2. Generating future timestamps based on the inferred frequency. | ||
| 3. Supporting `np.ndarray`, `pd.Series`, and `pd.DataFrame`. | ||
|
|
||
| ## Checks | ||
|
|
||
| - [x] Pre-commit linting (black, ruff). | ||
| - [x] Added regression tests demonstrating the fix. |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -724,6 +724,124 @@ def test_log_training_metric_ts_models(): | |
| assert automl.best_estimator == estimator | ||
|
|
||
|
|
||
| def test_prettify_prediction_auto_timestamps_data_types(): | ||
| """Test auto-timestamp generation with different input data types. | ||
|
|
||
| Before this PR fix, calling prettify_prediction() with test_data=None raised: | ||
| - ValueError for np.ndarray: "Can't enrich np.ndarray as self.test_data is None" | ||
| - NotImplementedError for pd.Series/DataFrame: "Need a non-None test_data for this to work" | ||
|
|
||
| This test verifies the fix works for np.ndarray, pd.Series, and pd.DataFrame inputs. | ||
| """ | ||
| from flaml.automl.time_series import TimeSeriesDataset | ||
|
|
||
| # Create training data with daily frequency | ||
| n = 30 | ||
| train_df = pd.DataFrame( | ||
| { | ||
| "date": pd.date_range(start="2023-01-01", periods=n, freq="D"), | ||
| "value": np.random.randn(n), | ||
| } | ||
| ) | ||
| tsds = TimeSeriesDataset(train_df, time_col="date", target_names="value") | ||
| assert len(tsds.test_data) == 0 | ||
|
|
||
| pred_steps = 5 | ||
| expected_start = pd.date_range(start=train_df["date"].max(), periods=2, freq="D")[1] | ||
|
|
||
| # Test np.ndarray | ||
| result = tsds.prettify_prediction(np.random.randn(pred_steps)) | ||
| assert isinstance(result, pd.DataFrame) | ||
| assert len(result) == pred_steps | ||
| assert result["date"].iloc[0] == expected_start | ||
|
|
||
| # Test pd.Series | ||
| result = tsds.prettify_prediction(pd.Series(np.random.randn(pred_steps))) | ||
| assert isinstance(result, pd.DataFrame) | ||
| assert len(result) == pred_steps | ||
| assert result["date"].iloc[0] == expected_start | ||
|
|
||
| # Test pd.DataFrame | ||
| result = tsds.prettify_prediction(pd.DataFrame({"value": np.random.randn(pred_steps)})) | ||
| assert isinstance(result, pd.DataFrame) | ||
| assert len(result) == pred_steps | ||
| assert result["date"].iloc[0] == expected_start | ||
|
|
||
|
|
||
| def test_prettify_prediction_auto_timestamps_frequencies(): | ||
| """Test auto-timestamp generation with different frequencies. | ||
|
|
||
| Before this PR fix, this would raise NotImplementedError when test_data is None. | ||
| Tests daily and monthly frequencies with np.ndarray input. | ||
| """ | ||
| from flaml.automl.time_series import TimeSeriesDataset | ||
|
|
||
| pred_steps = 6 | ||
|
|
||
| # Test daily frequency | ||
| train_df_daily = pd.DataFrame( | ||
| { | ||
| "date": pd.date_range(start="2023-01-01", periods=30, freq="D"), | ||
| "value": np.random.randn(30), | ||
| } | ||
| ) | ||
| tsds_daily = TimeSeriesDataset(train_df_daily, time_col="date", target_names="value") | ||
| result = tsds_daily.prettify_prediction(np.random.randn(pred_steps)) | ||
| expected_dates = pd.date_range(start=train_df_daily["date"].max(), periods=pred_steps + 1, freq="D")[1:] | ||
| pd.testing.assert_index_equal(pd.DatetimeIndex(result["date"]), expected_dates, check_names=False) | ||
|
|
||
| # Test monthly frequency | ||
| train_df_monthly = pd.DataFrame( | ||
| { | ||
| "date": pd.date_range(start="2022-01-01", periods=24, freq="MS"), | ||
| "value": np.random.randn(24), | ||
| } | ||
| ) | ||
| tsds_monthly = TimeSeriesDataset(train_df_monthly, time_col="date", target_names="value") | ||
| result = tsds_monthly.prettify_prediction(np.random.randn(pred_steps)) | ||
| expected_dates = pd.date_range(start=train_df_monthly["date"].max(), periods=pred_steps + 1, freq="MS")[1:] | ||
| pd.testing.assert_index_equal(pd.DatetimeIndex(result["date"]), expected_dates, check_names=False) | ||
|
|
||
|
|
||
| def test_auto_timestamps_e2e(budget=3): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test already works without the PR. Should have a test that won't work on current release and will be fixed with PR. |
||
| """E2E test: train a model and predict without explicit test_data timestamps. | ||
|
|
||
| This showcases the improvement from this PR - users can now make predictions | ||
| without providing explicit test data timestamps. | ||
| """ | ||
| try: | ||
| import statsmodels # noqa: F401 | ||
| except ImportError: | ||
| print("statsmodels not installed, skipping E2E test") | ||
| return | ||
|
|
||
| # Create training data | ||
| n = 100 | ||
| train_df = pd.DataFrame( | ||
| { | ||
| "ds": pd.date_range(start="2020-01-01", periods=n, freq="D"), | ||
| "y": np.sin(np.linspace(0, 10, n)) + np.random.randn(n) * 0.1, | ||
| } | ||
| ) | ||
|
|
||
| # Train model | ||
| automl = AutoML() | ||
| automl.fit( | ||
| dataframe=train_df, | ||
| label="y", | ||
| period=10, | ||
| task="ts_forecast", | ||
| time_budget=budget, | ||
| estimator_list=["arima"], | ||
| ) | ||
|
|
||
| # Predict using steps (no explicit test_data) - this is the key improvement | ||
| y_pred = automl.predict(10) | ||
| assert y_pred is not None | ||
| assert len(y_pred) == 10 | ||
| print("E2E test passed: model trained and predicted without explicit test_data!") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| # test_forecast_automl(60) | ||
| # test_multivariate_forecast_num(5) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.