Skip to content

Commit

Permalink
Merge branch 'master' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-eschle authored Apr 16, 2024
2 parents ac23334 + 4ec799e commit bcb8136
Show file tree
Hide file tree
Showing 50 changed files with 929 additions and 803 deletions.
21 changes: 10 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,17 @@ jobs:
os:
- ubuntu-latest
python-version:
- "3.8"
- "3.9"
- "3.10"
- "3.11"
- "3.12"
include:
- os: windows-latest
python-version: "3.8"
- os: windows-latest
python-version: "3.11"
- os: macos-latest
python-version: "3.8"
python-version: "3.9"
- os: macos-latest
python-version: "3.11"
python-version: "3.9"
- os: macos-14
python-version: "3.12" # old versions not supported
name: Check Python ${{ matrix.python-version }} ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand All @@ -68,14 +66,15 @@ jobs:
${{ runner.os }}-pip-
- name: Install package
run: python -m pip install -e .[test] pytest-xdist # for multiprocessing

run: |
python -m pip install --upgrade pip
python -m pip install -e .[test] pytest-xdist # for multiprocessing
- name: Test package
run: python -m pytest --doctest-modules --cov=hepstats --cov-report=xml -n3
run: python -m pytest --doctest-modules --cov=hepstats --cov-report=xml -n auto

- name: Upload coverage to Codecov
if: matrix.python-version == '3.11' && matrix.os == 'ubuntu-latest'
if: matrix.python-version == '3.12' && matrix.os == 'ubuntu-latest'
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }} # technically not needed, but prevents failures: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
Expand Down
25 changes: 17 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
repos:
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.4.0
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
Expand All @@ -25,7 +21,7 @@ repos:
hooks:
- id: check-manifest
args: [ --update, --no-build-isolation ]
additional_dependencies: [ setuptools-scm ]
additional_dependencies: [ setuptools-scm, setuptools-scm-git-archive ]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
hooks:
Expand Down Expand Up @@ -62,11 +58,9 @@ repos:
- repo: https://github.com/nbQA-dev/nbQA
rev: 1.8.5
hooks:
- id: nbqa-isort
additional_dependencies: [ isort==5.6.4 ]

- id: nbqa-pyupgrade
additional_dependencies: [ pyupgrade==2.7.4 ]
additional_dependencies: [ pyupgrade ]
args: [ --py38-plus ]


Expand Down Expand Up @@ -94,3 +88,18 @@ repos:
- id: check-github-actions
- id: check-dependabot
- id: check-readthedocs

- repo: https://github.com/MarcoGorelli/auto-walrus
rev: 0.3.3
hooks:
- id: auto-walrus

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.3.7"
hooks:
- id: ruff
types_or: [ python, pyi, jupyter ]
args: [ --fix, --unsafe-fixes, --show-fixes , --line-length=120]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi, jupyter ]
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
Changelog
=========

master
main
******

* Add support for Python 3.12, drop support for Python 3.8
* Improved support for zfit 0.20+

Thanks to @MoritzNeuberger for finding and proposing a hypothesis test fix.

Version 0.7.0
*************

Expand Down
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Path setup --------------------------------------------------------------
from __future__ import annotations

import sys
from pathlib import Path

from hepstats import __version__ as version

project_dir = Path(__file__).parents[1]
Expand Down
2 changes: 1 addition & 1 deletion docs/getting_started/hypotests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ The background plus signal can then be fitted to the data.
Parameters

