Skip to content

Commit

Permalink
DX: enforce imports to top by default (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenvitor authored Sep 30, 2024
1 parent d6acad8 commit df6cc12
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 69 deletions.
24 changes: 23 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions docs/report/004.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions docs/report/014.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1460,8 +1459,6 @@
},
"outputs": [],
"source": [
"from ampform.helicity.naming import HelicityAmplitudeNameGenerator\n",
"\n",
"A = sp.IndexedBase(R\"\\mathcal{A}\")\n",
"\n",
"\n",
Expand Down
35 changes: 15 additions & 20 deletions docs/report/015.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
Expand All @@ -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",
Expand Down Expand Up @@ -1326,8 +1327,6 @@
}
],
"source": [
"import inspect\n",
"\n",
"beta = sp.Symbol(\"beta_1^12\", real=True)\n",
"beta_expr = angles[beta]\n",
"\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)"
Expand Down
18 changes: 16 additions & 2 deletions docs/report/017.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['svg']\n",
"\n",
"from __future__ import annotations\n",
"\n",
"import os\n",
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 2 additions & 10 deletions docs/report/021.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
4 changes: 1 addition & 3 deletions docs/report/026.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
29 changes: 6 additions & 23 deletions docs/report/033.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
"source_hidden": true
},
"mystnb": {
"code_prompt_show": "Hide warnings"
"code_prompt_show": "Import Python libraries"
},
"tags": [
"hide-input"
Expand All @@ -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",
Expand All @@ -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\")"
]
},
{
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit df6cc12

Please sign in to comment.