Skip to content

Commit

Permalink
feat: Add nbQA to pre-commit hooks (#1165)
Browse files Browse the repository at this point in the history
* Add nbqa to pre-commit hooks
   - Apply nbqa-black and nbqa-pyupgrade
* Add nbqa config options to pyproject.toml
* Run nbqa over all notebooks
* Add --py36-plus option as arg to pyupgrade pre-commit hook
   - Amends PR #1164
  • Loading branch information
matthewfeickert authored Nov 6, 2020
1 parent a3b34a5 commit e4011ff
Show file tree
Hide file tree
Showing 25 changed files with 1,198 additions and 897 deletions.
8 changes: 7 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.7.3
hooks:
- id: pyupgrade
- id: pyupgrade
args: ["--py36-plus"]
- repo: https://github.com/nbQA-dev/nbQA
rev: 0.4.0
hooks:
- id: nbqa-black
- id: nbqa-pyupgrade
22 changes: 12 additions & 10 deletions docs/examples/experiments/edwardpyhf.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"metadata": {},
"outputs": [],
"source": [
"nuispar = tf.constant([3.])\n",
"nuispar = tf.constant([3.0])\n",
"\n",
"x = Poisson(rate = tf.ones(1)*nuispar)\n",
"n = Normal(loc = nuispar, scale = tf.ones(1))\n",
"joined = tf.concat([x,n], axis=0) # p(n, x | nuispar) = Pois(n|nuispar) * Normal(x |mu = nuispar, sigma = 1)"
"x = Poisson(rate=tf.ones(1) * nuispar)\n",
"n = Normal(loc=nuispar, scale=tf.ones(1))\n",
"joined = tf.concat(\n",
" [x, n], axis=0\n",
") # p(n, x | nuispar) = Pois(n|nuispar) * Normal(x |mu = nuispar, sigma = 1)"
]
},
{
Expand All @@ -30,7 +32,7 @@
"outputs": [],
"source": [
"results = []\n",
"for i in range(1000): #thee is probably a batched evaluation version .. this is stupid\n",
"for i in range(1000): # thee is probably a batched evaluation version .. this is stupid\n",
" with tf.Session() as sess:\n",
" r = sess.run(joined)\n",
" results.append(r)"
Expand Down Expand Up @@ -85,11 +87,11 @@
"source": [
"%pylab inline\n",
"r = np.array(results)\n",
"f, axarr = plt.subplots(1,3)\n",
"f.set_size_inches(12,4)\n",
"axarr[0].scatter(r[:,0],r[:,1])\n",
"axarr[1].hist(r[:,0], bins = np.linspace(0,10,11))\n",
"axarr[2].hist(r[:,1])"
"f, axarr = plt.subplots(1, 3)\n",
"f.set_size_inches(12, 4)\n",
"axarr[0].scatter(r[:, 0], r[:, 1])\n",
"axarr[1].hist(r[:, 0], bins=np.linspace(0, 10, 11))\n",
"axarr[2].hist(r[:, 1])"
]
},
{
Expand Down
19 changes: 11 additions & 8 deletions docs/examples/notebooks/ImpactPlot.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"import jsonpatch\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline"
]
},
Expand Down Expand Up @@ -82,17 +83,21 @@
" _, model, data = make_model([\"CRtt_meff\"])\n",
"\n",
" pyhf.set_backend(\"numpy\", pyhf.optimize.minuit_optimizer(verbose=True))\n",
" \n",
"\n",
" constraints = constraints or []\n",
" init_pars = model.config.suggested_init()\n",
" fixed_params = model.config.suggested_fixed()\n",
" \n",
" for idx,fixed_val in constraints:\n",
"\n",
" for idx, fixed_val in constraints:\n",
" init_pars[idx] = fixed_val\n",
" fixed_params[idx] = True\n",
" \n",
"\n",
" result = pyhf.infer.mle.fit(\n",
" data, model, init_pars=init_pars, fixed_params=fixed_params, return_uncertainties=True\n",
" data,\n",
" model,\n",
" init_pars=init_pars,\n",
" fixed_params=fixed_params,\n",
" return_uncertainties=True,\n",
" )\n",
" bestfit = result[:, 0]\n",
" errors = result[:, 1]\n",
Expand Down Expand Up @@ -151,9 +156,7 @@
" )\n",
" labels = np.asarray(\n",
" [\n",
" \"{}[{:02}]\".format(k, i)\n",
" if model.config.param_set(k).n_parameters > 1\n",
" else k\n",
" f\"{k}[{i:02}]\" if model.config.param_set(k).n_parameters > 1 else k\n",
" for k in model.config.par_order\n",
" if model.config.param_set(k).constrained\n",
" for i in range(model.config.param_set(k).n_parameters)\n",
Expand Down
9 changes: 6 additions & 3 deletions docs/examples/notebooks/Recast.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"import jsonpatch\n",
"import pyhf\n",
"import pyhf.contrib.viz.brazil as brazil\n",
"\n",
"%pylab inline"
]
},
Expand Down Expand Up @@ -124,7 +125,7 @@
"fig, ax = plt.subplots(1, 1)\n",
"ax.set_title(\"Hypothesis Tests\")\n",
"ax.set_ylabel(\"CLs\")\n",
"ax.set_xlabel(u\"µ\")\n",
"ax.set_xlabel(\"µ\")\n",
"brazil.plot_results(ax, test_mus, results, test_size=0.05)"
]
},
Expand All @@ -149,7 +150,9 @@
"source": [
"new_signal = [20.0, 10.0]\n",
"patch = jsonpatch.JsonPatch(\n",
" [{\"op\": \"replace\", \"path\": \"/channels/0/samples/0/data\", \"value\": new_signal},]\n",
" [\n",
" {\"op\": \"replace\", \"path\": \"/channels/0/samples/0/data\", \"value\": new_signal},\n",
" ]\n",
")"
]
},
Expand Down Expand Up @@ -231,7 +234,7 @@
"fig, ax = plt.subplots(1, 1)\n",
"ax.set_title(\"Hypothesis Tests\")\n",
"ax.set_ylabel(\"CLs\")\n",
"ax.set_xlabel(u\"µ\")\n",
"ax.set_xlabel(\"µ\")\n",
"brazil.plot_results(ax, test_mus, results, test_size=0.05)"
]
}
Expand Down
86 changes: 42 additions & 44 deletions docs/examples/notebooks/ShapeFactor.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"import pyhf\n",
"from pyhf import Model\n",
"\n",
"logging.basicConfig(level = logging.INFO)"
"logging.basicConfig(level=logging.INFO)"
]
},
{
Expand All @@ -46,7 +46,7 @@
"outputs": [],
"source": [
"def prep_data(sourcedata):\n",
" spec = {\n",
" spec = {\n",
" 'channels': [\n",
" {\n",
" 'name': 'signal',\n",
Expand All @@ -55,12 +55,8 @@
" 'name': 'signal',\n",
" 'data': sourcedata['signal']['bindata']['sig'],\n",
" 'modifiers': [\n",
" {\n",
" 'name': 'mu',\n",
" 'type': 'normfactor',\n",
" 'data': None\n",
" }\n",
" ]\n",
" {'name': 'mu', 'type': 'normfactor', 'data': None}\n",
" ],\n",
" },\n",
" {\n",
" 'name': 'bkg1',\n",
Expand All @@ -69,11 +65,11 @@
" {\n",
" 'name': 'coupled_shapefactor',\n",
" 'type': 'shapefactor',\n",
" 'data': None\n",
" 'data': None,\n",
" }\n",
" ]\n",
" }\n",
" ]\n",
" ],\n",
" },\n",
" ],\n",
" },\n",
" {\n",
" 'name': 'control',\n",
Expand All @@ -85,15 +81,15 @@
" {\n",
" 'name': 'coupled_shapefactor',\n",
" 'type': 'shapefactor',\n",
" 'data': None\n",
" 'data': None,\n",
" }\n",
" ]\n",
" ],\n",
" }\n",
" ]\n",
" }\n",
" ],\n",
" },\n",
" ]\n",
" }\n",
" pdf = Model(spec)\n",
" pdf = Model(spec)\n",
" data = []\n",
" for c in pdf.spec['channels']:\n",
" data += sourcedata[c['name']]['bindata']['data']\n",
Expand Down Expand Up @@ -126,27 +122,24 @@
],
"source": [
"source = {\n",
" \"channels\": {\n",
" \"signal\": {\n",
" \"binning\": [2,-0.5,1.5],\n",
" \"bindata\": {\n",
" \"data\": [220.0, 230.0],\n",
" \"bkg1\": [100.0, 70.0],\n",
" \"sig\": [ 20.0, 20.0]\n",
" }\n",
" },\n",
" \"control\": {\n",
" \"binning\": [2,-0.5,1.5],\n",
" \"bindata\": {\n",
" \"data\": [200.0, 300.0],\n",
" \"bkg1\": [100.0, 100.0]\n",
" }\n",
" \"channels\": {\n",
" \"signal\": {\n",
" \"binning\": [2, -0.5, 1.5],\n",
" \"bindata\": {\n",
" \"data\": [220.0, 230.0],\n",
" \"bkg1\": [100.0, 70.0],\n",
" \"sig\": [20.0, 20.0],\n",
" },\n",
" },\n",
" \"control\": {\n",
" \"binning\": [2, -0.5, 1.5],\n",
" \"bindata\": {\"data\": [200.0, 300.0], \"bkg1\": [100.0, 100.0]},\n",
" },\n",
" }\n",
" }\n",
"}\n",
"\n",
"data, pdf = prep_data(source['channels'])\n",
"print('data: {}'.format(data))\n",
"print(f'data: {data}')\n",
"\n",
"init_pars = pdf.config.suggested_init()\n",
"print('expected data: {}'.format(pdf.expected_data(init_pars)))\n",
Expand Down Expand Up @@ -177,10 +170,10 @@
}
],
"source": [
"print('initialization parameters: {}'.format(pdf.config.suggested_init()))\n",
"print(f'initialization parameters: {pdf.config.suggested_init()}')\n",
"\n",
"unconpars = pyhf.infer.mle.fit(data, pdf)\n",
"print('parameters post unconstrained fit: {}'.format(unconpars))"
"print(f'parameters post unconstrained fit: {unconpars}')"
]
},
{
Expand Down Expand Up @@ -225,12 +218,13 @@
}
],
"source": [
"def plot_results(testmus, cls_obs, cls_exp, poi_tests, test_size = 0.05):\n",
" plt.plot(poi_tests,cls_obs, c = 'k')\n",
" for i,c in zip(range(5),['grey','grey','grey','grey','grey']):\n",
" plt.plot(poi_tests, cls_exp[i], c = c)\n",
" plt.plot(testmus,[test_size]*len(testmus), c = 'r')\n",
" plt.ylim(0,1)\n",
"def plot_results(testmus, cls_obs, cls_exp, poi_tests, test_size=0.05):\n",
" plt.plot(poi_tests, cls_obs, c='k')\n",
" for i, c in zip(range(5), ['grey', 'grey', 'grey', 'grey', 'grey']):\n",
" plt.plot(poi_tests, cls_exp[i], c=c)\n",
" plt.plot(testmus, [test_size] * len(testmus), c='r')\n",
" plt.ylim(0, 1)\n",
"\n",
"\n",
"def invert_interval(test_mus, cls_obs, cls_exp, test_size=0.05):\n",
" crossing_test_stats = {'exp': [], 'obs': None}\n",
Expand All @@ -247,8 +241,12 @@
"\n",
"\n",
"poi_tests = np.linspace(0, 5, 61)\n",
"tests = [pyhf.infer.hypotest(poi_test, data, pdf, init_pars, par_bounds, return_expected_set=True) \n",
" for poi_test in poi_tests]\n",
"tests = [\n",
" pyhf.infer.hypotest(\n",
" poi_test, data, pdf, init_pars, par_bounds, return_expected_set=True\n",
" )\n",
" for poi_test in poi_tests\n",
"]\n",
"cls_obs = np.array([test[0] for test in tests]).flatten()\n",
"cls_exp = [np.array([test[1][i] for test in tests]).flatten() for i in range(5)]\n",
"\n",
Expand Down
Loading

0 comments on commit e4011ff

Please sign in to comment.