+--------+--------+---------------+-----------+
| name | value | minuit_hesse | at limit |
| name | value | hesse | at limit |
+========+========+===============+===========+
| Nsig | 4.518 | +/- 5.8 | False |
+--------+--------+---------------+-----------+
Expand Down
5 changes: 2 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ dependencies:
- numpy
- scipy
- iminuit
- tensorflow>=2.4
- tensorflow-probability
#- zfit
#- zfit # todo: conda-forge is 0.18.1, we need 0.20.0
- asdf
- matplotlib
- pip:
- .
- zfit >=0.6.4
- zfit >=0.20.0
100 changes: 55 additions & 45 deletions notebooks/hypotests/FC_interval_asy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,19 @@
},
"outputs": [],
"source": [
"import os\n",
"import time\n",
"from __future__ import annotations\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import zfit\n",
"from zfit.loss import UnbinnedNLL\n",
"from zfit.minimize import Minuit\n",
"\n",
"zfit.settings.set_seed(10)\n",
"\n",
"from utils import one_minus_cl_plot, plotfitresult, pltdist\n",
"\n",
"from hepstats.hypotests import ConfidenceInterval\n",
"from hepstats.hypotests.calculators import AsymptoticCalculator\n",
"from hepstats.hypotests.exceptions import POIRangeError\n",
"from hepstats.hypotests.parameters import POIarray"
"from hepstats.hypotests.parameters import POIarray\n",
"from zfit.loss import UnbinnedNLL\n",
"from zfit.minimize import Minuit\n",
"\n",
"zfit.settings.set_seed(10)"
]
},
{
Expand All @@ -66,7 +62,7 @@
"outputs": [],
"source": [
"bounds = (-10, 10)\n",
"obs = zfit.Space('x', limits=bounds)\n",
"obs = zfit.Space(\"x\", limits=bounds)\n",
"\n",
"mean = zfit.Parameter(\"mean\", 0)\n",
"sigma = zfit.Parameter(\"sigma\", 1.0)\n",
Expand Down Expand Up @@ -98,17 +94,16 @@
"outputs": [],
"source": [
"# Create the negative log likelihood\n",
"nll = UnbinnedNLL(model=model, data=data) \n",
"nll = UnbinnedNLL(model=model, data=data)\n",
"\n",
"# Instantiate a minuit minimizer\n",
"minimizer = Minuit(verbosity=0)\n",
"\n",
"# minimisation of the loss function\n",
"minimum = minimizer.minimize(loss=nll)\n",
"minimum.hesse();\n",
"print(minimum)\n",
"minimum.hesse()\n",
"\n",
"x_err = minimum.params[mean][\"minuit_hesse\"][\"error\"]"
"x_err = minimum.params[mean][\"hesse\"][\"error\"]"
]
},
{
Expand Down Expand Up @@ -154,61 +149,60 @@
"outputs": [],
"source": [
"for n in np.arange(-6, 7, 1.0):\n",
" \n",
" x = n * x_err\n",
" \n",
"\n",
" if n not in results:\n",
" \n",
" zfit.settings.set_seed(5)\n",
" \n",
"\n",
" data.resample(param_values={mean: x})\n",
" \n",
"\n",
" minimum = minimizer.minimize(loss=nll)\n",
" minimum.hesse(); \n",
" \n",
" minimum.hesse()\n",
"\n",
" results_n = {}\n",
" results_n[\"x\"] = minimum.params[mean][\"value\"]\n",
" results_n[\"x_err\"] = minimum.params[mean][\"minuit_hesse\"][\"error\"]\n",
" \n",
" results_n[\"x_err\"] = minimum.params[mean][\"hesse\"][\"error\"]\n",
"\n",
" calculator = AsymptoticCalculator(minimum, minimizer)\n",
" \n",
" x_min = results_n[\"x\"] - results_n[\"x_err\"]*3\n",
" x_max = results_n[\"x\"] + results_n[\"x_err\"]*3\n",
" \n",
"\n",
" x_min = results_n[\"x\"] - results_n[\"x_err\"] * 3\n",
" x_max = results_n[\"x\"] + results_n[\"x_err\"] * 3\n",
"\n",
" if n < -1:\n",
" x_max = max(0.5 * results_n[\"x_err\"], x_max)\n",
" \n",
"\n",
" poinull = POIarray(mean, np.linspace(x_min, x_max, 50))\n",
" \n",
"\n",
" results_n[\"calculator\"] = calculator\n",
" results_n[\"poinull\"] = poinull\n",
" \n",
"\n",
" else:\n",
" results_n = results[n]\n",
" calculator = results_n[\"calculator\"]\n",
" poinull = results_n[\"poinull\"]\n",
"\n",
" if \"mu_lower\" not in results_n:\n",
" \n",
" for qtilde in [True, False]:\n",
" \n",
" while True:\n",
" try:\n",
" ci = ConfidenceInterval(calculator, poinull, qtilde=qtilde)\n",
" interval = ci.interval(alpha=0.05, printlevel=0)\n",
" break\n",
" except POIRangeError:\n",
" values = poinull.values\n",
" poinull = POIarray(mean, np.concatenate([values, [values[-1] + np.diff(values)[0]]]))\n",
" poinull = POIarray(\n",
" mean,\n",
" np.concatenate([values, [values[-1] + np.diff(values)[0]]]),\n",
" )\n",
" results_n[\"poinull\"] = poinull\n",
" \n",
"\n",
" if qtilde:\n",
" results_n[\"mu_lower\"] = interval[\"lower\"]\n",
" results_n[\"mu_upper\"] = interval[\"upper\"]\n",
" else:\n",
" results_n[\"mu_lower_unbound\"] = interval[\"lower\"]\n",
" results_n[\"mu_upper_unbound\"] = interval[\"upper\"]\n",
" \n",
"\n",
" results[n] = results_n"
]
},
Expand All @@ -235,15 +229,31 @@
"source": [
"f = plt.figure(figsize=(9, 8))\n",
"\n",
"plt.plot([v[\"x\"]/v[\"x_err\"] for v in results.values()], \n",
" [v[\"mu_upper_unbound\"]/v[\"x_err\"] for v in results.values()], color=\"black\", label=\"90 % CL, no boundaries\")\n",
"plt.plot([v[\"x\"]/v[\"x_err\"] for v in results.values()], \n",
" [v[\"mu_lower_unbound\"]/v[\"x_err\"] for v in results.values()], color=\"black\")\n",
"plt.plot([v[\"x\"]/v[\"x_err\"] for v in results.values()], \n",
" [v[\"mu_upper\"]/v[\"x_err\"] for v in results.values()], \"--\", color=\"crimson\", label=r\"90 % CL, $\\mu > 0$\")\n",
"plt.plot([v[\"x\"]/v[\"x_err\"] for v in results.values()], \n",
" [v[\"mu_lower\"]/v[\"x_err\"] for v in results.values()], \"--\", color=\"crimson\")\n",
"plt.ylim(0.)\n",
"plt.plot(\n",
" [v[\"x\"] / v[\"x_err\"] for v in results.values()],\n",
" [v[\"mu_upper_unbound\"] / v[\"x_err\"] for v in results.values()],\n",
" color=\"black\",\n",
" label=\"90 % CL, no boundaries\",\n",
")\n",
"plt.plot(\n",
" [v[\"x\"] / v[\"x_err\"] for v in results.values()],\n",
" [v[\"mu_lower_unbound\"] / v[\"x_err\"] for v in results.values()],\n",
" color=\"black\",\n",
")\n",
"plt.plot(\n",
" [v[\"x\"] / v[\"x_err\"] for v in results.values()],\n",
" [v[\"mu_upper\"] / v[\"x_err\"] for v in results.values()],\n",
" \"--\",\n",
" color=\"crimson\",\n",
" label=r\"90 % CL, $\\mu > 0$\",\n",
")\n",
"plt.plot(\n",
" [v[\"x\"] / v[\"x_err\"] for v in results.values()],\n",
" [v[\"mu_lower\"] / v[\"x_err\"] for v in results.values()],\n",
" \"--\",\n",
" color=\"crimson\",\n",
")\n",
"plt.ylim(0.0)\n",
"plt.legend(fontsize=15)\n",
"plt.ylabel(r\"Mean $\\mu$\", fontsize=15)\n",
"plt.xlabel(\"Measured mean $x$\", fontsize=15);"
Expand Down
Loading

0 comments on commit bcb8136

Please sign in to comment.