-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval_model.py
379 lines (315 loc) · 15.1 KB
/
eval_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# -*- coding: utf-8 -*-
# =============================================================================
__author__ = "Brayan Felipe Zapata "
__copyright__ = "Copyright 2007, The Cogent Project"
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Brayan Felipe Zapata"
__email__ = "bzapata@smgsoftware.com"
__status__ = "Production"
# =============================================================================
"""Codigo para ejecutar un entrenamiento completo de una base de datos """
# =============================================================================
import os
import re
import yaml
import numpy as np
import pandas as pd
import logging
from datetime import datetime
from dateutil.relativedelta import relativedelta
from scipy import stats, signal
from pathlib import Path
from src.lib.class_load import LoadFiles
from src.data.save_models import SAVE_DIR
from src.lib.utils import select_best_model
from src.lib.factory_data import SQLDataSourceFactory, get_data, create_table, set_data
from src.lib.factory_models import ModelContext
from src.lib.factory_prepare_data import (
DataCleaner,
DataModel,
MeanImputation,
OutliersToIQRMean,
PrepareDtypeColumns,
base_dtypes
)
from src.models.DP_model import Modelos
from src.features.features_redis import HandleRedis
from src.features.features_postgres import HandleDBpsql
from src.models.args_data_model import Parameters
from src.data.logs import LOGS_DIR
path_folder = os.path.dirname(__file__)
folder_model = Path(path_folder).joinpath("scr/data/save_models")
handler_load = LoadFiles()
handler_redis = HandleRedis()
data_source = HandleDBpsql()
ruta_actual = os.path.dirname(__file__)
# =================================================================
# Configuracion Logger
# =================================================================
# Configura un logger personalizado en lugar de usar el logger raíz
logfile = ruta_actual + "/src/data/config/logging.conf"
logging.config.fileConfig(os.path.join(LOGS_DIR, logfile))
logger = logging.getLogger("predict")
logger.debug("Inciando secuencia de entrenamiento")
# =================================================================
# Cargar los parametros
# =================================================================
CONFIG_FILE = ruta_actual + "/src/data/config/config.yaml"
with open(CONFIG_FILE, "r", encoding="utf-8") as file:
parameters = yaml.safe_load(file)
logger.debug("Archivo de configuraciones cargado")
parametros = Parameters(**parameters)
schema = schema = parametros.connection_data_source['postgresql']['options'].split(',')[
1]
# =================================================================
# Cargar datos de la fuente de datos
# =================================================================
# Interacion para hacer un cache de los datos en redis
try:
logger.debug("verficando si existe data cache")
data = handler_redis.get_cache_data(
hash_name=parametros.query_template["table"],
config=parametros.connection_data_source,
)
# Condicional para actualizar datos en caso de existan datos en redis
if data is not None:
logger.debug("Existe data en cache")
# Secuencia de codigo para perdir nuevos datos a la base de datos
date_col_query = parameters["query_template"]["columns"]["0"]
LAST_DAY = str(data.iloc[-1][0])
parameters["query_template"]["where"] = f" \"{date_col_query}\" > '{LAST_DAY}'"
parameters["query_template"]["order"] = "".join(
['"' + columna + '"' for columna in [date_col_query]]
)
logger.debug("Realizando peticion a la fuente de datos")
# Extraccion de la nueva data para actualizar
new = get_data(SQLDataSourceFactory(**parameters))
logger.debug("Actualizando cache en redis")
data = handler_redis.set_cache_data(
hash_name=parametros.query_template["table"],
old_dataframe=data,
new_dataframe=new,
exp_time=parametros.exp_time_cache,
config=parametros.connection_data_source,
)
logger.debug("Actualizacion completa de datos en redis")
# Verificar que existieran datos en cache
if data is None:
logger.debug("No existe cache de datos")
data = get_data(SQLDataSourceFactory(**parameters))
logger.debug("Insertando datos de cache en redis")
data = handler_redis.set_cache_data(
hash_name=parametros.query_template["table"],
old_dataframe=data,
new_dataframe=None,
exp_time=parametros.exp_time_cache,
config=parametros.connection_data_source,
)
except ValueError as error:
logger.debug("[ERROR] No se puede hacer un cache de la fuente de datos")
logger.debug(error)
exit()
# =================================================================
# Limpieza de datos
# =================================================================
new_types = []
for dtypo in parameters["type_data"].values():
new_types.append(base_dtypes[dtypo])
# metodo para transformar los tipo de datos
strategy = {int: np.mean, float: np.mean, object: stats.mode}
# Estrategias para imputar los datos faltantes de NA
replace = {
int: lambda x: int(float(x.replace(",", ""))),
float: lambda x: float(x.replace(",", "")),
object: lambda x: x.strip(),
}
# =================================================================
update_dtype_columns = PrepareDtypeColumns(
replace_dtypes=new_types,
strategy_imputation=strategy,
preprocess_function=replace,
**parameters,
)
# Ejecucion de fabrica para aplicar y ordenar los tipos de datos y los valores
cleaner = DataCleaner()
cleaner.strategy = update_dtype_columns
data_ = cleaner.clean(data)
# Condicion de filtrado para informacion segun los valores
filter_label: str = parameters["filter_data"]["filter_1_feature"]
filter_col: str = parameters["filter_data"]["filter_1_column"]
filter_product = data_.dataframe[filter_col] == filter_label
filter_data = data_.dataframe[filter_product].sort_values(
by=parameters["filter_data"]["date_column"])
# Segmento de codigo para filtrado del datos obsoletos
filte_date_col: str = parameters["filter_data"]["date_column"]
filter_data['year'] = filter_data[filte_date_col].dt.year
year = filter_data.groupby('year').size().to_frame()
consecutive_year = year.reset_index()['year'].diff().to_frame()
index_gap_year = consecutive_year[consecutive_year['year'] > 1]
# Condicional para verificar que saltos de tiempo en anos
if not index_gap_year.empty:
index_gap_year = index_gap_year.sort_values(
by='year', ascending=False).head(1).index.values[0]
remove_year_before = year.reset_index()['year'].iloc[index_gap_year]
filter_data = filter_data[filter_data['year'] >= remove_year_before]
outliners = OutliersToIQRMean(**parameters)
cleaner.strategy = outliners
outlines_data = cleaner.clean(filter_data)
# validate_outlines = cleaner.clean(validate_data)
# Filtrado de datos para eliminar valores negativos
filter_values = outlines_data["quantity"] <= 0
outlines_data[filter_values] = 0.1
# =================================================================
# Filtro pasabajos
# =================================================================
fs = 1 / 24 / 3600 # 1 day in Hz (sampling frequency)
nyquist = fs / 0.5 # 2 # 0.5 times the sampling frequency
cutoff = 0.5 # 0.1 fraction of nyquist frequency, here it is 5 days
# cutoff= 4.999999999999999 days
b, a = signal.butter(5, cutoff, btype="lowpass") # low pass filter
dUfilt = signal.filtfilt(b, a, outlines_data["quantity"])
dUfilt = np.array(dUfilt)
dUfilt = dUfilt.transpose()
outlines_data["low_past"] = dUfilt
# =================================================================
# Preparacion de datos para el modelo
# =================================================================
data_for_model = DataModel(**parameters)
cleaner.strategy = data_for_model
data_ready, scaler_data = cleaner.clean(outlines_data)
# Creacion del dataframe para del filtro pasa bajo para los datos
low_pass_data = outlines_data["low_past"]
low_pass_data = low_pass_data.to_frame()
low_pass_data.rename(columns={"low_past": "quantity"}, inplace=True)
data_ready_lp, scaler_data_lp = cleaner.clean(low_pass_data)
validation_months = parametros.validation_months
last_month_data = data_ready_lp.pd_dataframe().tail(1).index.values[0]
last_month_data = pd.Timestamp(last_month_data)
backtrack_date = last_month_data - relativedelta(months=validation_months)
data_prediction_cov, data_test_cov = data_ready_lp.split_after(
pd.Timestamp(backtrack_date))
data_prediction, data_test = data_ready.split_after(
pd.Timestamp(backtrack_date))
# =================================================================
# Cargar modelo
# =================================================================
# Rutas de los parametros para predicciones
save_dir = Path(SAVE_DIR).joinpath(
parameters["filter_data"]["filter_1_feature"])
models_metrics = save_dir.joinpath(
"train_metrics").with_suffix(".json").as_posix()
MODE_USED = select_best_model(models_metrics)
scaler_name = save_dir.joinpath("scaler").with_suffix(".pkl").as_posix()
scaler_lp_name = save_dir.joinpath("scaler_lp").with_suffix(".pkl").as_posix()
last_pred = save_dir.joinpath("previus").with_suffix(".json").as_posix()
model_train = save_dir.joinpath(
f"model_{MODE_USED}").with_suffix(".pt").as_posix()
parameters_model = save_dir.joinpath(
f"parametros_{MODE_USED}").with_suffix(".json").as_posix()
modelo = ModelContext(model_name=MODE_USED,
data=data_ready,
split=83,
covarianze=data_ready_lp,
** parameters
)
# Cargar escaler
scaler = handler_load.load_scaler(scaler_name)
scaler_lp = handler_load.load_scaler(scaler_lp_name)
# Cargar modelo para hacer las predicciones
IntModel = Modelos[MODE_USED]
trained_parameters = handler_load.json_to_dict(json_file=parameters_model)[0]
model_update_parameters = IntModel(**trained_parameters)
model_trained = model_update_parameters.load(model_train)
# Variable que almacena ultimo valor de la serie de tiempo para luego
# adjuntarle los valores de las predicciones, y poder graficar las predicciones
data_prediction, data_test = data_ready.split_after(
pd.Timestamp(backtrack_date))
plot_time_series = data_prediction.tail(1)
for i in range(validation_months):
forward = backtrack_date+relativedelta(months=i)
data_prediction_cov, data_test_cov = data_ready_lp.split_after(
pd.Timestamp(forward))
data_prediction, data_test = data_ready.split_after(
pd.Timestamp(forward))
past_pred, future_pred = modelo.validation(model=model_trained,
data=data_prediction,
past_cov=data_prediction_cov)
plot_time_series = plot_time_series.append(future_pred)
# =================================================================
# inverser transform past
# =================================================================
# Invertir predicciones escaler de entrenamietno
pred_scale = scaler.inverse_transform(past_pred)
data_frame_predicciones = pred_scale.pd_dataframe()
column_field = list(data_frame_predicciones.columns)
data_frame_predicciones.reset_index(inplace=True)
data_frame_predicciones[parameters["filter_data"]
["predict_column"]].clip(lower=0, inplace=True)
# =================================================================
# inverse transform past
# =================================================================
# Invertir predicciones escaler de entrenamietno
pred_scale_future = scaler.inverse_transform(plot_time_series)
dataframe_pred_future = pred_scale_future.pd_dataframe()
column_field = list(dataframe_pred_future.columns)
dataframe_pred_future.reset_index(inplace=True)
dataframe_pred_future[parameters["filter_data"]
["predict_column"]].clip(lower=0, inplace=True)
dataframe_pred_future = dataframe_pred_future[1:]
outlines_data.reset_index(inplace=True)
outlines_data['low_past'] = filter_label
outlines_data.rename({"low_past": filter_col}, axis="columns", inplace=True)
# =================================================================
# create send data DB eval predictions
# =================================================================
date_col = parameters["filter_data"]["date_column"]
data_col = parameters["filter_data"]["predict_column"]
data_frame_predicciones['tag'] = 'historical'
data_frame_predicciones['code'] = filter_label
filter_time_col: str = parameters["filter_data"]["date_column"]
data_frame_predicciones.rename(
{"time": filter_time_col}, axis="columns", inplace=True)
# Modificar registros de predicciones pasadas
mask = (outlines_data[date_col] >= data_frame_predicciones[date_col].min()) & (
outlines_data[date_col] <= data_frame_predicciones[date_col].max())
sub_index_df = outlines_data.loc[mask]
for i, val in data_frame_predicciones.iterrows():
i = i-1
error_c = np.abs(
data_frame_predicciones[data_col].iloc[i] - sub_index_df[data_col].iloc[i])
error_p = (np.abs(data_frame_predicciones[data_col].iloc[i] -
sub_index_df[data_col].iloc[i]) / data_frame_predicciones[data_col].iloc[i]) * 100
data_frame_predicciones.loc[i+1, 'error'] = round(error_c, 2)
data_frame_predicciones.loc[i+1, 'error_per'] = round(error_p, 2)
# Crear tabla para datos
parameters["query_template_write"]["table"] = "evaluacion_modelos"
parameters["query_template_write"]["columns"]["0"] = date_col
parameters["query_template_write"]["columns"]["1"] = data_col
parameters["query_template_write"]["columns"]["2"] = "tag"
parameters["query_template_write"]["columns"]["3"] = "code"
parameters["query_template_write"]["columns"]["4"] = "error"
parameters["query_template_write"]["columns"]["5"] = "error_per"
parameters["type_data_out"] = {
date_col: "date", data_col: "float", "tag": "string", "code": "string", "error": "float", "error_per": "float"}
# Crear tabla para guardas la informacion
logger.debug(
"Creando tabla agrupacion de datos reales semanales caso de ser necesario")
create_table(SQLDataSourceFactory(**parameters))
# Modificar registros de predicciones futuras
dataframe_pred_future['tag'] = 'future'
dataframe_pred_future['code'] = filter_label
mask_future = (outlines_data[date_col] >= dataframe_pred_future[date_col].min()) & (
outlines_data[date_col] <= dataframe_pred_future[date_col].max())
sub_index_df_future = outlines_data.loc[mask_future]
for i, val in dataframe_pred_future.iterrows():
i = i-1
error_c = np.abs(
dataframe_pred_future[data_col].iloc[i] - sub_index_df_future[data_col].iloc[i])
error_p = (np.abs(dataframe_pred_future[data_col].iloc[i] -
sub_index_df_future[data_col].iloc[i]) / dataframe_pred_future[data_col].iloc[i]) * 100
dataframe_pred_future.loc[i+1, 'error'] = round(error_c, 2)
dataframe_pred_future.loc[i+1, 'error_per'] = round(error_p, 2)
# Escribir en DB
set_data(SQLDataSourceFactory(**parameters), data_frame_predicciones)
set_data(SQLDataSourceFactory(**parameters), dataframe_pred_future)