Skip to content

Commit

Permalink
enh: update to zfit 0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-eschle committed Apr 10, 2024
1 parent 51d3955 commit f8f0cdc
Show file tree
Hide file tree
Showing 32 changed files with 408 additions and 381 deletions.
11 changes: 11 additions & 0 deletions TensorFlow/HPC_with_TensorFlow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@
" x2 = np.mean(logx)\n",
" return x + x1 + x2\n",
"\n",
"\n",
"calc_np_numba = calc_np # numba.jit(nopython=True, parallel=True)(calc_np)"
]
},
Expand Down Expand Up @@ -1570,6 +1571,7 @@
"\n",
" return x + x1 + x2\n",
"\n",
"\n",
"calc_tf_func = tf.function(calc_tf, autograph=False)"
]
},
Expand Down Expand Up @@ -1698,9 +1700,12 @@
" sum_init += x\n",
" return sum_init\n",
"\n",
"\n",
"calc_tf_func2 = tf.function(calc_tf2, autograph=False)\n",
"\n",
"# @numba.njit(parallel=True) # njit is equal to jit(nopython=True), meaning \"compile everything or raise error\"\n",
"\n",
"\n",
"def calc_numba2(x, n):\n",
" sum_init = np.zeros_like(x)\n",
" for i in range(1, n + 1):\n",
Expand Down Expand Up @@ -1882,9 +1887,11 @@
"def true_fn():\n",
" return 1.\n",
"\n",
"\n",
"def false_fn():\n",
" return 0.\n",
"\n",
"\n",
"value = tf.cond(tf.greater(111., 42.), true_fn=true_fn, false_fn=false_fn)"
]
},
Expand Down Expand Up @@ -1927,9 +1934,11 @@
"def cond(x, y):\n",
" return x > y\n",
"\n",
"\n",
"def body(x, y):\n",
" return x / 2, y + 1\n",
"\n",
"\n",
"x, y = tf.while_loop(cond=cond,\n",
" body=body,\n",
" loop_vars=[100., 1.])"
Expand Down Expand Up @@ -2013,6 +2022,7 @@
"def do_map(func, tensor):\n",
" return tf.map_fn(func, tensor)\n",
"\n",
"\n",
"do_map(tf.math.sin, rnd1_big)"
]
},
Expand All @@ -2030,6 +2040,7 @@
"def do_map_vec(func, tensor):\n",
" return tf.vectorized_map(func, tensor)\n",
"\n",
"\n",
"do_map_vec(tf.math.sin, rnd1_big)"
]
},
Expand Down
3 changes: 3 additions & 0 deletions _unused/Summary.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@
"source": [
"# This shortcut function will be available in zfit, but here\n",
"# 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",
"\n",
"\n",
"mu1 = zfit.Parameter(\"mu\", 5.0, 0., 10)\n",
"sigma1 = zfit.Parameter(\"sigma\", 1, 0.1, 5.)\n",
"gauss1 = zfit.pdf.Gauss(mu=mu1, sigma=sigma1)\n",
Expand Down
6 changes: 3 additions & 3 deletions _unused/kstmumu_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ def plot_pdf_data(data, model, title, n_bins=40):
histtype="step",
)
# plot the pdfs
y = model.pdf(x).numpy()
y_sig = (model.pdfs[0].pdf(x) * model.fracs[0]).numpy() # notice the frac!
y_bkg = (model.pdfs[1].pdf(x) * model.fracs[1]).numpy() # notice the frac!
y = model.pdf(x)
y_sig = model.pdfs[0].pdf(x) * model.fracs[0] # notice the frac!
y_bkg = model.pdfs[1].pdf(x) * model.fracs[1] # notice the frac!

