Skip to content

Commit 779b178

Browse files
you-n-gDong Zhou
andauthored
Merging Backtest improve (#694)
* Fix private import * temporarily fix create exp conflicts for remote mlflow Co-authored-by: Dong Zhou <Zhou.Dong@microsoft.com>
1 parent 007082a commit 779b178

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

qlib/utils/exceptions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ class LoadObjectError(QlibException):
1818
"""Error type for Recorder when can not load object"""
1919

2020
pass
21+
22+
23+
class ExpAlreadyExistError(Exception):
24+
"""Experiment already exists"""
25+
26+
pass

qlib/workflow/expm.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from urllib.parse import urlparse
55
import mlflow
66
from filelock import FileLock
7-
from mlflow.exceptions import MlflowException
7+
from mlflow.exceptions import MlflowException, RESOURCE_ALREADY_EXISTS, ErrorCode
88
from mlflow.entities import ViewType
99
import os, logging
1010
from pathlib import Path
@@ -15,6 +15,7 @@
1515
from ..config import C
1616
from .recorder import Recorder
1717
from ..log import get_module_logger
18+
from ..utils.exceptions import ExpAlreadyExistError
1819

1920
logger = get_module_logger("workflow", logging.INFO)
2021

@@ -94,6 +95,10 @@ def create_exp(self, experiment_name: Optional[Text] = None):
9495
Returns
9596
-------
9697
An experiment object.
98+
99+
Raise
100+
-----
101+
ExpAlreadyExistError
97102
"""
98103
raise NotImplementedError(f"Please implement the `create_exp` method.")
99104

@@ -200,7 +205,14 @@ def _get_or_create_exp(self, experiment_id=None, experiment_name=None) -> (objec
200205
if pr.scheme == "file":
201206
with FileLock(os.path.join(pr.netloc, pr.path, "filelock")) as f:
202207
return self.create_exp(experiment_name), True
203-
return self.create_exp(experiment_name), True
208+
# NOTE: for other schemes like http, we double check to avoid create exp conflicts
209+
try:
210+
return self.create_exp(experiment_name), True
211+
except ExpAlreadyExistError:
212+
return (
213+
self._get_exp(experiment_id=experiment_id, experiment_name=experiment_name),
214+
False,
215+
)
204216

205217
def _get_exp(self, experiment_id=None, experiment_name=None) -> Experiment:
206218
"""
@@ -345,10 +357,15 @@ def end_exp(self, recorder_status: Text = Recorder.STATUS_S):
345357
def create_exp(self, experiment_name: Optional[Text] = None):
346358
assert experiment_name is not None
347359
# init experiment
348-
experiment_id = self.client.create_experiment(experiment_name)
360+
try:
361+
experiment_id = self.client.create_experiment(experiment_name)
362+
except MlflowException as e:
363+
if e.error_code == ErrorCode.Name(RESOURCE_ALREADY_EXISTS):
364+
raise ExpAlreadyExistError()
365+
raise e
366+
349367
experiment = MLflowExperiment(experiment_id, experiment_name, self.uri)
350368
experiment._default_name = self._default_exp_name
351-
352369
return experiment
353370

354371
def _get_exp(self, experiment_id=None, experiment_name=None):

0 commit comments

Comments
 (0)