Skip to content

Commit

Permalink
style: 💄 lint (#200)
Browse files Browse the repository at this point in the history
* style: 💄 lint

fix lint matched pylint

* style: 💄  fix lint & add isort config

fix lint problems and add isort config
  • Loading branch information
duyifanict authored Dec 2, 2024
1 parent 80361c6 commit 39ecf8b
Show file tree
Hide file tree
Showing 70 changed files with 294 additions and 198 deletions.
3 changes: 3 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[settings]
src_paths=basicts,tests
skip_glob=baselines/*,assets/*,examples/*
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/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .launcher import launch_training, launch_evaluation
from .launcher import launch_evaluation, launch_training
from .runners import BaseEpochRunner

__version__ = '0.4.6.2'
Expand Down
2 changes: 1 addition & 1 deletion basicts/data/base_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List
from dataclasses import dataclass
from typing import List

import numpy as np
from torch.utils.data import Dataset
Expand Down
2 changes: 1 addition & 1 deletion basicts/data/simple_tsf_dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import inspect
import json
import logging
from typing import List

Expand Down
6 changes: 4 additions & 2 deletions basicts/launcher.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
from typing import Dict, Union, Optional
import traceback
from typing import Dict, Optional, Union

import easytorch
from easytorch.utils import get_logger, set_visible_devices
from easytorch.config import init_cfg
from easytorch.device import set_device_type
from easytorch.utils import get_logger, set_visible_devices


def evaluation_func(cfg: Dict,
ckpt_path: str = None,
Expand Down
2 changes: 1 addition & 1 deletion basicts/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .mae import masked_mae
from .mape import masked_mape
from .mse import masked_mse
from .rmse import masked_rmse
from .mape import masked_mape
from .wape import masked_wape

ALL_METRICS = {
Expand Down
3 changes: 2 additions & 1 deletion basicts/metrics/mae.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import numpy as np
import torch


def masked_mae(prediction: torch.Tensor, target: torch.Tensor, null_val: float = np.nan) -> torch.Tensor:
"""
Expand Down
3 changes: 2 additions & 1 deletion basicts/metrics/mape.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import numpy as np
import torch


def masked_mape(prediction: torch.Tensor, target: torch.Tensor, null_val: float = np.nan) -> torch.Tensor:
"""
Expand Down
3 changes: 2 additions & 1 deletion basicts/metrics/mse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import numpy as np
import torch


def masked_mse(prediction: torch.Tensor, target: torch.Tensor, null_val: float = np.nan) -> torch.Tensor:
"""
Expand Down
3 changes: 2 additions & 1 deletion basicts/metrics/rmse.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import torch
import numpy as np
import torch

from .mse import masked_mse


def masked_rmse(prediction: torch.Tensor, target: torch.Tensor, null_val: float = np.nan) -> torch.Tensor:
"""
Calculate the Masked Root Mean Squared Error (RMSE) between predicted and target values,
Expand Down
3 changes: 2 additions & 1 deletion basicts/metrics/wape.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import numpy as np
import torch


def masked_wape(prediction: torch.Tensor, target: torch.Tensor, null_val: float = np.nan) -> torch.Tensor:
"""
Expand Down
2 changes: 1 addition & 1 deletion basicts/runners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .base_epoch_runner import BaseEpochRunner
from .base_tsf_runner import BaseTimeSeriesForecastingRunner
from .runner_zoo.simple_tsf_runner import SimpleTimeSeriesForecastingRunner
from .runner_zoo.no_bp_runner import NoBPRunner
from .runner_zoo.simple_tsf_runner import SimpleTimeSeriesForecastingRunner

__all__ = ['BaseEpochRunner', 'BaseTimeSeriesForecastingRunner',
'SimpleTimeSeriesForecastingRunner', 'NoBPRunner']
30 changes: 16 additions & 14 deletions basicts/runners/base_epoch_runner.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import logging
import os
import time
import logging
from abc import ABCMeta, abstractmethod
from typing import Tuple, Union, Optional, Dict
from typing import Dict, Optional, Tuple, Union

import setproctitle
from tqdm import tqdm
from packaging import version
import torch
from easytorch.config import get_ckpt_save_dir
from easytorch.core.checkpoint import (backup_last_ckpt, clear_ckpt, load_ckpt,
save_ckpt)
from easytorch.core.data_loader import build_data_loader, build_data_loader_ddp
from easytorch.core.meter_pool import MeterPool
from easytorch.device import to_device
from easytorch.utils import (TimePredictor, get_local_rank, get_logger,
is_master, master_only, set_env)
from packaging import version
from torch import nn
from torch.utils.data import Dataset, DataLoader
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.distributed import DistributedSampler
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm

from easytorch.core.meter_pool import MeterPool
from easytorch.core.checkpoint import load_ckpt, save_ckpt, backup_last_ckpt, clear_ckpt
from easytorch.core.data_loader import build_data_loader, build_data_loader_ddp
from easytorch.config import get_ckpt_save_dir
from easytorch.utils import TimePredictor, get_logger, get_local_rank, is_master, master_only, set_env
from easytorch.device import to_device

from . import optim
from ..utils import get_dataset_name
from . import optim


class BaseEpochRunner(metaclass=ABCMeta):
"""
Expand Down
17 changes: 9 additions & 8 deletions basicts/runners/base_tsf_runner.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import os
import functools
import inspect
import json
import math
import inspect
import functools
from typing import Tuple, Union, Optional, Dict
import os
from typing import Dict, Optional, Tuple, Union

import torch
import numpy as np
from tqdm import tqdm
import torch
from easydict import EasyDict
from easytorch.utils import master_only
from tqdm import tqdm

from ..metrics import (masked_mae, masked_mape, masked_mse, masked_rmse,
masked_wape)
from .base_epoch_runner import BaseEpochRunner
from ..metrics import masked_mae, masked_mape, masked_rmse, masked_wape, masked_mse


class BaseTimeSeriesForecastingRunner(BaseEpochRunner):
Expand Down Expand Up @@ -343,7 +344,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: 1 addition & 0 deletions basicts/runners/optim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from torch.optim import Optimizer

from .builder import build_lr_scheduler, build_optim
9 changes: 3 additions & 6 deletions basicts/runners/optim/optimizers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# define more optimizers here
import os
import inspect
from typing import Union, Tuple, Optional
from typing import Optional, Tuple, Union

import torch
from easytorch.device import _DEVICE_TYPE
from easytorch.utils import get_local_rank, get_world_size
from torch import Tensor
from torch.optim import AdamW

from easytorch.device import _DEVICE_TYPE
from easytorch.utils import get_world_size, get_local_rank


__all__ = ['AdamWnanoGPT', 'Muon']

class AdamWnanoGPT(AdamW):
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
6 changes: 4 additions & 2 deletions basicts/runners/runner_zoo/simple_tsf_runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Dict

import torch

from ..base_tsf_runner import BaseTimeSeriesForecastingRunner


class SimpleTimeSeriesForecastingRunner(BaseTimeSeriesForecastingRunner):
"""
A Simple Runner for Time Series Forecasting:
Expand Down Expand Up @@ -90,13 +92,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/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .base_scaler import BaseScaler
from .z_score_scaler import ZScoreScaler
from .min_max_scaler import MinMaxScaler
from .z_score_scaler import ZScoreScaler

__all__ = [
'BaseScaler',
Expand Down
4 changes: 2 additions & 2 deletions basicts/scaler/min_max_scaler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import torch
import numpy as np
import torch

from .base_scaler import BaseScaler

Expand Down 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
2 changes: 1 addition & 1 deletion basicts/scaler/z_score_scaler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import torch
import numpy as np
import torch

from .base_scaler import BaseScaler

Expand Down
11 changes: 6 additions & 5 deletions basicts/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from .xformer import data_transformation_4_xformer
from .serialization import load_adj, load_pkl, dump_pkl, \
load_dataset_data, get_regular_settings, load_dataset_desc
from .misc import clock, check_nan_inf, remove_nan_inf, \
partial_func as partial
from .config import get_dataset_name
from .misc import check_nan_inf, clock
from .misc import partial_func as partial
from .misc import remove_nan_inf
from .serialization import (dump_pkl, get_regular_settings, load_adj,
load_dataset_data, load_dataset_desc, load_pkl)
from .xformer import data_transformation_4_xformer

__all__ = ['load_adj', 'load_pkl', 'dump_pkl',
'clock', 'check_nan_inf',
Expand Down
1 change: 1 addition & 0 deletions basicts/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Dict


def get_dataset_name(cfg: Dict) -> str:
"""
Extract the dataset name from the configuration dictionary.
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
12 changes: 6 additions & 6 deletions basicts/utils/serialization.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
import pickle

import numpy as np
from .adjacent_matrix_norm import (
calculate_scaled_laplacian,
calculate_symmetric_normalized_laplacian,
calculate_symmetric_message_passing_adj,
calculate_transition_matrix
)

from .adjacent_matrix_norm import (calculate_scaled_laplacian,
calculate_symmetric_message_passing_adj,
calculate_symmetric_normalized_laplacian,
calculate_transition_matrix)


def get_regular_settings(dataset_name: str) -> dict:
Expand Down
2 changes: 2 additions & 0 deletions examples/arch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pylint: disable=unused-argument
import torch
from torch import nn


class MultiLayerPerceptron(nn.Module):
"""
A simple Multi-Layer Perceptron (MLP) model with two fully connected layers.
Expand Down
Loading

0 comments on commit 39ecf8b

Please sign in to comment.