Skip to content

Commit

Permalink
[807] Move the REG_CONSTANT/EPS to constant.py. (microsoft#811)
Browse files Browse the repository at this point in the history
* [807] Move the REG_CONSTANT to constant.py.

* import REG_US.

* Move EPS to constant.py.
  • Loading branch information
ChiahungTai authored Jan 9, 2022
1 parent 382abab commit 184ce34
Show file tree
Hide file tree
Showing 26 changed files with 42 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ We recommend users to prepare their own data if they have a high-quality dataset
```python
import qlib
from qlib.data import D
from qlib.config import REG_CN
from qlib.constant import REG_CN
# Initialization
mount_path = "~/.qlib/qlib_data/cn_data" # target_dir
Expand Down
2 changes: 1 addition & 1 deletion docs/component/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ The `trade unit` defines the unit number of stocks can be used in a trade, and t

.. code-block:: python
from qlib.config import REG_CN
from qlib.constant import REG_CN
qlib.init(provider_uri='~/.qlib/qlib_data/cn_data', region=REG_CN)
Expand Down
10 changes: 5 additions & 5 deletions docs/start/initialization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Initialize Qlib before calling other APIs: run following code in python.
import qlib
# region in [REG_CN, REG_US]
from qlib.config import REG_CN
from qlib.constant import REG_CN
provider_uri = "~/.qlib/qlib_data/cn_data" # target_dir
qlib.init(provider_uri=provider_uri, region=REG_CN)
Expand All @@ -42,10 +42,10 @@ Besides `provider_uri` and `region`, `qlib.init` has other parameters. The follo
- `provider_uri`
Type: str. The URI of the Qlib data. For example, it could be the location where the data loaded by ``get_data.py`` are stored.
- `region`
Type: str, optional parameter(default: `qlib.config.REG_CN`).
Currently: ``qlib.config.REG_US`` ('us') and ``qlib.config.REG_CN`` ('cn') is supported. Different value of `region` will result in different stock market mode.
- ``qlib.config.REG_US``: US stock market.
- ``qlib.config.REG_CN``: China stock market.
Type: str, optional parameter(default: `qlib.constant.REG_CN`).
Currently: ``qlib.constant.REG_US`` ('us') and ``qlib.constant.REG_CN`` ('cn') is supported. Different value of `region` will result in different stock market mode.
- ``qlib.constant.REG_US``: US stock market.
- ``qlib.constant.REG_CN``: China stock market.

Different modes will result in different trading limitations and costs.
The region is just `shortcuts for defining a batch of configurations <https://github.com/microsoft/qlib/blob/main/qlib/config.py#L239>`_. Users can set the key configurations manually if the existing region setting can't meet their requirements.
Expand Down
3 changes: 2 additions & 1 deletion examples/highfreq/highfreq_processor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pandas as pd
from qlib.constant import EPS
from qlib.data.dataset.processor import Processor
from qlib.data.dataset.utils import fetch_df_by_index

Expand Down Expand Up @@ -27,7 +28,7 @@ def fit(self, df_features):
part_values = np.log1p(part_values)
self.feature_med[name] = np.nanmedian(part_values)
part_values = part_values - self.feature_med[name]
self.feature_std[name] = np.nanmedian(np.absolute(part_values)) * 1.4826 + 1e-12
self.feature_std[name] = np.nanmedian(np.absolute(part_values)) * 1.4826 + EPS
part_values = part_values / self.feature_std[name]
self.feature_vmax[name] = np.nanmax(part_values)
self.feature_vmin[name] = np.nanmin(part_values)
Expand Down
3 changes: 2 additions & 1 deletion examples/highfreq/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import qlib
import pickle
from qlib.config import REG_CN, HIGH_FREQ_CONFIG
from qlib.constant import REG_CN
from qlib.config import HIGH_FREQ_CONFIG

from qlib.utils import init_instance_by_config
from qlib.data.dataset.handler import DataHandlerLP
Expand Down
2 changes: 1 addition & 1 deletion examples/hyperparameter/LightGBM/hyperparameter_158.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import qlib
import optuna
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.utils import init_instance_by_config
from qlib.tests.config import CSI300_DATASET_CONFIG
from qlib.tests.data import GetData
Expand Down
2 changes: 1 addition & 1 deletion examples/hyperparameter/LightGBM/hyperparameter_360.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import qlib
import optuna
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.utils import init_instance_by_config
from qlib.tests.data import GetData
from qlib.tests.config import get_dataset_config, CSI300_MARKET, DATASET_ALPHA360_CLASS
Expand Down
2 changes: 1 addition & 1 deletion examples/model_interpreter/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


import qlib
from qlib.config import REG_CN
from qlib.constant import REG_CN

from qlib.utils import init_instance_by_config
from qlib.tests.data import GetData
Expand Down
2 changes: 1 addition & 1 deletion examples/model_rolling/task_manager_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import fire
import qlib
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.workflow import R
from qlib.workflow.task.gen import RollingGen, task_generator
from qlib.workflow.task.manage import TaskManager, run_task
Expand Down
3 changes: 2 additions & 1 deletion examples/nested_decision_execution/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@
import qlib
import fire
import pandas as pd
from qlib.config import REG_CN, HIGH_FREQ_CONFIG
from qlib.constant import REG_CN
from qlib.config import HIGH_FREQ_CONFIG
from qlib.data import D
from qlib.utils import exists_qlib_data, init_instance_by_config, flatten_dict
from qlib.workflow import R
Expand Down
2 changes: 1 addition & 1 deletion examples/online_srv/update_online_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import copy
import fire
import qlib
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.model.trainer import task_train
from qlib.workflow.online.utils import OnlineToolR
from qlib.tests.config import CSI300_GBDT_TASK
Expand Down
2 changes: 1 addition & 1 deletion examples/rolling_process_data/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pickle

from datetime import datetime
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.data.dataset.handler import DataHandlerLP
from qlib.utils import init_instance_by_config
from qlib.tests.data import GetData
Expand Down
1 change: 0 additions & 1 deletion examples/run_all_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from pprint import pprint

import qlib
from qlib.config import REG_CN
from qlib.workflow import R
from qlib.tests.data import GetData

Expand Down
2 changes: 1 addition & 1 deletion examples/workflow_by_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"\n",
"import qlib\n",
"import pandas as pd\n",
"from qlib.config import REG_CN\n",
"from qlib.constant import REG_CN\n",
"from qlib.utils import exists_qlib_data, init_instance_by_config\n",
"from qlib.workflow import R\n",
"from qlib.workflow.record_temp import SignalRecord, PortAnaRecord\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/workflow_by_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the MIT License.

import qlib
from qlib.config import REG_CN
from qlib.constant import REG_CN
from qlib.utils import init_instance_by_config, flatten_dict
from qlib.workflow import R
from qlib.workflow.record_temp import SignalRecord, PortAnaRecord, SigAnaRecord
Expand Down
3 changes: 2 additions & 1 deletion qlib/backtest/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import pandas as pd

from ..data.data import D
from ..config import C, REG_CN
from ..config import C
from ..constant import REG_CN
from ..log import get_module_logger
from .decision import Order, OrderDir, OrderHelper
from .high_performance_ds import BaseQuote, PandasQuote, NumpyQuote
Expand Down
6 changes: 2 additions & 4 deletions qlib/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from typing import Optional, Union
from typing import TYPE_CHECKING

from qlib.constant import REG_CN, REG_US

if TYPE_CHECKING:
from qlib.utils.time import Freq

Expand Down Expand Up @@ -74,10 +76,6 @@ def set_conf_from_C(self, config_c):
self.update(**config_c.__dict__["_config"])


# REGION CONST
REG_CN = "cn"
REG_US = "us"

# pickle.dump protocol version: https://docs.python.org/3/library/pickle.html#data-stream-format
PROTOCOL_VERSION = 4

Expand Down
9 changes: 9 additions & 0 deletions qlib/constant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# REGION CONST
REG_CN = "cn"
REG_US = "us"

# Epsilon for avoiding division by zero.
EPS = 1e-12
3 changes: 2 additions & 1 deletion qlib/contrib/model/pytorch_tra.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from tqdm import tqdm

from qlib.utils import get_or_create_path
from qlib.constant import EPS
from qlib.log import get_module_logger
from qlib.model.base import Model
from qlib.contrib.data.dataset import MTSDatasetH
Expand Down Expand Up @@ -791,7 +792,7 @@ def minmax_norm(x):
xmin = x.min(dim=-1, keepdim=True).values
xmax = x.max(dim=-1, keepdim=True).values
mask = (xmin == xmax).squeeze()
x = (x - xmin) / (xmax - xmin + 1e-12)
x = (x - xmin) / (xmax - xmin + EPS)
x[mask] = 1
return x

Expand Down
5 changes: 1 addition & 4 deletions qlib/data/dataset/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
from typing import Union, Text
import numpy as np
import pandas as pd
import copy

from ...log import TimeInspector
from ...constant import EPS
from .utils import fetch_df_by_index
from ...utils.serial import Serializable
from ...utils.paral import datetime_groupby_apply

EPS = 1e-12


def get_group_columns(df: pd.DataFrame, group: Union[Text, None]):
"""
Expand Down
2 changes: 1 addition & 1 deletion qlib/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from .data import GetData
from .. import init
from ..config import REG_CN
from ..constant import REG_CN


class TestAutoData(unittest.TestCase):
Expand Down
3 changes: 2 additions & 1 deletion scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ qlib.init(provider_uri=provider_uri, region=REG_US)
```python
import qlib
from qlib.config import REG_CN
from qlib.constant import REG_CN

provider_uri = "~/.qlib/qlib_data/cn_data" # target_dir
qlib.init(provider_uri=provider_uri, region=REG_CN)
```
2 changes: 1 addition & 1 deletion scripts/data_collector/fund/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pandas as pd
from loguru import logger
from dateutil.tz import tzlocal
from qlib.config import REG_CN as REGION_CN
from qlib.constant import REG_CN as REGION_CN

CUR_DIR = Path(__file__).resolve().parent
sys.path.append(str(CUR_DIR.parent.parent))
Expand Down
2 changes: 1 addition & 1 deletion scripts/data_collector/yahoo/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from qlib.tests.data import GetData
from qlib.utils import code_to_fname, fname_to_code, exists_qlib_data
from qlib.config import REG_CN as REGION_CN
from qlib.constant import REG_CN as REGION_CN

CUR_DIR = Path(__file__).resolve().parent
sys.path.append(str(CUR_DIR.parent.parent))
Expand Down
1 change: 0 additions & 1 deletion tests/backtest/test_high_freq_trading.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import qlib
from qlib.tests import TestAutoData
import unittest
from qlib.config import REG_CN, HIGH_FREQ_CONFIG
import pandas as pd


Expand Down
1 change: 0 additions & 1 deletion tests/rolling_tests/test_update_pred.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pandas as pd

import qlib
from qlib.config import REG_CN
from qlib.data import D
from qlib.model.trainer import task_train
from qlib.tests import TestAutoData
Expand Down

0 comments on commit 184ce34

Please sign in to comment.