Skip to content

Commit

Permalink
DX: switch to Ruff formatter (#252)
Browse files Browse the repository at this point in the history
* DX: do not run notebooks in temp dirs
  • Loading branch information
redeboer authored Feb 12, 2024
1 parent fa3437e commit af4d02b
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 172 deletions.
1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ vscode:
- github.vscode-github-actions
- github.vscode-pull-request-github
- julialang.language-julia
- ms-python.black-formatter
- ms-python.python
- ms-python.vscode-pylance
- ms-toolsai.vscode-jupyter-cell-tags
Expand Down
31 changes: 10 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -55,6 +55,15 @@ repos:
- id: pin-nb-requirements
- id: set-nb-cells

- 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 All @@ -80,19 +89,6 @@ repos:
.*\.svg
)$
- 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 @@ -153,10 +149,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 @@ -9,7 +9,6 @@
"github.vscode-github-actions",
"github.vscode-pull-request-github",
"julialang.language-julia",
"ms-python.black-formatter",
"ms-python.python",
"ms-python.vscode-pylance",
"ms-toolsai.vscode-jupyter-cell-tags",
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.mypy-type-checker",
Expand Down
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,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",
"cSpell.enabled": true,
"diffEditor.experimental.showMoves": true,
"editor.formatOnSave": true,
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/ComPWA/compwa.github.io/main.svg)](https://results.pre-commit.ci/latest/github/ComPWA/compwa.github.io/main)
[![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://github.com/raw/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

This repository contains the source code for the [compwa.github.io](https://compwa.github.io) pages.
Expand Down
12 changes: 3 additions & 9 deletions docs/adr/001/sympy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,7 @@
"\n",
"import sympy as sp\n",
"\n",
"x, A1, mu1, sigma1, A2, mu2, sigma2 = sp.symbols(\n",
" \"x, A1, mu1, sigma1, A2, mu2, sigma2\"\n",
")\n",
"x, A1, mu1, sigma1, A2, mu2, sigma2 = sp.symbols(\"x, A1, mu1, sigma1, A2, mu2, sigma2\")\n",
"gaussian1 = (\n",
" A1 / (sigma1 * sp.sqrt(2.0 * math.pi)) * sp.exp(-((x - mu1) ** 2) / (2 * sigma1))\n",
")\n",
Expand Down Expand Up @@ -591,9 +589,7 @@
}
],
"source": [
"numpy_gauss_sum = sp.lambdify(\n",
" (x, A1, mu1, sigma1, A2, mu2, sigma2), gauss_sum, \"numpy\"\n",
")\n",
"numpy_gauss_sum = sp.lambdify((x, A1, mu1, sigma1, A2, mu2, sigma2), gauss_sum, \"numpy\")\n",
"print(inspect.getsource(numpy_gauss_sum))"
]
},
Expand Down Expand Up @@ -683,9 +679,7 @@
"\n",
"\n",
"def native_jax_gauss_sum(x_, A1_, mu1_, sigma1_, A2_, mu2_, sigma2_):\n",
" return jax_gaussian(x_, A1_, mu1_, sigma1_) + jax_gaussian(\n",
" x_, A2_, mu2_, sigma2_\n",
" )"
" return jax_gaussian(x_, A1_, mu1_, sigma1_) + jax_gaussian(x_, A2_, mu2_, sigma2_)"
]
},
{
Expand Down
4 changes: 1 addition & 3 deletions docs/adr/002/composition.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@
" ff02 = blatt_weisskopf(q0_squared, meson_radius, angular_momentum)\n",
" width = gamma0 * (mass0 / mass) * (ff2 / ff02)\n",
" width = width * sp.sqrt(q_squared / q0_squared)\n",
" return (\n",
" relativistic_breit_wigner(mass, mass0, width) * mass0 * gamma0 * sp.sqrt(ff2)\n",
" )"
" return relativistic_breit_wigner(mass, mass0, width) * mass0 * gamma0 * sp.sqrt(ff2)"
]
},
{
Expand Down
5 changes: 1 addition & 4 deletions docs/adr/002/function.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@
" width = width * sp.sqrt(q_squared / q0_squared)\n",
" # Expression\n",
" return (\n",
" RelativisticBreitWigner(mass, mass0, width)\n",
" * mass0\n",
" * gamma0\n",
" * sp.sqrt(ff2)\n",
" RelativisticBreitWigner(mass, mass0, width) * mass0 * gamma0 * sp.sqrt(ff2)\n",
" )\n",
"\n",
" @classmethod\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/002.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,7 @@
"intensity = LambdifiedFunction(sympy_model, backend=\"jax\")\n",
"data_converter = HelicityTransformer(model.adapter)\n",
"rng = TFUniformRealNumberGenerator(seed=0)\n",
"phsp_sample = generate_phsp(\n",
" 10_000, model.adapter.reaction_info, random_generator=rng\n",
")\n",
"phsp_sample = generate_phsp(10_000, model.adapter.reaction_info, random_generator=rng)\n",
"phsp_set = data_converter.transform(phsp_sample)"
]
},
Expand Down
13 changes: 5 additions & 8 deletions docs/report/003.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@
"outputs": [],
"source": [
"s_thr = (m1 + m2) ** 2\n",
"integrand = (\n",
" PhaseSpaceFactor(s_prime, m1, m2) * FormFactor(s_prime, m1, m2, L, q0)\n",
") / ((s_prime - s_thr) * (s_prime - s - epsilon * sp.I))\n",
"integrand = (PhaseSpaceFactor(s_prime, m1, m2) * FormFactor(s_prime, m1, m2, L, q0)) / (\n",
" (s_prime - s_thr) * (s_prime - s - epsilon * sp.I)\n",
")\n",
"integrand"
]
},
Expand Down Expand Up @@ -857,8 +857,7 @@
"outputs": [],
"source": [
"sigma = {\n",
" l_val: (s_domain - s_thr_val) / np.pi * integral_values[l_val]\n",
" for l_val in l_values\n",
" l_val: (s_domain - s_thr_val) / np.pi * integral_values[l_val] for l_val in l_values\n",
"}\n",
"sigma_scaled = {l_val: 16 * np.pi * sigma[l_val] for l_val in l_values}"
]
Expand Down Expand Up @@ -1185,9 +1184,7 @@
"s_values = np.linspace(-0.15, 1.4, num=200)\n",
"fig, axes = plt.subplots(nrows=2, figsize=(6, 7), sharex=True)\n",
"ax1, ax2 = axes\n",
"fig.suptitle(\n",
" f\"Symbolic dispersion integrals for $m_1={m1_val:.2f}, m_2={m2_val:.2f}$\"\n",
")\n",
"fig.suptitle(f\"Symbolic dispersion integrals for $m_1={m1_val:.2f}, m_2={m2_val:.2f}$\")\n",
"for ax in axes:\n",
" ax.axhline(0, linewidth=0.5, c=\"black\")\n",
" ax.axvline(s_thr_val, **threshold_style)\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/005.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,7 @@
],
"source": [
"K_symbol = sp.IndexedBase(\"K\", shape=(n_channels, n_channels))\n",
"K = sp.Matrix(\n",
" [[K_symbol[i, j] for j in range(n_channels)] for i in range(n_channels)]\n",
")\n",
"K = sp.Matrix([[K_symbol[i, j] for j in range(n_channels)] for i in range(n_channels)])\n",
"display(K_symbol[i, j], K)"
]
},
Expand Down
4 changes: 1 addition & 3 deletions docs/report/009.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@
" c=color,\n",
" rotation=+90,\n",
" )\n",
" threshold_indicators.append(\n",
" (line_thr, line_diff, text_thr, text_diff)\n",
" )\n",
" threshold_indicators.append((line_thr, line_diff, text_thr, text_diff))\n",
" for i, (_, line_diff, _, text_diff) in enumerate(threshold_indicators):\n",
" m_a = kwargs[f\"m_a{i}\"]\n",
" m_b = kwargs[f\"m_b{i}\"]\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/report/010.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@
" m = sp.Symbol(\"m\", real=True)\n",
" epsilon = sp.Symbol(\"epsilon\", real=True)\n",
" i = sp.Symbol(\"i\", integer=True, negative=False)\n",
" expr = f_vector(\n",
" n_resonances, n_channels, angular_momentum=angular_momentum\n",
" ).doit()[i, 0]\n",
" expr = f_vector(n_resonances, n_channels, angular_momentum=angular_momentum).doit()[\n",
" i, 0\n",
" ]\n",
" expr = symplot.substitute_indexed_symbols(expr)\n",
" expr = expr.subs(m, m + epsilon * sp.I)\n",
" np_expr, sliders = symplot.prepare_sliders(expr, m)\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/013.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,7 @@
" intensity_func = create_function(substituted_expression, backend=\"jax\")\n",
" intensities_all = np.array(intensity_func(phsp).real)\n",
" intensities_k = compute_sub_intensities(model, \"K^*\", phsp, full_expression)\n",
" intensities_delta = compute_sub_intensities(\n",
" model, \"Delta^*\", phsp, full_expression\n",
" )\n",
" intensities_delta = compute_sub_intensities(model, \"Delta^*\", phsp, full_expression)\n",
" intensities_lambda = compute_sub_intensities(\n",
" model, \"Lambda^*\", phsp, full_expression\n",
" )\n",
Expand Down
13 changes: 7 additions & 6 deletions docs/report/014.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,7 @@
"source": [
"wigner_functions = {\n",
" sp.Mul(*[\n",
" formulate_wigner_d(transition, node_id)\n",
" for node_id in transition.topology.nodes\n",
" formulate_wigner_d(transition, node_id) for node_id in transition.topology.nodes\n",
" ])\n",
" for transition in reaction.transitions\n",
"}\n",
Expand Down Expand Up @@ -985,10 +984,12 @@
"def formulate_intensity_with_coefficient(reaction: ReactionInfo):\n",
" amplitudes = {\n",
" sp.Mul(\n",
" C[[\n",
" create_helicity_symbol(transition.topology, state_id)\n",
" for state_id in transition.final_states\n",
" ]],\n",
" C[\n",
" [\n",
" create_helicity_symbol(transition.topology, state_id)\n",
" for state_id in transition.final_states\n",
" ]\n",
" ],\n",
" *[\n",
" formulate_wigner_d(transition, node_id)\n",
" for node_id in transition.topology.nodes\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/015.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,7 @@
" particle_name = state.particle.latex\n",
" s = sp.Rational(state.particle.spin)\n",
" m = sp.Rational(state.spin_projection)\n",
" display(\n",
" Math(Rf\"|s_{i},m_{i}\\rangle=|{s},{m}\\rangle \\quad ({particle_name})\")\n",
" )\n",
" display(Math(Rf\"|s_{i},m_{i}\\rangle=|{s},{m}\\rangle \\quad ({particle_name})\"))\n",
" if functor is formulate_rotation_chain:\n",
" args = (transition, i)\n",
" else:\n",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/016.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -600,9 +600,7 @@
"outputs": [],
"source": [
"func = sp.lambdify([x, omega1, omega2, phi1, phi2], expr.doit())\n",
"fourier_func = sp.lambdify(\n",
" [w, omega1, omega2, phi1, phi2, a, b], fourier_expr.doit()\n",
")"
"fourier_func = sp.lambdify([w, omega1, omega2, phi1, phi2, a, b], fourier_expr.doit())"
]
},
{
Expand Down
12 changes: 2 additions & 10 deletions docs/report/017.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@
"@implement_doit_method\n",
"class Kibble(UnevaluatedExpression):\n",
" def __new__(cls, sigma1, sigma2, sigma3, m0, m1, m2, m3, **hints) -> Kibble:\n",
" return create_expression(\n",
" cls, sigma1, sigma2, sigma3, m0, m1, m2, m3, **hints\n",
" )\n",
" return create_expression(cls, sigma1, sigma2, sigma3, m0, m1, m2, m3, **hints)\n",
"\n",
" def evaluate(self) -> sp.Expr:\n",
" sigma1, sigma2, sigma3, m0, m1, m2, m3 = self.args\n",
Expand Down Expand Up @@ -609,13 +607,7 @@
" T2: T0 / 3 - r * sp.cos(theta + 2 * sp.pi / 3),\n",
" T3: T0 / 3 - r * sp.cos(theta + 4 * sp.pi / 3),\n",
" T0: (\n",
" m0**2\n",
" + m1**2\n",
" + m2**2\n",
" + m3**2\n",
" - (m2 + m3) ** 2\n",
" - (m1 + m3) ** 2\n",
" - (m1 + m2) ** 2\n",
" m0**2 + m1**2 + m2**2 + m3**2 - (m2 + m3) ** 2 - (m1 + m3) ** 2 - (m1 + m2) ** 2\n",
" ),\n",
"}"
]
Expand Down
8 changes: 2 additions & 6 deletions docs/report/018.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,7 @@
" initial_state_mass=reaction.initial_state[-1].mass,\n",
" final_state_masses={i: p.mass for i, p in reaction.final_state.items()},\n",
")\n",
"transformer = SympyDataTransformer.from_sympy(\n",
" model.kinematic_variables, backend=\"jax\"\n",
")\n",
"transformer = SympyDataTransformer.from_sympy(model.kinematic_variables, backend=\"jax\")\n",
"phsp = phsp_generator.generate(1_000_000, rng)\n",
"phsp = transformer(phsp)"
]
Expand Down Expand Up @@ -350,9 +348,7 @@
"\n",
" Z_weights = griddata(np.transpose([x, y]), weights, (X, Y))\n",
" Z_unweighted, x_edges, y_edges = jnp.histogram2d(x, y, bins=n_bins)\n",
" Z_weighted, x_edges, y_edges = jnp.histogram2d(\n",
" x, y, bins=n_bins, weights=weights\n",
" )\n",
" Z_weighted, x_edges, y_edges = jnp.histogram2d(x, y, bins=n_bins, weights=weights)\n",
" # https://numpy.org/doc/stable/reference/generated/numpy.histogram2d.html\n",
" Z_unweighted = Z_unweighted.T\n",
" Z_weighted = Z_weighted.T\n",
Expand Down
10 changes: 4 additions & 6 deletions docs/report/020.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -810,9 +810,7 @@
],
"source": [
"minuit2 = Minuit2()\n",
"fit_result_with_caching = minuit2.optimize(\n",
" estimator_with_caching, initial_parameters\n",
")\n",
"fit_result_with_caching = minuit2.optimize(estimator_with_caching, initial_parameters)\n",
"fit_result_with_caching"
]
},
Expand Down Expand Up @@ -920,9 +918,9 @@
" # this here is rather to take full control, it is always possible\n",
"\n",
" # updating the parameters of the model. This seems not very TF compatible?\n",
" self.intensity.update_parameters(\n",
" {p.name: float(p) for p in self.params.values()}\n",
" )\n",
" self.intensity.update_parameters({\n",
" p.name: float(p) for p in self.params.values()\n",
" })\n",
"\n",
" # converting the data to a dict for tensorwaves\n",
" data = {ob: jnp.asarray(ar) for ob, ar in zip(self.obs, z.unstack_x(x))}\n",
Expand Down
Loading

0 comments on commit af4d02b

Please sign in to comment.