Skip to content

Commit

Permalink
deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
vividfog committed Mar 7, 2024
1 parent 830c387 commit 845d3a6
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 71 deletions.
2 changes: 0 additions & 2 deletions .env.local.template
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ DB_PATH="data/prediction.db"
PREDICTIONS_FILE="prediction.json"
# Averages file name
AVERAGES_FILE="averages.json"
# Past performance comparison file
PAST_PERFORMANCE_FILE="past_performance.json"

# 3. Optional but fun to have: OpenAI API key for generating --narrate
OPENAI_API_KEY="sk-abcdefghijklmnopqrstuvwxyz"
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,19 @@ The script uses environment variables for configuration. These can be set in a f
How to use:

```
usage: nordpool_predict_fi.py [-h] [--train] [--eval] [--training-stats] [--dump] [--past-performance] [--plot] [--predict] [--add-history] [--narrate] [--commit] [--deploy] [--publish] [--github]
usage: nordpool_predict_fi.py [-h] [--train] [--eval] [--training-stats] [--dump] [--plot] [--predict] [--add-history] [--narrate] [--commit] [--deploy] [--publish] [--github]
options:
-h, --help show this help message and exit
--train Train a new model candidate using the data in the database
--eval Show evaluation metrics for the current database
--training-stats Show training stats for candidate models in the database as a CSV
--dump Dump the SQLite database to CSV format
--past-performance Generate past performance stats for recent months
--plot Plot all predictions and actual prices to a PNG file in the data folder
--predict Generate price predictions from now onwards
--add-history Add all missing predictions to the database post-hoc; use with --predict
--narrate Narrate the predictions into text using an LLM
--commit Commit the results to DB and deploy folder; use with --predict, --narrate, --past-performance
--commit Commit the results to DB and deploy folder; use with --predict, --narrate
--deploy Deploy the output files to the deploy folder but not GitHub
--github Push the deployed files to a GitHub repo; use with --deploy
```
Expand All @@ -67,7 +66,7 @@ Examples:

- Start with: `python nordpool_predict_fi.py --predict` to create a set of price predictions for 7 days into the past and 5 days into the future with NO commit to DB

- Longer end to end pipeline: Train a new model, show eval stats for it, update a price forecast data frame with it, narrate the forecast, commit it to your SQLite database and deploy the json/md outputs with that data: `python nordpool_predict_fi.py --train --eval --predict --narrate --commit --deploy`
- Longer end to end pipeline: Train a new model, show eval stats for it, update a price forecast data frame with it, narrate the forecast, commit it to your SQLite database and deploy the json/md outputs with that data: `python nordpool_predict_fi.py --train --predict --narrate --commit --deploy`

Optionally, you can do a retrospective update to the PricePredict field for the whole DB by including `--add-history` into the command line above

Expand Down
67 changes: 2 additions & 65 deletions nordpool_predict_fi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def get_mandatory_env_variable(name):
repo_path = get_mandatory_env_variable('REPO_PATH')
predictions_file = get_mandatory_env_variable('PREDICTIONS_FILE')
averages_file = get_mandatory_env_variable('AVERAGES_FILE')
past_performance_file = get_mandatory_env_variable('PAST_PERFORMANCE_FILE')
fingrid_api_key = get_mandatory_env_variable('FINGRID_API_KEY')
fmisid_ws_env = get_mandatory_env_variable('FMISID_WS')
fmisid_t_env = get_mandatory_env_variable('FMISID_T')
Expand All @@ -67,12 +66,11 @@ def get_mandatory_env_variable(name):
parser.add_argument('--eval', action='store_true', help='Show evaluation metrics for the current database')
parser.add_argument('--training-stats', action='store_true', help='Show training stats for candidate models in the database as a CSV')
parser.add_argument('--dump', action='store_true', help='Dump the SQLite database to CSV format')
parser.add_argument('--past-performance', action='store_true', help='Generate past performance stats for recent months')
parser.add_argument('--plot', action='store_true', help='Plot all predictions and actual prices to a PNG file in the data folder')
parser.add_argument('--predict', action='store_true', help='Generate price predictions from now onwards')
parser.add_argument('--add-history', action='store_true', help='Add all missing predictions to the database post-hoc; use with --predict')
parser.add_argument('--narrate', action='store_true', help='Narrate the predictions into text using an LLM')
parser.add_argument('--commit', action='store_true', help='Commit the results to DB and deploy folder; use with --predict, --narrate, --past-performance')
parser.add_argument('--commit', action='store_true', help='Commit the results to DB and deploy folder; use with --predict, --narrate')
parser.add_argument('--deploy', action='store_true', help='Deploy the output files to the deploy folder but not GitHub')
# --publish will be deprecated in the future, prefer --deploy instead:
parser.add_argument('--publish', action='store_true', help='Deploy the output files to the deploy folder but not GitHub', dest='deploy') #redirected
Expand Down Expand Up @@ -344,67 +342,6 @@ def get_mandatory_env_variable(name):
else:
print(narration)

