Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DX: switch to Ruff formatter #518

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 10 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions docs/amplitude-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 8 additions & 10 deletions docs/usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 2 additions & 6 deletions docs/usage/unbinned-fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
63 changes: 21 additions & 42 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ doc = [
"sphobjinv",
"tensorwaves[all]",
]
format = [
"black",
]
jax = [
"jax",
"jaxlib",
]
jupyter = [
"black",
"isort",
"jupyterlab",
"jupyterlab-code-formatter",
Expand All @@ -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",
]
Expand All @@ -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]",
Expand All @@ -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",
]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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",
Expand All @@ -331,6 +305,10 @@ src = [
]
target-version = "py38"

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"

[tool.ruff.lint]
extend-select = [
"A",
Expand Down Expand Up @@ -376,6 +354,7 @@ ignore = [
"D407",
"D416",
"E501",
"ISC001",
"PLW1514",
"RUF012",
"S307",
Expand Down
1 change: 1 addition & 0 deletions src/tensorwaves/optimizer/minuit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# cspell: ignore nfcn
"""Minuit2 adapter to the `iminuit.Minuit` package."""

from __future__ import annotations

import logging
Expand Down
1 change: 1 addition & 0 deletions src/tensorwaves/optimizer/scipy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# cspell:ignore BFGS disp nfev
"""Adapter to the `scipy.optimize` package."""

from __future__ import annotations

import logging
Expand Down
Loading