From 9e0e2ff7362989b2267702d20dfc013bc753f78b Mon Sep 17 00:00:00 2001 From: Jactus Date: Tue, 15 Jun 2021 14:46:31 +0800 Subject: [PATCH 1/3] Update QlibRecorder wrapper --- qlib/workflow/__init__.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/qlib/workflow/__init__.py b/qlib/workflow/__init__.py index 2b2535edc..7652ff709 100644 --- a/qlib/workflow/__init__.py +++ b/qlib/workflow/__init__.py @@ -525,14 +525,33 @@ class QlibRecorder: self.get_exp().get_recorder().set_tags(**kwargs) +# error type for reinitialization when starting an experiment +class RecorderInitializationError(Exception): + def __init__(self, message): + super(RecorderInitializationError, self).__init__(message) + + +class RecorderWrapper(Wrapper): + """ + Wrapper class for QlibRecorder, which detects whether users reinitialize qlib when already starting an experiment. + """ + + def register(self, provider): + if self._provider is not None: + raise RecorderInitializationError( + "Please don't reinitialize Qlib if QlibRecorder is already acivated. Otherwise, the experiment stored location will be modified." + ) + self._provider = provider + + import sys if sys.version_info >= (3, 9): from typing import Annotated - QlibRecorderWrapper = Annotated[QlibRecorder, Wrapper] + QlibRecorderWrapper = Annotated[QlibRecorder, RecorderWrapper] else: QlibRecorderWrapper = QlibRecorder # global record -R: QlibRecorderWrapper = Wrapper() +R: QlibRecorderWrapper = RecorderWrapper() From 64582e9d4622a7f1f6b1a16a67099e289c03e1b9 Mon Sep 17 00:00:00 2001 From: Jactus Date: Tue, 15 Jun 2021 15:02:11 +0800 Subject: [PATCH 2/3] Add QlibException --- qlib/utils/exceptions.py | 12 ++++++++++++ qlib/workflow/__init__.py | 7 +------ 2 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 qlib/utils/exceptions.py diff --git a/qlib/utils/exceptions.py b/qlib/utils/exceptions.py new file mode 100644 index 000000000..69712172b --- /dev/null +++ b/qlib/utils/exceptions.py @@ -0,0 +1,12 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +# Base exception class +class QlibException(Exception): + def __init__(self, message): + super(QlibException, self).__init__(message) + + +# Error type for reinitialization when starting an experiment +class RecorderInitializationError(QlibException): + pass diff --git a/qlib/workflow/__init__.py b/qlib/workflow/__init__.py index 7652ff709..e5cdbb71c 100644 --- a/qlib/workflow/__init__.py +++ b/qlib/workflow/__init__.py @@ -7,6 +7,7 @@ from .expm import MLflowExpManager from .exp import Experiment from .recorder import Recorder from ..utils import Wrapper +from ..utils.exceptions import RecorderInitializationError class QlibRecorder: @@ -525,12 +526,6 @@ class QlibRecorder: self.get_exp().get_recorder().set_tags(**kwargs) -# error type for reinitialization when starting an experiment -class RecorderInitializationError(Exception): - def __init__(self, message): - super(RecorderInitializationError, self).__init__(message) - - class RecorderWrapper(Wrapper): """ Wrapper class for QlibRecorder, which detects whether users reinitialize qlib when already starting an experiment. From 0fe8b281ba7d4e1269d8d273196a853a1aa686a2 Mon Sep 17 00:00:00 2001 From: Jactus Date: Wed, 16 Jun 2021 12:28:20 +0800 Subject: [PATCH 3/3] Update R wrapper logic --- qlib/workflow/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/qlib/workflow/__init__.py b/qlib/workflow/__init__.py index e5cdbb71c..98b2c9925 100644 --- a/qlib/workflow/__init__.py +++ b/qlib/workflow/__init__.py @@ -533,9 +533,11 @@ class RecorderWrapper(Wrapper): def register(self, provider): if self._provider is not None: - raise RecorderInitializationError( - "Please don't reinitialize Qlib if QlibRecorder is already acivated. Otherwise, the experiment stored location will be modified." - ) + expm = getattr(self._provider, "exp_manager") + if expm.active_experiment is not None: + raise RecorderInitializationError( + "Please don't reinitialize Qlib if QlibRecorder is already acivated. Otherwise, the experiment stored location will be modified." + ) self._provider = provider