diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 809da50..9b495c9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,3 +26,8 @@ repos: rev: 20.8b1 hooks: - id: black + - repo: https://github.com/hadialqattan/pycln + rev: v1.2.4 # Possible releases: https://github.com/hadialqattan/pycln/releases + hooks: + - id: pycln + args: [--config=pyproject.toml] diff --git a/docs/source/conf.py b/docs/source/conf.py index 7f568f2..8916765 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -2,8 +2,6 @@ # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # -import os -import sys from typing import List # Configuration file for the Sphinx documentation builder. diff --git a/examples/movielens/movielens_1m.py b/examples/movielens/movielens_1m.py index 7a773b8..0a04506 100644 --- a/examples/movielens/movielens_1m.py +++ b/examples/movielens/movielens_1m.py @@ -1,6 +1,4 @@ import json -import logging -import os from typing import List, Tuple, Type from scipy import sparse as sps diff --git a/irspack/dataset/downloader.py b/irspack/dataset/downloader.py index 12bdb7a..d205ba5 100644 --- a/irspack/dataset/downloader.py +++ b/irspack/dataset/downloader.py @@ -1,4 +1,3 @@ -import os import urllib.request from abc import ABCMeta, abstractmethod from io import BytesIO diff --git a/irspack/dataset/movielens/base.py b/irspack/dataset/movielens/base.py index d0c2b04..e45a8fa 100644 --- a/irspack/dataset/movielens/base.py +++ b/irspack/dataset/movielens/base.py @@ -1,4 +1,4 @@ -from abc import ABCMeta, abstractmethod +from abc import abstractmethod from io import BytesIO import pandas as pd diff --git a/irspack/definitions.py b/irspack/definitions.py index 06e0bbe..4dbd5df 100644 --- a/irspack/definitions.py +++ b/irspack/definitions.py @@ -1,4 +1,4 @@ -from typing import Any, List, Optional, Union +from typing import Union import numpy as np from scipy import sparse as sps diff --git a/irspack/optimizers/_optimizers.py b/irspack/optimizers/_optimizers.py index 1f1ee08..c2424fc 100644 --- a/irspack/optimizers/_optimizers.py +++ b/irspack/optimizers/_optimizers.py @@ -1,5 +1,5 @@ import logging -from typing import Any, Dict, List, Optional, Type +from typing import Any, Dict, List, Optional from irspack.definitions import InteractionMatrix diff --git a/irspack/recommenders/__init__.py b/irspack/recommenders/__init__.py index 1ad5299..747e125 100644 --- a/irspack/recommenders/__init__.py +++ b/irspack/recommenders/__init__.py @@ -5,7 +5,7 @@ ) from irspack.recommenders.base_earlystop import BaseRecommenderWithEarlyStopping from irspack.recommenders.dense_slim import DenseSLIMConfig, DenseSLIMRecommender -from irspack.recommenders.edlae import EDLAERecommender +from irspack.recommenders.edlae import EDLAEConfig, EDLAERecommender from irspack.recommenders.ials import IALSConfig, IALSRecommender from irspack.recommenders.knn import ( AsymmetricCosineKNNConfig, @@ -41,6 +41,8 @@ "RP3betaRecommender", "DenseSLIMConfig", "DenseSLIMRecommender", + "EDLAERecommender", + "EDLAEConfig", "SLIMConfig", "SLIMRecommender", "IALSConfig", diff --git a/irspack/recommenders/base.py b/irspack/recommenders/base.py index df3c415..6ed26ed 100644 --- a/irspack/recommenders/base.py +++ b/irspack/recommenders/base.py @@ -1,5 +1,14 @@ from abc import ABCMeta, abstractmethod -from typing import TYPE_CHECKING, Any, Dict, Optional, Type, Union, no_type_check +from typing import ( + TYPE_CHECKING, + Any, + Dict, + Optional, + Type, + TypeVar, + Union, + no_type_check, +) import numpy as np from optuna.trial import Trial @@ -16,6 +25,8 @@ UserIndexArray, ) +R = TypeVar("R", bound="BaseRecommender") + def _sparse_to_array(U: Any) -> np.ndarray: if sps.issparse(U): @@ -78,15 +89,17 @@ def __init__(self, X_train_all: InteractionMatrix, **kwargs: Any) -> None: @classmethod def from_config( - cls, X_train_all: InteractionMatrix, config: RecommenderConfig - ) -> "BaseRecommender": + cls: Type[R], + X_train_all: InteractionMatrix, + config: RecommenderConfig, + ) -> R: if not isinstance(config, cls.config_class): raise ValueError( f"Different config has been given. config must be {cls.config_class}" ) return cls(X_train_all, **config.dict()) - def learn(self) -> "BaseRecommender": + def learn(self: R) -> R: """Learns and returns itself. Returns: @@ -245,7 +258,7 @@ def get_score_block(self, begin: int, end: int) -> DenseScoreArray: return _sparse_to_array(self.U[begin:end].dot(self._X_csc)) -class BaseRecommenderWithUserEmbedding(BaseRecommender): +class BaseRecommenderWithUserEmbedding: """Defines a recommender with user embedding (e.g., matrix factorization.). These class can be a base CF estimator for CB2CF (with user profile -> user embedding NN). """ @@ -276,7 +289,7 @@ def get_score_from_user_embedding( raise NotImplementedError("get_score_from_item_embedding must be implemtented.") -class BaseRecommenderWithItemEmbedding(BaseRecommender): +class BaseRecommenderWithItemEmbedding: """Defines a recommender with item embedding (e.g., matrix factorization.). These class can be a base CF estimator for CB2CF (with item profile -> item embedding NN). """ diff --git a/irspack/recommenders/edlae.py b/irspack/recommenders/edlae.py index b30738a..7cee0f2 100644 --- a/irspack/recommenders/edlae.py +++ b/irspack/recommenders/edlae.py @@ -1,7 +1,7 @@ import gc import numpy as np -from scipy import linalg, sparse +from scipy import linalg from ..definitions import InteractionMatrix from .base import BaseSimilarityRecommender, RecommenderConfig diff --git a/irspack/recommenders/truncsvd.py b/irspack/recommenders/truncsvd.py index c5be827..58850a4 100644 --- a/irspack/recommenders/truncsvd.py +++ b/irspack/recommenders/truncsvd.py @@ -1,7 +1,6 @@ import warnings from typing import Optional -from numpy import random from sklearn.decomposition import TruncatedSVD from ..definitions import ( @@ -11,6 +10,7 @@ UserIndexArray, ) from .base import ( + BaseRecommender, BaseRecommenderWithItemEmbedding, BaseRecommenderWithUserEmbedding, RecommenderConfig, @@ -23,6 +23,7 @@ class TruncatedSVDConfig(RecommenderConfig): class TruncatedSVDRecommender( + BaseRecommender, BaseRecommenderWithUserEmbedding, BaseRecommenderWithItemEmbedding, ): diff --git a/irspack/split/userwise.py b/irspack/split/userwise.py index 7b10dea..591475e 100644 --- a/irspack/split/userwise.py +++ b/irspack/split/userwise.py @@ -2,7 +2,6 @@ import numpy as np import pandas as pd -from numpy.lib.arraysetops import unique from scipy import sparse as sps from irspack.definitions import InteractionMatrix, OptionalRandomState diff --git a/pyproject.toml b/pyproject.toml index 6796ae2..2d85b90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,3 +13,6 @@ include_trailing_comma = true line_length = 88 multi_line_output = 3 use_parentheses = true + +[tool.pycln] +all = true diff --git a/tests/autopilot/mock_classes.py b/tests/autopilot/mock_classes.py index ea13f83..9fd1472 100644 --- a/tests/autopilot/mock_classes.py +++ b/tests/autopilot/mock_classes.py @@ -1,6 +1,6 @@ import pickle import time -from typing import IO, Any, Dict, List, Optional +from typing import IO, Any, Dict, List import numpy as np import scipy.sparse as sps diff --git a/tests/utils/test_utils.py b/tests/utils/test_utils.py index 3cca303..8741e6a 100644 --- a/tests/utils/test_utils.py +++ b/tests/utils/test_utils.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest import scipy.sparse as sps