plt.plot(x, y * plot_scaling, label="Sum - Model", linewidth=linewidth * 2)
plt.plot(
Expand Down
11 changes: 11 additions & 0 deletions _website/tutorials/TensorFlow/HPC_with_TensorFlow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,7 @@
" x2 = np.mean(logx)\n",
" return x + x1 + x2\n",
"\n",
"\n",
"calc_np_numba = calc_np # numba.jit(nopython=True, parallel=True)(calc_np)"
]
},
Expand Down Expand Up @@ -1570,6 +1571,7 @@
"\n",
" return x + x1 + x2\n",
"\n",
"\n",
"calc_tf_func = tf.function(calc_tf, autograph=False)"
]
},
Expand Down Expand Up @@ -1698,9 +1700,12 @@
" sum_init += x\n",
" return sum_init\n",
"\n",
"\n",
"calc_tf_func2 = tf.function(calc_tf2, autograph=False)\n",
"\n",
"# @numba.njit(parallel=True) # njit is equal to jit(nopython=True), meaning \"compile everything or raise error\"\n",
"\n",
"\n",
"def calc_numba2(x, n):\n",
" sum_init = np.zeros_like(x)\n",
" for i in range(1, n + 1):\n",
Expand Down Expand Up @@ -1882,9 +1887,11 @@
"def true_fn():\n",
" return 1.\n",
"\n",
"\n",
"def false_fn():\n",
" return 0.\n",
"\n",
"\n",
"value = tf.cond(tf.greater(111., 42.), true_fn=true_fn, false_fn=false_fn)"
]
},
Expand Down Expand Up @@ -1927,9 +1934,11 @@
"def cond(x, y):\n",
" return x > y\n",
"\n",
"\n",
"def body(x, y):\n",
" return x / 2, y + 1\n",
"\n",
"\n",
"x, y = tf.while_loop(cond=cond,\n",
" body=body,\n",
" loop_vars=[100., 1.])"
Expand Down Expand Up @@ -2013,6 +2022,7 @@
"def do_map(func, tensor):\n",
" return tf.map_fn(func, tensor)\n",
"\n",
"\n",
"do_map(tf.math.sin, rnd1_big)"
]
},
Expand All @@ -2030,6 +2040,7 @@
"def do_map_vec(func, tensor):\n",
" return tf.vectorized_map(func, tensor)\n",
"\n",
"\n",
"do_map_vec(tf.math.sin, rnd1_big)"
]
},
Expand Down
10 changes: 3 additions & 7 deletions _website/tutorials/components/05 - Exploring the FitResult.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@
"metadata": {},
"outputs": [],
"source": [
"obs = zfit.Space('x', limits=(0, 10))\n",
"obs = zfit.Space('x', 0, 10)\n",
"mu = zfit.Parameter('mu', 5, 0, 10)\n",
"sigma = zfit.Parameter('sigma', 1, 0, 10)\n",
"nsig = zfit.Parameter('nsig', 1000, 0, 10000)\n",
"gauss_nonext = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma,\n",
" # extended=nsig # requires zfit>=0.13\n",
" )\n",
"gauss = gauss_nonext.create_extended(nsig)\n",
"\n",
"gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma,extended=nsig)\n",
"data = gauss.sample()\n",
"print(f\"The sampled data (poisson fluctuated) has {data.nevents} events.\")"
]
Expand Down Expand Up @@ -319,7 +315,7 @@
"outputs": [],
"source": [
"weighted_data = zfit.Data.from_tensor(obs=obs, tensor=data.value(), weights=znp.random.uniform(0.1, 5, size=(data.nevents,)))\n",
"weighted_nll = zfit.loss.UnbinnedNLL(model=gauss_nonext, data=weighted_data)\n",
"weighted_nll = zfit.loss.UnbinnedNLL(model=gauss, data=weighted_data)\n",
"weighted_result = minimizer.minimize(weighted_nll)"
]
},
Expand Down
4 changes: 2 additions & 2 deletions _website/tutorials/components/20 - Composite Models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"source": [
"frac = zfit.Parameter(\"frac_gauss\", 0.5, 0, 1)\n",
"\n",
"obs1 = zfit.Space('obs1', limits=(-5, 5))\n",
"obs1 = zfit.Space('obs1',-5, 5)\n",
"\n",
"mu1 = zfit.Parameter(\"mu1\", 1.)\n",
"sigma1 = zfit.Parameter(\"sigma1\", 1.)\n",
Expand Down Expand Up @@ -96,7 +96,7 @@
"metadata": {},
"outputs": [],
"source": [
"obs2 = zfit.Space('obs2', limits=(-3, 7))\n",
"obs2 = zfit.Space('obs2', -3, 7)\n",
"mu3 = zfit.Parameter(\"mu3\", 1.)\n",
"sigma3 = zfit.Parameter(\"sigma3\", 1.)\n",
"gauss3 = zfit.pdf.Gauss(obs=obs2, mu=mu3, sigma=sigma3) # different obs than above."
Expand Down
6 changes: 3 additions & 3 deletions _website/tutorials/components/30 - Binned models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"\n",
"normal_np = np.random.normal(loc=2., scale=3., size=10000)\n",
"\n",
"obs = zfit.Space(\"x\", limits=(-10, 10))\n",
"obs = zfit.Space(\"x\", -10, 10)\n",
"\n",
"mu = zfit.Parameter(\"mu\", 1., -4, 6)\n",
"sigma = zfit.Parameter(\"sigma\", 1., 0.1, 10)\n",
Expand Down Expand Up @@ -81,7 +81,7 @@
"obs_bin = zfit.Space(\"x\", binning=binning)\n",
"\n",
"data = data_nobin.to_binned(obs_bin)\n",
"model = zfit.pdf.BinnedFromUnbinnedPDF(model_nobin, obs_bin)\n",
"model = model_nobin.to_binned(obs_bin)\n",
"loss = zfit.loss.BinnedNLL(model, data)"
]
},
Expand Down Expand Up @@ -160,7 +160,7 @@
"\n",
"plt.figure()\n",
"mplhep.histplot(model_hist, density=1, label=\"model\")\n",
"mplhep.histplot(data.to_hist(), density=1, label=\"data\")\n",
"mplhep.histplot(data, density=1, label=\"data\")\n",
"plt.legend()\n",
"plt.title(\"After fit\")"
]
Expand Down
11 changes: 5 additions & 6 deletions _website/tutorials/components/33 - Binned fits.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"source": [
"normal_np = np.random.normal(loc=2., scale=1.3, size=10000)\n",
"\n",
"obs = zfit.Space(\"x\", limits=(-10, 10))\n",
"obs = zfit.Space(\"x\", -10, 10)\n",
"\n",
"mu = zfit.Parameter(\"mu\", 1., -4, 6)\n",
"sigma = zfit.Parameter(\"sigma\", 1., 0.1, 10)\n",
Expand Down Expand Up @@ -487,8 +487,7 @@
"metadata": {},
"outputs": [],
"source": [
"bkg_hist = zfit.Data.from_numpy(obs=obs, array=np.random.exponential(scale=20, size=100_000) - 10).to_binned(\n",
" obs_binned)\n",
"bkg_hist = zfit.Data(np.random.exponential(scale=20, size=100_000) - 10, obs=obs_binned)\n",
"bkg_hist_m1 = zfit.Data.from_numpy(obs=obs,\n",
" array=np.random.exponential(scale=35, size=100_000) - 10).to_binned(\n",
" obs_binned)\n",
Expand Down Expand Up @@ -571,8 +570,8 @@
"outputs": [],
"source": [
"modifier_constraints = zfit.constraint.GaussianConstraint(params=list(modifiers.values()), observation=np.ones(len(modifiers)),\n",
" uncertainty=np.ones(len(modifiers)))\n",
"# alpha_constraint = zfit.constraint.GaussianConstraint(alpha, 0, 1)"
" uncertainty=0.1 * np.ones(len(modifiers)))\n",
"alpha_constraint = zfit.constraint.GaussianConstraint(alpha, 0, 1)"
]
},
{
Expand All @@ -581,7 +580,7 @@
"metadata": {},
"outputs": [],
"source": [
"loss_binned = zfit.loss.ExtendedBinnedNLL(model, data, constraints=modifier_constraints)"
"loss_binned = zfit.loss.ExtendedBinnedNLL(model, data, constraints=[modifier_constraints, alpha_constraint])"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"import numpy as np\n",
"import tensorflow as tf\n",
"import zfit\n",
"import zfit.z.numpy as znp # this is numpy-like\n",
"from zfit import z # this is basically tf, just wrapped"
]
},
Expand Down Expand Up @@ -166,7 +167,7 @@
"metadata": {},
"outputs": [],
"source": [
"graph_func(z.constant(5))"
"graph_func(znp.array(5))"
]
},
{
Expand All @@ -175,7 +176,7 @@
"metadata": {},
"outputs": [],
"source": [
"graph_func(z.constant(7))"
"graph_func(znp.array(7))"
]
},
{
Expand Down Expand Up @@ -210,7 +211,7 @@
"outputs": [],
"source": [
"try:\n",
" graph_func_fail(z.constant(5.))\n",
" graph_func_fail(znp.array(5.))\n",
"except NotImplementedError as error:\n",
" print(f\"Error was raised, last line: {error}\")"
]
Expand Down Expand Up @@ -262,7 +263,7 @@
"metadata": {},
"outputs": [],
"source": [
"graph_func_fail(z.constant(5))"
"graph_func_fail(znp.array(5))"
]
},
{
Expand Down Expand Up @@ -299,6 +300,7 @@
"def numpy_func(x, a):\n",
" return np.square(x) * a\n",
"\n",
"\n",
"@z.function\n",
"def wrapped_numpy_func(x_tensor, a_tensor):\n",
" result = z.py_function(func=numpy_func, inp=[x_tensor, a_tensor], Tout=zfit.ztypes.float) # or tf.float64\n",
Expand Down
Loading

0 comments on commit f8f0cdc

Please sign in to comment.