-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
handler demo cache #606
Merged
Merged
handler demo cache #606
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6864d5
handler demo cache
you-n-g 4581da3
Update data_cache_demo.py
Wangwuyi123 759705b
example to reusing processed data in memory
you-n-g 3e01dc9
Merge remote-tracking branch 'me/handler_cache_demo' into handler_cac…
you-n-g 39b9790
Skip dumping task of task_train
you-n-g 34b3bdc
Merge branch 'main' into handler_cache_demo
you-n-g f8d5d4d
FIX Black
you-n-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Introduction | ||
The examples in this folder try to demonstrate some common usage of data-related modules of Qlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
""" | ||
The motivation of this demo | ||
- To show the data modules of Qlib is Serializable, users can dump processed data to disk to avoid duplicated data preprocessing | ||
""" | ||
|
||
from copy import deepcopy | ||
from pathlib import Path | ||
import pickle | ||
from pprint import pprint | ||
import subprocess | ||
import yaml | ||
from qlib.log import TimeInspector | ||
|
||
from qlib import init | ||
from qlib.data.dataset.handler import DataHandlerLP | ||
from qlib.utils import init_instance_by_config | ||
|
||
# For general purpose, we use relative path | ||
DIRNAME = Path(__file__).absolute().resolve().parent | ||
|
||
if __name__ == "__main__": | ||
init() | ||
|
||
config_path = DIRNAME.parent / "benchmarks/LightGBM/workflow_config_lightgbm_Alpha158.yaml" | ||
|
||
# 1) show original time | ||
with TimeInspector.logt("The original time without handler cache:"): | ||
subprocess.run(f"qrun {config_path}", shell=True) | ||
|
||
# 2) dump handler | ||
task_config = yaml.safe_load(config_path.open()) | ||
hd_conf = task_config["task"]["dataset"]["kwargs"]["handler"] | ||
pprint(hd_conf) | ||
hd: DataHandlerLP = init_instance_by_config(hd_conf) | ||
hd_path = DIRNAME / "handler.pkl" | ||
hd.to_pickle(hd_path, dump_all=True) | ||
|
||
# 3) create new task with handler cache | ||
new_task_config = deepcopy(task_config) | ||
new_task_config["task"]["dataset"]["kwargs"]["handler"] = f"file://{hd_path}" | ||
new_task_config | ||
new_task_path = DIRNAME / "new_task.yaml" | ||
print("The location of the new task", new_task_path) | ||
|
||
# save new task | ||
with new_task_path.open("w") as f: | ||
yaml.safe_dump(new_task_config, f) | ||
|
||
# 4) train model with new task | ||
with TimeInspector.logt("The time for task with handler cache:"): | ||
subprocess.run(f"qrun {new_task_path}", shell=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
""" | ||
The motivation of this demo | ||
- To show the data modules of Qlib is Serializable, users can dump processed data to disk to avoid duplicated data preprocessing | ||
""" | ||
|
||
from copy import deepcopy | ||
from pathlib import Path | ||
import pickle | ||
from pprint import pprint | ||
import subprocess | ||
|
||
import yaml | ||
|
||
from qlib import init | ||
from qlib.data.dataset.handler import DataHandlerLP | ||
from qlib.log import TimeInspector | ||
from qlib.model.trainer import task_train | ||
from qlib.utils import init_instance_by_config | ||
|
||
# For general purpose, we use relative path | ||
DIRNAME = Path(__file__).absolute().resolve().parent | ||
|
||
if __name__ == "__main__": | ||
init() | ||
|
||
repeat = 2 | ||
exp_name = "data_mem_reuse_demo" | ||
|
||
config_path = DIRNAME.parent / "benchmarks/LightGBM/workflow_config_lightgbm_Alpha158.yaml" | ||
task_config = yaml.safe_load(config_path.open()) | ||
|
||
# 1) without using processed data in memory | ||
with TimeInspector.logt("The original time without reusing processed data in memory:"): | ||
for i in range(repeat): | ||
task_train(task_config["task"], experiment_name=exp_name) | ||
|
||
# 2) prepare processed data in memory. | ||
hd_conf = task_config["task"]["dataset"]["kwargs"]["handler"] | ||
pprint(hd_conf) | ||
hd: DataHandlerLP = init_instance_by_config(hd_conf) | ||
|
||
# 3) with reusing processed data in memory | ||
new_task = deepcopy(task_config["task"]) | ||
new_task["dataset"]["kwargs"]["handler"] = hd | ||
print(new_task) | ||
|
||
with TimeInspector.logt("The time with reusing processed data in memory:"): | ||
# this will save the time to reload and process data from disk(in `DataHandlerLP`) | ||
# It still takes a lot of time in the backtest phase | ||
for i in range(repeat): | ||
task_train(new_task, experiment_name=exp_name) | ||
|
||
# 4) User can change other parts exclude processed data in memory(handler) | ||
new_task = deepcopy(task_config["task"]) | ||
new_task["dataset"]["kwargs"]["segments"]["train"] = ("20100101", "20131231") | ||
with TimeInspector.logt("The time with reusing processed data in memory:"): | ||
task_train(new_task, experiment_name=exp_name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line of code is incomplete:
new_task_config