generated from Tauffer-Consulting/domino_pieces_repository_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
200 additions
and
21 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
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 |
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,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" | ||
} | ||
} |
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,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." | ||
) |
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,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) | ||
) |
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,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" | ||
} | ||
} |
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,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." | ||
) |
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,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), | ||
) |
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,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 |
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
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