Skip to content

Commit

Permalink
Merge pull request #18 from SpeysideHEP/update-v0.2
Browse files Browse the repository at this point in the history
Adapt to the latest spey version
  • Loading branch information
jackaraz authored Feb 28, 2025
2 parents f676f77 + 4ae942c commit c19d0a3
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 40 deletions.
81 changes: 81 additions & 0 deletions .github/workflows/check+build+deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Check/Build/Deploy

on:
release:
types: [published]
push:
branches: ["releases/**"]
workflow_dispatch:

jobs:
# check:
# name: OS ${{ matrix.os }}, Python ${{ matrix.python-version }}
# runs-on: ${{ matrix.os }}

# strategy:
# fail-fast: false
# matrix:
# python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
# os: [ubuntu-latest, macos-latest]

# steps:
# - uses: actions/checkout@v3
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v3
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install dependencies
# run: |
# python -m pip install --upgrade pip
# python -m pip install pytest pytest-cov
# python -m pip install -e .
# - name: Test with pytest
# run: |
# python -c 'import spey;spey.about()'
# pytest --cov=spey tests/*py #--cov-fail-under 99

build:
name: Build wheel
runs-on: ubuntu-latest
# needs: check
steps:
- name: Checkout git repository
uses: actions/checkout@v4

- name: Build Spey wheel
run: |
python -m pip install --upgrade pip
python -m pip install wheel
python setup.py bdist_wheel
- name: Upload wheels as artifacts
uses: actions/upload-artifact@v4
with:
name: wheel
path: ./dist/*.whl

deploy:
if: ${{ !github.event.release.prerelease && !contains(github.ref, '-rc') && !contains(github.ref, '-beta') && !contains(github.ref, '-alpha') }}
runs-on: ubuntu-latest
needs: build
environment:
name: pypi
url: https://pypi.org/project/spey-pyhf/
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
pattern: wheel
merge-multiple: true
path: dist

- name: Debug
run: ls -l dist

- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@v1.4.2
with:
user: ${{ secrets.TWINE_USERNAME }}
password: ${{ secrets.TWINE_PSSWRD }}
verbose: true
6 changes: 3 additions & 3 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"description": "pyhf plug-in for spey package",
"license": "MIT",
"title": "SpeysideHEP/spey-pyhf: v0.1.10",
"version": "v0.1.10",
"title": "SpeysideHEP/spey-pyhf: v0.2.0",
"version": "v0.2.0",
"upload_type": "software",
"creators": [
{
Expand All @@ -29,7 +29,7 @@
},
{
"scheme": "url",
"identifier": "https://github.com/SpeysideHEP/spey-pyhf/tree/v0.1.10",
"identifier": "https://github.com/SpeysideHEP/spey-pyhf/tree/v0.2.0",
"relation": "isSupplementTo"
},
{
Expand Down
25 changes: 13 additions & 12 deletions CITATIONS.bib
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
@article{10.21468/SciPostPhys.16.1.032,
author = {Jack Y. Araz},
doi = {10.21468/SciPostPhys.16.1.032},
journal = {SciPost Phys.},
pages = {032},
publisher = {SciPost},
title = {{Spey: Smooth inference for reinterpretation studies}},
url = {https://scipost.org/10.21468/SciPostPhys.16.1.032},
volume = {16},
year = {2024},
bdsk-url-1 = {https://scipost.org/10.21468/SciPostPhys.16.1.032},
bdsk-url-2 = {https://doi.org/10.21468/SciPostPhys.16.1.032}
@article{Araz:2023bwx,
author = {Araz, Jack Y.},
title = {{Spey: Smooth inference for reinterpretation studies}},
eprint = {2307.06996},
archiveprefix = {arXiv},
primaryclass = {hep-ph},
reportnumber = {IPPP/23/34},
doi = {10.21468/SciPostPhys.16.1.032},
journal = {SciPost Phys.},
volume = {16},
number = {1},
pages = {032},
year = {2024}
}

@software{spey_pyhf_zenodo,
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Jack Y. Araz
Copyright (c) 2025 Jack Y. Araz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
sys.path.insert(0, str(Path("./ext").resolve()))

project = "spey-pyhf"
copyright = "2024, Jack Y. Araz"
copyright = "2025, Jack Y. Araz"
author = "Jack Y. Araz"
release = get_distribution("spey_pyhf").version
version = ".".join(release.split(".")[:3])
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
with open("src/spey_pyhf/_version.py", encoding="UTF-8") as f:
version = f.readlines()[-1].split()[-1].strip("\"'")

requirements = ["pyhf==0.7.6", "spey>=0.1.11"]
requirements = ["pyhf==0.7.6", "spey>=0.2.1"]

docs = [
"sphinx==6.2.1",
Expand Down Expand Up @@ -49,7 +49,7 @@
]
},
install_requires=requirements,
python_requires=">=3.8",
python_requires=">=3.8, <3.13",
classifiers=[
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
Expand Down
2 changes: 1 addition & 1 deletion src/spey_pyhf/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version of the spey - pyhf plugin"""

__version__ = "0.1.10"
__version__ = "0.2.0-beta"
13 changes: 6 additions & 7 deletions src/spey_pyhf/helper_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import logging
from typing import Dict, Iterator, List, Optional, Tuple, Union

from spey import log_once

__all__ = ["WorkspaceInterpreter"]


Expand Down Expand Up @@ -114,15 +116,12 @@ def expected_background_yields(self) -> Dict[str, List[float]]:
if channel["name"] not in self._signal_dict:
undefined_channels.append(channel["name"])
if len(undefined_channels) > 0:
log.warning(
log_once(
"Some of the channels are not defined in the patch set, "
"these channels will be kept in the statistical model. "
)
log.warning(
"If these channels are meant to be removed, please indicate them in the patch set."
)
log.warning(
"Please check the following channel(s): " + ", ".join(undefined_channels)
"If these channels are meant to be removed, please indicate them in the patch set. "
"Please check the following channel(s): " + ", ".join(undefined_channels),
log_type="warning",
)
return yields

Expand Down
4 changes: 2 additions & 2 deletions src/spey_pyhf/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import copy
import warnings
from typing import Callable, Dict, List, Optional, Text, Tuple, Union
from typing import Callable, Dict, List, Optional, Tuple, Union

import numpy as np
from spey.base import ModelConfig
Expand Down Expand Up @@ -73,7 +73,7 @@ class PyhfInterface(BackendBase):
"""Version of the backend"""
author: str = "SpeysideHEP"
"""Author of the backend"""
spey_requires: str = ">=0.1.9,<0.2.0"
spey_requires: str = ">=0.2.0, <0.3.0"
"""Spey version required for the backend"""
doi: List[str] = ["10.5281/zenodo.1169739", "10.21105/joss.02823"]
"""Citable DOI for the backend"""
Expand Down
10 changes: 9 additions & 1 deletion src/spey_pyhf/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@

from spey.system.exceptions import MethodNotAvailable

log = logging.getLogger("Spey")


class PyhfManager:
"""Manager to unify the usage of pyhf through out the package"""

pyhf = importlib.import_module("pyhf")

def __init__(self):
PyhfManager.pyhf.set_backend("numpy", precision="64b")
if importlib.util.find_spec("jax") is not None:
PyhfManager.pyhf.set_backend("jax", precision="64b")
log.debug("pyhf backend set to Jax")
else:
PyhfManager.pyhf.set_backend("numpy", precision="64b")
log.debug("pyhf backend set to NumPy")

PyhfManager.pyhf.pdf.log.setLevel(logging.CRITICAL)
PyhfManager.pyhf.workspace.log.setLevel(logging.CRITICAL)
Expand Down Expand Up @@ -61,6 +68,7 @@ def backend(self, backend: str) -> None:
"""
if backend not in self.available_backends:
raise MethodNotAvailable(f"{backend} backend currently not available.")
log.debug(f"setting pyhf backend to {backend}")
PyhfManager.pyhf.set_backend(backend)

@property
Expand Down
17 changes: 7 additions & 10 deletions src/spey_pyhf/simplify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import warnings
from contextlib import contextmanager
from pathlib import Path
from typing import Callable, List, Literal, Optional, Text, Union
from typing import Callable, List, Literal, Optional, Union

import numpy as np
import spey
Expand Down Expand Up @@ -170,7 +170,7 @@ class Simplify(spey.ConverterBase):
"""Version of the backend"""
author: str = "SpeysideHEP"
"""Author of the backend"""
spey_requires: str = ">=0.1.5,<0.2.0"
spey_requires: str = ">=0.2.0,<0.3.0"
"""Spey version required for the backend"""

def __call__(
Expand Down Expand Up @@ -340,15 +340,18 @@ def __call__(
# if the initial value is NaN continue search
continue
current_fit_opts["constraints"] += constraints
with warnings.catch_warnings(record=True) as w:
with warnings.catch_warnings(record=True) as warnings_list:
_, new_params = fit(
**current_fit_opts,
initial_parameters=init_params.tolist(),
bounds=current_fit_opts[
"model_configuration"
].suggested_bounds,
)
warnings_list += w
for warning in warnings_list:
log.debug(
f"{warning.message} (file: {warning.filename}, L:{warning.lineno})"
)

try:
current_sample = statistical_model.backend.get_sampler(
Expand All @@ -366,12 +369,6 @@ def __call__(
)
continue

if len(warnings_list) > 0:
log.warning(
f"{len(warnings_list)} warning(s) generated during sampling."
" This might be due to edge cases in nuisance parameter sampling."
)

samples = np.vstack(samples)

covariance_matrix = np.cov(samples, rowvar=0)
Expand Down

0 comments on commit c19d0a3

Please sign in to comment.