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

FEAT: make 2D histograms interactive with sliders #73

Merged
merged 7 commits into from
Aug 8, 2024
Merged
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
86 changes: 49 additions & 37 deletions docs/ampform/LambdaKpi0.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -430,43 +430,6 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"%config InlineBackend.figure_formats = ['png']\n",
"bin_values, xedges, yedges = jnp.histogram2d(\n",
" phsp[\"m_01\"].real ** 2,\n",
" phsp[\"m_12\"].real ** 2,\n",
" bins=200,\n",
" weights=intensity_func(phsp),\n",
" density=True,\n",
")\n",
"bin_values = jnp.where(bin_values < 1e-6, jnp.nan, bin_values)\n",
"X, Y = jnp.meshgrid(xedges[:-1], yedges[:-1])\n",
"\n",
"fig, ax = plt.subplots(dpi=150)\n",
"mesh = ax.pcolormesh(X, Y, bin_values.T, cmap=\"jet\", vmax=0.15)\n",
"ax.set_title(\"Model-weighted Phase space Dalitz Plot\")\n",
"ax.set_xlabel(R\"$m^2(\\Lambda K^+)\\;\\left[\\mathrm{GeV}^2\\right]$\")\n",
"ax.set_ylabel(R\"$m^2(K^+ \\pi^0)\\;\\left[\\mathrm{GeV}^2\\right]$\")\n",
"fig.colorbar(mesh, ax=ax)\n",
"fig.tight_layout()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-output",
"hide-input"
Expand Down Expand Up @@ -548,6 +511,55 @@
"UI = w.Tab(children=tabs, titles=tuple(categorized_sliders_m))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"source_hidden": true
},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"%matplotlib widget\n",
"%config InlineBackend.figure_formats = ['png']\n",
"fig_2d, ax_2d = plt.subplots(dpi=200)\n",
"ax_2d.set_title(\"Model-weighted Phase space Dalitz Plot\")\n",
"ax_2d.set_xlabel(R\"$m^2(\\Lambda K^+)\\;\\left[\\mathrm{GeV}^2\\right]$\")\n",
"ax_2d.set_ylabel(R\"$m^2(K^+ \\pi^0)\\;\\left[\\mathrm{GeV}^2\\right]$\")\n",
"\n",
"mesh = None\n",
"\n",
"\n",
"def update_histogram(**parameters):\n",
" global mesh\n",
" intensity_func.update_parameters(parameters)\n",
" intensity_weights = intensity_func(phsp)\n",
" bin_values, xedges, yedges = jnp.histogram2d(\n",
" phsp[\"m_01\"].real ** 2,\n",
" phsp[\"m_12\"].real ** 2,\n",
" bins=200,\n",
" weights=intensity_weights,\n",
" density=True,\n",
" )\n",
" bin_values = jnp.where(bin_values < 1e-6, jnp.nan, bin_values)\n",
" x, y = jnp.meshgrid(xedges[:-1], yedges[:-1])\n",
" if mesh is None:\n",
" mesh = ax_2d.pcolormesh(x, y, bin_values.T, cmap=\"jet\", vmax=0.15)\n",
" else:\n",
" mesh.set_array(bin_values.T)\n",
" fig_2d.canvas.draw_idle()\n",
"\n",
"\n",
"interactive_plot = w.interactive_output(update_histogram, sliders)\n",
"fig_2d.tight_layout()\n",
"fig_2d.colorbar(mesh, ax=ax_2d)\n",
"display(UI, interactive_plot)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down