Skip to content

Commit

Permalink
fix: remove old norm_range arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-eschle committed Nov 7, 2024
1 parent 2f57092 commit 31a9794
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 57 deletions.
26 changes: 13 additions & 13 deletions _unused/API.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,26 @@
"\n",
"The main methods of the PDF are:\n",
"\n",
"- Getting the probability through the `prob` method, which **MUST** be called with a data array `x` and a normalization range `norm_range` as inputs. For example:\n",
"- Getting the probability through the `prob` method, which **MUST** be called with a data array `x` and a normalization range `norm` as inputs. For example:\n",
"\n",
" ```python\n",
" # Get the probabilities of some random generated events\n",
" probs = gauss.prob(x=np.random.random(10), norm_range=(-30., 30))\n",
" probs = gauss.prob(x=np.random.random(10), norm=(-30., 30))\n",
" ```\n",
"\n",
"- Getting the value of its integral in some given `limits` with the `integrate` method. While the `norm_range` **MUST** be given here, it **MAY** also be requested that this integral is calculated over the unnormalized PDF by setting it to `False`:\n",
"- Getting the value of its integral in some given `limits` with the `integrate` method. While the `norm` **MUST** be given here, it **MAY** also be requested that this integral is calculated over the unnormalized PDF by setting it to `False`:\n",
"\n",
" ```python\n",
" # Calculate the integral between -5 and 3 over the PDF normalized between -30 and 30\n",
" integral_norm = gauss.integrate(limits=(-5, 3), norm_range=(-30., 30))\n",
" integral_norm = gauss.integrate(limits=(-5, 3), norm=(-30., 30))\n",
" # Calculate the unnormalized integral\n",
" integral_unnorm = gauss.integrate(limits=(-5, 3), norm_range=False)\n",
" integral_unnorm = gauss.integrate(limits=(-5, 3), norm=False)\n",
" ```\n",
"\n",
"- Getting the gradient through the **gradient** method, which **MUST** get the data arra `x` and the normalization range `norm_range` as inputs (which, as always, can be set to `False` and therefore no normalization is done). Additionally, the list of parameters with respect to which the integral is performed **MAY** be given through the `params` argument:\n",
"- Getting the gradient through the **gradient** method, which **MUST** get the data arra `x` and the normalization range `norm` as inputs (which, as always, can be set to `False` and therefore no normalization is done). Additionally, the list of parameters with respect to which the integral is performed **MAY** be given through the `params` argument:\n",
"\n",
" ```python\n",
" gradient = gauss.gradient(x=np.random.random(10), norm_range=(-30, 30), params=['mu'])\n",
" gradient = gauss.gradient(x=np.random.random(10), norm=(-30, 30), params=['mu'])\n",
" ```\n",
"\n",
"- Sampling from the PDF is done through the `sample` method, which **MUST** include the number of events `n_draws` as well as the limits from which to draw (`limits`):\n",
Expand All @@ -127,22 +127,22 @@
" sample = gauss.sample(n_draws=1000, limits=(-10, 10))\n",
" ```\n",
"\n",
"Additionally, extended PDFs, which will result in anything using a `norm_range` to not return the probability but the number probability (the function will be normalized to this yield instead of 1 inside the `norm_range`), can be created through the `set_yield` method, which **MUST** get a parameter as input:\n",
"Additionally, extended PDFs, which will result in anything using a `norm` to not return the probability but the number probability (the function will be normalized to this yield instead of 1 inside the `norm`), can be created through the `set_yield` method, which **MUST** get a parameter as input:\n",
"\n",
"```python\n",
"yield1 = Parameter(\"yield1\", 100, 0, 1000)\n",
"gauss.set_yield(yield1)\n",
"# This integral yields approx 100\n",
"integral_extended = gauss.integrate(limits=(-10, 10), norm_range=(-10, 10))\n",
"integral_extended = gauss.integrate(limits=(-10, 10), norm=(-10, 10))\n",
"```\n",
"\n",
"The `is_extended` property can be then used to check whether a PDF is extended or not.\n",
"\n",
"Finally, there **MUST** be the option to *temporarily* set the norm_range of a pdf with a context manager in order to perform several operations and make code more readable.\n",
"Finally, there **MUST** be the option to *temporarily* set the norm of a pdf with a context manager in order to perform several operations and make code more readable.\n",
"\n",
"```python\n",
"with pdf.temp_norm_range((-30, 30)):\n",
" pdf.prob(data) # norm_range is now set\n",
"with pdf.temp_norm((-30, 30)):\n",
" pdf.prob(data) # norm is now set\n",
" pdf.integrate(limits=(-5, 3))\n",
"```\n"
]
Expand All @@ -153,7 +153,7 @@
"source": [
"## Loss functions\n",
"\n",
"Loss functions can then be build using `pdf.prob`, following a common interface, in which the model, the dataset and the fit range (which internally sets `norm_range` in the PDF and makes sure data only within that range are used) **MUST** be given, and where parameter constraints in form of a dictionary `{param: constraint}` **MAY** be given.\n",
"Loss functions can then be build using `pdf.prob`, following a common interface, in which the model, the dataset and the fit range (which internally sets `norm` in the PDF and makes sure data only within that range are used) **MUST** be given, and where parameter constraints in form of a dictionary `{param: constraint}` **MAY** be given.\n",
"\n",
"As an example for unbinned NLL, one would write\n",
"\n",
Expand Down
28 changes: 14 additions & 14 deletions _unused/Summary.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"```python\n",
"from zfit.minimize import Minuit\n",
"\n",
"nll = zfit.unbinned_nll(pdf, data, norm_range=(-10, 10))\n",
"nll = zfit.unbinned_nll(pdf, data, norm=(-10, 10))\n",
"minimizer = Minuit()\n",
"minimizer.minimize(nll, params)\n",
"minimizer.hesse(params)\n",
Expand Down Expand Up @@ -85,26 +85,26 @@
"\n",
"The main methods of the PDF are then\n",
"\n",
"- Getting the probability through the `probs` method, which **MUST** be called with a data array `x` and a normalization range `norm_range` as inputs. For example:\n",
"- Getting the probability through the `probs` method, which **MUST** be called with a data array `x` and a normalization range `norm` as inputs. For example:\n",
"\n",
" ```\n",
" # Get the probabilities of some random generated events\n",
" probs = gauss.prob(x=np.random.random(10), norm_range=(-30., 30))\n",
" probs = gauss.prob(x=np.random.random(10), norm=(-30., 30))\n",
" ```\n",
"- there **MUST** be the option to *temporarely* set the norm_range of a pdf with a contextmanager.**(ALBERT: mention it? ok?)**\n",
"- there **MUST** be the option to *temporarely* set the norm of a pdf with a contextmanager.**(ALBERT: mention it? ok?)**\n",
"\n",
" ```python\n",
" with pdf.temp_norm_range((1, 5)):\n",
" pdf.prob(data) # norm_range is now set\n",
" with pdf.temp_norm((1, 5)):\n",
" pdf.prob(data) # norm is now set\n",
" ```\n",
"\n",
"- Getting the value of its integral in some given `limits`. While the `norm_range` is also mandatory here, it may be requested that this integral is calculated over the unnormalized PDF by setting it to `False`:\n",
"- Getting the value of its integral in some given `limits`. While the `norm` is also mandatory here, it may be requested that this integral is calculated over the unnormalized PDF by setting it to `False`:\n",
"\n",
" ```python\n",
" # Calculate the integral between -5 and 3 over the PDF normalized between -30 and 30\n",
" integral_norm = gauss.integrate(limits=(-5, 3), norm_range=(-30., 30))\n",
" integral_norm = gauss.integrate(limits=(-5, 3), norm=(-30., 30))\n",
" # Calculate the unnormalized integral\n",
" integral_unnorm = gauss.integrate(limits=(-5, 3), norm_range=False)\n",
" integral_unnorm = gauss.integrate(limits=(-5, 3), norm=False)\n",
" ```\n",
"\n",
"- Sampling from the PDF is done through the `sample` method, which **MUST** include the number of events `n_draws` as well as the limits from which to draw (`limits`):\n",
Expand All @@ -114,13 +114,13 @@
" sample = gauss.sample(n_draws=1000, limits=(-10, 10))\n",
" ```\n",
"\n",
"Additionally, extended PDFs, which will result in anything using a `norm_range` to not return the probability but the number probability (the function will be normalized to this yield instead of 1 inside the `norm_range`), can be created through the `set_yield` method, which **MUST** get a parameter as input:\n",
"Additionally, extended PDFs, which will result in anything using a `norm` to not return the probability but the number probability (the function will be normalized to this yield instead of 1 inside the `norm`), can be created through the `set_yield` method, which **MUST** get a parameter as input:\n",
"\n",
"```python\n",
"yield1 = Parameter(\"yield1\", 100, 0, 1000)\n",
"gauss.set_yield(yield1)\n",
"# This integral yields approx 100\n",
"integral_extended = gauss.integrate(limits=(-10, 10), norm_range=(-10, 10))\n",
"integral_extended = gauss.integrate(limits=(-10, 10), norm=(-10, 10))\n",
"```\n",
"\n",
"The `is_extended` property can be then used to check whether a PDF is extended or not.\n",
Expand All @@ -131,7 +131,7 @@
"```python\n",
"my_loss = zfit.unbinned_nll(gauss,\n",
" data,\n",
" norm_range=(-10, 10),\n",
" norm=(-10, 10),\n",
" constraints={})\n",
"```"
]
Expand Down Expand Up @@ -254,8 +254,8 @@
"# we use the core one to highlight the use of tensorflow graphs\n",
"\n",
"\n",
"def api_unbinned_nll(pdf, data, norm_range):\n",
" return zfit.core.loss.unbinned_nll(pdf.prob(data, norm_range=norm_range))\n",
"def api_unbinned_nll(pdf, data, norm):\n",
" return zfit.core.loss.unbinned_nll(pdf.prob(data, norm=norm))\n",
"\n",
"\n",
"mu1 = zfit.Parameter(\"mu\", 5.0, 0., 10)\n",
Expand Down
4 changes: 2 additions & 2 deletions _unused/kstmumu_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def invariant_mass(four_momenta):
lambda_rare.set_value(-0.003)

