From c35cca6df57675a467e1791161cc70e85b96c008 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Sun, 1 Dec 2024 21:39:23 +0100 Subject: [PATCH 1/8] Use same requirements in pyproject.toml and tox.ini --- pyproject.toml | 21 +++++++++++++++++++++ tox.ini | 27 ++++----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a5638d3..1730f60 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,27 @@ notebooks = [ "torchvision>=0.16.2,<0.21", "ipywidgets>=8.1.1,<8.2", ] +dev-tests = [ + "pytest>=8.3.1,<8.4", + "pytest-cov>=5.0.0,<5.1", + "pytest-mock>=3.14.0,<3.15", + "scikit-learn>=1.5.1,<1.6", +] +dev-ruff = [ + "ruff>=0.5.6,<0.6", +] +dev-mypy = [ + "mypy>=1.11.0,<1.12", + "types-requests>=2.32.0,<2.33", + "types-toml>=0.10.0,<0.11", + "types-tqdm>=4.66,<4.67", +] +dev-linters = [ + "frouros[dev-ruff, dev-mypy]", +] +dev = [ + "frouros[dev-tests, dev-linters]", +] [project.urls] homepage = "https://frouros.readthedocs.io" diff --git a/tox.ini b/tox.ini index c831900..ddb8e22 100644 --- a/tox.ini +++ b/tox.ini @@ -20,11 +20,7 @@ python = [testenv] # Force to upgrade pip/wheel/setuptools to the latest version download = True -deps = - pytest>=8.3.1,<8.4 - pytest-cov>=5.0.0,<5.1 - pytest-mock>=3.14.0,<3.15 - scikit-learn>=1.5.1,<1.6 +extras = dev-tests commands = pytest --cov={[base]package} \ --cov-report term \ --cov-report=xml \ @@ -35,31 +31,16 @@ addopts = -ra -q norecursedirs = docs [testenv:ruff] -basepython = {[base]python} -skip_install = {[base]skip_install} -deps = - ruff>=0.5.6,<0.6 +extras = dev-ruff commands = ruff check --config pyproject.toml . ruff format --config pyproject.toml . [testenv:mypy] -basepython = {[base]python} -skip_install = {[base]skip_install} -deps = - mypy>=1.11.0,<1.12 - types-requests>=2.32.0,<2.33 - types-toml>=0.10.0,<0.11 - types-tqdm>=4.66,<4.67 +extras = dev-mypy commands = mypy --config-file pyproject.toml . [testenv:linters] -basepython = {[base]python} -skip_install = {[base]skip_install} -setenv = - PYTHONPATH = $PYTHONPATH:{toxinidir}:{[base]venv}/lib/{[base]python}/site-packages -deps = - {[testenv:ruff]deps} - {[testenv:mypy]deps} +extras = dev-linters commands = {[testenv:ruff]commands} {[testenv:mypy]commands} From 174c7eb357eac85efc98e13f8e1569359fcd95a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Sun, 1 Dec 2024 21:57:07 +0100 Subject: [PATCH 2/8] Update pyproject.toml dependencies versions --- pyproject.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 1730f60..9102a8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,18 +60,18 @@ notebooks = [ ] dev-tests = [ "pytest>=8.3.1,<8.4", - "pytest-cov>=5.0.0,<5.1", + "pytest-cov>=6.0.0,<6.1", "pytest-mock>=3.14.0,<3.15", "scikit-learn>=1.5.1,<1.6", ] dev-ruff = [ - "ruff>=0.5.6,<0.6", + "ruff>=0.8.1,<0.9", ] dev-mypy = [ - "mypy>=1.11.0,<1.12", + "mypy>=1.13.0,<1.14", "types-requests>=2.32.0,<2.33", "types-toml>=0.10.0,<0.11", - "types-tqdm>=4.66,<4.67", + "types-tqdm>=4.66,<4.68", ] dev-linters = [ "frouros[dev-ruff, dev-mypy]", From 1f90526bc88852514a8ecda837ea94ec8949696f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Sun, 1 Dec 2024 22:03:34 +0100 Subject: [PATCH 3/8] Update pre-commit-config-yaml versions --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d17b1ac..ccc691b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,7 +2,7 @@ default_language_version: python: python3.10 repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: check-added-large-files - id: check-docstring-first @@ -14,7 +14,7 @@ repos: args: ["--branch", "main"] - id: trailing-whitespace - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.5.6 + rev: v0.8.1 hooks: - id: ruff types_or: [ @@ -30,7 +30,7 @@ repos: jupyter, ] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.11.1 + rev: v1.13.0 hooks: - id: mypy args: [ @@ -42,5 +42,5 @@ repos: additional_dependencies: [ types-requests<2.33, types-toml<0.11, - types-tqdm<4.67, + types-tqdm<4.68, ] From 073ca392d632fc18c63e2a66cbfc2fec6c94d9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Mon, 2 Dec 2024 22:51:11 +0100 Subject: [PATCH 4/8] Fix tox.ini to read dependencies from pyproject.toml --- pyproject.toml | 8 +++----- tox.ini | 43 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9102a8b..2fe808b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -53,7 +53,7 @@ docs = [ "myst-nb>=1.0.0,<1.2", ] notebooks = [ - "scikit-learn>=1.3.2,<1.6", + "scikit-learn>=1.5.1,<1.6", "torch>=2.1.2,<2.6", "torchvision>=0.16.2,<0.21", "ipywidgets>=8.1.1,<8.2", @@ -73,11 +73,9 @@ dev-mypy = [ "types-toml>=0.10.0,<0.11", "types-tqdm>=4.66,<4.68", ] -dev-linters = [ - "frouros[dev-ruff, dev-mypy]", -] dev = [ - "frouros[dev-tests, dev-linters]", + "frouros[docs,notebooks,dev-tests,dev-ruff,dev-mypy]", + "tox>=4.23.2,<4.24", ] [project.urls] diff --git a/tox.ini b/tox.ini index ddb8e22..6e555f3 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -minversion = 4.16.0 +minversion = 4.23.2 envlist = py3{9, 10, 11, 12} linters @@ -9,6 +9,12 @@ python = python3 skip_install = true package = frouros venv = {toxinidir}/.venv +deps = + # Package used as a workaround to install the dependencies read from pyproject.toml dev section. + toml>=0.10.2,<0.11 +pyproject_reader = + # Workaround to install the dependencies read from pyproject.toml dev section. Use toml to read the file and install the dependencies. + python -c "import toml, subprocess; deps = toml.load('pyproject.toml')['project']['optional-dependencies']['{env:DEPS_SECTION}']; subprocess.run(['pip', 'install'] + deps)" [gh-actions] python = @@ -20,8 +26,13 @@ python = [testenv] # Force to upgrade pip/wheel/setuptools to the latest version download = True -extras = dev-tests -commands = pytest --cov={[base]package} \ +deps = {[base]deps} +setenv = + DEPS_SECTION = dev-tests +commands_pre = + {[base]pyproject_reader} +commands = + pytest --cov={[base]package} \ --cov-report term \ --cov-report=xml \ --cov-fail-under=90 @@ -31,16 +42,36 @@ addopts = -ra -q norecursedirs = docs [testenv:ruff] -extras = dev-ruff +basepython = {[base]python} +skip_install = {[base]skip_install} +deps = {[base]deps} +setenv = + DEPS_SECTION = dev-ruff +commands_pre = + {[base]pyproject_reader} commands = ruff check --config pyproject.toml . ruff format --config pyproject.toml . [testenv:mypy] -extras = dev-mypy +basepython = {[base]python} +skip_install = {[base]skip_install} +deps = {[base]deps} +setenv = + DEPS_SECTION = dev-mypy +commands_pre = + {[base]pyproject_reader} commands = mypy --config-file pyproject.toml . [testenv:linters] -extras = dev-linters +basepython = {[base]python} +skip_install = {[base]skip_install} +setenv = + PYTHONPATH = $PYTHONPATH:{toxinidir}:{[base]venv}/lib/{[base]python}/site-packages +deps = + {[base]deps} +commands_pre = + {[testenv:ruff]commands_pre} + {[testenv:mypy]commands_pre} commands = {[testenv:ruff]commands} {[testenv:mypy]commands} From eccfd5caa1b767174d57e44a4b7d96fee5b3a328 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Mon, 2 Dec 2024 22:55:24 +0100 Subject: [PATCH 5/8] Update CONTRIBUTING.md --- CONTRIBUTING.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6caa8d5..76bd2d1 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,7 +18,7 @@ Recommended steps for first time contributors: cd frouros git checkout main git pull - pip install -e . + pip install -e '.[dev]' ``` 4. (Optional but recommended) Install pre-commit hooks: ```bash @@ -33,5 +33,5 @@ Recommended steps for first time contributors: ## Reporting a bug -1. Check that there is not an [issue](https://github.com/IFCA/frouros/issues) that currently highlights the bug or a [pull request](https://github.com/IFCA/frouros/pulls) that solves it. -2. Create an [issue](https://github.com/IFCA/frouros/issues/new) in GitHub. +1. Check that there is not an [issue](https://github.com/IFCA-Advanced-Computing/frouros/issues) that currently highlights the bug or a [pull request](https://github.com/IFCA-Advanced-Computing/frouros/pulls) that solves it. +2. Create an [issue](https://github.com/IFCA-Advanced-Computing/frouros/issues/new) in GitHub. From 56552a6916e9466e3821661f4894f4a96bd6b88d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Mon, 2 Dec 2024 22:56:46 +0100 Subject: [PATCH 6/8] Read pyproject.toml from pre-commit-config.yaml --- .pre-commit-config.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ccc691b..ddd3582 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -34,10 +34,8 @@ repos: hooks: - id: mypy args: [ - --strict, - --ignore-missing-imports, - --disable-error-code=misc, - --disable-error-code=no-any-return, + --config-file, + pyproject.toml, ] additional_dependencies: [ types-requests<2.33, From 95ebcf29c0d0103b8aab55a3460dfc2fecfeb829 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Mon, 2 Dec 2024 22:57:38 +0100 Subject: [PATCH 7/8] Fix pep8 --- frouros/datasets/base.py | 10 +++++----- .../data_drift/batch/statistical_test/bws.py | 2 +- .../batch/statistical_test/chisquare.py | 15 +++++++++++---- frouros/utils/stats.py | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/frouros/datasets/base.py b/frouros/datasets/base.py index c541a84..513b828 100644 --- a/frouros/datasets/base.py +++ b/frouros/datasets/base.py @@ -33,11 +33,11 @@ def __init__( :type file_path: str """ self.url = url - self.file_path: Optional[Path] = ( - Path(file_path) - if file_path - else Path(tempfile.NamedTemporaryFile(delete=False).name) - ) + if file_path: + self.file_path = Path(file_path) + else: + with tempfile.NamedTemporaryFile(delete=False) as temp_file: + self.file_path = Path(temp_file.name) @property def file_path(self) -> Optional[Path]: diff --git a/frouros/detectors/data_drift/batch/statistical_test/bws.py b/frouros/detectors/data_drift/batch/statistical_test/bws.py index c8b3313..cd139ef 100644 --- a/frouros/detectors/data_drift/batch/statistical_test/bws.py +++ b/frouros/detectors/data_drift/batch/statistical_test/bws.py @@ -61,7 +61,7 @@ def _statistical_test( x=X_ref, y=X, alternative=kwargs.get("alternative", "two-sided"), - method=kwargs.get("method", None), + method=kwargs.get("method"), ) test = StatisticalResult( statistic=test.statistic, diff --git a/frouros/detectors/data_drift/batch/statistical_test/chisquare.py b/frouros/detectors/data_drift/batch/statistical_test/chisquare.py index cae74e0..99e3ffc 100644 --- a/frouros/detectors/data_drift/batch/statistical_test/chisquare.py +++ b/frouros/detectors/data_drift/batch/statistical_test/chisquare.py @@ -2,13 +2,20 @@ import collections import typing -from typing import Any, Optional, Set, Tuple, Union +from typing import ( + Any, + Optional, + Union, +) import numpy as np from scipy.stats import chi2_contingency from frouros.callbacks.batch.base import BaseCallbackBatch -from frouros.detectors.data_drift.base import CategoricalData, UnivariateData +from frouros.detectors.data_drift.base import ( + CategoricalData, + UnivariateData, +) from frouros.detectors.data_drift.batch.statistical_test.base import ( BaseStatisticalTest, StatisticalResult, @@ -82,11 +89,11 @@ def _statistical_test( def _calculate_frequencies( X_ref: np.ndarray, # noqa: N803 X: np.ndarray, - ) -> Tuple[list[int], list[int]]: + ) -> tuple[list[int], list[int]]: X_ref_counter, X_counter = [ # noqa: N806 *map(collections.Counter, [X_ref, X]) # noqa: N806 ] - possible_values: Set[str] = set([*X_ref_counter.keys()] + [*X_counter.keys()]) # noqa: N806 + possible_values: set[str] = set([*X_ref_counter.keys()] + [*X_counter.keys()]) # noqa: N806 f_exp, f_obs = {}, {} for value in possible_values: f_exp[value] = X_ref_counter.get(value, 0) # noqa: N806 diff --git a/frouros/utils/stats.py b/frouros/utils/stats.py index 58f6756..c5aabb4 100644 --- a/frouros/utils/stats.py +++ b/frouros/utils/stats.py @@ -269,7 +269,7 @@ def permutation( # pylint: disable=too-many-arguments,too-many-locals with Pool(processes=num_jobs) as pool: permuted_statistics = pool.starmap_async( partial(statistic, **statistical_args), - iterable=tqdm(permuted_data) if verbose else permuted_data, # type: ignore + iterable=tqdm(permuted_data) if verbose else permuted_data, ).get() return permuted_statistics, max_num_permutations From 3e96f3d5b60d3d998098c7b44c3b4d4888fbbff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20C=C3=A9spedes=20Sisniega?= Date: Mon, 2 Dec 2024 22:59:25 +0100 Subject: [PATCH 8/8] Shorten tox.ini workaround comment --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 6e555f3..1cf9ef4 100644 --- a/tox.ini +++ b/tox.ini @@ -13,7 +13,7 @@ deps = # Package used as a workaround to install the dependencies read from pyproject.toml dev section. toml>=0.10.2,<0.11 pyproject_reader = - # Workaround to install the dependencies read from pyproject.toml dev section. Use toml to read the file and install the dependencies. + # Workaround to install the dependencies read from pyproject.toml dev section. python -c "import toml, subprocess; deps = toml.load('pyproject.toml')['project']['optional-dependencies']['{env:DEPS_SECTION}']; subprocess.run(['pip', 'install'] + deps)" [gh-actions]