Skip to content

Commit

Permalink
add doc and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
20DM committed Jul 22, 2024
1 parent 81adb42 commit 51f13c1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
.. _`Uncertainties`: https://hepdata-submission.readthedocs.io/en/latest/data_yaml.html#uncertainties
2 changes: 2 additions & 0 deletions hepdata_lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
15 changes: 15 additions & 0 deletions tests/test_uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 51f13c1

Please sign in to comment.