From 51f13c107270af2c7c3ba6aec561f9701683ce06 Mon Sep 17 00:00:00 2001 From: Christian Gutschow Date: Mon, 22 Jul 2024 19:57:13 +0200 Subject: [PATCH] add doc and unit test --- docs/usage.rst | 4 +++- hepdata_lib/helpers.py | 2 ++ tests/test_uncertainty.py | 15 +++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/usage.rst b/docs/usage.rst index 410f0002..31a183d9 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -370,5 +370,7 @@ multiple dependent variables and a (different) subset of the bins has missing co In this case the uncertainties should be set to zero for the missing bins with a non-numeric central value like ``'-'``. The warning message can be suppressed by passing an optional argument ``zero_uncertainties_warning=False`` when defining an instance of the ``Variable`` class. +Furthermore, note that `None` can be used to suppress the uncertainty for individual bins in cases where +the uncertainty components may only apply to a subset of the values. -.. _`Uncertainties`: https://hepdata-submission.readthedocs.io/en/latest/data_yaml.html#uncertainties \ No newline at end of file +.. _`Uncertainties`: https://hepdata-submission.readthedocs.io/en/latest/data_yaml.html#uncertainties diff --git a/hepdata_lib/helpers.py b/hepdata_lib/helpers.py index b0c7e754..1e10e85e 100644 --- a/hepdata_lib/helpers.py +++ b/hepdata_lib/helpers.py @@ -283,6 +283,8 @@ def sanitize_value(value): return value if isinstance(value,int): return value + if value is None: + return value return float(value) diff --git a/tests/test_uncertainty.py b/tests/test_uncertainty.py index e121e3bf..a4748928 100644 --- a/tests/test_uncertainty.py +++ b/tests/test_uncertainty.py @@ -115,3 +115,18 @@ def test_zero_uncertainties(self): # Check that 'errors' key is missing only if zero uncertainties self.assertTrue(all('errors' in dictionary['values'][i] for i in [0, 1, 3])) self.assertTrue('errors' not in dictionary['values'][2]) + + def test_inhomogenous_uncertainties(self): + '''Test cases where an uncertainty only applies to a subset of the bins''' + var = Variable("testvar", is_independent=False, is_binned=False, values=[1,2,3], + zero_uncertainties_warning=False) + uncA = Uncertainty('errorA', is_symmetric=True) + uncA.values = [ 0.1, 0.2, None ] + var.add_uncertainty(uncA) + uncB = Uncertainty('errorB', is_symmetric=True) + uncB.values = [ 0.1, 0.2, 0.3 ] + var.add_uncertainty(uncB) + dictionary = var.make_dict() + self.assertTrue(len([ errs['label'] for i in [0,1,2] \ + for errs in dictionary['values'][i]['errors'] \ + if errs['label'] == 'errorA'])==2)