Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more detailed docs for workflow #639

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions qlib/workflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the MIT License.

from contextlib import contextmanager
from typing import Text, Optional
from typing import Any, Dict, Text, Optional
from .expm import ExpManager
from .exp import Experiment
from .recorder import Recorder
Expand Down Expand Up @@ -380,11 +380,11 @@ def get_recorder(
.. code-block:: Python

# Case 1
with R.start('test'):
with R.start(experiment_name='test'):
recorder = R.get_recorder()

# Case 2
with R.start('test'):
with R.start(experiment_name='test'):
recorder = R.get_recorder(recorder_id='2e7a4efd66574fa49039e00ffaefa99d')

# Case 3
Expand Down Expand Up @@ -433,12 +433,18 @@ def delete_recorder(self, recorder_id=None, recorder_name=None):
"""
self.get_exp().delete_recorder(recorder_id, recorder_name)

def save_objects(self, local_path=None, artifact_path=None, **kwargs):
def save_objects(self, local_path=None, artifact_path=None, **kwargs: Dict[Text, Any]):
"""
Method for saving objects as artifacts in the experiment to the uri. It supports either saving
from a local file/directory, or directly saving objects. User can use valid python's keywords arguments
to specify the object to be saved as well as its name (name: value).

In summary, this API is designs for saving **objects** to **the experiments management backend path**,
1. Qlib provide two methods to specify **objects**
- Passing in the object directly by passing with `**kwargs` (e.g. R.save_objects(trained_model=model))
- Passing in the local path to the object, i.e. `local_path` parameter.
2. `artifact_path` represents the **the experiments management backend path**

- If `active recorder` exists: it will save the objects through the active recorder.
- If `active recorder` not exists: the system will create a default experiment, and a new recorder and save objects under it.

Expand All @@ -451,21 +457,35 @@ def save_objects(self, local_path=None, artifact_path=None, **kwargs):
.. code-block:: Python

# Case 1
with R.start('test'):
with R.start(experiment_name='test'):
pred = model.predict(dataset)
R.save_objects(**{"pred.pkl": pred}, artifact_path='prediction')
rid = R.get_recorder().id
...
R.get_recorder(recorder_id=rid).load_object("prediction/pred.pkl") # after saving objects, you can load the previous object with this api

# Case 2
with R.start('test'):
R.save_objects(local_path='results/pred.pkl')
with R.start(experiment_name='test'):
R.save_objects(local_path='results/pred.pkl', artifact_path="prediction")
rid = R.get_recorder().id
...
R.get_recorder(recorder_id=rid).load_object("prediction/pred.pkl") # after saving objects, you can load the previous object with this api


Parameters
----------
local_path : str
if provided, them save the file or directory to the artifact URI.
artifact_path : str
the relative path for the artifact to be stored in the URI.
"""
**kwargs: Dict[Text, Any]
the object to be saved.
For example, `{"pred.pkl": pred}`
"""
if local_path is not None and len(kwargs) > 0:
raise ValueError(
"You can choose only one of `local_path`(save the files in a path) or `kwargs`(pass in the objects directly)"
)
self.get_exp().get_recorder().save_objects(local_path, artifact_path, **kwargs)

def load_object(self, name: Text):
Expand Down
2 changes: 2 additions & 0 deletions qlib/workflow/recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def save_objects(self, local_path=None, artifact_path=None, **kwargs):
Save objects such as prediction file or model checkpoints to the artifact URI. User
can save object through keywords arguments (name:value).

Please refer to the docs of qlib.workflow:R.save_objects

Parameters
----------
local_path : str
Expand Down