Skip to content

Commit

Permalink
Updated Streamlit App with new ENR prediction model
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinetavant committed Sep 16, 2024
1 parent cb6d38f commit 943ecdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 55 deletions.
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ dependencies = [
"eccodes",
"cfgrib",
"requests",
"statsmodels",
"geojson",
"shapely",
"tqdm",
Expand Down Expand Up @@ -80,7 +79,7 @@ build = "sphinx-build -b html doc/ doc/_build"
[tool.hatch.envs.serve]
extra-dependencies = [
"streamlit",
"streamlit-analytics2",
# "streamlit-analytics2",
]

[tool.hatch.envs.serve.scripts]
Expand Down
69 changes: 16 additions & 53 deletions src/energy_forecast/dashboard/pages/2_Production.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,15 @@
"""
from energy_forecast.energy import ECO2MixDownloader
from energy_forecast import ROOT_DIR
from energy_forecast.meteo import get_region_sun, get_region_wind, memory
from energy_forecast.meteo import ArpegeSimpleAPI, memory
from energy_forecast.enr_production_model import ENRProductionModel
import pandas as pd
import streamlit as st
import altair as alt
alt.renderers.set_embed_options(time_format_locale="fr-FR", format_locale="fr-FR")

@memory.cache
def compute_data_pv_power(date: str)->pd.DataFrame:
"""Estimate the power generated by solar panels.
Use the data from :func:`get_region_sun` and a linear model to estimate the power generated by solar panels.
Parameters
----------
date : str
The date at which the power is estimated.
Returns
-------
pd.DataFrame
The power generated by solar panels for each hour of the day.
The columns are ["time", "pv_power"]
"""
sun_data = get_region_sun(date)
sun_data.columns = [
c.replace(" ", "_").replace("'", "_").replace("-", "_").lower()
for c in sun_data.columns
]
model_params = pd.read_csv(ROOT_DIR / "data/silver/sun_model_2_params.csv")
model_params.columns = ["region", "coef"]
model_params_ser = model_params.set_index("region").iloc[:, 0]
production = sun_data * model_params_ser
pv_power = production.sum(axis=1).to_frame() / 24
pv_power.reset_index(inplace=True)
pv_power.columns = ["time", "pv_power"]
return pv_power

@memory.cache
def compute_data_eolien(date)->pd.DataFrame:
def compute_my_prodiction(date)->pd.DataFrame:
"""Estimate the power generated by eolien.
Use the data from :func:`get_region_swind` and a linear model to estimate the power generated by wind turbines.
Expand All @@ -59,20 +29,12 @@ def compute_data_eolien(date)->pd.DataFrame:
The power generated by solar panels for each hour of the day.
The columns are ["time", "eolien_power"]
"""
wind_data = get_region_wind(date)
wind_data.columns = [
c.replace(" ", "_").replace("'", "_").replace("-", "_").lower()
for c in wind_data.columns
]
model_params = pd.read_csv(ROOT_DIR / "data/silver/wind_model_2_params.csv")
model_params.columns = ["region", "coef"]
model_params_ser = model_params.set_index("region").iloc[:, 0]
production = wind_data * model_params_ser
eolien_power = production.sum(axis=1).to_frame() / 24
eolien_power.reset_index(inplace=True)
eolien_power.columns = ["time", "eolien_power"]
return eolien_power

wind_data = ArpegeSimpleAPI(date).departement_wind()
sun_data = ArpegeSimpleAPI(date).departement_sun()
my_model = ENRProductionModel.load(filename="model_departements.pkl")

predictions = my_model.predict(sun_data, wind_data)
return predictions

@memory.cache
def compute_energy(date:str):
Expand Down Expand Up @@ -101,8 +63,8 @@ def compute_energy(date:str):
- ``"Eolien Prediction"``: the wind power production prediction (from linear model using weather date)
"""
pv_power = compute_data_pv_power(date)
eolien_power = compute_data_eolien(date)
predictions = compute_my_prodiction(date)

current_year = pd.Timestamp(date).year
r = ECO2MixDownloader(year=current_year)
r.download()
Expand All @@ -116,9 +78,9 @@ def compute_energy(date:str):
energy.index = energy.index.tz_localize("Europe/Paris").tz_convert("UTC").tz_localize(None)
energy = pd.concat([
energy,
pv_power.set_index("time").rename(columns={"pv_power": "PV Prediction"}),
eolien_power.set_index("time").rename(columns={"eolien_power": "Eolien Prediction"})
], axis=1).reset_index()
predictions.rename(columns={"sun": "PV Prediction",
"wind": "Eolien Prediction"})
], axis=1).reset_index(names="time")

return energy

Expand All @@ -129,6 +91,7 @@ def compute_energy(date:str):
# date = pd.Timestamp.now().strftime("%Y-%m-%d")
energy = compute_energy(date_input)
# keep only full hours, needed to plot the data without empty spaces
print(energy)
energy = energy[energy["time"].dt.minute == 0]

st.title("Prévision de production ENR")
Expand Down Expand Up @@ -162,4 +125,4 @@ def compute_energy(date:str):
)
st.altair_chart(sun_chart,
use_container_width=True
)
)

0 comments on commit 943ecdb

Please sign in to comment.