From 5fa56703ae07d4fd797e592b81cf5e4896e12d94 Mon Sep 17 00:00:00 2001 From: Young Date: Tue, 26 Oct 2021 23:32:33 +0800 Subject: [PATCH] add handler pickle attr, enhance init_instance_by_config --- qlib/contrib/data/utils/sepdf.py | 3 ++- qlib/data/dataset/handler.py | 8 +++++++- qlib/utils/__init__.py | 12 +++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/qlib/contrib/data/utils/sepdf.py b/qlib/contrib/data/utils/sepdf.py index b8b4dacda..9650b7729 100644 --- a/qlib/contrib/data/utils/sepdf.py +++ b/qlib/contrib/data/utils/sepdf.py @@ -124,7 +124,8 @@ class SDFLoc: if isinstance(args, str): return self._sdf[args] elif isinstance(args, (tuple, list)): - return SepDataFrame({k: self._sdf[k] for k in args}, join=self.join) + new_df_dict = {k: self._sdf[k] for k in args} + return SepDataFrame(new_df_dict, join=self.join if self.join in args else args[0]) else: raise NotImplementedError(f"This type of input is not supported") elif self.axis == 0: diff --git a/qlib/data/dataset/handler.py b/qlib/data/dataset/handler.py index 47fda4686..134091c22 100644 --- a/qlib/data/dataset/handler.py +++ b/qlib/data/dataset/handler.py @@ -644,6 +644,12 @@ class DataHandlerLP(DataHandler): new_hd: DataHandlerLP = object.__new__(DataHandlerLP) new_hd.from_cast = True # add a mark for the casted instance - for key in list(DataHandlerLP.ATTR_MAP.values()) + ["instruments", "start_time", "end_time", "fetch_orig"]: + for key in list(DataHandlerLP.ATTR_MAP.values()) + [ + "instruments", + "start_time", + "end_time", + "fetch_orig", + "drop_raw", + ]: setattr(new_hd, key, getattr(handler, key, None)) return new_hd diff --git a/qlib/utils/__init__.py b/qlib/utils/__init__.py index f6a6632ea..12553411c 100644 --- a/qlib/utils/__init__.py +++ b/qlib/utils/__init__.py @@ -199,6 +199,7 @@ def get_callable_kwargs(config: Union[dict, str], default_module: Union[str, Mod ---------- config : [dict, str] similar to config + please refer to the doc of init_instance_by_config default_module : Python module or str It should be a python module to load the class type @@ -219,9 +220,12 @@ def get_callable_kwargs(config: Union[dict, str], default_module: Union[str, Mod _callable = config["class"] # the class type itself is passed in kwargs = config.get("kwargs", {}) elif isinstance(config, str): - module = get_module_by_module_path(default_module) + # a.b.c.ClassName + *m_path, cls = config.split(".") + m_path = ".".join(m_path) + module = get_module_by_module_path(default_module if m_path == "" else m_path) - _callable = getattr(module, config) + _callable = getattr(module, cls) kwargs = {} else: raise NotImplementedError(f"This type of input is not supported") @@ -260,7 +264,9 @@ def init_instance_by_config( 1) specify a pickle object - path like 'file:////obj.pkl' 2) specify a class name - - "ClassName": getattr(module, config)() will be used. + - "ClassName": getattr(module, "ClassName")() will be used. + 3) specify module path with class name + - "a.b.c.ClassName" getattr(, "ClassName")() will be used. object example: instance of accept_types default_module : Python module