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

ENH: generate particle property table from database #55

Merged
merged 18 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
216 changes: 123 additions & 93 deletions docs/ampform/LambdaKpi0.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
"cell_type": "code",
"execution_count": null,
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"metadata": {
"jupyter": {
"source_hidden": true
},
"mystnb": {
"code_prompt_show": "Hide warnings"
},
Expand All @@ -46,9 +43,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"mystnb": {
"code_prompt_show": "Import Python Libraries"
},
Expand All @@ -59,16 +53,18 @@
"outputs": [],
"source": [
"from collections import defaultdict\n",
"from fractions import Fraction\n",
"\n",
"import ampform\n",
"import graphviz\n",
"import jax\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"import qrules\n",
"from ampform.dynamics.builder import RelativisticBreitWignerBuilder\n",
"from ampform.io import aslatex, improve_latex_rendering\n",
"from IPython.display import Math\n",
"from IPython.display import HTML, Math, display\n",
"from qrules.particle import Particle, Spin, create_particle, load_pdg\n",
"from tensorwaves.data import (\n",
" SympyDataTransformer,\n",
Expand Down Expand Up @@ -99,101 +95,138 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"mystnb": {
"code_prompt_show": "Lambda"
},
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"particle_db[\"Lambda\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"mystnb": {
"code_prompt_show": "K+"
},
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"particle_db[\"K+\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"mystnb": {
"code_prompt_show": "pi0"
},
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"particle_db[\"pi0\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"mystnb": {
"code_prompt_show": "gamma"
},
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"particle_db[\"gamma\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"mystnb": {
"code_prompt_show": "p"
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-cell"
"hide-input"
]
},
"outputs": [],
"source": [
"particle_db[\"p\"]"
"particles = [\"Lambda\", \"K+\", \"pi0\", \"gamma\", \"p\"]\n",
"latex_particle_names = {\n",
" \"Lambda\": r\"\\Lambda\",\n",
" \"K+\": r\"K^+\",\n",
" \"pi0\": r\"\\pi^0\",\n",
" \"gamma\": r\"\\gamma\",\n",
" \"p\": \"p\",\n",
"}\n",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"\n",
"def format_parity(parity: int | None) -> str:\n",
" if parity is None:\n",
" return \"\"\n",
" if parity == -1:\n",
" return \"-\"\n",
" if parity == 1:\n",
" return \"+\"\n",
" return str(parity)\n",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"\n",
"def format_fraction(value: Fraction | None) -> str | None:\n",
" if value is None:\n",
" return None\n",
" if value.denominator == 1:\n",
" return str(value.numerator)\n",
" return rf\"\\frac{{{value.numerator}}}{{{value.denominator}}}\"\n",
"\n",
"\n",
"def format_fraction_isospin3(value: Fraction | None) -> str | None:\n",
" if value is None:\n",
" return None\n",
" if value.denominator == 1:\n",
" return str(value.numerator)\n",
" return rf\"$\\frac{{{value.numerator}}}{{{value.denominator}}}$\"\n",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"\n",
"\n",
"def particle_extract(name: str) -> dict:\n",
" particle = particle_db[name]\n",
"\n",
" isospin_magnitude_value = (\n",
" Fraction(particle.isospin.magnitude) if particle.isospin is not None else None\n",
" )\n",
" isospin_magnitude_latex = (\n",
" format_fraction(isospin_magnitude_value)\n",
" if isospin_magnitude_value is not None\n",
" else \"0\"\n",
" if particle.isospin and particle.isospin.magnitude == 0\n",
" else \"\"\n",
" )\n",
"\n",
" isospin_projection_value = (\n",
" Fraction(particle.isospin.projection) if particle.isospin is not None else None\n",
" )\n",
" isospin_projection_latex = (\n",
" format_fraction_isospin3(isospin_projection_value)\n",
" if isospin_projection_value is not None\n",
" else \"0\"\n",
" if particle.isospin and particle.isospin.projection == 0\n",
" else None\n",
" )\n",
"\n",
" spin_fraction_value = Fraction(particle.spin) if particle.spin is not None else None\n",
" spin_latex = (\n",
" format_fraction(spin_fraction_value)\n",
" if spin_fraction_value is not None\n",
" else \"0\"\n",
" if particle.spin == 0\n",
" else \"\"\n",
" )\n",
"\n",
" if particle.isospin is not None:\n",
" jpc_ig = rf\"${spin_latex}^{{{format_parity(particle.parity)}{format_parity(particle.c_parity)}}} \\ ({isospin_magnitude_latex}^{{{format_parity(particle.g_parity)}}})$\"\n",
" else:\n",
" jpc_ig = rf\"${spin_latex}^{{{format_parity(particle.parity)}{format_parity(particle.c_parity)}}}$\"\n",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"\n",
" return {\n",
" \"Name\": particle.name,\n",
" \"Latex\": particle.latex,\n",
" \"PID\": particle.pid,\n",
" r\"$J^{PC} (I^G)$\": jpc_ig,\n",
" r\"$I_3$\": isospin_projection_latex,\n",
" r\"$M$\": str(particle.mass),\n",
" r\"$\\Gamma$\": str(particle.width),\n",
" r\"$Q$\": particle.charge,\n",
" r\"$S$\": particle.strangeness,\n",
" r\"$B$\": particle.baryon_number,\n",
" }\n",
"\n",
"\n",
"info = [particle_extract(name) for name in particles]\n",
"particle_dataframe = pd.DataFrame(info)\n",
"\n",
"particle_dataframe_transposed = particle_dataframe.transpose().reset_index()\n",
"particle_dataframe_transposed.columns = [\"\", *particles]\n",
"particle_dataframe_transposed.columns = [\n",
" \"\",\n",
" *[f\"${latex_particle_names.get(name, name)}$\" for name in particles],\n",
"]\n",
"\n",
"\n",
"html_table = particle_dataframe_transposed.to_html(index=False, escape=False)\n",
"\n",
"mathjax_script = \"\"\"\n",
"<script type=\"text/javascript\" async\n",
" src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\">\n",
"</script>\n",
"\"\"\"\n",
"\n",
"display(HTML(mathjax_script + html_table))"
shenvitor marked this conversation as resolved.
Show resolved Hide resolved
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| Particle | Lambda | K+ | pi0 | gamma | p |\n",
"|------------------|-------------------|------------------|------------------|------------------|------------------|\n",
"| **Name** | Lambda | K+ | pi0 | gamma | p |\n",
"| **PID** | 3122 | 321 | 111 | 22 | 2212 |\n",
"| **Latex** | \\(\\Lambda\\) | \\(K^{+}\\) | \\(\\pi^{0}\\) | \\(\\gamma\\) | \\(p\\) |\n",
"| **Spin** | 0.5 | 0.0 | 0.0 | 1.0 | 0.5 |\n",
"| **Mass** | 1.115683 | 0.49367700000000003 | 0.1349768 | 0.0 | 0.93827208816 |\n",
"| **Width** | 2.501e-15 | 5.317e-17 | 7.81e-09 | | |\n",
"| **Charge** | | 1 | | | 1 |\n",
"| **Isospin** | Spin(0, 0) | Spin(1/2, +1/2) | Spin(1, 0) | | Spin(1/2, +1/2) |\n",
"| **Strangeness** | -1 | 1 | | | |\n",
"| **Baryon Number**| 1 | | | | 1 |\n",
"| **Parity** | +1 | -1 | -1 | -1 | +1 |\n",
"| **C Parity** | | | +1 | -1 | |\n",
"| **G Parity** | | | -1 | | |\n"
"In the table above, PID is the PDG ID from PDG particle numbering scheme,\n",
"$J$ is the spin, $P$ is the parity, $C$ is the C parity, $I$ is the isospin (magnitude), $G$ is the G parity.\n",
"$I_3$ is the isospin projection (or the 3rd component),\n",
"$M$ is the mass,\n",
"$\\Gamma$ is the width,\n",
"$Q$ is the charge,\n",
"$S$ is the strangeness number,\n",
"and $B$ is the baryon number."
]
},
{
Expand Down Expand Up @@ -363,9 +396,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"tags": [
"hide-input",
"scroll-output"
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"myst_nb",
"sphinx_comments",
"sphinx_copybutton",
"sphinx.ext.mathjax",
]
html_theme = "sphinx_book_theme"
master_doc = "index"
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ select = ["ALL"]

[tool.ruff.lint.per-file-ignores]
"*.ipynb" = [
"D103",
"E501",
redeboer marked this conversation as resolved.
Show resolved Hide resolved
"T201",
]
"docs/conf.py" = [
Expand Down