From 1281a4d0d5d0f686d22fbd101b32b7690f709dc7 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Sun, 26 Aug 2018 02:23:12 +0200 Subject: [PATCH 1/3] Add Hello World notebook --- docs/examples.rst | 1 + docs/examples/notebooks/hello-world.ipynb | 72 +++++++++++++++++++++++ tests/test_notebooks.py | 3 + 3 files changed, 76 insertions(+) create mode 100644 docs/examples/notebooks/hello-world.ipynb diff --git a/docs/examples.rst b/docs/examples.rst index 5bb5753762..8fe3feb077 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -11,6 +11,7 @@ Notebooks: .. toctree:: :maxdepth: 2 + examples/notebooks/hello-world.ipynb examples/notebooks/binderexample/StatisticalAnalysis.ipynb examples/notebooks/Recast.ipynb examples/notebooks/ShapeFactor.ipynb diff --git a/docs/examples/notebooks/hello-world.ipynb b/docs/examples/notebooks/hello-world.ipynb new file mode 100644 index 0000000000..2e35a21c3a --- /dev/null +++ b/docs/examples/notebooks/hello-world.ipynb @@ -0,0 +1,72 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Hello World, `pyhf` style" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pyhf\n", + "import pyhf.simplemodels\n", + "import pyhf.utils" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Observed: [0.05290116] Expected: [0.06445521]\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/mcf/Code/GitHub/pyhf/pyhf/tensor/numpy_backend.py:82: RuntimeWarning: divide by zero encountered in log\n", + " return np.log(tensor_in)\n", + "/home/mcf/Code/GitHub/pyhf/pyhf/tensor/numpy_backend.py:126: RuntimeWarning: divide by zero encountered in log\n", + " return np.exp(n*np.log(lam)-lam-gammaln(n+1.))\n" + ] + } + ], + "source": [ + "pdf = pyhf.simplemodels.hepdata_like(signal_data=[12.,11.], bkg_data=[50.,52.], bkg_uncerts=[3.,7.])\n", + "*_, CLs_obs,CLs_exp = pyhf.utils.runOnePoint(1.0, [51, 48] + pdf.config.auxdata, pdf)\n", + "print('Observed: {} Expected: {}'.format(CLs_obs, CLs_exp[2]))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/tests/test_notebooks.py b/tests/test_notebooks.py index 20bab35405..9ceb41b821 100644 --- a/tests/test_notebooks.py +++ b/tests/test_notebooks.py @@ -9,6 +9,9 @@ def test_notebooks(tmpdir): 'kernel_name': 'python{}'.format(sys.version_info.major) } + pm.execute_notebook( + 'docs/examples/notebooks/hello-world.ipynb', **common_kwargs) + pm.execute_notebook( 'docs/examples/notebooks/ShapeFactor.ipynb', **common_kwargs) pm.execute_notebook('docs/examples/notebooks/multichannel-coupled-histo.ipynb', From 1775a61c7c9a0f36ea5d224bedba95567ead0bc6 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Sun, 26 Aug 2018 02:38:13 +0200 Subject: [PATCH 2/3] Unpack runOnePoint output manually to support Python2 As Python2 doesn't have extended unpacking (c.f. PEP 3132) the unpacking in the example needs to be changed to pass the CI --- docs/examples/notebooks/hello-world.ipynb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/examples/notebooks/hello-world.ipynb b/docs/examples/notebooks/hello-world.ipynb index 2e35a21c3a..5b36e6366a 100644 --- a/docs/examples/notebooks/hello-world.ipynb +++ b/docs/examples/notebooks/hello-world.ipynb @@ -43,7 +43,8 @@ ], "source": [ "pdf = pyhf.simplemodels.hepdata_like(signal_data=[12.,11.], bkg_data=[50.,52.], bkg_uncerts=[3.,7.])\n", - "*_, CLs_obs,CLs_exp = pyhf.utils.runOnePoint(1.0, [51, 48] + pdf.config.auxdata, pdf)\n", + "# *_, CLs_obs,CLs_exp = pyhf.utils.runOnePoint(1.0, [51, 48] + pdf.config.auxdata, pdf) # Python3\n", + "_, _, _, _, CLs_obs,CLs_exp = pyhf.utils.runOnePoint(1.0, [51, 48] + pdf.config.auxdata, pdf)\n", "print('Observed: {} Expected: {}'.format(CLs_obs, CLs_exp[2]))" ] } From 09c04da530b47ffd7ffaac389c04cd6ae37b461c Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Sun, 26 Aug 2018 03:12:14 +0200 Subject: [PATCH 3/3] Add testing of Hello World example in README This is done by explicitly calling doctest on the README in the docs section of the CI. This is done instead of adding the README to --doctest-glob in pytest.ini so as to keep the example in the README displaying Python3 code. --- .travis.yml | 1 + README.md | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 93f2dc89f5..8a12bf5b02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,6 +43,7 @@ jobs: - sudo apt-get -qq install pandoc - pip install -U -q -e .[develop,tensorflow,mxnet,torch] script: + - python -m doctest README.md - cd docs && make html && cd - - touch docs/_build/html/.nojekyll deploy: diff --git a/README.md b/README.md index 82b0f3268e..2926dc5ae9 100644 --- a/README.md +++ b/README.md @@ -11,17 +11,19 @@ The HistFactory p.d.f. template [[CERN-OPEN-2012-016](https://cds.cern.ch/record/1456844)] is per-se independent of its implementation in ROOT and sometimes, it's useful to be able to run statistical analysis outside of ROOT, RooFit, RooStats framework. -This repo is a pure-python implementation of that statistical model for multi-bin histogram-based analysis and its interval estimation is based on the asymptotic formulas of "Asymptotic formulae for likelihood-based tests of new physics" [[arxiv:1007.1727](https://arxiv.org/abs/1007.1727)]. The aim is also to support modern computational graph libraries such as PyTorch and Tensorflow in order to make use of features such as autodifferentiation and GPU acceleration. +This repo is a pure-python implementation of that statistical model for multi-bin histogram-based analysis and its interval estimation is based on the asymptotic formulas of "Asymptotic formulae for likelihood-based tests of new physics" [[arxiv:1007.1727](https://arxiv.org/abs/1007.1727)]. The aim is also to support modern computational graph libraries such as PyTorch and TensorFlow in order to make use of features such as autodifferentiation and GPU acceleration. ## Hello World ```python >>> import pyhf >>> import pyhf.simplemodels ->>> p = pyhf.simplemodels.hepdata_like(signal_data=[12.,11.], bkg_data=[50.,52.], bkg_uncerts=[3.,7.]) ->>> *_, CLs_obs,CLs_exp = pyhf.runOnePoint(1.0, [51, 48] + p.config.auxdata, p) ->>> print('obs: {} exp: {}'.format(CLs_obs, CLs_exp[2])) -obs: [0.05290116] exp: [0.06445521] +>>> import pyhf.utils +>>> pdf = pyhf.simplemodels.hepdata_like(signal_data=[12.,11.], bkg_data=[50.,52.], bkg_uncerts=[3.,7.]) +>>> *_, CLs_obs,CLs_exp = pyhf.utils.runOnePoint(1.0, [51, 48] + pdf.config.auxdata, pdf) +>>> print('Observed: {} Expected: {}'.format(CLs_obs, CLs_exp[2])) +Observed: [0.05290116] Expected: [0.06445521] + ``` ## What does it support