-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add time series example notebook (#4654)
- Loading branch information
1 parent
fbe6ea2
commit 96868d1
Showing
9 changed files
with
41,861 additions
and
0 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
62 changes: 62 additions & 0 deletions
62
sagemaker-clarify/time_series_byom/model/code/inference.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,62 @@ | ||
from darts.models import LinearRegressionModel | ||
from darts.timeseries import TimeSeries | ||
from typing import Optional | ||
import pandas as pd | ||
from datetime import datetime, timedelta | ||
import json | ||
import os | ||
import logging | ||
|
||
def model_fn(model_dir, context): | ||
# model_path = os.path.join(model_dir, "model.pth") | ||
model = None | ||
with open(os.path.join(model_dir, 'model.pth'), 'rb') as f: | ||
model = LinearRegressionModel.load(f) | ||
return model | ||
|
||
def input_fn(request_body, request_content_type, context): | ||
# return request_body | ||
record_dict = json.loads(request_body) | ||
print(record_dict) | ||
record_list = record_dict["instances"] | ||
# record = record_list[0] | ||
return record_list | ||
|
||
def predict_fn(input_object, model, context): | ||
# return model.predict(10) | ||
start = input_object["start"] | ||
target = input_object["target"] | ||
dynamic_feat = input_object["dynamic_feat"] | ||
|
||
start_datetime = datetime.strptime(start, '%Y-%m-%d %H:%M:%S') | ||
datetimes = [start_datetime + timedelta(minutes=i*10) for i in range(len(target))] | ||
target_df = pd.DataFrame({'target': target}, index=datetimes) | ||
target_ts = TimeSeries.from_dataframe(target_df, value_cols=['target']) | ||
|
||
past_cov_df = pd.DataFrame({f"feature_{i+1}": feats[:len(target)] for i, feats in enumerate(dynamic_feat)}, index=datetimes) | ||
past_cov_ts = TimeSeries.from_dataframe(past_cov_df) | ||
|
||
start_future_datetime = datetimes[-1] + timedelta(minutes=10) | ||
num_future_steps = len(dynamic_feat[0]) - len(target) | ||
future_datetimes = [start_future_datetime + timedelta(minutes=i*10) for i in range(num_future_steps)] | ||
|
||
# Create the DataFrame for future_covariates | ||
future_cov_df = pd.DataFrame({ | ||
f"feature_{i+1}": feats[len(target):] for i, feats in enumerate(dynamic_feat) | ||
}, index=future_datetimes) | ||
future_cov_ts = TimeSeries.from_dataframe(future_cov_df) | ||
|
||
predictions = model.predict(10, series=target_ts, past_covariates=past_cov_ts, future_covariates=future_cov_ts) | ||
return predictions | ||
|
||
|
||
def output_fn(prediction, content_type, context): | ||
predictions_list = [item for sublist in prediction.values() for item in sublist] | ||
logging.warning('----------------') | ||
|
||
# Create a dictionary in the desired format | ||
pred_dict = {'predictions': {'mean': predictions_list}} | ||
logging.warning(f'Predictions: {pred_dict}') | ||
# Convert the dictionary to a JSON string | ||
pred_json = json.dumps(pred_dict) | ||
return pred_json |
9 changes: 9 additions & 0 deletions
9
sagemaker-clarify/time_series_byom/model/code/requirements.txt
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,9 @@ | ||
awscli | ||
Cython | ||
fastapi | ||
numpy | ||
pandas | ||
pytest | ||
scikit-learn | ||
torchvision | ||
darts==0.26.0 |
Binary file not shown.
1,459 changes: 1,459 additions & 0 deletions
1,459
sagemaker-clarify/time_series_byom/time_series_bring_your_own_model.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
sagemaker-clarify/time_series_byom/time_series_byom_mock_data.json
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,128 @@ | ||
[ | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 16:20:00", | ||
"p_mbar": 1008.89, | ||
"rain_mm": 0.23, | ||
"T_degC": 0.71 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 16:30:00", | ||
"p_mbar": 1008.76, | ||
"rain_mm": 0.21, | ||
"T_degC": 0.75 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 16:40:00", | ||
"p_mbar": 1008.66, | ||
"rain_mm": 0.19, | ||
"T_degC": 0.73 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 16:50:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.16, | ||
"T_degC": 0.37 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 17:00:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.13, | ||
"T_degC": 0.33 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 17:10:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.08, | ||
"T_degC": 0.34 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 17:20:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 0.19 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 17:30:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 0.03 | ||
}, | ||
{ | ||
"item_id": "seattle", | ||
"timestamp": "2020-01-01 17:40:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 0.11 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 16:20:00", | ||
"p_mbar": 1005.1, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.48 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 16:30:00", | ||
"p_mbar": 1005.1, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.41 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 16:40:00", | ||
"p_mbar": 977.92, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.37 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 16:50:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.40 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 17:00:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.41 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 17:10:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.45 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 17:20:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.40 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 17:30:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.38 | ||
}, | ||
{ | ||
"item_id": "sanjose", | ||
"timestamp": "2020-01-01 17:40:00", | ||
"p_mbar": null, | ||
"rain_mm": 0.0, | ||
"T_degC": 2.34 | ||
} | ||
] |
Oops, something went wrong.