mirror of
https://github.com/microsoft/qlib.git
synced 2026-07-14 16:26:55 +08:00
refactor qlib conf&init. Fix test bug
This commit is contained in:
201
qlib/config.py
201
qlib/config.py
@@ -1,5 +1,62 @@
|
||||
# Copyright (c) Microsoft Corporation.
|
||||
# Licensed under the MIT License.
|
||||
"""
|
||||
About the configs
|
||||
=================
|
||||
|
||||
The config will based on _default_config.
|
||||
Two modes are supported
|
||||
- client
|
||||
- server
|
||||
|
||||
"""
|
||||
|
||||
import copy
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
|
||||
class Config:
|
||||
|
||||
def __init__(self, default_conf):
|
||||
self.__dict__["_default_config"] = default_conf # avoiding conflictions with __getattr__
|
||||
self.reset()
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self.__dict__["_config"][key]
|
||||
|
||||
def __getattr__(self, attr):
|
||||
try:
|
||||
return self.__dict__["_config"][attr]
|
||||
except KeyError:
|
||||
return AttributeError(f"No such {attr} in self._config")
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.__dict__["_config"][key] = value
|
||||
|
||||
def __setattr__(self, attr, value):
|
||||
self.__dict__["_config"][attr] = value
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.__dict__["_config"]
|
||||
|
||||
def __getstate__(self):
|
||||
return self.__dict__
|
||||
|
||||
def __setstate__(self, state):
|
||||
self.__dict__.update(state)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.__dict__["_config"])
|
||||
|
||||
def __repr__(self):
|
||||
return str(self.__dict__["_config"])
|
||||
|
||||
def reset(self):
|
||||
self.__dict__["_config"] = copy.deepcopy(self._default_config)
|
||||
|
||||
def update(self, *args, **kwargs):
|
||||
self.__dict__["_config"].update(*args, **kwargs)
|
||||
|
||||
|
||||
# REGION CONST
|
||||
@@ -70,50 +127,52 @@ _default_config = {
|
||||
},
|
||||
}
|
||||
|
||||
_default_server_config = {
|
||||
# data provider config
|
||||
"calendar_provider": "LocalCalendarProvider",
|
||||
"instrument_provider": "LocalInstrumentProvider",
|
||||
"feature_provider": "LocalFeatureProvider",
|
||||
"expression_provider": "LocalExpressionProvider",
|
||||
"dataset_provider": "LocalDatasetProvider",
|
||||
"provider": "LocalProvider",
|
||||
# config it in qlib.init()
|
||||
"provider_uri": "",
|
||||
# redis
|
||||
"redis_host": "127.0.0.1",
|
||||
"redis_port": 6379,
|
||||
"redis_task_db": 1,
|
||||
"kernels": 64,
|
||||
# cache
|
||||
"expression_cache": "DiskExpressionCache",
|
||||
"dataset_cache": "DiskDatasetCache",
|
||||
}
|
||||
MODE_CONF = {
|
||||
'server': {
|
||||
# data provider config
|
||||
"calendar_provider": "LocalCalendarProvider",
|
||||
"instrument_provider": "LocalInstrumentProvider",
|
||||
"feature_provider": "LocalFeatureProvider",
|
||||
"expression_provider": "LocalExpressionProvider",
|
||||
"dataset_provider": "LocalDatasetProvider",
|
||||
"provider": "LocalProvider",
|
||||
# config it in qlib.init()
|
||||
"provider_uri": "",
|
||||
# redis
|
||||
"redis_host": "127.0.0.1",
|
||||
"redis_port": 6379,
|
||||
"redis_task_db": 1,
|
||||
"kernels": 64,
|
||||
# cache
|
||||
"expression_cache": "DiskExpressionCache",
|
||||
"dataset_cache": "DiskDatasetCache",
|
||||
},
|
||||
|
||||
_default_client_config = {
|
||||
# data provider config
|
||||
"calendar_provider": "LocalCalendarProvider",
|
||||
"instrument_provider": "LocalInstrumentProvider",
|
||||
"feature_provider": "LocalFeatureProvider",
|
||||
"expression_provider": "LocalExpressionProvider",
|
||||
"dataset_provider": "LocalDatasetProvider",
|
||||
"provider": "LocalProvider",
|
||||
# config it in user's own code
|
||||
"provider_uri": "~/.qlib/qlib_data/cn_data",
|
||||
# cache
|
||||
# Using parameter 'remote' to announce the client is using server_cache, and the writing access will be disabled.
|
||||
"expression_cache": "DiskExpressionCache",
|
||||
"dataset_cache": "DiskDatasetCache",
|
||||
"calendar_cache": None,
|
||||
# client config
|
||||
"kernels": 16,
|
||||
"mount_path": "~/.qlib/qlib_data/cn_data",
|
||||
"auto_mount": False, # The nfs is already mounted on our server[auto_mount: False].
|
||||
# The nfs should be auto-mounted by qlib on other
|
||||
# serversS(such as PAI) [auto_mount:True]
|
||||
"timeout": 100,
|
||||
"logging_level": "INFO",
|
||||
"region": REG_CN,
|
||||
'client': {
|
||||
# data provider config
|
||||
"calendar_provider": "LocalCalendarProvider",
|
||||
"instrument_provider": "LocalInstrumentProvider",
|
||||
"feature_provider": "LocalFeatureProvider",
|
||||
"expression_provider": "LocalExpressionProvider",
|
||||
"dataset_provider": "LocalDatasetProvider",
|
||||
"provider": "LocalProvider",
|
||||
# config it in user's own code
|
||||
"provider_uri": "~/.qlib/qlib_data/cn_data",
|
||||
# cache
|
||||
# Using parameter 'remote' to announce the client is using server_cache, and the writing access will be disabled.
|
||||
"expression_cache": "DiskExpressionCache",
|
||||
"dataset_cache": "DiskDatasetCache",
|
||||
"calendar_cache": None,
|
||||
# client config
|
||||
"kernels": 16,
|
||||
"mount_path": None,
|
||||
"auto_mount": False, # The nfs is already mounted on our server[auto_mount: False].
|
||||
# The nfs should be auto-mounted by qlib on other
|
||||
# serversS(such as PAI) [auto_mount:True]
|
||||
"timeout": 100,
|
||||
"logging_level": "INFO",
|
||||
"region": REG_CN,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,37 +190,43 @@ _default_region_config = {
|
||||
}
|
||||
|
||||
|
||||
class Config:
|
||||
def __getitem__(self, key):
|
||||
return _default_config[key]
|
||||
class QlibConfig(Config):
|
||||
# URI_TYPE
|
||||
LOCAL_URI = 'local'
|
||||
NFS_URI = 'nfs'
|
||||
|
||||
def __getattr__(self, attr):
|
||||
try:
|
||||
return _default_config[attr]
|
||||
except KeyError:
|
||||
return AttributeError(f"No such {attr} in _default_config")
|
||||
def set_mode(self, mode):
|
||||
# raise KeyError
|
||||
self.update(MODE_CONF[mode])
|
||||
# TODO: update region based on kwargs
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
_default_config[key] = value
|
||||
def set_region(self, region):
|
||||
# raise KeyError
|
||||
self.update(_default_region_config[region])
|
||||
|
||||
def __setattr__(self, attr, value):
|
||||
_default_config[attr] = value
|
||||
def resolve_path(self):
|
||||
# resolve path
|
||||
if self["mount_path"] is not None:
|
||||
self["mount_path"]= str(Path(self["mount_path"]).expanduser().resolve())
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in _default_config
|
||||
if self.get_uri_type() == QlibConfig.LOCAL_URI:
|
||||
self["provider_uri"] = str(Path(self["provider_uri"]).expanduser().resolve())
|
||||
|
||||
def __getstate__(self):
|
||||
return _default_config
|
||||
def get_uri_type(self):
|
||||
rm = re.match("^[^/ ]+:.+", self["provider_uri"])
|
||||
if rm is None:
|
||||
return QlibConfig.LOCAL_URI
|
||||
else:
|
||||
return QlibConfig.NFS_URI
|
||||
|
||||
def __setstate__(self, state):
|
||||
_default_config.update(state)
|
||||
|
||||
def __str__(self):
|
||||
return str(_default_config)
|
||||
|
||||
def __repr__(self):
|
||||
return str(_default_config)
|
||||
def get_data_path(self):
|
||||
if self.get_uri_type() == QlibConfig.LOCAL_URI:
|
||||
return self['provider_uri']
|
||||
elif self.get_uri_type() == QlibConfig.NFS_URI:
|
||||
return self['mount_path']
|
||||
else:
|
||||
raise NotImplementedError(f"This type of uri is not supported")
|
||||
|
||||
|
||||
# global config
|
||||
C = Config()
|
||||
C = QlibConfig(_default_config)
|
||||
|
||||
Reference in New Issue
Block a user