Skip to content

Commit

Permalink
feat: add time series example notebook (#4654)
Browse files Browse the repository at this point in the history
  • Loading branch information
zicanl-amazon committed May 14, 2024
1 parent fbe6ea2 commit 96868d1
Show file tree
Hide file tree
Showing 9 changed files with 41,861 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ These examples provide an Introduction to Smart Sifting library. Smart Sifting i
These examples provide an introduction to SageMaker Clarify which provides machine learning developers with greater visibility into their training data and models so they can identify and limit bias and explain predictions.

* [Fairness and Explainability with SageMaker Clarify](sagemaker-clarify/fairness_and_explainability) shows how to use SageMaker Clarify Processor API to measure the pre-training bias of a dataset and post-training bias of a model, and explain the importance of the input features on the model's decision.
* [TimeSeries Explainability with SageMaker Clarify](sagemaker-clarify/time_series_deepar) shows how to use SageMaker Clarify Processor API to explain the importance of the input features on the time-series model's decision.
* [Amazon SageMaker Clarify Model Monitors](sagemaker_model_monitor/fairness_and_explainability) shows how to use SageMaker Clarify Model Monitor API to schedule bias monitor to monitor predictions for bias drift on a regular basis, and schedule explainability monitor to monitor predictions for feature attribution drift on a regular basis.

### Publishing content from RStudio on Amazon SageMaker to RStudio Connect
Expand Down
62 changes: 62 additions & 0 deletions sagemaker-clarify/time_series_byom/model/code/inference.py
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
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 sagemaker-clarify/time_series_byom/time_series_bring_your_own_model.ipynb

Large diffs are not rendered by default.

128 changes: 128 additions & 0 deletions sagemaker-clarify/time_series_byom/time_series_byom_mock_data.json
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
}
]

0 comments on commit 96868d1

Please sign in to comment.