Skip to content

Commit

Permalink
Merge pull request #42 from zfit/zfit_020
Browse files Browse the repository at this point in the history
enh: update to zfit 0.20
  • Loading branch information
jonas-eschle authored Apr 12, 2024
2 parents 4328fd4 + 73185ea commit 2ba015b
Show file tree
Hide file tree
Showing 34 changed files with 476 additions and 719 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
push:

jobs:

notebooks:
runs-on: ubuntu-latest
timeout-minutes: 150
Expand All @@ -18,7 +17,7 @@ jobs:
fail-fast: False
matrix:
use-graph: [ 0, 1 ]
python-version: [ "3.8", "3.11" ]
python-version: [ "3.9", "3.12" ]
name: Run notebooks, Python ${{ matrix.python-version }}, compiled = ${{ matrix.use-graph }}
steps:
- uses: SimenB/github-actions-cpu-cores@v2
Expand All @@ -35,8 +34,8 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install pytest-xdist nbval
pip install -U -r requirements.txt
pip install git+https://github.com/zfit/zfit
pip install -r requirements.txt
- name: Run notebooks
run: |
ZFIT_DO_JIT=${{ matrix.use-graph }}
Expand Down
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
16 changes: 8 additions & 8 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 Expand Up @@ -143,8 +143,9 @@
"source": [
"## creating an extended PDF\n",
"\n",
"An extended PDF can either be created with the `create_extended(yield_param)` method or with the simple Python\n",
"syntax of multiplying a PDF with the parameter."
"An extended PDF can be created using the `extended` argument in the initialization.\n",
" \n",
"Alternatively, an extended PDF from a non-extended PDF can be created with the `create_extended(yield_param)` method."
]
},
{
Expand All @@ -153,7 +154,8 @@
"metadata": {},
"outputs": [],
"source": [
"yield1 = zfit.Parameter(\"yield_gauss1\", 100, 0, 1000)"
"yield1 = zfit.Parameter(\"yield_gauss1\", 100, 0, 1000)\n",
"gauss3_ext = zfit.pdf.Gauss(obs=obs2, mu=mu3, sigma=sigma3, extended=yield1)"
]
},
{
Expand All @@ -170,9 +172,7 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gauss3_ext = zfit.pdf.Gauss(obs=obs2, mu=mu3, sigma=sigma3, extended=yield1)"
]
"source": []
}
],
"metadata": {
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, sigma=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
Loading

0 comments on commit 2ba015b

Please sign in to comment.