# here we temporarily set the normalization range to the right side only
with comb_bkg_rare.set_norm_range(obs_bkg):
with comb_bkg_rare.set_norm(obs_bkg):
right_tale_loss = zfit.loss.UnbinnedNLL(comb_bkg_rare, right_tale_data_rare)
minimizer = zfit.minimize.Minuit(verbosity=7)
result_right_tale = minimizer.minimize(right_tale_loss)
Expand Down Expand Up @@ -187,7 +187,7 @@ def invariant_mass(four_momenta):
# create the model to fit

# set the normalization range of the exponential to the whole range
comb_bkg_rare.set_norm_range(obs)
comb_bkg_rare.set_norm(obs)

# parameters for the model
mu = zfit.Parameter("mu", 5270, 5200, 5350)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
"This can be useful for debugging, as now every Tensor has a value and every operation is executed immediately.\n",
"\n",
"Another problem is that building a graph becomes only efficient if we execute it multiple times, not just a single time. But for plotting a pdf for example, just a single call to `pdf` is needed.\n",
"Furthermore, since different objects (e.g. different datasets, `norm_range` etc.) will create a new graph, things can become very slow, caching many graphs that often are not needed anymore.\n",
"Furthermore, since different objects (e.g. different datasets, `norm` etc.) will create a new graph, things can become very slow, caching many graphs that often are not needed anymore.\n",
"\n",
"The `z.function` decorator is in fact more powerful then the pure `tf.function`: it allow to tell what kind of function is wrapped and this on the other hand allows zfit to be \"smart\" about which function to trace and which not. By default, any method of models (`pdf`, `integrate`,...) are executed eagerly, without graphs. On the other hand, if a loss is built, this builds a graph of everything. Mainly, this behavior is wanted.\n",
"\n",
Expand Down Expand Up @@ -357,7 +357,7 @@
"source": [
"## Graph caching and slowness\n",
"\n",
"Repeated calls to graph building functions are sometimes necessary, e.g. when scanning over a range and changing the `norm_range`, which renders the validity of the graph invalid. We can have a look at an example."
"Repeated calls to graph building functions are sometimes necessary, e.g. when scanning over a range and changing the `norm`, which renders the validity of the graph invalid. We can have a look at an example."
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"We could improve our PDF by registering an integral. This requires a few steps:\n",
" - define our integral as a function in python\n",
" - define in which space our integral is valid, e.g. whether it is an integral over all axis or only partial and whether any limit is valid or only special (e.g. from -inf to inf)\n",
" - register the integral and say if it supports additional things (e.g. norm_range)\n",
" - register the integral and say if it supports additional things (e.g. norm)\n",
"\n",
"Let's start defining the function. This takes, for an integral over all axes, three parameters:\n",
" - limits: the actual limits the integral is over\n",
Expand Down
8 changes: 4 additions & 4 deletions _website/tutorials/guides/custom_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@
" return limit + 0.5 * b * limit ** 2 + 1 / 3 * c * limit ** 3\n",
"\n",
"\n",
"def integral_func(limits, norm_range, params, model):\n",
" del norm_range, model # not needed\n",
"def integral_func(limits, norm, params, model):\n",
" del norm, model # not needed\n",
"\n",
" b = params['b']\n",
" c = params['c']\n",
Expand Down Expand Up @@ -466,7 +466,7 @@
"\n",
"**To remember**: in order to have full control over a public function such as `integrate`, `pdf`, `sample` or `normalization`, the private method, e.g. `_integrate` can be overriden and is _guaranteed_ to be called before other possibilities.\n",
"\n",
"In the case above, `pdf` called first `_pdf` (which is not implemented), so it calls `_unnormalized_pdf` and divides this by the `normalization`. The latter also does not have an explicit implementation (`_implementation`), so it uses the fallback and calls `integrate` over the `norm_range`. Since `_integrate` is not provided, the fallback tries to perform an analytic integral, which is not available. Therefore, it integrates the `_unnormalized_prob` numerically. In all of this calls, we can hook in by overriding the mentioned, specified methods.\n",
"In the case above, `pdf` called first `_pdf` (which is not implemented), so it calls `_unnormalized_pdf` and divides this by the `normalization`. The latter also does not have an explicit implementation (`_implementation`), so it uses the fallback and calls `integrate` over the `norm`. Since `_integrate` is not provided, the fallback tries to perform an analytic integral, which is not available. Therefore, it integrates the `_unnormalized_prob` numerically. In all of this calls, we can hook in by overriding the mentioned, specified methods.\n",
"\n",
"What we did not mention: `ZPDF` is just a wrapper around the actual `BasePDF` that should be preferred in general; it simply provides a convenient `__init__`. For the next example, we will implement a multidimensional PDF and use the custom `__init__`"
]
Expand All @@ -481,7 +481,7 @@
"source": [
"### Overriding `pdf`\n",
"\n",
"Before, we used `_unnormalized_pdf`, which is the common use-case. Even if we want to add an analytic integral, we can register it. Or do more fancy stuff like overriding the `_normalization`. We can however also get the full control of what our model output by directly overriding `_pdf`. The signature does not contain only `x` but additionally `norm_range`. This can have no limits (`norm_range.has_limits` is False), in which case the \"unnormalized pdf\" is requested. Otherwise, `norm_range` can have different limits and we have to take care of the proper normalization.\n",
"Before, we used `_unnormalized_pdf`, which is the common use-case. Even if we want to add an analytic integral, we can register it. Or do more fancy stuff like overriding the `_normalization`. We can however also get the full control of what our model output by directly overriding `_pdf`. The signature does not contain only `x` but additionally `norm`. This can have no limits (`norm.has_limits` is False), in which case the \"unnormalized pdf\" is requested. Otherwise, `norm` can have different limits and we have to take care of the proper normalization.\n",
"\n",
"This is usually not needed and inside zfit, all PDFs are implemented using the `_unnormalized_pdf`.\n",
"\n",
Expand Down
4 changes: 2 additions & 2 deletions _website/tutorials/introduction/Introduction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@
"\n",
"PDFs provide a few useful methods. The main features of a zfit PDF are:\n",
"\n",
"- `pdf`: the normalized value of the PDF. It takes an argument `norm_range` that can be set to `False`, in which case we retrieve the unnormalized value\n",
"- `integrate`: given a certain range, the PDF is integrated. As `pdf`, it takes a `norm_range` argument that integrates over the unnormalized `pdf` if set to `False`\n",
"- `pdf`: the normalized value of the PDF. It takes an argument `norm` that can be set to `False`, in which case we retrieve the unnormalized value\n",
"- `integrate`: given a certain range, the PDF is integrated. As `pdf`, it takes a `norm` argument that integrates over the unnormalized `pdf` if set to `False`\n",
"- `sample`: samples from the pdf and returns a `Data` object"
]
},
Expand Down
Loading

0 comments on commit 31a9794

Please sign in to comment.