From 6775cbeb75429f2c48a90411109f0bd6ff6dd221 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 12 Feb 2024 22:48:59 +0100 Subject: [PATCH] DX: switch to Ruff formatter (#518) * DX: do not run notebooks in temp dirs --- .gitpod.yml | 1 - .pre-commit-config.yaml | 31 +++++--------- .vscode/extensions.json | 2 +- .vscode/settings.json | 3 +- README.md | 1 - docs/amplitude-analysis.ipynb | 4 +- docs/usage.ipynb | 18 ++++----- docs/usage/unbinned-fit.ipynb | 8 +--- pyproject.toml | 63 ++++++++++------------------- src/tensorwaves/optimizer/minuit.py | 1 + src/tensorwaves/optimizer/scipy.py | 1 + 11 files changed, 46 insertions(+), 87 deletions(-) diff --git a/.gitpod.yml b/.gitpod.yml index 66885089..e64bb155 100644 --- a/.gitpod.yml +++ b/.gitpod.yml @@ -22,7 +22,6 @@ vscode: - executablebookproject.myst-highlight - github.vscode-github-actions - github.vscode-pull-request-github - - ms-python.black-formatter - ms-python.mypy-type-checker - ms-python.python - ms-python.vscode-pylance diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 54895f99..389f1605 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -40,7 +40,7 @@ repos: metadata.vscode - repo: https://github.com/ComPWA/policy - rev: 0.2.4 + rev: 0.2.5 hooks: - id: check-dev-files args: @@ -59,6 +59,15 @@ repos: - --additional-packages=IPython - --extras-require=doc,jax,pwa,viz + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.1 + hooks: + - id: ruff + args: [--fix] + types_or: [python, pyi, jupyter] + - id: ruff-format + types_or: [python, pyi, jupyter] + - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.5.0 hooks: @@ -86,19 +95,6 @@ repos: )$ - id: trailing-whitespace - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.1.1 - hooks: - - id: black - - id: black-jupyter - args: [--line-length=85] - types_or: [jupyter] - - - repo: https://github.com/asottile/blacken-docs - rev: 1.16.0 - hooks: - - id: blacken-docs - - repo: https://github.com/pre-commit/mirrors-prettier rev: v4.0.0-alpha.8 hooks: @@ -158,10 +154,3 @@ repos: rev: v1.1.350 hooks: - id: pyright - - - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.1 - hooks: - - id: ruff - args: [--fix] - types_or: [python, pyi, jupyter] diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 0b9a95b2..55cb02b9 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -8,7 +8,6 @@ "executablebookproject.myst-highlight", "github.vscode-github-actions", "github.vscode-pull-request-github", - "ms-python.black-formatter", "ms-python.mypy-type-checker", "ms-python.python", "ms-python.vscode-pylance", @@ -26,6 +25,7 @@ "bungcip.better-toml", "davidanson.vscode-markdownlint", "garaioag.garaio-vscode-unwanted-recommendations", + "ms-python.black-formatter", "ms-python.flake8", "ms-python.isort", "ms-python.pylint", diff --git a/.vscode/settings.json b/.vscode/settings.json index 442ff76b..3f8026f0 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,13 +19,12 @@ "editor.codeActionsOnSave": { "source.organizeImports": "explicit" }, - "editor.defaultFormatter": "ms-python.black-formatter", + "editor.defaultFormatter": "charliermarsh.ruff", "editor.rulers": [88] }, "[yaml]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "black-formatter.importStrategy": "fromEnvironment", "coverage-gutters.coverageFileNames": ["coverage.xml"], "coverage-gutters.coverageReportFileName": "**/htmlcov/index.html", "coverage-gutters.showGutterCoverage": false, diff --git a/README.md b/README.md index 0b7b2c45..3da70465 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,6 @@ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/db8f89e5588041d8a995968262c224ef)](https://www.codacy.com/gh/ComPWA/tensorwaves) [![Spelling checked](https://img.shields.io/badge/cspell-checked-brightgreen.svg)](https://github.com/streetsidesoftware/cspell/tree/master/packages/cspell) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) [![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff) TensorWaves is a fitter package that optimizes mathematical models to data samples. The diff --git a/docs/amplitude-analysis.ipynb b/docs/amplitude-analysis.ipynb index cebc5954..b54d8ff7 100644 --- a/docs/amplitude-analysis.ipynb +++ b/docs/amplitude-analysis.ipynb @@ -990,9 +990,7 @@ "metadata": {}, "outputs": [], "source": [ - "free_parameters = {\n", - " p for p in model.parameter_defaults if p.name in initial_parameters\n", - "}\n", + "free_parameters = {p for p in model.parameter_defaults if p.name in initial_parameters}\n", "fixed_parameters = {\n", " p: v for p, v in model.parameter_defaults.items() if p not in free_parameters\n", "}\n", diff --git a/docs/usage.ipynb b/docs/usage.ipynb index 8299c192..5d22f6d1 100644 --- a/docs/usage.ipynb +++ b/docs/usage.ipynb @@ -259,9 +259,7 @@ "\n", "\n", "class FitAnimation(Callback):\n", - " def __init__(\n", - " self, data, function, x_values, output_file, estimated_iterations=140\n", - " ):\n", + " def __init__(self, data, function, x_values, output_file, estimated_iterations=140):\n", " self.__function = function\n", " self.__fig, (self.__ax1, self.__ax2) = plt.subplots(\n", " nrows=2, figsize=(7, 7), tight_layout=True\n", @@ -284,10 +282,7 @@ " self.__ax2.set_xlim(0, estimated_iterations)\n", " self.__ax2.set_title(\"Parameter values\")\n", " self.__ax2.legend(\n", - " [\n", - " f\"${sp.latex(sp.Symbol(par_name))}$\"\n", - " for par_name in function.parameters\n", - " ],\n", + " [f\"${sp.latex(sp.Symbol(par_name))}$\" for par_name in function.parameters],\n", " loc=\"upper right\",\n", " )\n", "\n", @@ -811,9 +806,12 @@ ")\n", "def plot(dphi, k_r, k_phi, sigma):\n", " global color_mesh, X, Y\n", - " polar_function.update_parameters(\n", - " {R\"\\Delta\\phi\": dphi, \"k_r\": k_r, \"k_phi\": k_phi, \"sigma\": sigma}\n", - " )\n", + " polar_function.update_parameters({\n", + " R\"\\Delta\\phi\": dphi,\n", + " \"k_r\": k_r,\n", + " \"k_phi\": k_phi,\n", + " \"sigma\": sigma,\n", + " })\n", " Z = polar_function(polar_domain)\n", " if color_mesh is not None:\n", " color_mesh.remove()\n", diff --git a/docs/usage/unbinned-fit.ipynb b/docs/usage/unbinned-fit.ipynb index cc36b67c..d5679fcc 100644 --- a/docs/usage/unbinned-fit.ipynb +++ b/docs/usage/unbinned-fit.ipynb @@ -245,9 +245,7 @@ "function.update_parameters(initial_parameters)\n", "Z = function({\"x\": X, \"y\": Y})\n", "\n", - "fig, (ax1, ax2) = plt.subplots(\n", - " nrows=2, sharex=True, figsize=(8, 7), tight_layout=True\n", - ")\n", + "fig, (ax1, ax2) = plt.subplots(figsize=(8, 7), nrows=2, sharex=True, tight_layout=True)\n", "ax1.set_title(\"Data distribution\")\n", "ax2.set_title(\"Initial fit model\")\n", "ax1.hist2d(data[\"x\"], data[\"y\"], cmap=plt.cm.coolwarm, bins=(bins_x, bins_y))\n", @@ -325,9 +323,7 @@ "function.update_parameters(fit_result.parameter_values)\n", "Z = function({\"x\": X, \"y\": Y})\n", "\n", - "fig, (ax1, ax2) = plt.subplots(\n", - " nrows=2, sharex=True, figsize=(8, 7), tight_layout=True\n", - ")\n", + "fig, (ax1, ax2) = plt.subplots(figsize=(8, 7), nrows=2, sharex=True, tight_layout=True)\n", "ax1.set_title(\"Data distribution\")\n", "ax2.set_title(\"Model with optimized parameters\")\n", "ax1.hist2d(data[\"x\"], data[\"y\"], cmap=plt.cm.coolwarm, bins=(bins_x, bins_y))\n", diff --git a/pyproject.toml b/pyproject.toml index cebb7688..f51aaa3d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -85,14 +85,12 @@ doc = [ "sphobjinv", "tensorwaves[all]", ] -format = [ - "black", -] jax = [ "jax", "jaxlib", ] jupyter = [ + "black", "isort", "jupyterlab", "jupyterlab-code-formatter", @@ -103,21 +101,6 @@ jupyter = [ "python-lsp-server[rope]", "tensorwaves[doc]", ] -lint = [ - "ruff", - "tensorwaves[mypy]", -] -mypy = [ - "mypy >=0.570", # attrs support - "sphinx-api-relink >=0.0.4", - "tensorwaves[jax]", - "tensorwaves[pwa]", - "tensorwaves[test-types]", - "types-PyYAML", - "types-requests", - "types-tensorflow", - "types-tqdm", -] numba = [ "numba", ] @@ -136,9 +119,11 @@ scipy = [ "scipy >=1", ] sty = [ + "mypy >=0.570", # attrs support "pre-commit >=1.4.0", + "ruff", "tensorwaves[format]", - "tensorwaves[lint]", + "tensorwaves[types]", ] tensorflow = [ "tensorwaves[tf]", @@ -162,6 +147,16 @@ tf = [ "tensorflow >=2.4", # tensorflow.experimental.numpy 'tensorflow <2.12; python_version <"3.9.0"', ] +types = [ + "sphinx-api-relink >=0.0.4", + "tensorwaves[jax]", + "tensorwaves[pwa]", + "tensorwaves[test-types]", + "types-PyYAML", + "types-requests", + "types-tensorflow", + "types-tqdm", +] viz = [ "graphviz", ] @@ -190,29 +185,6 @@ where = ["src"] [tool.setuptools_scm] write_to = "src/tensorwaves/version.py" -[tool.black] -exclude = ''' -/( - .*\.egg-info - | .*build - | \.eggs - | \.git - | \.pytest_cache - | \.tox - | \.venv - | \.vscode - | dist -)/ -''' -include = '\.pyi?$' -preview = true -target-version = [ - "py310", - "py311", - "py38", - "py39", -] - [tool.coverage.run] branch = true source = ["src"] @@ -310,6 +282,8 @@ filterwarnings = [ ] markers = ["slow: marks tests as slow (select with '-m slow')"] norecursedirs = [ + ".ipynb_checkpoints", + ".virtual_documents", "_build", "docs/api", "tests/output", @@ -331,6 +305,10 @@ src = [ ] target-version = "py38" +[tool.ruff.format] +docstring-code-format = true +line-ending = "lf" + [tool.ruff.lint] extend-select = [ "A", @@ -376,6 +354,7 @@ ignore = [ "D407", "D416", "E501", + "ISC001", "PLW1514", "RUF012", "S307", diff --git a/src/tensorwaves/optimizer/minuit.py b/src/tensorwaves/optimizer/minuit.py index 75f1bddc..756737e3 100644 --- a/src/tensorwaves/optimizer/minuit.py +++ b/src/tensorwaves/optimizer/minuit.py @@ -1,5 +1,6 @@ # cspell: ignore nfcn """Minuit2 adapter to the `iminuit.Minuit` package.""" + from __future__ import annotations import logging diff --git a/src/tensorwaves/optimizer/scipy.py b/src/tensorwaves/optimizer/scipy.py index 24cada82..c3e2f750 100644 --- a/src/tensorwaves/optimizer/scipy.py +++ b/src/tensorwaves/optimizer/scipy.py @@ -1,5 +1,6 @@ # cspell:ignore BFGS disp nfev """Adapter to the `scipy.optimize` package.""" + from __future__ import annotations import logging