xautodl/exps/trading/organize_results.py

222 lines
7.0 KiB
Python
Raw Normal View History

2021-03-06 15:13:22 +01:00
#####################################################
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.02 #
#####################################################
2021-03-14 14:44:51 +01:00
# python exps/trading/organize_results.py #
2021-03-06 15:13:22 +01:00
#####################################################
import sys, argparse
import numpy as np
from typing import List, Text
from collections import defaultdict, OrderedDict
from pathlib import Path
from pprint import pprint
import ruamel.yaml as yaml
lib_dir = (Path(__file__).parent / ".." / ".." / "lib").resolve()
if str(lib_dir) not in sys.path:
sys.path.insert(0, str(lib_dir))
import qlib
from qlib.config import REG_CN
from qlib.workflow import R
class QResult:
2021-03-06 15:38:34 +01:00
def __init__(self):
self._result = defaultdict(list)
def append(self, key, value):
self._result[key].append(value)
@property
def result(self):
return self._result
2021-03-17 14:05:29 +01:00
def __len__(self):
return len(self._result)
2021-03-06 15:38:34 +01:00
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))
2021-03-08 04:22:20 +01:00
@staticmethod
def merge_dict(dict_list):
new_dict = dict()
for xkey in dict_list[0].keys():
values = [x for xdict in dict_list for x in xdict[xkey]]
new_dict[xkey] = values
return new_dict
2021-03-18 09:02:55 +01:00
def info(
self,
keys: List[Text],
separate: Text = "& ",
space: int = 25,
verbose: bool = True,
):
2021-03-06 15:38:34 +01:00
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:
2021-03-14 14:44:51 +01:00
# current_values = self._result[key]
current_values = [x * 100 for x in self._result[key]]
2021-03-06 15:38:34 +01:00
mean = np.mean(current_values)
std = np.std(current_values)
2021-03-14 14:44:51 +01:00
# values.append("{:.4f} $\pm$ {:.4f}".format(mean, std))
values.append("{:.2f} $\pm$ {:.2f}".format(mean, std))
2021-03-06 15:38:34 +01:00
value_str = separate.join([self.full_str(x, space) for x in values])
2021-03-08 04:22:20 +01:00
if verbose:
2021-03-06 15:38:34 +01:00
print(head_str)
print(value_str)
2021-03-08 04:22:20 +01:00
return head_str, value_str
2021-03-06 15:13:22 +01:00
2021-03-17 14:05:29 +01:00
def compare_results(heads, values, names, space=10, verbose=True, sort_key=False):
2021-03-06 15:38:34 +01:00
for idx, x in enumerate(heads):
2021-03-17 14:05:29 +01:00
assert x == heads[0], "[{:}] \n{:}\nvs\n{:}".format(idx, x, heads[0])
2021-03-06 15:38:34 +01:00
new_head = QResult.full_str("Name", space) + heads[0]
2021-03-08 04:22:20 +01:00
info_str_dict = dict(head=new_head, lines=[])
2021-03-06 15:38:34 +01:00
for name, value in zip(names, values):
xline = QResult.full_str(name, space) + value
2021-03-08 04:22:20 +01:00
info_str_dict["lines"].append(xline)
if verbose:
2021-03-19 05:13:35 +01:00
print("\nThere are {:} algorithms.".format(len(values)))
2021-03-08 04:22:20 +01:00
print(info_str_dict["head"])
if sort_key:
2021-03-18 09:02:55 +01:00
lines = sorted(
list(zip(values, info_str_dict["lines"])),
key=lambda x: float(x[0].split(" ")[0]),
)
2021-03-08 04:22:20 +01:00
lines = [x[1] for x in lines]
else:
lines = info_str_dict["lines"]
for xline in lines:
print(xline)
return info_str_dict
2021-03-06 15:13:22 +01:00
def filter_finished(recorders):
returned_recorders = dict()
not_finished = 0
for key, recorder in recorders.items():
if recorder.status == "FINISHED":
returned_recorders[key] = recorder
else:
not_finished += 1
return returned_recorders, not_finished
2021-03-08 04:22:20 +01:00
def query_info(save_dir, verbose):
R.set_uri(save_dir)
2021-03-06 15:13:22 +01:00
experiments = R.list_experiments()
2021-03-06 15:38:34 +01:00
key_map = {
2021-03-17 10:10:45 +01:00
"RMSE": "RMSE",
2021-03-06 15:38:34 +01:00
"IC": "IC",
"ICIR": "ICIR",
"Rank IC": "Rank_IC",
"Rank ICIR": "Rank_ICIR",
"excess_return_with_cost.annualized_return": "Annualized_Return",
2021-03-14 14:44:51 +01:00
# "excess_return_with_cost.information_ratio": "Information_Ratio",
2021-03-06 15:38:34 +01:00
"excess_return_with_cost.max_drawdown": "Max_Drawdown",
}
2021-03-06 15:13:22 +01:00
all_keys = list(key_map.values())
2021-03-08 04:22:20 +01:00
if verbose:
print("There are {:} experiments.".format(len(experiments)))
2021-03-06 15:13:22 +01:00
head_strs, value_strs, names = [], [], []
for idx, (key, experiment) in enumerate(experiments.items()):
2021-03-06 15:38:34 +01:00
if experiment.id == "0":
continue
2021-03-06 15:13:22 +01:00
recorders = experiment.list_recorders()
recorders, not_finished = filter_finished(recorders)
2021-03-08 04:22:20 +01:00
if verbose:
print(
"====>>>> {:02d}/{:02d}-th experiment {:9s} has {:02d}/{:02d} finished recorders.".format(
2021-03-18 09:02:55 +01:00
idx + 1,
len(experiments),
experiment.name,
len(recorders),
len(recorders) + not_finished,
2021-03-08 04:22:20 +01:00
)
2021-03-06 15:13:22 +01:00
)
result = QResult()
for recorder_id, recorder in recorders.items():
2021-03-06 15:38:34 +01:00
result.update(recorder.list_metrics(), key_map)
2021-03-17 14:05:29 +01:00
if not len(result):
print("There are no valid recorders for {:}".format(experiment))
continue
2021-03-08 04:22:20 +01:00
head_str, value_str = result.info(all_keys, verbose=verbose)
2021-03-06 15:13:22 +01:00
head_strs.append(head_str)
value_strs.append(value_str)
names.append(experiment.name)
2021-03-18 09:02:55 +01:00
info_str_dict = compare_results(
head_strs, value_strs, names, space=10, verbose=verbose
)
2021-03-08 04:22:20 +01:00
info_value_dict = dict(heads=head_strs, values=value_strs, names=names)
return info_str_dict, info_value_dict
2021-03-06 15:13:22 +01:00
if __name__ == "__main__":
parser = argparse.ArgumentParser("Show Results")
2021-03-08 04:22:20 +01:00
def str2bool(v):
if isinstance(v, bool):
return v
elif v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
parser.add_argument(
2021-03-18 09:02:55 +01:00
"--save_dir",
type=str,
nargs="+",
2021-03-19 05:13:35 +01:00
default=[],
2021-03-18 09:02:55 +01:00
help="The checkpoint directory.",
)
parser.add_argument(
"--verbose",
type=str2bool,
default=False,
help="Print detailed log information or not.",
2021-03-08 04:22:20 +01:00
)
2021-03-06 15:13:22 +01:00
args = parser.parse_args()
2021-03-08 04:22:20 +01:00
print("Show results of {:}".format(args.save_dir))
2021-03-19 05:13:35 +01:00
if not args.save_dir:
raise ValueError("Receive no input directory for [args.save_dir]")
2021-03-08 04:22:20 +01:00
2021-03-06 15:13:22 +01:00
provider_uri = "~/.qlib/qlib_data/cn_data"
qlib.init(provider_uri=provider_uri, region=REG_CN)
2021-03-08 04:22:20 +01:00
all_info_dict = []
for save_dir in args.save_dir:
_, info_dict = query_info(save_dir, args.verbose)
all_info_dict.append(info_dict)
info_dict = QResult.merge_dict(all_info_dict)
2021-03-18 09:02:55 +01:00
compare_results(
info_dict["heads"],
info_dict["values"],
info_dict["names"],
space=10,
verbose=True,
sort_key=True,
)