Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
phinate committed Jun 17, 2021
1 parent a23688a commit 77748e3
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 96 deletions.
5 changes: 3 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[flake8]
ignore = E203, E231, E501, E722, W503, B950
select = C,E,F,W,T,B,B9,I
select = C,E,F,W,T,B9,I
per-file-ignores =
tests/*: T
__init__.py: D104,
setup.py: D100
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ jobs:
run: python -m pytest -ra --cov-report=xml

- name: Upload coverage to codecov
if: ${{ matrix.python-version == '3.7' && matrix.runs-on == 'ubuntu-latest'}}$
uses: codecov/codecov-action@v1
with:
#token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
name: codecov-umbrella
fail_ci_if_error: true
if: ${{ matrix.python-version == '3.7' && matrix.runs-on == 'ubuntu-latest'}}
uses: codecov/codecov-action@v1
with:
#token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
name: codecov-umbrella
fail_ci_if_error: true

dist:
name: Distribution build
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ dist: clean
python setup.py sdist bdist_wheel

clean:
rm -rf dist
rm -rf dist
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
[sk-badge]: https://scikit-hep.org/assets/images/Scikit--HEP-Project-blue.svg


[![DOI](https://zenodo.org/badge/235776682.svg)](https://zenodo.org/badge/latestdoi/235776682)
[![DOI](https://zenodo.org/badge/235776682.svg)](https://zenodo.org/badge/latestdoi/235776682)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/gradhep/neos/main?filepath=demo_kde_pyhf.ipynb)

<img src="nbs/assets/neos_logo.png" alt="neos logo" width="250">
Expand Down Expand Up @@ -102,13 +102,13 @@ init_random_params, predict = stax.serial(
)
```

Now, let's compose a workflow that can make use of this network in a typical high-energy physics statistical analysis.
Now, let's compose a workflow that can make use of this network in a typical high-energy physics statistical analysis.

Our workflow is as follows:
- From a set of normal distributions with different means, we'll generate four blobs of `(x,y)` points, corresponding to a signal process, a nominal background process, and two variations of the background from varying the background distribution's mean up and down.
- We'll then feed these points into the previously defined neural network for each blob, and construct a histogram of the output using kernel density estimation. The difference between the two background variations is used as a systematic uncertainty on the nominal background.
- We can then leverage the magic of `pyhf` to construct an [event-counting statistical model](https://scikit-hep.org/pyhf/intro.html#histfactory) from the histogram yields.
- Finally, we calculate the p-value of a test between the nominal signal and background-only hypotheses. This uses a [profile likelihood-based test statistic](https://arxiv.org/abs/1007.1727).
- Finally, we calculate the p-value of a test between the nominal signal and background-only hypotheses. This uses a [profile likelihood-based test statistic](https://arxiv.org/abs/1007.1727).

In code, `neos` can specify this workflow through function composition:

Expand Down
3 changes: 2 additions & 1 deletion nbs/02_makers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@
"kernelspec": {
"display_name": "Python 3.9.1 64-bit ('3.9.1': pyenv)",
"name": "python3"
}
},
"language_info": {}
},
"nbformat": 4,
"nbformat_minor": 4
Expand Down
23 changes: 15 additions & 8 deletions nbs/demo_kde_pyhf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -105,14 +105,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": "(DeviceArray(0.05989214, dtype=float64),\n [(DeviceArray([[-2.6561793e-05, -1.3431838e-04, 3.7267394e-04, ...,\n -9.5937648e-05, -3.1907926e-04, -4.7556667e-05],\n [ 3.2410408e-05, 1.6153457e-04, -2.3639399e-04, ...,\n -7.6833248e-06, 3.2346524e-04, 2.8453616e-05]], dtype=float32),\n DeviceArray([-1.0775060e-06, 1.6448357e-05, 2.5383127e-04, ...,\n 9.4325784e-05, -2.0307447e-04, -1.4521212e-05], dtype=float32)),\n (),\n (DeviceArray([[ 2.0301204e-07, -2.2220850e-06, 6.1780779e-06, ...,\n -1.0296096e-07, 6.6260043e-07, 4.6643160e-07],\n [ 4.1513945e-07, 9.9888112e-08, 1.8419865e-05, ...,\n 4.3947058e-08, 6.2674635e-06, -1.1988274e-06],\n [-2.0467695e-08, -1.2231366e-05, 5.6882186e-08, ...,\n -6.0378164e-07, -1.1139725e-05, 3.0422871e-06],\n ...,\n [ 4.0854286e-07, -6.6176659e-07, 1.1947506e-05, ...,\n -2.9116174e-08, 3.6848719e-06, 7.2041793e-08],\n [-5.8339246e-08, -1.7854960e-05, 5.4534485e-07, ...,\n -9.0966802e-07, -1.6797023e-05, 4.3758305e-06],\n [ 4.5675144e-08, -3.7403470e-06, 5.2949540e-06, ...,\n -2.3437008e-07, -4.0581904e-06, 8.9615037e-07]], dtype=float32),\n DeviceArray([ 1.49559655e-05, -8.68027928e-05, 5.29851706e-04, ...,\n -3.46148317e-06, 5.94751364e-05, -9.33103820e-06], dtype=float32)),\n (),\n (DeviceArray([[ 3.7021862e-05],\n [-2.0765483e-04],\n [ 3.0189482e-04],\n ...,\n [-4.2103562e-05],\n [ 2.0076217e-04],\n [ 6.4033600e-05]], dtype=float32),\n DeviceArray([-5.677808e-06], dtype=float32)),\n ()])"
},
"execution_count": null,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -133,7 +133,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -173,7 +173,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -257,7 +257,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -329,9 +329,16 @@
}
],
"metadata": {
"interpreter": {
"hash": "653c4ff722aa20b955619ff1fd61a2acdddc645229f21af3c6e92ae1ff77e485"
},
"kernelspec": {
"display_name": "Python 3.8.3 ('venv': venv)",
"name": "python3"
},
"language_info": {
"name": "python",
"version": ""
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ warn_unused_configs = true

disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_untyped_calls = false
disallow_untyped_defs = false
disallow_incomplete_defs = true
check_untyped_defs = true
disallow_untyped_decorators = true
Expand Down
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ author = Nathan Simpson
author_email = [email protected]
maintainer = Nathan Simpson
maintainer_email = [email protected]
license = BSD-3-Clause
license = Apache-2.0
license_file = LICENSE
platforms =
Any
classifiers =
Development Status :: 1 - Planning
Intended Audience :: Developers
Intended Audience :: Science/Research
License :: OSI Approved :: Apache Software License
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Topic :: Scientific/Engineering
project_urls =
Documentation = https://neos.readthedocs.io/
Expand Down
2 changes: 1 addition & 1 deletion src/neos/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/00_data.ipynb (unless otherwise specified).

__all__ = ['generate_blobs']
__all__ = ["generate_blobs"]

# Cell
import jax
Expand Down
4 changes: 2 additions & 2 deletions src/neos/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__all__ = ('neos_pyhf_example')
__all__ = ["neos_pyhf_example"]

from .nn_pyhf_cls import neos_pyhf_example
from .nn_pyhf_cls import neos_pyhf_example
36 changes: 18 additions & 18 deletions src/neos/examples/nn_pyhf_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# To add a new markdown cell, type '# %% [markdown]'
# %% [markdown]
# # KDE demo, with histosys!
#
#
# > It works :)
# %% [markdown]
# ![](assets/kde_pyhf_animation.gif)
Expand All @@ -14,19 +14,18 @@
import jax.experimental.optimizers as optimizers
import jax.experimental.stax as stax
import jax.random
from jax.random import PRNGKey
import numpy as np

import pyhf
pyhf.set_backend('jax')
pyhf.default_backend = pyhf.tensor.jax_backend(precision='64b')
from jax.random import PRNGKey
from relaxed import infer

from .. import data, makers
from relaxed import infer

pyhf.set_backend("jax")
pyhf.default_backend = pyhf.tensor.jax_backend(precision="64b")

def neos_pyhf_example(rng=PRNGKey(1)):

def neos_pyhf_example(rng=PRNGKey(1)):

# regression net
init_random_params, predict = stax.serial(
Expand All @@ -42,15 +41,20 @@ def neos_pyhf_example(rng=PRNGKey(1)):
# ## Compose differentiable workflow

# %%
dgen = data.generate_blobs(rng,blobs=4)
dgen = data.generate_blobs(rng, blobs=4)

# Specify our hyperparameters ahead of time for the kde histograms
bins = np.linspace(0,1,4)
bandwidth=0.27
bins = np.linspace(0, 1, 4)
bandwidth = 0.27
reflect_infinite_bins = True

hmaker = makers.hists_from_nn(dgen, predict, hpar_dict = dict(bins=bins,bandwidth=bandwidth),method='kde', reflect_infinities=reflect_infinite_bins)

hmaker = makers.hists_from_nn(
dgen,
predict,
hpar_dict=dict(bins=bins, bandwidth=bandwidth),
method="kde",
reflect_infinities=reflect_infinite_bins,
)

# %%
nnm = makers.histosys_model_from_hists(hmaker)
Expand All @@ -76,7 +80,6 @@ def loss(params, test_mu):
opt_init, opt_update, opt_params = optimizers.adam(1e-3)

def train_network(N):
cls_vals = []
_, network = init_random_params(jax.random.PRNGKey(1), (-1, 2))
state = opt_init(network)
losses = []
Expand All @@ -95,16 +98,13 @@ def update_and_value(i, opt_state, mu):
losses.append(value)
metrics = {"loss": losses}



yield network, metrics, epoch_time

maxN = 50 # make me bigger for better results!

# Training
for i, (network, metrics, epoch_time) in enumerate(train_network(maxN)):
print(f"epoch {i}:", f'CLs = {metrics["loss"][-1]}, took {epoch_time}s')
pass # print(f"epoch {i}:", f'CLs = {metrics["loss"][-1]}, took {epoch_time}s')
# %%

return metrics["loss"][-1]

return metrics["loss"][-1]
Loading

0 comments on commit 77748e3

Please sign in to comment.