# Past performance can be used with the previous arguments
if args.past_performance:
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

past_df = db_query_all(db_path)
past_df = past_df.sort_values(by='timestamp')

past_df['timestamp'] = pd.to_datetime(past_df['timestamp'])
before_filtering_length = len(past_df)

# Filter out rows where 'timestamp' is earlier than 90 days ago or later than now
now = datetime.now(pytz.utc)
past_df = past_df[(past_df['timestamp'] >= now - timedelta(days=90)) & (past_df['timestamp'] <= now)]

nan_rows = past_df[past_df['Price_cpkWh'].isna() | past_df['PricePredict_cpkWh'].isna()]

# Drop empty or NaN rows
past_df = past_df.dropna(subset=['Price_cpkWh', 'PricePredict_cpkWh'])

# Calculate the metrics
past_df = past_df.dropna(subset=['Price_cpkWh', 'PricePredict_cpkWh'])
y_true = past_df['Price_cpkWh']
y_pred = past_df['PricePredict_cpkWh']
mae = mean_absolute_error(y_true, y_pred)
mse = mean_squared_error(y_true, y_pred)
rmse = np.sqrt(mse)
r2 = r2_score(y_true, y_pred)

print("Mean Absolute Error:", mae, "c/kWh")
print("Mean Squared Error:", mse, "c/kWh")
print("Root Mean Squared Error:", rmse, "c/kWh")
print("R-squared:", r2)

if args.commit:
# Prepare data for Apex Charts
past_performance_data = {
"data": [
{"name": "Actual Price", "data": []},
{"name": "Predicted Price", "data": []}
],
"metrics": {
"mae": mae,
"mse": mse,
"rmse": rmse,
"r2": r2
}
}

# Convert timestamps to milliseconds since epoch and pair with values
for _, row in past_df.iterrows():
timestamp_ms = int(row['timestamp'].timestamp() * 1000)
past_performance_data["data"][0]["data"].append([timestamp_ms, row['Price_cpkWh']])
past_performance_data["data"][1]["data"].append([timestamp_ms, row['PricePredict_cpkWh']])

# Save to JSON file
past_performance_json_path = os.path.join(deploy_folder_path, past_performance_file)
with open(past_performance_json_path, 'w') as f:
json.dump(past_performance_data, f)

print(f"Past performance data saved to {past_performance_json_path}")

# Deploy can be done solo, or with --predict and --narrate
# This argument was previously called --publish but for now they both point here
# Note that we have a dedicated --github argument to push to GitHub (not many need to use this step)
Expand Down Expand Up @@ -472,7 +409,7 @@ def get_mandatory_env_variable(name):

# Commit and push the updates to GitHub
if args.github:
files_to_push = [predictions_file, averages_file, narration_file, past_performance_file]
files_to_push = [predictions_file, averages_file, narration_file]

try:
if push_updates_to_github(repo_path, deploy_folder_path, files_to_push, commit_message):
Expand Down

0 comments on commit 845d3a6

Please sign in to comment.