mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-12 07:16:54 +08:00
Add configurable dataset (#1535)
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
# Copyright (c) Microsoft Corporation.
|
# Copyright (c) Microsoft Corporation.
|
||||||
# Licensed under the MIT License.
|
# Licensed under the MIT License.
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
from qlib.utils.data import update_config
|
||||||
from ...data.dataset.handler import DataHandlerLP
|
from ...data.dataset.handler import DataHandlerLP
|
||||||
from ...data.dataset.processor import Processor
|
from ...data.dataset.processor import Processor
|
||||||
from ...utils import get_callable_kwargs
|
from ...utils import get_callable_kwargs
|
||||||
@@ -57,12 +59,13 @@ class Alpha360(DataHandlerLP):
|
|||||||
fit_end_time=None,
|
fit_end_time=None,
|
||||||
filter_pipe=None,
|
filter_pipe=None,
|
||||||
inst_processors=None,
|
inst_processors=None,
|
||||||
|
data_loader: Optional[dict]=None,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
||||||
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
|
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
|
||||||
|
|
||||||
data_loader = {
|
_data_loader = {
|
||||||
"class": "QlibDataLoader",
|
"class": "QlibDataLoader",
|
||||||
"kwargs": {
|
"kwargs": {
|
||||||
"config": {
|
"config": {
|
||||||
@@ -74,12 +77,14 @@ class Alpha360(DataHandlerLP):
|
|||||||
"inst_processors": inst_processors,
|
"inst_processors": inst_processors,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if data_loader is not None:
|
||||||
|
update_config(_data_loader, data_loader)
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
instruments=instruments,
|
instruments=instruments,
|
||||||
start_time=start_time,
|
start_time=start_time,
|
||||||
end_time=end_time,
|
end_time=end_time,
|
||||||
data_loader=data_loader,
|
data_loader=_data_loader,
|
||||||
learn_processors=learn_processors,
|
learn_processors=learn_processors,
|
||||||
infer_processors=infer_processors,
|
infer_processors=infer_processors,
|
||||||
**kwargs
|
**kwargs
|
||||||
@@ -153,12 +158,13 @@ class Alpha158(DataHandlerLP):
|
|||||||
process_type=DataHandlerLP.PTYPE_A,
|
process_type=DataHandlerLP.PTYPE_A,
|
||||||
filter_pipe=None,
|
filter_pipe=None,
|
||||||
inst_processors=None,
|
inst_processors=None,
|
||||||
|
data_loader: Optional[dict]=None,
|
||||||
**kwargs
|
**kwargs
|
||||||
):
|
):
|
||||||
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
infer_processors = check_transform_proc(infer_processors, fit_start_time, fit_end_time)
|
||||||
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
|
learn_processors = check_transform_proc(learn_processors, fit_start_time, fit_end_time)
|
||||||
|
|
||||||
data_loader = {
|
_data_loader = {
|
||||||
"class": "QlibDataLoader",
|
"class": "QlibDataLoader",
|
||||||
"kwargs": {
|
"kwargs": {
|
||||||
"config": {
|
"config": {
|
||||||
@@ -170,11 +176,13 @@ class Alpha158(DataHandlerLP):
|
|||||||
"inst_processors": inst_processors,
|
"inst_processors": inst_processors,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if data_loader is not None:
|
||||||
|
update_config(_data_loader, data_loader)
|
||||||
super().__init__(
|
super().__init__(
|
||||||
instruments=instruments,
|
instruments=instruments,
|
||||||
start_time=start_time,
|
start_time=start_time,
|
||||||
end_time=end_time,
|
end_time=end_time,
|
||||||
data_loader=data_loader,
|
data_loader=_data_loader,
|
||||||
infer_processors=infer_processors,
|
infer_processors=infer_processors,
|
||||||
learn_processors=learn_processors,
|
learn_processors=learn_processors,
|
||||||
process_type=process_type,
|
process_type=process_type,
|
||||||
|
|||||||
12
qlib/finco/tpl/README.md
Normal file
12
qlib/finco/tpl/README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
This is a set of templates that should be copied for a new project.
|
||||||
|
|
||||||
|
Here are the explanations for the templates folder.
|
||||||
|
|
||||||
|
| folder | explanations |
|
||||||
|
|--------|------------------------------------------------------------------|
|
||||||
|
| sl | Default configuration for supervised learning |
|
||||||
|
| sl-cfg | Like configuration in sl. But the dataset is highly configurable |
|
||||||
|
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
- [ ] [Copier](https://copier.readthedocs.io/en/stable/#quick-start) may be useful if the generation process becomes complicated
|
||||||
12
qlib/finco/tpl/__init__.py
Normal file
12
qlib/finco/tpl/__init__.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# Copyright (c) Microsoft Corporation.
|
||||||
|
# Licensed under the MIT License.
|
||||||
|
from pathlib import Path
|
||||||
|
DIRNAME = Path(__file__).absolute().resolve().parent
|
||||||
|
|
||||||
|
|
||||||
|
def get_tpl_path() -> Path:
|
||||||
|
"""
|
||||||
|
return the template path
|
||||||
|
Because the template path is located in the folder. We don't know where it is located. So __file__ for this module will be used.
|
||||||
|
"""
|
||||||
|
return DIRNAME
|
||||||
83
qlib/finco/tpl/sl-cfg/workflow_config_ds.yaml
Normal file
83
qlib/finco/tpl/sl-cfg/workflow_config_ds.yaml
Normal file
File diff suppressed because one or more lines are too long
@@ -1,6 +0,0 @@
|
|||||||
This is a set of templates that should be copied for a new project.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# TODO
|
|
||||||
- [ ] [Copier](https://copier.readthedocs.io/en/stable/#quick-start) may be useful if the generation process becomes complicated
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class Singleton():
|
class Singleton():
|
||||||
_instance = None
|
_instance = None
|
||||||
def __new__(cls, *args, **kwargs):
|
def __new__(cls, *args, **kwargs):
|
||||||
@@ -7,10 +8,11 @@ class Singleton():
|
|||||||
cls._instance = super().__new__(cls, *args, **kwargs)
|
cls._instance = super().__new__(cls, *args, **kwargs)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
|
|
||||||
def parse_json(response):
|
def parse_json(response):
|
||||||
try:
|
try:
|
||||||
return json.loads(response)
|
return json.loads(response)
|
||||||
except json.decoder.JSONDecodeError:
|
except json.decoder.JSONDecodeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
raise Exception(f"Failed to parse response: {response}, please report it or help us to fix it.")
|
raise Exception(f"Failed to parse response: {response}, please report it or help us to fix it.")
|
||||||
35
tests/finco/test_cfg.py
Normal file
35
tests/finco/test_cfg.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Copyright (c) Microsoft Corporation.
|
||||||
|
# Licensed under the MIT License.
|
||||||
|
import unittest
|
||||||
|
from qlib.finco.tpl import get_tpl_path
|
||||||
|
import ruamel.yaml as yaml
|
||||||
|
|
||||||
|
from qlib.data.dataset.handler import DataHandlerLP
|
||||||
|
from qlib.utils import init_instance_by_config
|
||||||
|
from qlib.tests import TestAutoData
|
||||||
|
|
||||||
|
|
||||||
|
class FincoTpl(TestAutoData):
|
||||||
|
|
||||||
|
def test_tpl_consistence(self):
|
||||||
|
"""Motivation: make sure the configuable template is consistent with the default config"""
|
||||||
|
tpl_p = get_tpl_path()
|
||||||
|
with (tpl_p / "sl" / "workflow_config.yaml").open("rb") as fp:
|
||||||
|
config = yaml.safe_load(fp)
|
||||||
|
# init_data_handler
|
||||||
|
hd: DataHandlerLP = init_instance_by_config(config["task"]["dataset"]["kwargs"]["handler"])
|
||||||
|
# NOTE: The config in workflow_config_ds.yaml is generated by the following code:
|
||||||
|
# dump in yaml format to file without auto linebreak
|
||||||
|
# print(yaml.dump(hd.data_loader.fields, width=10000, stream=open("_tmp", "w")))
|
||||||
|
|
||||||
|
with (tpl_p / "sl-cfg" / "workflow_config_ds.yaml").open("rb") as fp:
|
||||||
|
config = yaml.safe_load(fp)
|
||||||
|
hd_ds: DataHandlerLP = init_instance_by_config(config["task"]["dataset"]["kwargs"]["handler"])
|
||||||
|
self.assertEqual(hd_ds.data_loader.fields, hd.data_loader.fields)
|
||||||
|
|
||||||
|
check = hd_ds.fetch().fillna(0.) == hd.fetch().fillna(0.)
|
||||||
|
self.assertTrue(check.all().all())
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user