Refine TT workflow

This commit is contained in:
D-X-Y
2021-03-06 06:38:34 -08:00
parent 511e2443d0
commit 349d9fcc9f
3 changed files with 135 additions and 88 deletions

View File

@@ -21,60 +21,59 @@ from qlib.workflow import R
class QResult:
def __init__(self):
self._result = defaultdict(list)
def __init__(self):
self._result = defaultdict(list)
def append(self, key, value):
self._result[key].append(value)
def append(self, key, value):
self._result[key].append(value)
@property
def result(self):
return self._result
@property
def result(self):
return self._result
def update(self, metrics, filter_keys=None):
for key, value in metrics.items():
if filter_keys is not None and key in filter_keys:
key = filter_keys[key]
elif filter_keys is not None:
continue
self.append(key, value)
def update(self, metrics, filter_keys=None):
for key, value in metrics.items():
if filter_keys is not None and key in filter_keys:
key = filter_keys[key]
elif filter_keys is not None:
continue
self.append(key, value)
@staticmethod
def full_str(xstr, space):
xformat = "{:" + str(space) + "s}"
return xformat.format(str(xstr))
@staticmethod
def full_str(xstr, space):
xformat = '{:' + str(space) + 's}'
return xformat.format(str(xstr))
def info(self, keys: List[Text], separate: Text = '', space: int = 25, show=True):
avaliable_keys = []
for key in keys:
if key not in self.result:
print('There are invalid key [{:}].'.format(key))
else:
avaliable_keys.append(key)
head_str = separate.join([self.full_str(x, space) for x in avaliable_keys])
values = []
for key in avaliable_keys:
current_values = self._result[key]
mean = np.mean(current_values)
std = np.std(current_values)
values.append('{:.4f} $\pm$ {:.4f}'.format(mean, std))
value_str = separate.join([self.full_str(x, space) for x in values])
if show:
print(head_str)
print(value_str)
else:
return head_str, value_str
def info(self, keys: List[Text], separate: Text = "", space: int = 25, show=True):
avaliable_keys = []
for key in keys:
if key not in self.result:
print("There are invalid key [{:}].".format(key))
else:
avaliable_keys.append(key)
head_str = separate.join([self.full_str(x, space) for x in avaliable_keys])
values = []
for key in avaliable_keys:
current_values = self._result[key]
mean = np.mean(current_values)
std = np.std(current_values)
values.append("{:.4f} $\pm$ {:.4f}".format(mean, std))
value_str = separate.join([self.full_str(x, space) for x in values])
if show:
print(head_str)
print(value_str)
else:
return head_str, value_str
def compare_results(heads, values, names, space=10):
for idx, x in enumerate(heads):
assert x == heads[0], '[{:}] {:} vs {:}'.format(idx, x, heads[0])
new_head = QResult.full_str('Name', space) + heads[0]
print(new_head)
for name, value in zip(names, values):
xline = QResult.full_str(name, space) + value
print(xline)
for idx, x in enumerate(heads):
assert x == heads[0], "[{:}] {:} vs {:}".format(idx, x, heads[0])
new_head = QResult.full_str("Name", space) + heads[0]
print(new_head)
for name, value in zip(names, values):
xline = QResult.full_str(name, space) + value
print(xline)
def filter_finished(recorders):
@@ -92,20 +91,22 @@ def main(xargs):
R.reset_default_uri(xargs.save_dir)
experiments = R.list_experiments()
key_map = {"IC": "IC",
"ICIR": "ICIR",
"Rank IC": "Rank_IC",
"Rank ICIR": "Rank_ICIR",
"excess_return_with_cost.annualized_return": "Annualized_Return",
"excess_return_with_cost.information_ratio": "Information_Ratio",
"excess_return_with_cost.max_drawdown": "Max_Drawdown"}
key_map = {
"IC": "IC",
"ICIR": "ICIR",
"Rank IC": "Rank_IC",
"Rank ICIR": "Rank_ICIR",
"excess_return_with_cost.annualized_return": "Annualized_Return",
"excess_return_with_cost.information_ratio": "Information_Ratio",
"excess_return_with_cost.max_drawdown": "Max_Drawdown",
}
all_keys = list(key_map.values())
print("There are {:} experiments.".format(len(experiments)))
head_strs, value_strs, names = [], [], []
for idx, (key, experiment) in enumerate(experiments.items()):
if experiment.id == '0':
continue
if experiment.id == "0":
continue
recorders = experiment.list_recorders()
recorders, not_finished = filter_finished(recorders)
print(
@@ -115,7 +116,7 @@ def main(xargs):
)
result = QResult()
for recorder_id, recorder in recorders.items():
result.update(recorder.list_metrics(), key_map)
result.update(recorder.list_metrics(), key_map)
head_str, value_str = result.info(all_keys, show=False)
head_strs.append(head_str)
value_strs.append(value_str)