Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ pyvenv*/
/.agents
/.claude/
/.codex
/AGENTS.md
/CLAUDE.md
AGENTS.md
CLAUDE.md

# Exceptions
!.cspell.json
Expand Down
33 changes: 16 additions & 17 deletions docs/amplitude-analysis.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@
"\n",
"Let's have a look at our [first guess for the parameter values](#determine-free-parameters). Recall that a {class}`.ParametrizedFunction` object computes the intensity for a certain {obj}`.DataSample`. This can be seen nicely when we use these intensities as weights on the phase space sample and plot it together with the original data sample. Here, we look at the invariant mass distribution projection of the final states `1` and `2`, which, [as we saw before](compwa-step-2.3), is the final state particle pair $\\pi^0\\pi^0$.\n",
"\n",
"Don't forget to use {meth}`~.ParametrizedFunction.update_parameters` first!"
"Don't forget to first create a function with these initial parameter values using {meth}`~.ParametrizedFunction.with_parameters`!"
]
},
{
Expand Down Expand Up @@ -1473,8 +1473,8 @@
"outputs": [],
"source": [
"original_parameters = optimized_function.parameters\n",
"optimized_function.update_parameters(initial_parameters)\n",
"compare_model(\"m_12\", data_real, phsp_real, optimized_function)"
"initial_function = optimized_function.with_parameters(initial_parameters)\n",
"compare_model(\"m_12\", data_real, phsp_real, initial_function)"
]
},
{
Expand Down Expand Up @@ -1622,7 +1622,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the same method as above, we renew the parameters of the {class}`.ParametrizedFunction` and plot it again over the phase space sample."
"Using the same method as above, we create a new {class}`.ParametrizedFunction` with the optimized parameter values and plot it again over the phase space sample."
]
},
{
Expand All @@ -1631,7 +1631,7 @@
"metadata": {},
"outputs": [],
"source": [
"optimized_function.update_parameters(fit_result.parameter_values)\n",
"optimized_function = optimized_function.with_parameters(fit_result.parameter_values)\n",
"compare_model(\"m_12\", data_real, phsp_real, optimized_function)"
]
},
Expand Down Expand Up @@ -1752,22 +1752,21 @@
" input_data: DataSample,\n",
" resonances: list[str],\n",
"):\n",
" original_parameters = dict(func.parameters)\n",
" negative_lookahead = f\"(?!{'|'.join(map(re.escape, resonances))})\"\n",
" # https://regex101.com/r/WrgGyD/1\n",
" pattern = rf\"^(\\\\mathcal{{H}}|C_)({negative_lookahead}.)*$\"\n",
" set_parameters_to_zero(func, pattern)\n",
" array = func(input_data)\n",
" func.update_parameters(original_parameters)\n",
" return array\n",
" zeroed_parameters = get_zeroed_parameters(func, pattern)\n",
" return func(input_data, zeroed_parameters)\n",
"\n",
"\n",
"def set_parameters_to_zero(func: ParametrizedFunction, name_pattern: str) -> None:\n",
" new_parameters = dict(func.parameters)\n",
" for par_name in func.parameters:\n",
" if re.match(name_pattern, par_name) is not None:\n",
" new_parameters[par_name] = 0\n",
" func.update_parameters(new_parameters)"
"def get_zeroed_parameters(\n",
" func: ParametrizedFunction, name_pattern: str\n",
") -> dict[str, complex]:\n",
" return {\n",
" par_name: 0\n",
" for par_name in func.parameters\n",
" if re.match(name_pattern, par_name) is not None\n",
" }"
]
},
{
Expand Down Expand Up @@ -1908,7 +1907,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.13"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def get_tensorflow_url() -> str:
"**.ipynb_checkpoints",
"*build",
"adr*",
"AGENTS.md",
"CLAUDE.md",
"tests",
]
extensions = [
Expand Down
36 changes: 24 additions & 12 deletions docs/usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@
"bin_values, bin_edges, _ = ax.hist(data[\"x\"], bins=50, alpha=0.7, label=\"data\")\n",
"x_values = (bin_edges[1:] + bin_edges[:-1]) / 2\n",
"y_values = bin_values\n",
"function.update_parameters(initial_parameters)\n",
"lines = ax.plot(\n",
" x_values, function({\"x\": x_values}), c=\"red\", linewidth=2, label=\"model\"\n",
" x_values,\n",
" function({\"x\": x_values}, initial_parameters),\n",
" c=\"red\",\n",
" linewidth=2,\n",
" label=\"model\",\n",
")\n",
"ax.legend(loc=\"upper right\")\n",
"plt.show()"
Expand Down Expand Up @@ -201,14 +204,15 @@
"class FitAnimation(Callback):\n",
" def __init__(self, data, function, x_values, output_file, estimated_iterations=140):\n",
" self.__function = function\n",
" self.__parameters = dict(function.parameters)\n",
" self.__fig, (self.__ax1, self.__ax2) = plt.subplots(\n",
" nrows=2, figsize=(7, 7), tight_layout=True\n",
" )\n",
" self.__ax2.set_yticks(np.arange(-30, 80, 10))\n",
" self.__ax1.hist(data[\"x\"], bins=50, alpha=0.7, label=\"data\")\n",
" self.__line = self.__ax1.plot(\n",
" x_values,\n",
" function({\"x\": x_values}),\n",
" function({\"x\": x_values}, self.__parameters),\n",
" c=\"red\",\n",
" linewidth=2,\n",
" label=\"model\",\n",
Expand All @@ -217,46 +221,54 @@
"\n",
" self.__par_lines = [\n",
" self.__ax2.plot(0, value, label=par)[0]\n",
" for par, value in function.parameters.items()\n",
" for par, value in self.__parameters.items()\n",
" ]\n",
" self.__ax2.set_xlim(0, estimated_iterations)\n",
" self.__ax2.set_title(\"Parameter values\")\n",
" self.__ax2.legend(\n",
" [f\"${sp.latex(sp.Symbol(par_name))}$\" for par_name in function.parameters],\n",
" [f\"${sp.latex(sp.Symbol(par_name))}$\" for par_name in self.__parameters],\n",
" loc=\"upper right\",\n",
" )\n",
"\n",
" self.__writer = PillowWriter(fps=15)\n",
" self.__writer.setup(self.__fig, outfile=output_file)\n",
"\n",
" def on_optimize_start(self, logs):\n",
" self._update_parameters(logs)\n",
" self._update_plot()\n",
"\n",
" def on_optimize_end(self, logs):\n",
" self._update_parameters(logs)\n",
" self._update_plot()\n",
" self.__writer.finish()\n",
"\n",
" def on_iteration_end(self, iteration, logs):\n",
" self._update_parameters(logs)\n",
" self._update_plot()\n",
" self.__writer.finish()\n",
"\n",
" def on_function_call_end(self, function_call, logs):\n",
" self._update_parameters(logs)\n",
" self._update_plot()\n",
"\n",
" def _update_parameters(self, logs):\n",
" if logs is not None:\n",
" self.__parameters.update(logs[\"parameters\"])\n",
"\n",
" def _update_plot(self):\n",
" self._update_parametrization_plot()\n",
" self._update_traceback()\n",
" self.__writer.grab_frame()\n",
"\n",
" def _update_parametrization_plot(self):\n",
" title = self._render_parameters(self.__function.parameters)\n",
" title = self._render_parameters(self.__parameters)\n",
" self.__ax1.set_title(title)\n",
" self.__line.set_ydata(self.__function({\"x\": x_values}))\n",
" self.__line.set_ydata(self.__function({\"x\": x_values}, self.__parameters))\n",
"\n",
" def _update_traceback(self):\n",
" for line in self.__par_lines:\n",
" par_name = line.get_label()\n",
" new_value = function.parameters[par_name]\n",
" new_value = self.__parameters[par_name]\n",
" x = line.get_xdata()\n",
" x = [*x, x[-1] + 1]\n",
" y = [*line.get_ydata(), new_value]\n",
Expand Down Expand Up @@ -730,13 +742,13 @@
")\n",
"def plot(dphi, k_r, k_phi, sigma):\n",
" global color_mesh, X, Y\n",
" polar_function.update_parameters({\n",
" parameters = {\n",
" R\"\\Delta\\phi\": dphi,\n",
" \"k_r\": k_r,\n",
" \"k_phi\": k_phi,\n",
" \"sigma\": sigma,\n",
" })\n",
" Z = polar_function(polar_domain)\n",
" }\n",
" Z = polar_function(polar_domain, parameters)\n",
" if color_mesh is not None:\n",
" color_mesh.remove()\n",
" color_mesh = ax_interactive.pcolormesh(X, Y, Z, cmap=\"coolwarm\")"
Expand Down Expand Up @@ -793,7 +805,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
14 changes: 7 additions & 7 deletions docs/usage/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
"source": [
"For the rest, the procedure is really just the same as that sketched in {ref}`compwa-step-3`.\n",
"\n",
"We tweak the parameters a bit, then use {meth}`.ParametrizedBackendFunction.update_parameters` to change the function..."
"We tweak the parameters a bit, then use {meth}`.ParametrizedBackendFunction.with_parameters` to create a new function with these parameter values..."
]
},
{
Expand All @@ -606,7 +606,7 @@
" \"sigma_0\": 0.4,\n",
" \"sigma_1\": 0.4,\n",
"}\n",
"function_1d.update_parameters(initial_parameters)"
"function_1d = function_1d.with_parameters(initial_parameters)"
]
},
{
Expand Down Expand Up @@ -730,7 +730,7 @@
"outputs": [],
"source": [
"optimized_parameters = fit_result.parameter_values\n",
"function_1d.update_parameters(optimized_parameters)"
"function_1d = function_1d.with_parameters(optimized_parameters)"
]
},
{
Expand Down Expand Up @@ -1008,7 +1008,7 @@
" \"sigma_0\": 0.4,\n",
" \"sigma_1\": 0.4,\n",
"}\n",
"function_2d.update_parameters(initial_parameters)"
"function_2d = function_2d.with_parameters(initial_parameters)"
]
},
{
Expand Down Expand Up @@ -1148,7 +1148,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"If we update the parameters in the {class}`.ParametrizedFunction` with the optimized parameter values found by the {class}`.Optimizer`, we can compare the data distribution with the function."
"If we create a new {class}`.ParametrizedFunction` with the optimized parameter values found by the {class}`.Optimizer`, we can compare the data distribution with the function."
]
},
{
Expand All @@ -1158,7 +1158,7 @@
"outputs": [],
"source": [
"optimized_parameters = fit_result.parameter_values\n",
"function_2d.update_parameters(optimized_parameters)"
"function_2d = function_2d.with_parameters(optimized_parameters)"
]
},
{
Expand Down Expand Up @@ -1263,7 +1263,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.13"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
18 changes: 13 additions & 5 deletions docs/usage/binned-fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@
},
"outputs": [],
"source": [
"function.update_parameters(initial_parameters)\n",
"fig, ax = plt.subplots(figsize=(8, 5))\n",
"ax.set_xlabel(\"$x$\")\n",
"ax.hist(x_distribution, bins=n_bins, label=\"Data distribution\")\n",
"ax.plot(x_values, function({\"x\": x_values}), label=\"Initial fit model\", c=\"red\")\n",
"ax.plot(\n",
" x_values,\n",
" function({\"x\": x_values}, initial_parameters),\n",
" label=\"Initial fit model\",\n",
" c=\"red\",\n",
")\n",
"ax.legend()\n",
"plt.show()"
]
Expand Down Expand Up @@ -198,11 +202,15 @@
},
"outputs": [],
"source": [
"function.update_parameters(fit_result.parameter_values)\n",
"fig, ax = plt.subplots(figsize=(8, 5))\n",
"ax.set_xlabel(\"$x$\")\n",
"ax.hist(x_distribution, bins=n_bins, label=\"Data distribution\")\n",
"ax.plot(x_values, function({\"x\": x_values}), label=\"Optimized model\", c=\"red\")\n",
"ax.plot(\n",
" x_values,\n",
" function({\"x\": x_values}, fit_result.parameter_values),\n",
" label=\"Optimized model\",\n",
" c=\"red\",\n",
")\n",
"ax.legend()\n",
"plt.show()"
]
Expand Down Expand Up @@ -270,7 +278,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
7 changes: 4 additions & 3 deletions docs/usage/chi-squared.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"source": [
"original_parameters = function.parameters\n",
"initial_parameters = {\"a\": -25, \"b\": 1.5, \"c\": 2.6}\n",
"function.update_parameters(initial_parameters)"
"function = function.with_parameters(initial_parameters)"
]
},
{
Expand Down Expand Up @@ -207,7 +207,8 @@
},
"outputs": [],
"source": [
"compare_model(function, x_values, observed_y)"
"optimized_function = function.with_parameters(fit_result.parameter_values)\n",
"compare_model(optimized_function, x_values, observed_y)"
]
}
],
Expand All @@ -230,7 +231,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
8 changes: 3 additions & 5 deletions docs/usage/unbinned-fit.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@
"Y = np.linspace(*ylim, bins_y)\n",
"X, Y = np.meshgrid(X, Y)\n",
"\n",
"function.update_parameters(initial_parameters)\n",
"Z = function({\"x\": X, \"y\": Y})\n",
"Z = function({\"x\": X, \"y\": Y}, initial_parameters)\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(figsize=(8, 7), nrows=2, sharex=True, tight_layout=True)\n",
"ax1.set_title(\"Data distribution\")\n",
Expand Down Expand Up @@ -258,8 +257,7 @@
},
"outputs": [],
"source": [
"function.update_parameters(fit_result.parameter_values)\n",
"Z = function({\"x\": X, \"y\": Y})\n",
"Z = function({\"x\": X, \"y\": Y}, fit_result.parameter_values)\n",
"\n",
"fig, (ax1, ax2) = plt.subplots(figsize=(8, 7), nrows=2, sharex=True, tight_layout=True)\n",
"ax1.set_title(\"Data distribution\")\n",
Expand Down Expand Up @@ -292,7 +290,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
"version": "3.13.14"
}
},
"nbformat": 4,
Expand Down
Loading
Loading