Skip to content

Commit

Permalink
pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicvaz committed May 7, 2024
1 parent 51d5953 commit 213c0e8
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 21 deletions.
3 changes: 2 additions & 1 deletion dependencies/requirements_0.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
prophet==1.1.5
pandas==2.1.3
plotly==5.18.0
plotly==5.18.0
yfinance==0.2.38
24 changes: 24 additions & 0 deletions pieces/GetYahooFinanceDataPiece/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "GetYahooFinanceDataPiece",
"description": "This piece gets data from Yahoo Finance.",
"dependency": {
"requirements_file": "requirements_0.txt"
},
"container_resources": {
"requests": {
"cpu": "100m",
"memory": "128Mi"
},
"limits": {
"cpu": "500m",
"memory": "512Mi"
}
},
"tags": [
"Example"
],
"style": {
"node_label": "Get Yahoo Finance Data",
"icon_class_name": "fa-solid:database"
}
}
20 changes: 20 additions & 0 deletions pieces/GetYahooFinanceDataPiece/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from pydantic import BaseModel, Field
from datetime import date



class InputModel(BaseModel):
ticker: str = Field(
description="Ticker of the stock to get data from."
) # TODO change to ENUM ?
start_date: date = Field(
description="Start date of the data to get."
)
end_date: date = Field(
description="End date of the data to get."
)

class OutputModel(BaseModel):
data_path: str = Field(
description="Path to the file containing the trained model."
)
24 changes: 24 additions & 0 deletions pieces/GetYahooFinanceDataPiece/piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from domino.base_piece import BasePiece
from .models import InputModel, OutputModel
import yfinance as yf
from pathlib import Path


class GetYahooFinanceDataPiece(BasePiece):
"""
This Piece trains a Prophet model using the data provided in the input file.
"""
def piece_function(self, input_data: InputModel):
ticker = input_data.ticker
start_date = input_data.start_date
end_date = input_data.end_date

df = yf.download(ticker, start=start_date, end=end_date)
df.reset_index(inplace=True)

df_path = Path(self.results_path) / f"{ticker}_data.csv"
df.to_csv(df_path, index=False)

return OutputModel(
data_path=str(df_path)
)
24 changes: 24 additions & 0 deletions pieces/ProphetPredictPiece/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "ProphetPredictPiece",
"description": "Piece to predict using Prophet",
"dependency": {
"requirements_file": "requirements_0.txt"
},
"container_resources": {
"requests": {
"cpu": "100m",
"memory": "128Mi"
},
"limits": {
"cpu": "500m",
"memory": "512Mi"
}
},
"tags": [
"Prophet"
],
"style": {
"node_label": "Prophet Predict",
"icon_class_name": "fa-solid:database"
}
}
26 changes: 26 additions & 0 deletions pieces/ProphetPredictPiece/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pydantic import BaseModel, Field
from enum import Enum
from typing import List
from datetime import date


class InputModel(BaseModel):
model_path: str = Field(
title="Model Path",
description="Path to the file containing the trained model."
)
periods: int = Field(
title="Periods",
description="Number of periods to forecast."
)


class OutputModel(BaseModel):
forecast_data_path: str = Field(
title="Forecast Data Path",
description="Path to the file containing the forecast data."
)
forecast_figure_path: str = Field(
title="Forecast Figure Path",
description="Path to the file containing the results figure."
)
40 changes: 40 additions & 0 deletions pieces/ProphetPredictPiece/piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from domino.base_piece import BasePiece
from .models import InputModel, OutputModel
import pandas as pd
from prophet import Prophet
import pickle
from pathlib import Path
from prophet.plot import plot_plotly, plot_components_plotly



class ProphetPredictPiece(BasePiece):
"""
This Piece uses a trained Prophet model to make predictions on new data.
"""
def piece_function(self, input_data: InputModel):

with open(input_data.model_path, "rb") as f:
model = pickle.load(f)

future = model.make_future_dataframe(periods=input_data.periods)
forecast = model.predict(future)

self.results_path = Path(self.results_path)

forecast_data_path = self.results_path / "forecast_data.csv"
forecast.to_csv(forecast_data_path, index=False)

forecast_figure_path = self.results_path / "forecast_figure.json"
forecast_figure = plot_plotly(model, forecast)

forecast_figure.write_json(str(forecast_figure_path))
self.display_result = {
"file_type": "plotly_json",
"file_path": str(forecast_figure_path)
}

return OutputModel(
forecast_data_path=str(forecast_data_path),
forecast_figure_path=str(forecast_figure_path),
)
15 changes: 15 additions & 0 deletions pieces/ProphetPredictPiece/test_example_simple_piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from domino.testing import piece_dry_run

def test_example_simple_piece():
input_data = dict(
distribution_name="gaussian",
distribution_mean=0.,
distribution_sd=1.
)
output_data = piece_dry_run(
"ExampleSimplePiece",
input_data
)

assert output_data["message"] is not None
assert output_data["sample_result"] is not None
8 changes: 4 additions & 4 deletions pieces/ProphetTrainModelPiece/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ExampleSimplePiece",
"description": "This is an example of a simple Domino Piece",
"name": "ProphetTrainModelPiece",
"description": "Piece to train a prophet model",
"dependency": {
"requirements_file": "requirements_0.txt"
},
Expand All @@ -15,10 +15,10 @@
}
},
"tags": [
"Example"
"Prophet"
],
"style": {
"node_label": "Simple Piece",
"node_label": "Prophet Train",
"icon_class_name": "fa-solid:database"
}
}
23 changes: 12 additions & 11 deletions pieces/ProphetTrainModelPiece/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ class SeasonalityMode(str, Enum):

class InputModel(BaseModel):
input_data_file: str = Field(
description="Path to the input data file. Accepted formats: `.csv`, `.json`."
)
datetime_column_name: str = Field(
description="Name of the column containing the datetime values."
)
target_column_name: str = Field(
description="Name of the column containing the target values."
)
title="Input Data File",
description="Path to the input data file. Accepted formats: `.csv`, `.json`. Should use the following format: `ds` (datetime), `y` (target).",
)
# datetime_column_name: str = Field(
# description="Name of the column containing the datetime values."
# )
# target_column_name: str = Field(
# description="Name of the column containing the target values."
# )
test_set_percentage: float = Field(
default=20.0,
ge=1,
Expand Down Expand Up @@ -55,6 +56,6 @@ class OutputModel(BaseModel):
model_file_path: str = Field(
description="Path to the file containing the trained model."
)
results_figure_file_path: str = Field(
description="Path to the file containing the results figure."
)
# results_figure_file_path: str = Field(
# description="Path to the file containing the results figure."
# )
14 changes: 9 additions & 5 deletions pieces/ProphetTrainModelPiece/piece.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
from .models import InputModel, OutputModel
import pandas as pd
from prophet import Prophet
from prophet.serialize import model_to_json
import plotly.graph_objs as go

import pickle
from pathlib import Path

class ProphetTrainModelPiece(BasePiece):
"""
Expand All @@ -21,9 +20,14 @@ def piece_function(self, input_data: InputModel):
else:
raise ValueError("File format not supported. Please pass a CSV or JSON file.")

model = Prophet()
model.fit(df)

# Serialize model
model_file_path = self.results_path / "prophet_model.json"
with open(str(model_file_path), "wb") as f:
pickle.dump(model, f)

return OutputModel(
model_file_path=,
results_figure_file_path=
model_file_path=str(model_file_path),
)

0 comments on commit 213c0e8

Please sign in to comment.