From ff95776cc003b4c6305ede64b1444059609e3675 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 10 Feb 2025 14:34:04 +0100 Subject: [PATCH] fix(qav3): switch to file backend optuna storage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../LightGBMRegressorQuickAdapterV35.py | 14 ++++++++++++-- .../XGBoostRegressorQuickAdapterV35.py | 12 ++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/quickadapter/user_data/freqaimodels/LightGBMRegressorQuickAdapterV35.py b/quickadapter/user_data/freqaimodels/LightGBMRegressorQuickAdapterV35.py index 027412e..cbed34a 100644 --- a/quickadapter/user_data/freqaimodels/LightGBMRegressorQuickAdapterV35.py +++ b/quickadapter/user_data/freqaimodels/LightGBMRegressorQuickAdapterV35.py @@ -10,6 +10,7 @@ import scipy as spy import optuna import sklearn import warnings +import re N_TRIALS = 36 TEST_SIZE = 0.1 @@ -67,7 +68,7 @@ class LightGBMRegressorQuickAdapterV35(BaseRegressionModel): start = time.time() if self.__optuna_hyperopt: - study_name = dk.pair + study_name = str(dk.pair) storage_dir = str(dk.full_path).rsplit("/", 1) pruner = optuna.pruners.HyperbandPruner() study = optuna.create_study( @@ -78,7 +79,11 @@ class LightGBMRegressorQuickAdapterV35(BaseRegressionModel): ), pruner=pruner, direction=optuna.study.StudyDirection.MINIMIZE, - storage=f"sqlite:///{storage_dir}/optuna-lgbm.sqlite", + storage=optuna.storages.JournalStorage( + optuna.storages.journal.JournalFileBackend( + f"{storage_dir}/optuna-lgbm-{sanitize_path(study_name)}.log" + ) + ), load_if_exists=True, ) study.optimize( @@ -334,3 +339,8 @@ def hp_objective(trial, X, y, train_weights, X_test, y_test, test_weights, param error = sklearn.metrics.root_mean_squared_error(y_test, y_pred) return error + + +def sanitize_path(path: str) -> str: + allowed = re.compile(r"[^A-Za-z0-9 _\-\.\(\)]") + return allowed.sub("_", path) diff --git a/quickadapter/user_data/freqaimodels/XGBoostRegressorQuickAdapterV35.py b/quickadapter/user_data/freqaimodels/XGBoostRegressorQuickAdapterV35.py index 9bc07eb..823b590 100644 --- a/quickadapter/user_data/freqaimodels/XGBoostRegressorQuickAdapterV35.py +++ b/quickadapter/user_data/freqaimodels/XGBoostRegressorQuickAdapterV35.py @@ -10,6 +10,7 @@ import scipy as spy import optuna import sklearn import warnings +import re N_TRIALS = 36 TEST_SIZE = 0.1 @@ -67,7 +68,7 @@ class XGBoostRegressorQuickAdapterV35(BaseRegressionModel): start = time.time() if self.__optuna_hyperopt: - study_name = dk.pair + study_name = str(dk.pair) storage_dir = str(dk.full_path).rsplit("/", 1) pruner = optuna.pruners.HyperbandPruner() study = optuna.create_study( @@ -78,7 +79,9 @@ class XGBoostRegressorQuickAdapterV35(BaseRegressionModel): ), pruner=pruner, direction=optuna.study.StudyDirection.MINIMIZE, - storage=f"sqlite:///{storage_dir}/optuna-xgboost.sqlite", + storage=optuna.storages.journal.JournalFileBackend( + f"{storage_dir}/optuna-xgboost-{sanitize_path(study_name)}.log" + ), load_if_exists=True, ) study.optimize( @@ -342,3 +345,8 @@ def hp_objective(trial, X, y, train_weights, X_test, y_test, test_weights, param error = sklearn.metrics.root_mean_squared_error(y_test, y_pred) return error + + +def sanitize_path(path: str) -> str: + allowed = re.compile(r"[^A-Za-z0-9 _\-\.\(\)]") + return allowed.sub("_", path) -- 2.43.0