1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-18 18:04:31 +08:00

add data loder test

This commit is contained in:
Linlang
2024-07-04 20:43:41 +08:00
parent ce596f9dfa
commit 3f86171051
4 changed files with 47 additions and 27 deletions

View File

@@ -90,7 +90,6 @@ class Alpha360(DataHandlerLP):
return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"] return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"]
class Alpha360vwap(Alpha360): class Alpha360vwap(Alpha360):
def get_label_config(self): def get_label_config(self):
return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"] return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"]
@@ -154,7 +153,6 @@ class Alpha158(DataHandlerLP):
return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"] return ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"]
class Alpha158vwap(Alpha158): class Alpha158vwap(Alpha158):
def get_label_config(self): def get_label_config(self):
return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"] return ["Ref($vwap, -2)/Ref($vwap, -1) - 1"], ["LABEL0"]

View File

@@ -1,11 +1,12 @@
from qlib.data.dataset.loader import DataLoader, QlibDataLoader from qlib.data.dataset.loader import DataLoader, QlibDataLoader
class Alpha360DL(QlibDataLoader): class Alpha360DL(QlibDataLoader):
"""Dataloader to get Alpha360""" """Dataloader to get Alpha360"""
def __init__(self, config=None, **kwargs): def __init__(self, config=None, **kwargs):
_config = { _config = {
"feature":self.get_feature_config(), "feature": self.get_feature_config(),
} }
if config is not None: if config is not None:
_config.update(config) _config.update(config)
@@ -59,23 +60,26 @@ class Alpha360DL(QlibDataLoader):
class Alpha158DL(QlibDataLoader): class Alpha158DL(QlibDataLoader):
"""Dataloader to get Alpha158""" """Dataloader to get Alpha158"""
def __init__(self, config=None, **kwargs): def __init__(self, config=None, **kwargs):
_config = { _config = {
"feature":self.get_feature_config(), "feature": self.get_feature_config(),
} }
if config is not None: if config is not None:
_config.update(config) _config.update(config)
super().__init__(config=_config, **kwargs) super().__init__(config=_config, **kwargs)
@staticmethod @staticmethod
def get_feature_config(config={ def get_feature_config(
config={
"kbar": {}, "kbar": {},
"price": { "price": {
"windows": [0], "windows": [0],
"feature": ["OPEN", "HIGH", "LOW", "VWAP"], "feature": ["OPEN", "HIGH", "LOW", "VWAP"],
}, },
"rolling": {}, "rolling": {},
}): }
):
"""create factors from config """create factors from config
config = { config = {
@@ -304,4 +308,3 @@ class Alpha158DL(QlibDataLoader):
names += ["VSUMD%d" % d for d in windows] names += ["VSUMD%d" % d for d in windows]
return fields, names return fields, names

View File

@@ -7,7 +7,7 @@ from pathlib import Path
import warnings import warnings
import pandas as pd import pandas as pd
from typing import Tuple, Union, List from typing import Tuple, Union, List, Dict
from qlib.data import D from qlib.data import D
from qlib.utils import load_dataset, init_instance_by_config, time_to_slc_point from qlib.utils import load_dataset, init_instance_by_config, time_to_slc_point
@@ -283,7 +283,8 @@ class NestedDataLoader(DataLoader):
""" """
We have multiple DataLoader, we can use this class to combine them. We have multiple DataLoader, we can use this class to combine them.
""" """
def __init__(self, dataloader_l: list[dict], join="left") -> None:
def __init__(self, dataloader_l: List[Dict], join="left") -> None:
""" """
Parameters Parameters
@@ -311,14 +312,19 @@ class NestedDataLoader(DataLoader):
it will pass to pd.concat when merging it. it will pass to pd.concat when merging it.
""" """
super().__init__() super().__init__()
self.data_loader_l = [(dl if isinstance(dl, DataLoader) else init_instance_by_config(dl)) for dl in dataloader_l] self.data_loader_l = [
(dl if isinstance(dl, DataLoader) else init_instance_by_config(dl)) for dl in dataloader_l
]
self.join = join self.join = join
def load(self, instruments=None, start_time=None, end_time=None) -> pd.DataFrame: def load(self, instruments=None, start_time=None, end_time=None) -> pd.DataFrame:
df_l = [] df_full = None
for dl in self.data_loader_l: for dl in self.data_loader_l:
df_l = dl.load(instruments, start_time, end_time) df_current = dl.load(instruments, start_time, end_time)
df_full = pd.concat(df_l, axis=1, join=self.join) if df_full is None:
df_full = df_current
else:
df_full = pd.merge(df_full, df_current, left_index=True, right_index=True, how=self.join)
return df_full.sort_index(axis=1) return df_full.sort_index(axis=1)

View File

@@ -1,35 +1,48 @@
# TODO: # TODO:
# dump alpha 360 to dataframe and merge it with Alpha158 # dump alpha 360 to dataframe and merge it with Alpha158
import sys
import unittest import unittest
import qlib
from pathlib import Path
sys.path.append(str(Path(__file__).resolve().parent))
from qlib.data.dataset.loader import NestedDataLoader from qlib.data.dataset.loader import NestedDataLoader
from qlib.contrib.data.loader import Alpha158DL, Alpha360DL
class TestDataLoader(unittest.TestCase): class TestDataLoader(unittest.TestCase):
def test_nested_data_loader(self): def test_nested_data_loader(self):
qlib.init(provider_uri="C:/Users/v-lvlinlang/.qlib/qlib_data/cn_data_simple")
nd = NestedDataLoader( nd = NestedDataLoader(
dataloader_l=[ dataloader_l=[
{ {
"class": "qlib.contrib.data.loader.Alpha158DL", "class": "qlib.contrib.data.loader.Alpha158DL",
}, { },
{
"class": "qlib.contrib.data.loader.Alpha360DL", "class": "qlib.contrib.data.loader.Alpha360DL",
"kwargs": { "kwargs": {"config": {"label": (["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"])}},
"config": { },
"label": ( ["Ref($close, -2)/Ref($close, -1) - 1"], ["LABEL0"])
}
}
}
] ]
) )
# Of course you can use StaticDataLoader # Of course you can use StaticDataLoader
nd.load dataset = nd.load()
...
assert dataset is not None
columns = dataset.columns.tolist()
columns_list = [tup[1] for tup in columns]
for col in Alpha158DL.get_feature_config()[1]:
assert col in columns_list
for col in Alpha360DL.get_feature_config()[1]:
assert col in columns_list
# 断言标签也包含在数据中
assert "LABEL0" in columns_list
# Then you can use it wth DataHandler; # Then you can use it wth DataHandler;