Skip to content

Commit

Permalink
style: 💄 lint
Browse files Browse the repository at this point in the history
fix lint matched pylint
  • Loading branch information
duyifanict committed Nov 28, 2024
1 parent 80361c6 commit 3761512
Show file tree
Hide file tree
Showing 23 changed files with 152 additions and 95 deletions.
9 changes: 7 additions & 2 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
[MASTER]

# Files or directories to be skipped. They should be base names, not paths.
ignore=third_party
ignore=baselines,assets

# Files or directories matching the regex patterns are skipped. The regex
# matches against base names, not paths.
ignore-patterns=
ignore-patterns=^\.|^_|^.*\.md|^.*\.txt|^.*\.CFF|^LICENSE

# Pickle collected data for later comparisons.
persistent=no
Expand Down Expand Up @@ -85,6 +85,7 @@ disable=abstract-method,
input-builtin,
intern-builtin,
invalid-str-codec,
invalid-name,
locally-disabled,
logging-format-interpolation,
logging-fstring-interpolation,
Expand Down Expand Up @@ -433,3 +434,7 @@ valid-metaclass-classmethod-first-arg=mcs
overgeneral-exceptions=builtins.StandardError,
builtins.Exception,
builtins.BaseException

[DESIGN]
# https://pylint.pycqa.org/en/latest/user_guide/messages/refactor/too-many-positional-arguments.html
max-positional-arguments=10
2 changes: 1 addition & 1 deletion basicts/runners/base_tsf_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def train_iters(self, epoch: int, iter_index: int, data: Union[torch.Tensor, Tup
forward_return['prediction'] = forward_return['prediction'][:, :cl_length, :, :]
forward_return['target'] = forward_return['target'][:, :cl_length, :, :]
loss = self.metric_forward(self.loss, forward_return)
self.update_epoch_meter(f'train/loss', loss.item())
self.update_epoch_meter('train/loss', loss.item())

for metric_name, metric_func in self.metrics.items():
metric_item = self.metric_forward(metric_func, forward_return)
Expand Down
1 change: 0 additions & 1 deletion basicts/runners/optim/optimizers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# define more optimizers here
import os
import inspect
from typing import Union, Tuple, Optional

Expand Down
1 change: 0 additions & 1 deletion basicts/runners/runner_zoo/no_bp_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ class NoBPRunner(SimpleTimeSeriesForecastingRunner):

def backward(self, loss: torch.Tensor):
pass
return
4 changes: 2 additions & 2 deletions basicts/runners/runner_zoo/simple_tsf_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ def forward(self, data: Dict, epoch: int = None, iter_num: int = None, train: bo
# Select input features
history_data = self.select_input_features(history_data)
future_data_4_dec = self.select_input_features(future_data)

if not train:
# For non-training phases, use only temporal features
future_data_4_dec[..., 0] = torch.empty_like(future_data_4_dec[..., 0])

# Forward pass through the model
model_return = self.model(history_data=history_data, future_data=future_data_4_dec,
model_return = self.model(history_data=history_data, future_data=future_data_4_dec,
batch_seen=iter_num, epoch=epoch, train=train)

# Parse model return
Expand Down
2 changes: 1 addition & 1 deletion basicts/scaler/min_max_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def transform(self, input_data: torch.Tensor) -> torch.Tensor:
Returns:
torch.Tensor: The normalized data with the same shape as the input.
"""

_min = self.min.to(input_data.device)
_max = self.max.to(input_data.device)
input_data[..., self.target_channel] = (input_data[..., self.target_channel] - _min) / (_max - _min)
Expand Down
1 change: 0 additions & 1 deletion basicts/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import time
from typing import Dict
from functools import partial

import torch
Expand Down
1 change: 1 addition & 0 deletions examples/arch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=unused-argument
import torch
from torch import nn

Expand Down
54 changes: 35 additions & 19 deletions examples/complete_config.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
############################## Import Dependencies ##############################

import os
import sys
from easydict import EasyDict

# TODO: Remove this when basicts can be installed via pip
sys.path.append(os.path.abspath(__file__ + '/../../..'))

# Import metrics & loss functions
from basicts.metrics import masked_mae, masked_mape, masked_rmse
# Import dataset class
Expand All @@ -15,10 +7,14 @@
from basicts.runners import SimpleTimeSeriesForecastingRunner
# Import scaler class
from basicts.scaler import ZScoreScaler
# Import dataset settings
from basicts.utils import get_regular_settings
# Import model architecture
from .arch import MultiLayerPerceptron as MLP

from basicts.utils import get_regular_settings

import os
from easydict import EasyDict

############################## Hot Parameters ##############################

Expand Down Expand Up @@ -82,7 +78,9 @@

############################## Scaler Configuration ##############################

CFG.SCALER = EasyDict() # Scaler settings. Default: None. If not specified, the data will not be normalized, i.e., the data will be used directly for training, validation, and test.
# Scaler settings. Default: None.
# If not specified, the data will not be normalized, i.e., the data will be used directly for training, validation, and test.
CFG.SCALER = EasyDict()

# Scaler settings
CFG.SCALER.TYPE = ZScoreScaler # Scaler class
Expand All @@ -101,11 +99,22 @@
CFG.MODEL.NAME = MODEL_ARCH.__name__ # Model name, must be specified, used for saving checkpoints and set the process title.
CFG.MODEL.ARCH = MODEL_ARCH # Model architecture, must be specified.
CFG.MODEL.PARAM = MODEL_PARAM # Model parameters
CFG.MODEL.FORWARD_FEATURES = [0, 1, 2] # Features used as input. The size of input data `history_data` is usually [B, L, N, C], this parameter specifies the index of the last dimension, i.e., history_data[:, :, :, CFG.MODEL.FORWARD_FEATURES].
CFG.MODEL.TARGET_FEATURES = [0] # Features used as output. The size of target data `future_data` is usually [B, L, N, C], this parameter specifies the index of the last dimension, i.e., future_data[:, :, :, CFG.MODEL.TARGET_FEATURES].
CFG.MODEL.TARGET_TIME_SERIES = [5, 6] # The index of the time series to be predicted, default is None. This setting is particularly useful in a Multivariate-to-Univariate setup. For example, if 7 time series are input and the last two need to be predicted, you can set `CFG.MODEL.TARGET_TIME_SERIES=[5, 6]` to achieve this.
CFG.MODEL.SETUP_GRAPH = False # Whether to set up the computation graph. Default: False. Implementation of many works (e.g., DCRNN, GTS) acts like TensorFlow, which creates parameters in the first feedforward process.
CFG.MODEL.DDP_FIND_UNUSED_PARAMETERS = False # Controls the `find_unused_parameters parameter` of `torch.nn.parallel.DistributedDataParallel`. In distributed computing, if there are unused parameters in the forward process, PyTorch usually raises a RuntimeError. In such cases, this parameter should be set to True.
# Features used as input. The size of input data `history_data` is usually [B, L, N, C],
# this parameter specifies the index of the last dimension, i.e., history_data[:, :, :, CFG.MODEL.FORWARD_FEATURES].
CFG.MODEL.FORWARD_FEATURES = [0, 1, 2]
# Features used as output. The size of target data `future_data` is usually [B, L, N, C],
# this parameter specifies the index of the last dimension, i.e., future_data[:, :, :, CFG.MODEL.TARGET_FEATURES].
CFG.MODEL.TARGET_FEATURES = [0]
# The index of the time series to be predicted, default is None. This setting is particularly useful in a Multivariate-to-Univariate setup.
# For example, if 7 time series are input and the last two need to be predicted, you can set `CFG.MODEL.TARGET_TIME_SERIES=[5, 6]` to achieve this.
CFG.MODEL.TARGET_TIME_SERIES = [5, 6]
# Whether to set up the computation graph. Default: False.
# Implementation of many works (e.g., DCRNN, GTS) acts like TensorFlow, which creates parameters in the first feedforward process.
CFG.MODEL.SETUP_GRAPH = False
# Controls the `find_unused_parameters parameter` of `torch.nn.parallel.DistributedDataParallel`.
# In distributed computing, if there are unused parameters in the forward process, PyTorch usually raises a RuntimeError.
# In such cases, this parameter should be set to True.
CFG.MODEL.DDP_FIND_UNUSED_PARAMETERS = False

############################## Metrics Configuration ##############################

Expand All @@ -132,7 +141,12 @@
MODEL_ARCH.__name__,
'_'.join([DATA_NAME, str(CFG.TRAIN.NUM_EPOCHS), str(INPUT_LEN), str(OUTPUT_LEN)])
) # Directory to save checkpoints. Default: 'checkpoints/{MODEL_NAME}/{DATASET_NAME}_{NUM_EPOCHS}_{INPUT_LEN}_{OUTPUT_LEN}'
CFG.TRAIN.CKPT_SAVE_STRATEGY = None # Checkpoint save strategy. `CFG.TRAIN.CKPT_SAVE_STRATEGY` should be None, an int value, a list or a tuple. None: remove last checkpoint file every epoch. Default: None. Int: save checkpoint every `CFG.TRAIN.CKPT_SAVE_STRATEGY` epoch. List or Tuple: save checkpoint when epoch in `CFG.TRAIN.CKPT_SAVE_STRATEGY, remove last checkpoint file when last_epoch not in ckpt_save_strategy
# Checkpoint save strategy. `CFG.TRAIN.CKPT_SAVE_STRATEGY` should be None, an int value, a list or a tuple.
# Default: None.
# None: remove last checkpoint file every epoch.
# Int: save checkpoint every `CFG.TRAIN.CKPT_SAVE_STRATEGY` epoch.
# List or Tuple: save checkpoint when epoch in `CFG.TRAIN.CKPT_SAVE_STRATEGY, remove last checkpoint file when last_epoch not in ckpt_save_strategy
CFG.TRAIN.CKPT_SAVE_STRATEGY = None
CFG.TRAIN.FINETUNE_FROM = None # Checkpoint path for fine-tuning. Default: None. If not specified, the model will be trained from scratch.
CFG.TRAIN.STRICT_LOAD = True # Whether to strictly load the checkpoint. Default: True.

Expand Down Expand Up @@ -205,10 +219,12 @@
CFG.TEST.DATA.NUM_WORKERS = 0
CFG.TEST.DATA.PIN_MEMORY = False

############################## Evaluation Configuration ##############################

########################### Evaluation Configuration ##########################
CFG.EVAL = EasyDict()

# Evaluation parameters
CFG.EVAL.HORIZONS = [3, 6, 12] # The prediction horizons for evaluation. Default value: []. NOTE: HORIZONS[i] refers to testing **on the i-th** time step, representing the loss for that specific time step. This is a common setting in spatiotemporal forecasting. For long-sequence predictions, it is recommended to keep HORIZONS set to the default value [] to avoid confusion.
# The prediction horizons for evaluation. Default value: [].
# NOTE: HORIZONS[i] refers to testing **on the i-th** time step, representing the loss for that specific time step.
# This is a common setting in spatiotemporal forecasting. For long-sequence predictions, it is recommended to keep HORIZONS set to the default value [] to avoid confusion.
CFG.EVAL.HORIZONS = []
CFG.EVAL.USE_GPU = True # Whether to use GPU for evaluation. Default: True
46 changes: 28 additions & 18 deletions examples/complete_config_cn.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@

############################## 导入依赖 ##############################

import os
import sys
from easydict import EasyDict

# TODO: 当 basicts 可以通过 pip 安装时,移除这行代码
sys.path.append(os.path.abspath(__file__ + '/../../..'))

# 导入指标和损失函数
from basicts.metrics import masked_mae, masked_mape, masked_rmse
# 导入数据集类
Expand All @@ -16,10 +7,14 @@
from basicts.runners import SimpleTimeSeriesForecastingRunner
# 导入缩放器类
from basicts.scaler import ZScoreScaler
# 导入数据集配置
from basicts.utils import get_regular_settings
# 导入模型架构
from .arch import MultiLayerPerceptron as MLP

from basicts.utils import get_regular_settings

import os
from easydict import EasyDict

############################## 热门参数 ##############################

Expand Down Expand Up @@ -102,11 +97,19 @@
CFG.MODEL.NAME = MODEL_ARCH.__name__ # 模型名称,必须指定,用于保存检查点和设置进程标题。
CFG.MODEL.ARCH = MODEL_ARCH # 模型架构,必须指定。
CFG.MODEL.PARAM = MODEL_PARAM # 模型参数,必须指定。
CFG.MODEL.FORWARD_FEATURES = [0, 1, 2] # 作为输入使用的特征。输入数据的大小通常为 [B, L, N, C],此参数指定最后一个维度的索引,即 history_data[:, :, :, CFG.MODEL.FORWARD_FEATURES]。
CFG.MODEL.TARGET_FEATURES = [0] # 作为输出使用的特征。目标数据的大小通常为 [B, L, N, C],此参数指定最后一个维度的索引,即 future_data[:, :, :, CFG.MODEL.TARGET_FEATURES]。
CFG.MODEL.TARGET_TIME_SERIES = None # 待预测的时间序列索引,默认为None。该参数在多变量到单变量预测(Multivariate-to-Univariate)的场景下特别有用。例如,当输入7条时间序列时,若需要预测最后两条序列,可以通过设置`CFG.MODEL.TARGET_TIME_SERIES=[5, 6]`来实现。
CFG.MODEL.SETUP_GRAPH = False # 是否设置计算图。默认值:False。许多论文的实现(如 DCRNN,GTS)类似于 TensorFlow,需要第一次前向传播时建立计算图并创建参数。
CFG.MODEL.DDP_FIND_UNUSED_PARAMETERS = False # 控制 torch.nn.parallel.DistributedDataParallel 的 `find_unused_parameters` 参数。在分布式计算中,如果前向传播过程中存在未使用的参数,PyTorch 通常会抛出 RuntimeError。在这种情况下,应将此参数设置为 True。
# 作为输入使用的特征。输入数据的大小通常为 [B, L, N, C],
# 此参数指定最后一个维度的索引,即 history_data[:, :, :, CFG.MODEL.FORWARD_FEATURES]。
CFG.MODEL.FORWARD_FEATURES = [0, 1, 2]
# 作为输出使用的特征。目标数据的大小通常为 [B, L, N, C],此参数指定最后一个维度的索引,即 future_data[:, :, :, CFG.MODEL.TARGET_FEATURES]。
CFG.MODEL.TARGET_FEATURES = [0]
# 待预测的时间序列索引,默认为None。该参数在多变量到单变量预测(Multivariate-to-Univariate)的场景下特别有用。
# 例如,当输入7条时间序列时,若需要预测最后两条序列,可以通过设置`CFG.MODEL.TARGET_TIME_SERIES=[5, 6]`来实现。
CFG.MODEL.TARGET_TIME_SERIES = None
# 是否设置计算图。默认值:False。许多论文的实现(如 DCRNN,GTS)类似于 TensorFlow,需要第一次前向传播时建立计算图并创建参数。
CFG.MODEL.SETUP_GRAPH = False
# 控制 torch.nn.parallel.DistributedDataParallel 的 `find_unused_parameters` 参数。
# 在分布式计算中,如果前向传播过程中存在未使用的参数,PyTorch 通常会抛出 RuntimeError。在这种情况下,应将此参数设置为 True。
CFG.MODEL.DDP_FIND_UNUSED_PARAMETERS = False

############################## 指标配置 ##############################

Expand All @@ -128,12 +131,17 @@

# 训练参数
CFG.TRAIN.NUM_EPOCHS = NUM_EPOCHS
# 保存检查点的目录。默认值:'checkpoints/{MODEL_NAME}/{DATASET_NAME}_{NUM_EPOCHS}_{INPUT_LEN}_{OUTPUT_LEN}'
CFG.TRAIN.CKPT_SAVE_DIR = os.path.join(
'checkpoints',
MODEL_ARCH.__name__,
'_'.join([DATA_NAME, str(CFG.TRAIN.NUM_EPOCHS), str(INPUT_LEN), str(OUTPUT_LEN)])
) # 保存检查点的目录。默认值:'checkpoints/{MODEL_NAME}/{DATASET_NAME}_{NUM_EPOCHS}_{INPUT_LEN}_{OUTPUT_LEN}'
CFG.TRAIN.CKPT_SAVE_STRATEGY = None # 检查点保存策略。`CFG.TRAIN.CKPT_SAVE_STRATEGY` 可以是 None、整数值、列表或元组。默认值:None。None:每个 epoch 移除最后一个检查点文件。整数:每隔 `CFG.TRAIN.CKPT_SAVE_STRATEGY` 个 epoch 保存一次检查点。列表或元组:当 epoch 在 `CFG.TRAIN.CKPT_SAVE_STRATEGY` 中时保存检查点,当 last_epoch 不在 ckpt_save_strategy 中时移除最后一个检查点文件。“移除”操作是将最后一个检查点文件重命名为 .bak 文件,BasicTS会每个10个epoch清空一次.bak文件。
)
# 检查点保存策略。`CFG.TRAIN.CKPT_SAVE_STRATEGY` 可以是 None、整数值、列表或元组。默认值:None。
# None:每个 epoch 移除最后一个检查点文件。整数:每隔 `CFG.TRAIN.CKPT_SAVE_STRATEGY` 个 epoch 保存一次检查点。
# 列表或元组:当 epoch 在 `CFG.TRAIN.CKPT_SAVE_STRATEGY` 中时保存检查点,当 last_epoch 不在 ckpt_save_strategy 中时移除最后一个检查点文件。
# “移除”操作是将最后一个检查点文件重命名为 .bak 文件,BasicTS会每个10个epoch清空一次.bak文件。
CFG.TRAIN.CKPT_SAVE_STRATEGY = None
CFG.TRAIN.FINETUNE_FROM = None # 微调的检查点路径。默认值:None。如果未指定,模型将从头开始训练。
CFG.TRAIN.STRICT_LOAD = True # 是否严格加载检查点。默认值:True。

Expand Down Expand Up @@ -213,5 +221,7 @@
CFG.EVAL = EasyDict()

# 评估参数
CFG.EVAL.HORIZONS = [3, 6, 12] # 评估时的预测时间范围。默认值为 []。注意:HORIZONS[i] 指的是在 ”第 i 个时间片“ 上进行测试,表示该时间片的损失(Loss)。这是时空预测中的常见配置。对于长序列预测,建议将 HORIZONS 保持为默认值 [],以避免引发误解。
# 评估时的预测时间范围。默认值为 []。注意:HORIZONS[i] 指的是在 ”第 i 个时间片“ 上进行测试,表示该时间片的损失(Loss)。
# 这是时空预测中的常见配置。对于长序列预测,建议将 HORIZONS 保持为默认值 [],以避免引发误解。
CFG.EVAL.HORIZONS = []
CFG.EVAL.USE_GPU = True # 是否在评估时使用 GPU。默认值:True
8 changes: 3 additions & 5 deletions examples/regular_config.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import os
import sys
from easydict import EasyDict
sys.path.append(os.path.abspath(__file__ + '/../../..'))

from basicts.metrics import masked_mae, masked_mape, masked_rmse
from basicts.data import TimeSeriesForecastingDataset
from basicts.runners import SimpleTimeSeriesForecastingRunner
from basicts.scaler import ZScoreScaler
from basicts.utils import get_regular_settings
from .arch import MultiLayerPerceptron as MLP

import os
from easydict import EasyDict

############################## Hot Parameters ##############################
# Dataset & Metrics configuration
DATA_NAME = 'PEMS08' # Dataset name
Expand Down
1 change: 1 addition & 0 deletions experiments/evaluate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pylint: disable=wrong-import-position
import os
import sys
from argparse import ArgumentParser
Expand Down
3 changes: 2 additions & 1 deletion experiments/train.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Run a baseline model in BasicTS framework.
# pylint: disable=wrong-import-position
import os
import sys
from argparse import ArgumentParser
Expand All @@ -23,5 +24,5 @@ def main():
basicts.launch_training(args.cfg, args.gpus, node_rank=0)


if __name__ == "__main__":
if __name__ == '__main__':
main()
Loading

0 comments on commit 3761512

Please sign in to comment.