-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* handler demo cache * Update data_cache_demo.py * example to reusing processed data in memory * Skip dumping task of task_train * FIX Black Co-authored-by: Wangwuyi123 <[email protected]>
- Loading branch information
1 parent
fdbc666
commit a2be6e2
Showing
4 changed files
with
198 additions
and
38 deletions.
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