Skip to content

Commit

Permalink
Merge pull request #10 from SpeysideHEP/load_efficiency
Browse files Browse the repository at this point in the history
Initialisation efficiency improvement
  • Loading branch information
jackaraz authored Jan 25, 2024
2 parents 657330b + de45f3f commit a9401bc
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

[![arxiv](https://img.shields.io/static/v1?style=plastic&label=arXiv&message=2307.06996&color=brightgreen&logo=arXiv)](https://arxiv.org/abs/2307.06996)
[![DOI](https://img.shields.io/static/v1?style=plastic&label=DOI&message=TBA&color=blue)]()
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.10527973.svg)](https://doi.org/10.5281/zenodo.10527973)
[![DOI](https://zenodo.org/badge/617041391.svg)](https://zenodo.org/badge/latestdoi/617041391)
[![doc](https://img.shields.io/static/v1?style=plastic&label&message=Documentation&logo=gitbook&logoColor=white&color=gray)](http://spey-pyhf.readthedocs.io/)

<img src="https://raw.githubusercontent.com/SpeysideHEP/spey/main/docs/img/spey-logo.png" alt="Spey logo" style="float: right; margin-right: 20px" align="right" width=250px/>

[![github](https://img.shields.io/static/v1?style=plastic&label&message=GitHub&logo=github&logoColor=black&color=white)](https://github.com/SpeysideHEP/spey-pyhf)
[![PyPI - Version](https://img.shields.io/pypi/v/spey-pyhf?style=plastic)](https://pypi.org/project/spey-pyhf/)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/spey-pyhf?style=plastic&link=https%3A%2F%2Fpypi.org%2Fproject%2Fspey-pyhf)](https://pypi.org/project/spey-pyhf/)
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Welcome to the documentation of the ``pyhf`` plugin for Spey
:target: https://github.com/SpeysideHEP/spey-pyhf/issues

.. image:: https://zenodo.org/badge/617041391.svg
:target: https://zenodo.org/doi/10.5281/zenodo.10527973
:target: https://zenodo.org/badge/latestdoi/617041391

.. toctree::
:maxdepth: 2
Expand Down
7 changes: 5 additions & 2 deletions docs/releases/changelog-v0.1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Release notes v0.1

## New features since last release
## New features since the last release

* A mapping functionality has been developed between ``pyhf``'s full
statistical models and simplified likelihoods framework. Methodology
Expand All @@ -18,9 +18,12 @@
* Update `pyhf` version.
([#8](https://github.com/SpeysideHEP/spey-pyhf/pull/8))

* Model loading has been improved for prefit and postfit scenarios.
([#10](https://github.com/SpeysideHEP/spey-pyhf/pull/10))

## Bug fixes

* Bugfix in `simplify` module, where signal injector was not initiating properly.
* Bugfix in `simplify` module, where signal injector was not initiated properly.
([#9](https://github.com/SpeysideHEP/spey-pyhf/pull/9))

* Bugfix in apriori likelihood computation for full likelihoods mentioned in
Expand Down
2 changes: 1 addition & 1 deletion 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", mode="r", encoding="UTF-8") as f:
version = f.readlines()[-1].split()[-1].strip("\"'")

requirements = ["pyhf==0.7.6", "spey>=0.1.1"]
requirements = ["pyhf==0.7.6", "spey>=0.1.5"]

docs = [
"sphinx==6.2.1",
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.3"
__version__ = "0.1.4"
64 changes: 28 additions & 36 deletions src/spey_pyhf/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,18 @@ def __post_init__(self):
with open(self.background_only_model, "r", encoding="uft-8") as f:
self.background_only_model = json.load(f)

self.background_only_model_apriori = copy.deepcopy(self.background_only_model)
interpreter = WorkspaceInterpreter(self.background_only_model)
interpreter.add_patch(self.signal_patch)

# set data as expected background events
self.background_only_model_apriori["observations"] = [
{"name": name, "data": data}
for name, data in interpreter.expected_background_yields.items()
]
self.expected_background_yields = interpreter.expected_background_yields

self.workspace_apriori = manager.pyhf.Workspace(
self.background_only_model_apriori
)
self.workspace = manager.pyhf.Workspace(self.background_only_model)

self._model = None
# Initialise config
model = self()[1]

min_ratio = []
for idc, channel in enumerate(self.background_only_model.get("channels", [])):
current_signal = []
Expand Down Expand Up @@ -236,10 +233,6 @@ def __post_init__(self):
-np.min(min_ratio).astype(np.float32) if len(min_ratio) > 0 else -np.inf
)

self._models = {"post": None, "pre": None}
# Initialise config
model = self()[1]

self._config = {
"poi_index": model.config.poi_index,
"minimum_poi": self._minimum_poi,
Expand All @@ -260,7 +253,11 @@ def channel_properties(self) -> Iterator[Tuple[int, Text, int]]:
for idx, channel in enumerate(self.channels):
yield idx, channel, self.workspace.channel_nbins[channel]

def __call__(self, expected: ExpectationType = ExpectationType.observed) -> Tuple:
def __call__(
self,
expected: ExpectationType = ExpectationType.observed,
include_aux: bool = True,
) -> Tuple:
"""
Retreive pyhf workspace
Expand All @@ -276,40 +273,35 @@ def __call__(self, expected: ExpectationType = ExpectationType.observed) -> Tupl
the truth.
* :obj:`~spey.ExpectationType.apriori`: Computes the expected p-values with via pre-fit
prescription which means that the SM will be assumed to be the truth.
include_aux (`bool`, default `True`): Include auxiliary data.
Returns:
``Tuple``:
workspace, model and data
"""
if expected == ExpectationType.apriori:
if self._models["pre"] is None:
self._models["pre"] = self.workspace_apriori.model(
patches=[self.signal_patch],
modifier_settings={
"normsys": {"interpcode": "code4"},
"histosys": {"interpcode": "code4p"},
},
)

return (
self.workspace_apriori,
self._models["pre"],
self.workspace_apriori.data(self._models["pre"]),
)

if self._models["post"] is None:
self._models["post"] = self.workspace.model(
if self._model is None:
self._model = self.workspace.model(
patches=[self.signal_patch],
modifier_settings={
"normsys": {"interpcode": "code4"},
"histosys": {"interpcode": "code4p"},
},
)
return (
self.workspace,
self._models["post"],
self.workspace.data(self._models["post"]),
)

if expected == ExpectationType.apriori:
data = sum(
(
self.expected_background_yields[ch]
for ch in self._model.config.channels
),
[],
)
if include_aux:
data += self._model.config.auxdata
else:
data = self.workspace.data(self._model, include_auxdata=include_aux)

return self.workspace, self._model, data

def config(
self, allow_negative_signal: bool = True, poi_upper_bound: Optional[float] = None
Expand Down
2 changes: 1 addition & 1 deletion src/spey_pyhf/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PyhfInterface(BackendBase):
"""Version of the backend"""
author: Text = "SpeysideHEP"
"""Author of the backend"""
spey_requires: Text = ">=0.1.0"
spey_requires: Text = ">=0.1.5,<0.2.0"
"""Spey version required for the backend"""
doi: List[Text] = ["10.5281/zenodo.1169739", "10.21105/joss.02823"]
"""Citable DOI for the backend"""
Expand Down
10 changes: 6 additions & 4 deletions src/spey_pyhf/simplify.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Interface to convert pyhf likelihoods to simplified likelihood framework"""
import copy
import warnings
from typing import Callable, List, Optional, Text, Union
from typing import Callable, List, Optional, Text, Union, Literal

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

def __call__(
self,
statistical_model: spey.StatisticalModel,
fittype: Text = "postfit",
convert_to: Text = "default_pdf.correlated_background",
fittype: Literal["postfit", "prefit"] = "postfit",
convert_to: Literal[
"default_pdf.correlated_background", "default_pdf.third_moment_expansion"
] = "default_pdf.correlated_background",
number_of_samples: int = 1000,
control_region_indices: Optional[Union[List[int], List[Text]]] = None,
include_modifiers_in_control_model: bool = False,
Expand Down

0 comments on commit a9401bc

Please sign in to comment.