1
0
mirror of https://github.com/microsoft/qlib.git synced 2026-07-11 06:46:56 +08:00

update fix CI tests bugs

This commit is contained in:
Young
2021-10-02 08:28:17 +00:00
committed by you-n-g
parent 3a152f9b8b
commit 873129aa9b
10 changed files with 101 additions and 74 deletions

View File

@@ -16,7 +16,7 @@ from ..data.dataset.handler import DataHandlerLP
from ..backtest import backtest as normal_backtest
from ..utils import init_instance_by_config, get_module_by_module_path
from ..log import get_module_logger
from ..utils import flatten_dict
from ..utils import flatten_dict, class_casting
from ..utils.time import Freq
from ..strategy.base import BaseStrategy
from ..contrib.eva.alpha import calc_ic, calc_long_short_return, calc_long_short_prec
@@ -32,6 +32,7 @@ class RecordTemp:
"""
artifact_path = None
depend_cls = None # the depend class of the record; the record will depend on the results generated by `depend_cls`
@classmethod
def get_path(cls, path=None):
@@ -98,21 +99,30 @@ class RecordTemp:
"""
return []
def check(self, cls="self"):
def check(self, include_self: bool = False):
"""
Check if the records is properly generated and saved.
It is useful in fololwing examples
- checking if the depended files complete before genrating new things.
- checking if the final files is completed
Parameters
----------
include_self : bool
is the file generated by self included
Raise
------
FileExistsError: whether the records are stored properly.
"""
artifacts = set(self.recorder.list_artifacts())
if cls == "self":
cls = self
flist = cls.list()
for item in flist:
if item not in artifacts:
raise FileExistsError(item)
if include_self:
for item in self.list():
if item not in artifacts:
raise FileExistsError(item)
if self.depend_cls is not None:
with class_casting(self, self.depend_cls):
self.check(include_self=True)
class SignalRecord(RecordTemp):
@@ -127,26 +137,20 @@ class SignalRecord(RecordTemp):
@staticmethod
def generate_label(dataset):
# NOTE:
# Python doesn't provide the downcasting mechanism.
# We use the trick here to downcast the class
orig_cls = dataset.__class__
dataset.__class__ = DatasetH
params = dict(segments="test", col_set="label", data_key=DataHandlerLP.DK_R)
try:
# Assume the backend handler is DataHandlerLP
raw_label = dataset.prepare(**params)
except TypeError:
# The argument number is not right
del params["data_key"]
# The backend handler should be DataHandler
raw_label = dataset.prepare(**params)
except AttributeError:
# The data handler is initialize with `drop_raw=True`...
# So raw_label is not available
raw_label = None
dataset.__class__ = orig_cls
with class_casting(dataset, DatasetH):
params = dict(segments="test", col_set="label", data_key=DataHandlerLP.DK_R)
try:
# Assume the backend handler is DataHandlerLP
raw_label = dataset.prepare(**params)
except TypeError:
# The argument number is not right
del params["data_key"]
# The backend handler should be DataHandler
raw_label = dataset.prepare(**params)
except AttributeError:
# The data handler is initialize with `drop_raw=True`...
# So raw_label is not available
raw_label = None
return raw_label
def generate(self, **kwargs):
@@ -235,7 +239,7 @@ class SigAnaRecord(RecordTemp):
"""
artifact_path = "sig_analysis"
pre_class = SignalRecord
depend_cls = SignalRecord
def __init__(self, recorder, ana_long_short=False, ann_scaler=252, label_col=0):
super().__init__(recorder=recorder)
@@ -244,7 +248,7 @@ class SigAnaRecord(RecordTemp):
self.label_col = label_col
def generate(self, **kwargs):
self.check(self.pre_class)
self.check()
pred = self.load("pred.pkl")
label = self.load("label.pkl")