From 562deadb7c27933dbda9068e5ac80f337f9396bc Mon Sep 17 00:00:00 2001 From: Aryan Bhosale <36108149+aryanbhosale@users.noreply.github.com> Date: Thu, 11 Jul 2024 16:14:44 +0530 Subject: [PATCH] save results as csv (#142) * save results as csv * results folder * typer outputs CLI * update req * frontend * refactor * rm fe * rm typer help * rm streamlit --- examples/inverter_example.py | 29 +++++++++++++++++++++++------ requirements.txt | 4 +++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/examples/inverter_example.py b/examples/inverter_example.py index 043893f2..7d7b1ee1 100644 --- a/examples/inverter_example.py +++ b/examples/inverter_example.py @@ -1,15 +1,14 @@ -""" Example code to run the forecast""" import pandas as pd -from datetime import datetime +from datetime import datetime, timezone from quartz_solar_forecast.forecast import run_forecast from quartz_solar_forecast.pydantic_models import PVSite -from datetime import datetime, timezone +import os +import typer # Set plotly backend to be plotly, you might have to install plotly pd.options.plotting.backend = "plotly" -def main(): - +def main(save_outputs: bool = False): timestamp = datetime.now().timestamp() timestamp_str = datetime.fromtimestamp(timestamp, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') ts = pd.to_datetime(timestamp_str) @@ -26,6 +25,24 @@ def main(): predictions_with_recent_pv_df["power_kw_no_live_pv"] = predictions_df["power_kw"] + if save_outputs: + # Get the directory of the current script + script_dir = os.path.dirname(os.path.abspath(__file__)) + + # Create a 'results' directory if it doesn't exist + results_dir = os.path.join(script_dir, 'results') + os.makedirs(results_dir, exist_ok=True) + + # Save dataframe to CSV file in the 'results' directory + current_time = datetime.now().strftime("%Y%m%d_%H%M%S") + csv_filename = f"predictions_with_recent_pv_{current_time}.csv" + csv_path = os.path.join(results_dir, csv_filename) + predictions_with_recent_pv_df.to_csv(csv_path, index=True) + + print(f"CSV file saved at: {csv_path}") + else: + print("Outputs not saved to CSV. Use --save-outputs to save.") + # plot fig = predictions_with_recent_pv_df.plot( title="PV Forecast", @@ -36,4 +53,4 @@ def main(): fig.show(renderer="browser") if __name__ == "__main__": - main() + typer.run(main) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7a341315..3f79c939 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,6 @@ openmeteo-requests==1.2.0 requests-cache==1.2.0 retry-requests==2.0.0 gdown==5.1.0 -xgboost==2.0.3 \ No newline at end of file +xgboost==2.0.3 +plotly +typer \ No newline at end of file