Update pipelineFinal.py
Browse files- pipelineFinal.py +138 -130
pipelineFinal.py
CHANGED
|
@@ -1,130 +1,138 @@
|
|
| 1 |
-
from calendar import c
|
| 2 |
-
from os import pipe
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import pickle
|
| 5 |
-
from skforecast.utils import load_forecaster
|
| 6 |
-
from filterdf import filter_datasets
|
| 7 |
-
from mergedf import merging_datasets
|
| 8 |
-
import numpy as np
|
| 9 |
-
import plotly.graph_objects as go
|
| 10 |
-
|
| 11 |
-
def load_csv(input_file):
|
| 12 |
-
try:
|
| 13 |
-
# Leer el archivo CSV
|
| 14 |
-
df = pd.read_csv(input_file)
|
| 15 |
-
|
| 16 |
-
# Verificar si el DataFrame está vacío
|
| 17 |
-
if df.empty:
|
| 18 |
-
raise ValueError("El archivo subido está vacío o no tiene datos válidos.")
|
| 19 |
-
|
| 20 |
-
# Retornar las primeras 5 filas como tabla HTML
|
| 21 |
-
# return df.head().to_html()
|
| 22 |
-
return df
|
| 23 |
-
except Exception as e:
|
| 24 |
-
raise f"Error al cargar el archivo CSV:{e}"
|
| 25 |
-
|
| 26 |
-
def load_model(name):
|
| 27 |
-
|
| 28 |
-
model = load_forecaster(name,verbose=True)
|
| 29 |
-
return model
|
| 30 |
-
|
| 31 |
-
def load_pipeline():
|
| 32 |
-
with open('pipeline.pkl', 'rb') as file:
|
| 33 |
-
pipeline = pickle.load(file)
|
| 34 |
-
return pipeline
|
| 35 |
-
|
| 36 |
-
def unscale_data(scaler, predictions):
|
| 37 |
-
placeholder = np.zeros((len(predictions), 11))
|
| 38 |
-
placeholder[:, 0] = predictions['target']
|
| 39 |
-
predictions_scaled = scaler.inverse_transform(placeholder)[:, 0]
|
| 40 |
-
predictions_scaled[predictions_scaled < 0] = 0
|
| 41 |
-
predictions = pd.DataFrame(predictions_scaled, columns=predictions.columns, index=predictions.index)
|
| 42 |
-
return predictions
|
| 43 |
-
|
| 44 |
-
def create_plots(predictions):
|
| 45 |
-
# Gráfico de las predicciones vs valores reales en el conjunto de test del modelo con mejores parametros
|
| 46 |
-
fig = go.Figure()
|
| 47 |
-
trace2 = go.Scatter(x=predictions.index, y=predictions['target'], name="Estimado", mode="lines", line_color="#4EA72E")
|
| 48 |
-
fig.add_trace(trace2)
|
| 49 |
-
fig.update_layout(
|
| 50 |
-
yaxis_title="Producción (kWh)",
|
| 51 |
-
width=750,
|
| 52 |
-
height=350,
|
| 53 |
-
margin=dict(l=20, r=0, t=35, b=20),
|
| 54 |
-
legend=dict(
|
| 55 |
-
orientation="v",
|
| 56 |
-
yanchor="top",
|
| 57 |
-
xanchor="right",
|
| 58 |
-
x=0.99,
|
| 59 |
-
y=0.99
|
| 60 |
-
)
|
| 61 |
-
)
|
| 62 |
-
return fig
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def pipeline_final(texto,steps,train=None,client=None,historical_weather=None,electricity_prices=None,gas_prices=None):
|
| 66 |
-
#prueba
|
| 67 |
-
#texto = 'No'
|
| 68 |
-
# #steps
|
| 69 |
-
# steps = 24
|
| 70 |
-
# #dfs
|
| 71 |
-
|
| 72 |
-
# train = 'files_prueba/train_filtered.csv'
|
| 73 |
-
# client = 'files_prueba/client_filtered.csv'
|
| 74 |
-
# historical_weather = 'files_prueba/historical_weather_filtered.csv'
|
| 75 |
-
# electricity_prices = 'files_prueba/electricity_prices_filtered.csv'
|
| 76 |
-
# gas_prices = 'files_prueba/gas_prices_filtered.csv'
|
| 77 |
-
pipeline = load_pipeline()
|
| 78 |
-
scaler = pipeline['scale']
|
| 79 |
-
|
| 80 |
-
#load model
|
| 81 |
-
model = load_model('LSTM_forecaster.joblib')
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
if texto == 'Si':
|
| 85 |
-
pred = model.predict(steps=steps)
|
| 86 |
-
|
| 87 |
-
pred = unscale_data(scaler, pred)
|
| 88 |
-
|
| 89 |
-
pred_reset = pred.reset_index(drop=False)
|
| 90 |
-
|
| 91 |
-
pred_reset = pred_reset.astype(str)
|
| 92 |
-
|
| 93 |
-
pred_reset = pred_reset.rename(columns={'index': 'fecha'})
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
#
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
pred_reset =
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from calendar import c
|
| 2 |
+
from os import pipe
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pickle
|
| 5 |
+
from skforecast.utils import load_forecaster
|
| 6 |
+
from filterdf import filter_datasets
|
| 7 |
+
from mergedf import merging_datasets
|
| 8 |
+
import numpy as np
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
+
|
| 11 |
+
def load_csv(input_file):
|
| 12 |
+
try:
|
| 13 |
+
# Leer el archivo CSV
|
| 14 |
+
df = pd.read_csv(input_file)
|
| 15 |
+
|
| 16 |
+
# Verificar si el DataFrame está vacío
|
| 17 |
+
if df.empty:
|
| 18 |
+
raise ValueError("El archivo subido está vacío o no tiene datos válidos.")
|
| 19 |
+
|
| 20 |
+
# Retornar las primeras 5 filas como tabla HTML
|
| 21 |
+
# return df.head().to_html()
|
| 22 |
+
return df
|
| 23 |
+
except Exception as e:
|
| 24 |
+
raise f"Error al cargar el archivo CSV:{e}"
|
| 25 |
+
|
| 26 |
+
def load_model(name):
|
| 27 |
+
|
| 28 |
+
model = load_forecaster(name,verbose=True)
|
| 29 |
+
return model
|
| 30 |
+
|
| 31 |
+
def load_pipeline():
|
| 32 |
+
with open('pipeline.pkl', 'rb') as file:
|
| 33 |
+
pipeline = pickle.load(file)
|
| 34 |
+
return pipeline
|
| 35 |
+
|
| 36 |
+
def unscale_data(scaler, predictions):
|
| 37 |
+
placeholder = np.zeros((len(predictions), 11))
|
| 38 |
+
placeholder[:, 0] = predictions['target']
|
| 39 |
+
predictions_scaled = scaler.inverse_transform(placeholder)[:, 0]
|
| 40 |
+
predictions_scaled[predictions_scaled < 0] = 0
|
| 41 |
+
predictions = pd.DataFrame(predictions_scaled, columns=predictions.columns, index=predictions.index)
|
| 42 |
+
return predictions
|
| 43 |
+
|
| 44 |
+
def create_plots(predictions):
|
| 45 |
+
# Gráfico de las predicciones vs valores reales en el conjunto de test del modelo con mejores parametros
|
| 46 |
+
fig = go.Figure()
|
| 47 |
+
trace2 = go.Scatter(x=predictions.index, y=predictions['target'], name="Estimado", mode="lines", line_color="#4EA72E")
|
| 48 |
+
fig.add_trace(trace2)
|
| 49 |
+
fig.update_layout(
|
| 50 |
+
yaxis_title="Producción (kWh)",
|
| 51 |
+
width=750,
|
| 52 |
+
height=350,
|
| 53 |
+
margin=dict(l=20, r=0, t=35, b=20),
|
| 54 |
+
legend=dict(
|
| 55 |
+
orientation="v",
|
| 56 |
+
yanchor="top",
|
| 57 |
+
xanchor="right",
|
| 58 |
+
x=0.99,
|
| 59 |
+
y=0.99
|
| 60 |
+
)
|
| 61 |
+
)
|
| 62 |
+
return fig
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def pipeline_final(texto,steps,train=None,client=None,historical_weather=None,electricity_prices=None,gas_prices=None):
|
| 66 |
+
#prueba
|
| 67 |
+
#texto = 'No'
|
| 68 |
+
# #steps
|
| 69 |
+
# steps = 24
|
| 70 |
+
# #dfs
|
| 71 |
+
|
| 72 |
+
# train = 'files_prueba/train_filtered.csv'
|
| 73 |
+
# client = 'files_prueba/client_filtered.csv'
|
| 74 |
+
# historical_weather = 'files_prueba/historical_weather_filtered.csv'
|
| 75 |
+
# electricity_prices = 'files_prueba/electricity_prices_filtered.csv'
|
| 76 |
+
# gas_prices = 'files_prueba/gas_prices_filtered.csv'
|
| 77 |
+
pipeline = load_pipeline()
|
| 78 |
+
scaler = pipeline['scale']
|
| 79 |
+
|
| 80 |
+
#load model
|
| 81 |
+
model = load_model('LSTM_forecaster.joblib')
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if texto == 'Si':
|
| 85 |
+
pred = model.predict(steps=steps)
|
| 86 |
+
|
| 87 |
+
pred = unscale_data(scaler, pred)
|
| 88 |
+
|
| 89 |
+
pred_reset = pred.reset_index(drop=False)
|
| 90 |
+
|
| 91 |
+
pred_reset = pred_reset.astype(str)
|
| 92 |
+
|
| 93 |
+
pred_reset = pred_reset.rename(columns={'index': 'fecha'})
|
| 94 |
+
|
| 95 |
+
pred_reset = pred_reset.rename(columns={'target': 'generacion'})
|
| 96 |
+
|
| 97 |
+
pred_reset['generacion'] = pred_reset['generacion'].astype(float).round(2).astype(str)
|
| 98 |
+
|
| 99 |
+
fig = create_plots(pred)
|
| 100 |
+
|
| 101 |
+
return fig , pred_reset
|
| 102 |
+
|
| 103 |
+
else:
|
| 104 |
+
train = load_csv(train)
|
| 105 |
+
client = load_csv(client)
|
| 106 |
+
historical_weather = load_csv(historical_weather)
|
| 107 |
+
electricity_prices = load_csv(electricity_prices)
|
| 108 |
+
gas_prices = load_csv(gas_prices)
|
| 109 |
+
#filter data sets
|
| 110 |
+
filter_datasets(train,client,historical_weather,electricity_prices,gas_prices)
|
| 111 |
+
#merge data sets
|
| 112 |
+
df = merging_datasets()
|
| 113 |
+
#load pipeline
|
| 114 |
+
# sclaing the data
|
| 115 |
+
df_processed = pipeline.transform(df)
|
| 116 |
+
|
| 117 |
+
df_processed = pd.DataFrame(df_processed, columns=df.columns, index=df.index)
|
| 118 |
+
|
| 119 |
+
pred = model.predict(steps=steps, last_window=df_processed)
|
| 120 |
+
|
| 121 |
+
pred = unscale_data(scaler, pred)
|
| 122 |
+
|
| 123 |
+
pred_reset = pred.reset_index(drop=False)
|
| 124 |
+
|
| 125 |
+
pred_reset = pred_reset.astype(str)
|
| 126 |
+
|
| 127 |
+
pred_reset = pred_reset.rename(columns={'index': 'fecha'})
|
| 128 |
+
|
| 129 |
+
pred_reset = pred_reset.rename(columns={'target': 'generacion'})
|
| 130 |
+
|
| 131 |
+
pred_reset['generacion'] = pred_reset['generacion'].astype(float).round(2).astype(str)
|
| 132 |
+
|
| 133 |
+
fig = create_plots(pred)
|
| 134 |
+
|
| 135 |
+
return fig , pred_reset
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|