From ed44340fb9e6253ca89cf0401fd4a4aec7a5f3b0 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Wed, 20 Sep 2023 09:54:10 -0700 Subject: [PATCH 1/6] update api --- tests/test_modifiers.py | 4 ++-- tests/test_pdf.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_modifiers.py b/tests/test_modifiers.py index 6432e75e3b..52b453d6a8 100644 --- a/tests/test_modifiers.py +++ b/tests/test_modifiers.py @@ -77,7 +77,7 @@ def test_staterror_holes(): model = pyhf.Model(spec, poi_name="") assert model.config.npars == 9 - _, factors = model._modifications( + _, factors = model.modifications( pyhf.tensorlib.astensor([2, 2.0, 1.0, 1.0, 3.0, 4.0, 1.0, 5.0, 6.0]) ) assert model.config.param_set("staterror_1").suggested_fixed == [ @@ -151,7 +151,7 @@ def test_shapesys_holes(): } model = pyhf.Model(spec, poi_name="mu") - _, factors = model._modifications( + _, factors = model.modifications( pyhf.tensorlib.astensor([1.0, 2.0, 1.0, 1.0, 3.0, 4.0, 1.0, 1.0, 5.0]) ) assert (factors[1][0, 0, 0, :] == [2.0, 1.0, 1.0, 3.0, 1.0, 1.0, 1.0, 1.0]).all() diff --git a/tests/test_pdf.py b/tests/test_pdf.py index e680dad3f6..f12e1448ab 100644 --- a/tests/test_pdf.py +++ b/tests/test_pdf.py @@ -754,7 +754,7 @@ def test_lumi_np_scaling(): [[[1.0, 1.0]]], [[[1.0, 1.0]]], ] - assert pdf._modifications(np.array(pars))[1][0].tolist() == [mods] + assert pdf.modifications(np.array(pars))[1][0].tolist() == [mods] assert pdf.expected_data(pars).tolist() == [120.0, 110.0, 1.0] pars[poi_slice], pars[lumi_slice] = [[1.0], [alpha_lumi]] @@ -763,7 +763,7 @@ def test_lumi_np_scaling(): [[[1.0, 1.0]]], [[[alpha_lumi, alpha_lumi]]], ] - assert pdf._modifications(np.array(pars))[1][0].tolist() == [mods] + assert pdf.modifications(np.array(pars))[1][0].tolist() == [mods] assert pytest.approx(pdf.expected_data(pars).tolist()) == [ 100 + 20.0 * alpha_lumi, 110.0 * alpha_lumi, From 5e53d66a1727b4759a8f69077dfa02d8290023fa Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Wed, 20 Sep 2023 09:56:52 -0700 Subject: [PATCH 2/6] update to public api --- .../notebooks/binderexample/StatisticalAnalysis.ipynb | 2 +- src/pyhf/pdf.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/examples/notebooks/binderexample/StatisticalAnalysis.ipynb b/docs/examples/notebooks/binderexample/StatisticalAnalysis.ipynb index 8385164ba9..b2c0ca5063 100644 --- a/docs/examples/notebooks/binderexample/StatisticalAnalysis.ipynb +++ b/docs/examples/notebooks/binderexample/StatisticalAnalysis.ipynb @@ -176,7 +176,7 @@ "outputs": [], "source": [ "def get_mc_counts(pars):\n", - " deltas, factors = pdf._modifications(pars)\n", + " deltas, factors = pdf.modifications(pars)\n", " allsum = pyhf.tensorlib.concatenate(\n", " deltas + [pyhf.tensorlib.astensor(pdf.nominal_rates)]\n", " )\n", diff --git a/src/pyhf/pdf.py b/src/pyhf/pdf.py index 2b03da9020..5f0771f921 100644 --- a/src/pyhf/pdf.py +++ b/src/pyhf/pdf.py @@ -650,7 +650,7 @@ def logpdf(self, maindata, pars): """ return self.make_pdf(pars).log_prob(maindata) - def _modifications(self, pars): + def modifications(self, pars): deltas = list( filter( lambda x: x is not None, @@ -695,7 +695,7 @@ def expected_data(self, pars, return_by_sample=False): """ tensorlib, _ = get_backend() pars = tensorlib.astensor(pars) - deltas, factors = self._modifications(pars) + deltas, factors = self.modifications(pars) allsum = tensorlib.concatenate(deltas + [self.nominal_rates]) @@ -826,8 +826,8 @@ def expected_auxdata(self, pars): pars = tensorlib.astensor(pars) return self.make_pdf(pars)[1].expected_data() - def _modifications(self, pars): - return self.main_model._modifications(pars) + def modifications(self, pars): + return self.main_model.modifications(pars) @property def nominal_rates(self): From e574c9a8d9b2dc572774defc744f0e80ce216073 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Wed, 20 Sep 2023 10:04:22 -0700 Subject: [PATCH 3/6] add documentation --- src/pyhf/pdf.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/pyhf/pdf.py b/src/pyhf/pdf.py index 5f0771f921..aedf788d1a 100644 --- a/src/pyhf/pdf.py +++ b/src/pyhf/pdf.py @@ -651,6 +651,20 @@ def logpdf(self, maindata, pars): return self.make_pdf(pars).log_prob(maindata) def modifications(self, pars): + """ + Obtain the additive and multiplicative modifications to the expected + event rates for the model parameters. + + Args: + pars (:obj:`tensor`): The model parameters + + Returns: + Tuple of additive and multiplicative modifications: + - deltas (:obj:`list`) is the result of an `apply(pars)` of combined modifiers + with 'addition' op_code + - factors (:obj:`list`) is the result of `apply(pars)` of combined modifiers + with 'multiplication' op_code + """ deltas = list( filter( lambda x: x is not None, @@ -827,6 +841,10 @@ def expected_auxdata(self, pars): return self.make_pdf(pars)[1].expected_data() def modifications(self, pars): + """ + The modifications applied to the :class:`~pyhf.pdf._MainModel`. See + :func:`pyhf.pdf._MainModel.modifications` for details. + """ return self.main_model.modifications(pars) @property From f7ba7368b8c893ba88e32e9464f95e00ed3bf8a5 Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Wed, 20 Sep 2023 10:13:22 -0700 Subject: [PATCH 4/6] add main model / constraint model to docs --- docs/api.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/api.rst b/docs/api.rst index 57e09b70d2..56f65a211a 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -44,6 +44,8 @@ Making Models from PDFs ~pdf.Model ~pdf._ModelConfig + ~pdf._MainModel + ~pdf._ConstraintModel ~mixins._ChannelSummaryMixin ~workspace.Workspace ~patchset.PatchSet From 3c3c564c98a07ca97c90094ce74abef21afdb53a Mon Sep 17 00:00:00 2001 From: Giordon Stark Date: Wed, 20 Sep 2023 11:15:55 -0700 Subject: [PATCH 5/6] Update src/pyhf/pdf.py Co-authored-by: Matthew Feickert --- src/pyhf/pdf.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pyhf/pdf.py b/src/pyhf/pdf.py index aedf788d1a..61776402a4 100644 --- a/src/pyhf/pdf.py +++ b/src/pyhf/pdf.py @@ -660,10 +660,10 @@ def modifications(self, pars): Returns: Tuple of additive and multiplicative modifications: - - deltas (:obj:`list`) is the result of an `apply(pars)` of combined modifiers - with 'addition' op_code - - factors (:obj:`list`) is the result of `apply(pars)` of combined modifiers - with 'multiplication' op_code + - deltas (:obj:`list`) is the result of an ``apply(pars)`` of combined modifiers + with ``"addition"`` ``op_code`` + - factors (:obj:`list`) is the result of ``apply(pars)`` of combined modifiers + with ``"multiplication"`` ``op_code` """ deltas = list( filter( From 6d6e93a6f73b6963f55e64404fb3ab8cf1d97794 Mon Sep 17 00:00:00 2001 From: Matthew Feickert Date: Wed, 20 Sep 2023 16:15:10 -0500 Subject: [PATCH 6/6] Add missing backtick. --- src/pyhf/pdf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pyhf/pdf.py b/src/pyhf/pdf.py index 61776402a4..ca051d1652 100644 --- a/src/pyhf/pdf.py +++ b/src/pyhf/pdf.py @@ -663,7 +663,7 @@ def modifications(self, pars): - deltas (:obj:`list`) is the result of an ``apply(pars)`` of combined modifiers with ``"addition"`` ``op_code`` - factors (:obj:`list`) is the result of ``apply(pars)`` of combined modifiers - with ``"multiplication"`` ``op_code` + with ``"multiplication"`` ``op_code`` """ deltas = list( filter(