From 01d37cf37f7ac5ca2a90e9b42a7f1bb7f1240626 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Thu, 26 Sep 2024 16:56:44 +0200 Subject: [PATCH] MAINT: move imports to top cell --- .pre-commit-config.yaml | 24 +++++++++++++++++++++++- docs/report/004.ipynb | 9 +++++---- docs/report/014.ipynb | 9 +++------ docs/report/015.ipynb | 35 +++++++++++++++-------------------- docs/report/017.ipynb | 18 ++++++++++++++++-- docs/report/021.ipynb | 12 ++---------- docs/report/026.ipynb | 4 +--- docs/report/033.ipynb | 29 ++++++----------------------- pyproject.toml | 3 +++ 9 files changed, 74 insertions(+), 69 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 014e9784..24f3d6b1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,13 +13,14 @@ repos: - id: check-useless-excludes - repo: https://github.com/ComPWA/policy - rev: 0.4.0 + rev: 0.4.1 hooks: - id: check-dev-files args: - --doc-apt-packages=graphviz - --dev-python-version=3.12 - --github-pages + - --imports-on-top - --no-prettierrc - --pin-requirements=bimonthly - --repo-name=compwa.github.io @@ -146,6 +147,27 @@ repos: docs/_static/favicon.ico )$ + - repo: https://github.com/nbQA-dev/nbQA + rev: 1.9.0 + hooks: + - id: nbqa-isort + args: [--float-to-top] + exclude: >- + (?x)^( + docs/adr/.*\.ipynb| + docs/report/001\.ipynb| + docs/report/002\.ipynb| + docs/report/003\.ipynb| + docs/report/008\.ipynb| + docs/report/013\.ipynb| + docs/report/018\.ipynb| + docs/report/020\.ipynb| + docs/report/026\.ipynb| + docs/report/027\.ipynb| + docs/report/028\.ipynb| + docs/symbolics\.ipynb + )$ + - repo: local hooks: - id: pin-nb-requirements diff --git a/docs/report/004.ipynb b/docs/report/004.ipynb index daba0609..335077c8 100644 --- a/docs/report/004.ipynb +++ b/docs/report/004.ipynb @@ -77,7 +77,11 @@ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import sympy as sp\n", - "from ampform.dynamics import PhaseSpaceFactor, relativistic_breit_wigner_with_ff\n", + "from ampform.dynamics import (\n", + " BreakupMomentumSquared,\n", + " PhaseSpaceFactor,\n", + " relativistic_breit_wigner_with_ff,\n", + ")\n", "from IPython.display import Math, display\n", "from ipywidgets import widgets\n", "from matplotlib import cm\n", @@ -374,9 +378,6 @@ }, "outputs": [], "source": [ - "from ampform.dynamics import BreakupMomentumSquared\n", - "\n", - "\n", "def breakup_momentum(s: sp.Symbol, m_a: sp.Symbol, m_b: sp.Symbol) -> sp.Expr:\n", " return sp.sqrt(BreakupMomentumSquared(s, m_a, m_b).doit())\n", "\n", diff --git a/docs/report/014.ipynb b/docs/report/014.ipynb index 78aaaf24..43535150 100644 --- a/docs/report/014.ipynb +++ b/docs/report/014.ipynb @@ -65,6 +65,7 @@ "import inspect\n", "import itertools\n", "import logging\n", + "from collections import defaultdict\n", "from functools import lru_cache\n", "from typing import TYPE_CHECKING, Iterable, Sequence\n", "\n", @@ -84,12 +85,14 @@ " _generate_kinematic_variables,\n", ")\n", "from ampform.helicity.decay import TwoBodyDecay\n", + "from ampform.helicity.naming import HelicityAmplitudeNameGenerator\n", "from ampform.sympy import (\n", " UnevaluatedExpression,\n", " create_expression,\n", " implement_doit_method,\n", ")\n", "from IPython.display import Math, display\n", + "from qrules import ReactionInfo\n", "from sympy.core.symbol import Str\n", "from sympy.physics.quantum.spin import Rotation as Wigner\n", "from sympy.printing.precedence import PRECEDENCE\n", @@ -641,10 +644,6 @@ }, "outputs": [], "source": [ - "from collections import defaultdict\n", - "\n", - "from qrules import ReactionInfo\n", - "\n", "if TYPE_CHECKING:\n", " if sys.version_info >= (3, 8):\n", " from typing import Literal\n", @@ -1460,8 +1459,6 @@ }, "outputs": [], "source": [ - "from ampform.helicity.naming import HelicityAmplitudeNameGenerator\n", - "\n", "A = sp.IndexedBase(R\"\\mathcal{A}\")\n", "\n", "\n", diff --git a/docs/report/015.ipynb b/docs/report/015.ipynb index 9d5550cd..98a4bb7f 100644 --- a/docs/report/015.ipynb +++ b/docs/report/015.ipynb @@ -61,15 +61,29 @@ }, "outputs": [], "source": [ + "import inspect\n", "import logging\n", "import warnings\n", "\n", "import ampform\n", "import graphviz\n", + "import numpy as np\n", "import qrules\n", "import sympy as sp\n", - "from ampform.helicity import formulate_wigner_d\n", + "from ampform.helicity import (\n", + " formulate_helicity_rotation_chain,\n", + " formulate_rotation_chain,\n", + " formulate_spin_alignment,\n", + " formulate_wigner_d,\n", + ")\n", + "from ampform.kinematics import (\n", + " compute_boost_chain,\n", + " compute_wigner_angles,\n", + " compute_wigner_rotation_matrix,\n", + " create_four_momentum_symbols,\n", + ")\n", "from IPython.display import Math, display\n", + "from qrules.topology import create_isobar_topologies\n", "\n", "LOGGER = logging.getLogger()\n", "LOGGER.setLevel(logging.ERROR)\n", @@ -518,13 +532,6 @@ }, "outputs": [], "source": [ - "from ampform.helicity import (\n", - " formulate_helicity_rotation_chain,\n", - " formulate_rotation_chain,\n", - " formulate_spin_alignment,\n", - ")\n", - "\n", - "\n", "def show_all_spin_matrices(transition, functor, cleanup: bool) -> None:\n", " for i in transition.final_states:\n", " state = transition.states[i]\n", @@ -1136,8 +1143,6 @@ } ], "source": [ - "from ampform.kinematics import compute_boost_chain, create_four_momentum_symbols\n", - "\n", "momenta = create_four_momentum_symbols(topology)\n", "for state_id in topology.outgoing_edge_ids:\n", " boosts = compute_boost_chain(topology, momenta, state_id)\n", @@ -1198,8 +1203,6 @@ } ], "source": [ - "from ampform.kinematics import compute_wigner_rotation_matrix\n", - "\n", "for state_id in topology.outgoing_edge_ids:\n", " expr = compute_wigner_rotation_matrix(topology, momenta, state_id)\n", " display(expr)" @@ -1218,8 +1221,6 @@ "metadata": {}, "outputs": [], "source": [ - "from ampform.kinematics import compute_wigner_angles\n", - "\n", "angles = {}\n", "for state_id in topology.outgoing_edge_ids:\n", " angle_definitions = compute_wigner_angles(topology, momenta, state_id)\n", @@ -1326,8 +1327,6 @@ } ], "source": [ - "import inspect\n", - "\n", "beta = sp.Symbol(\"beta_1^12\", real=True)\n", "beta_expr = angles[beta]\n", "\n", @@ -1368,8 +1367,6 @@ "metadata": {}, "outputs": [], "source": [ - "import numpy as np\n", - "\n", "phsp = {\n", " \"p0\": np.array([\n", " [0.63140486, 0.13166435, -0.35734744, 0.07760603],\n", @@ -1591,8 +1588,6 @@ } ], "source": [ - "from qrules.topology import create_isobar_topologies\n", - "\n", "topology_4body = create_isobar_topologies(4)[1]\n", "momenta_4body = create_four_momentum_symbols(topology_4body)\n", "compute_wigner_rotation_matrix(topology_4body, momenta_4body, state_id=3)" diff --git a/docs/report/017.ipynb b/docs/report/017.ipynb index bf3a838b..12777c0e 100644 --- a/docs/report/017.ipynb +++ b/docs/report/017.ipynb @@ -61,8 +61,6 @@ }, "outputs": [], "source": [ - "%config InlineBackend.figure_formats = ['svg']\n", - "\n", "from __future__ import annotations\n", "\n", "import os\n", @@ -91,6 +89,22 @@ "warnings.filterwarnings(\"ignore\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "jupyter": { + "source_hidden": true + }, + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "%config InlineBackend.figure_formats = ['svg']" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/docs/report/021.ipynb b/docs/report/021.ipynb index 0db2b6e1..e1c94407 100644 --- a/docs/report/021.ipynb +++ b/docs/report/021.ipynb @@ -77,9 +77,9 @@ "source": [ "from __future__ import annotations\n", "\n", - "import os\n", "import itertools\n", "import logging\n", + "import os\n", "from typing import TYPE_CHECKING\n", "\n", "import jax.numpy as jnp\n", @@ -97,15 +97,7 @@ ")\n", "from attrs import frozen\n", "from IPython.display import HTML, Image, Math, display\n", - "from ipywidgets import (\n", - " Button,\n", - " Combobox,\n", - " HBox,\n", - " HTMLMath,\n", - " Tab,\n", - " VBox,\n", - " interactive_output,\n", - ")\n", + "from ipywidgets import Button, Combobox, HBox, HTMLMath, Tab, VBox, interactive_output\n", "from matplotlib.colors import LogNorm\n", "from symplot import create_slider\n", "from sympy.core.symbol import Str\n", diff --git a/docs/report/026.ipynb b/docs/report/026.ipynb index e0ee3d1f..f347f5db 100644 --- a/docs/report/026.ipynb +++ b/docs/report/026.ipynb @@ -83,6 +83,7 @@ "import plotly.graph_objects as go\n", "import sympy as sp\n", "from ampform.io import aslatex\n", + "from ampform.kinematics.phasespace import Kallen\n", "from ampform.sympy import unevaluated\n", "from IPython.display import Math\n", "\n", @@ -112,9 +113,6 @@ "outputs": [], "source": [ "# @title\n", - "from ampform.kinematics.phasespace import Kallen\n", - "\n", - "\n", "@unevaluated(real=False)\n", "class PhaseSpaceFactor(sp.Expr):\n", " s: Any\n", diff --git a/docs/report/033.ipynb b/docs/report/033.ipynb index 63c9d5ea..ccde37f2 100644 --- a/docs/report/033.ipynb +++ b/docs/report/033.ipynb @@ -94,7 +94,7 @@ "source_hidden": true }, "mystnb": { - "code_prompt_show": "Hide warnings" + "code_prompt_show": "Import Python libraries" }, "tags": [ "hide-input" @@ -108,27 +108,6 @@ "import os\n", "import warnings\n", "\n", - "os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n", - "logging.disable(logging.WARNING)\n", - "warnings.filterwarnings(\"ignore\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "jupyter": { - "source_hidden": true - }, - "mystnb": { - "code_prompt_show": "Import Python libraries" - }, - "tags": [ - "hide-input" - ] - }, - "outputs": [], - "source": [ "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", @@ -140,7 +119,11 @@ "from iminuit import Minuit\n", "from matplotlib import gridspec\n", "from tqdm.auto import tqdm\n", - "from vector.backends.numpy import MomentumNumpy4D" + "from vector.backends.numpy import MomentumNumpy4D\n", + "\n", + "os.environ[\"TF_CPP_MIN_LOG_LEVEL\"] = \"3\"\n", + "logging.disable(logging.WARNING)\n", + "warnings.filterwarnings(\"ignore\")" ] }, { diff --git a/pyproject.toml b/pyproject.toml index bc45b566..654b60a9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -108,6 +108,9 @@ local_scheme = "no-local-version" version_scheme = "post-release" write_to = "version.py" +[tool.isort] +profile = "black" + [tool.pixi.project] channels = ["conda-forge"] platforms = ["linux-64"]