forked from nique_372/AiDataGenByLeo
82 lines
No EOL
3 KiB
Python
82 lines
No EOL
3 KiB
Python
# Copyright 2026, Niquel Mendoza | Leo.
|
|
# https://www.mql5.com/es/users/nique_372
|
|
# comunicator.py
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| Imports |
|
|
#+------------------------------------------------------------------+
|
|
import sys
|
|
import os
|
|
import json
|
|
from pathlib import Path
|
|
import numpy as np
|
|
|
|
from PyBase.Utils import SimpleLogger, Funciones
|
|
|
|
#+------------------------------------------------------------------+
|
|
#| |
|
|
#+------------------------------------------------------------------+
|
|
class CMqlComunication(SimpleLogger.CLoggerBase):
|
|
def __init__(self, config : dict):
|
|
super().__init__()
|
|
|
|
# Path
|
|
self.m_path_json : str = config["path_json"]
|
|
self.m_path_bin : str = config["path_bin"]
|
|
|
|
#
|
|
self.m_dict : dict = {}
|
|
|
|
@staticmethod
|
|
def _sanitize_metrics(metrics: dict) -> dict:
|
|
result = {}
|
|
for k, v in metrics.items():
|
|
if isinstance(v, (bool, np.bool_)):
|
|
result[k] = bool(v)
|
|
elif isinstance(v, str):
|
|
result[k] = v
|
|
elif isinstance(v, (int, float)):
|
|
result[k] = v
|
|
elif isinstance(v, np.integer):
|
|
result[k] = int(v)
|
|
elif isinstance(v, np.floating):
|
|
result[k] = float(round(v, 6))
|
|
# todo lo demás se omite
|
|
return result
|
|
|
|
def SendRegresion(self, path_relative_model : str , metrics : dict) -> None:
|
|
self.m_dict = {}
|
|
self.m_dict["metrics"] = self._sanitize_metrics(metrics)
|
|
self.m_dict["type"] = "Regresion"
|
|
self.m_dict["path"] = path_relative_model
|
|
|
|
try:
|
|
Path(self.m_path_json).parent.mkdir(parents=True, exist_ok=True) # creamos dir intermediarios
|
|
with open(self.m_path_json, "w", encoding="utf-16-le") as f:
|
|
json.dump(self.m_dict, f)
|
|
|
|
# Creamos el archivo bin para marcar exito
|
|
Path(self.m_path_bin).touch()
|
|
|
|
except Exception as e:
|
|
self.LogError(f"Fallo escribir en json: {e}")
|
|
|
|
def SendClasificacion(self, path_relative_model : str , metrics : dict) -> None:
|
|
self.m_dict = {}
|
|
self.m_dict["metrics"] = self._sanitize_metrics(metrics)
|
|
self.m_dict["type"] = "Clasification"
|
|
self.m_dict["path"] = path_relative_model
|
|
|
|
try:
|
|
Path(self.m_path_json).parent.mkdir(parents=True, exist_ok=True) # creamos dir intermediarios
|
|
with open(self.m_path_json, "w", encoding="utf-16-le") as f:
|
|
json.dump(self.m_dict, f)
|
|
Path(self.m_path_bin).touch()
|
|
|
|
except Exception as e:
|
|
self.LogError(f"Fallo escribir en json: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("hola mundo")
|
|
|