Repository: microsoft/qlib
File: qlib/workflow/__init__.py (Python)

Code before:
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from contextlib import contextmanager
from .expm import MLflowExpManager
from .exp import Experiment
from .recorder import Recorder
from ..utils import Wrapper


class QlibRecorder:
    """
    A global system that helps to manage the experiments.
    """

    def __init__(self, exp_manager):
        self.exp_manager = exp_manager

    def __repr__(self):
        return "{name}(manager={manager})".format(name=self.__class__.__name__, manager=self.exp_manager)

    @contextmanager
    def start(self, experiment_name=None, recorder_name=None, uri=None):
        """
        Method to start an experiment. This method can only be called within a Python's `with` statement. Here is the example code:

        .. code-block:: Python

            with R.start('test', 'recorder_1'):
                model.fit(dataset)
                R.log...
                ... # further operations

        Parameters
        ----------
        experiment_name : str
            name of the experiment one wants to start.
        recorder_name : str
            name of the recorder under the experiment one wants to start.
        uri : str
            the tracking uri of the experiment, where all the artifacts/metrics etc. will be stored.
            The default uri are set in the qlib.config.
        """
        run = self.start_exp(experiment_name, recorder_name, uri)
        try:
            yield run
        except Exception as e:
            self.end_exp(Recorder.STATUS_FA)  # end the experiment if something went wrong

Review comment (suggestion):
Thanks for pointing this out! 

Apparently, adding `uri` argument in this method will not change the one defined in the `config` file. Therefore, the next time when user call this function in the same experiment, they have to also specify this argument with the same value. Otherwise, inconsistent `uri` may occur.

Could you please also update the related `doc` to specify this situation? Thanks~

Code after:
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

from contextlib import contextmanager
from .expm import MLflowExpManager
from .exp import Experiment
from .recorder import Recorder
from ..utils import Wrapper


class QlibRecorder:
    """
    A global system that helps to manage the experiments.
    """

    def __init__(self, exp_manager):
        self.exp_manager = exp_manager

    def __repr__(self):
        return "{name}(manager={manager})".format(name=self.__class__.__name__, manager=self.exp_manager)

    @contextmanager
    def start(self, experiment_name=None, recorder_name=None, uri=None):
        """
        Method to start an experiment. This method can only be called within a Python's `with` statement. Here is the example code:

        .. code-block:: Python

            with R.start('test', 'recorder_1'):
                model.fit(dataset)
                R.log...
                ... # further operations

        Parameters
        ----------
        experiment_name : str
            name of the experiment one wants to start.
        recorder_name : str
            name of the recorder under the experiment one wants to start.
        uri : str
            The tracking uri of the experiment, where all the artifacts/metrics etc. will be stored.
            The default uri is set in the qlib.config. Note that this uri argument will not change the one defined in the config file.
            Therefore, the next time when users call this function in the same experiment,
            they have to also specify this argument with the same value. Otherwise, inconsistent uri may occur.
        """
        run = self.start_exp(experiment_name, recorder_name, uri)
        try:
            yield run