mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-09 22:10:56 +08:00
Format with black
This commit is contained in:
@@ -1032,12 +1032,12 @@ def register_all_wrappers():
|
||||
"""register_all_wrappers"""
|
||||
logger = get_module_logger("data")
|
||||
module = get_module_by_module_path("qlib.data")
|
||||
|
||||
|
||||
_calendar_provider = init_instance_by_config(C.calendar_provider, module)
|
||||
if getattr(C, "calendar_cache", None) is not None:
|
||||
_calendar_cache_config = {}
|
||||
_calendar_cache_config.update(C.calendar_cache)
|
||||
_calendar_cache_config['kwargs'].update(provider=_calendar_provider)
|
||||
_calendar_cache_config["kwargs"].update(provider=_calendar_provider)
|
||||
_calendar_provider = init_instance_by_config(_calendar_cache_config, module)
|
||||
register_wrapper(Cal, _calendar_provider, "qlib.data")
|
||||
logger.debug(f"registering Cal {C.calendar_provider}-{C.calenar_cache}")
|
||||
@@ -1056,7 +1056,7 @@ def register_all_wrappers():
|
||||
if getattr(C, "expression_cache", None) is not None:
|
||||
_expression_cache_config = {}
|
||||
_expression_cache_config.update(C.expression_cache)
|
||||
_expression_cache_config['kwargs'].update(provider=_eprovider)
|
||||
_expression_cache_config["kwargs"].update(provider=_eprovider)
|
||||
_eprovider = init_instance_by_config(C.expression_cache, module)
|
||||
register_wrapper(ExpressionD, _eprovider, "qlib.data")
|
||||
logger.debug(f"registering ExpressioneD {C.expression_provider}-{C.expression_cache}")
|
||||
@@ -1065,7 +1065,7 @@ def register_all_wrappers():
|
||||
if getattr(C, "dataset_cache", None) is not None:
|
||||
_dataset_cache_config = {}
|
||||
_dataset_cache_config.update(C.dataset_cache)
|
||||
_dataset_cache_config['kwargs'].update(provider=_dprovider)
|
||||
_dataset_cache_config["kwargs"].update(provider=_dprovider)
|
||||
_dprovider = init_instance_by_config(_dataset_cache_config, module)
|
||||
register_wrapper(DatasetD, _dprovider, "qlib.data")
|
||||
logger.debug(f"registering DataseteD {C.dataset_provider}-{C.dataset_cache}")
|
||||
|
||||
@@ -125,7 +125,7 @@ class DataHandler(Serializable):
|
||||
selector: Union[pd.Timestamp, slice, str],
|
||||
level: Union[str, int] = "datetime",
|
||||
col_set: Union[str, List[str]] = CS_ALL,
|
||||
squeeze: bool = False
|
||||
squeeze: bool = False,
|
||||
) -> pd.DataFrame:
|
||||
"""
|
||||
fetch data from underlying data source
|
||||
@@ -184,17 +184,18 @@ class DataHandler(Serializable):
|
||||
cur_date (pd.Timestamp or str): current date
|
||||
periods (int): number of periods
|
||||
"""
|
||||
trading_dates = self._data.index.unique(level='datetime')
|
||||
trading_dates = self._data.index.unique(level="datetime")
|
||||
cur_loc = trading_dates.get_loc(cur_date)
|
||||
pre_loc = cur_loc - periods + 1
|
||||
if pre_loc < 0:
|
||||
warnings.warn('`periods` is too large. the first date will be returned.')
|
||||
warnings.warn("`periods` is too large. the first date will be returned.")
|
||||
pre_loc = 0
|
||||
ref_date = trading_dates[pre_loc]
|
||||
return slice(ref_date, cur_date)
|
||||
|
||||
def get_range_iterator(self, periods: int, min_periods: Optional[int] = None,
|
||||
**kwargs) -> Iterator[Tuple[pd.Timestamp, pd.DataFrame]]:
|
||||
def get_range_iterator(
|
||||
self, periods: int, min_periods: Optional[int] = None, **kwargs
|
||||
) -> Iterator[Tuple[pd.Timestamp, pd.DataFrame]]:
|
||||
"""
|
||||
get a iterator of sliced data with given periods
|
||||
|
||||
@@ -203,7 +204,7 @@ class DataHandler(Serializable):
|
||||
min_periods (int): minimum periods for sliced dataframe
|
||||
kwargs (dict): will be passed to `self.fetch`
|
||||
"""
|
||||
trading_dates = self._data.index.unique(level='datetime')
|
||||
trading_dates = self._data.index.unique(level="datetime")
|
||||
if min_periods is None:
|
||||
min_periods = periods
|
||||
for cur_date in trading_dates[min_periods:]:
|
||||
|
||||
@@ -9,10 +9,12 @@ from typing import Tuple
|
||||
|
||||
from qlib.data import D
|
||||
|
||||
|
||||
class DataLoader(abc.ABC):
|
||||
'''
|
||||
"""
|
||||
DataLoader is designed for loading raw data from original data source.
|
||||
'''
|
||||
"""
|
||||
|
||||
@abc.abstractmethod
|
||||
def load(self, instruments, start_time=None, end_time=None) -> pd.DataFrame:
|
||||
"""
|
||||
@@ -47,7 +49,8 @@ class DataLoader(abc.ABC):
|
||||
|
||||
|
||||
class QlibDataLoader(DataLoader):
|
||||
'''Same as QlibDataLoader. The fields can be define by config'''
|
||||
"""Same as QlibDataLoader. The fields can be define by config"""
|
||||
|
||||
def __init__(self, config: Tuple[list, tuple, dict], filter_pipe=None):
|
||||
"""
|
||||
Parameters
|
||||
@@ -64,7 +67,7 @@ class QlibDataLoader(DataLoader):
|
||||
|
||||
<fields_info> := ["expr", ...] | (["expr", ...], ["col_name", ...])
|
||||
"""
|
||||
self.is_group = isinstance(config, dict)
|
||||
self.is_group = isinstance(config, dict)
|
||||
|
||||
if self.is_group:
|
||||
self.fields = {grp: self._parse_fields_info(fields_info) for grp, fields_info in config.items()}
|
||||
@@ -86,15 +89,17 @@ class QlibDataLoader(DataLoader):
|
||||
if isinstance(instruments, str):
|
||||
instruments = D.instruments(instruments, filter_pipe=self.filter_pipe)
|
||||
elif self.filter_pipe is not None:
|
||||
warnings.warn('`filter_pipe` is not None, but it will not be used with `instruments` as list')
|
||||
warnings.warn("`filter_pipe` is not None, but it will not be used with `instruments` as list")
|
||||
|
||||
def _get_df(exprs, names):
|
||||
df = D.features(instruments, exprs, start_time, end_time)
|
||||
df.columns = names
|
||||
return df
|
||||
|
||||
if self.is_group:
|
||||
df = pd.concat({grp: _get_df(exprs, names) for grp, (exprs, names) in self.fields.items()}, axis=1)
|
||||
else:
|
||||
exprs, names = self.fields
|
||||
df = _get_df(exprs, names)
|
||||
df = df.swaplevel().sort_index() # NOTE: always return <datetime, instrument>
|
||||
df = df.swaplevel().sort_index() # NOTE: always return <datetime, instrument>
|
||||
return df
|
||||
|
||||
@@ -701,7 +701,7 @@ class Rolling(ExpressionOps):
|
||||
if self.N == 0:
|
||||
return np.inf
|
||||
if 0 < self.N < 1:
|
||||
return int(np.log(1e-6) / np.log(1 - self.N)) # (1 - N)**window == 1e-6
|
||||
return int(np.log(1e-6) / np.log(1 - self.N)) # (1 - N)**window == 1e-6
|
||||
return self.feature.get_longest_back_rolling() + self.N - 1
|
||||
|
||||
def get_extended_window_size(self):
|
||||
|
||||
Reference in New Issue
Block a user