From 4ff98d375059a4a9022051d398dcb40450d2801a Mon Sep 17 00:00:00 2001 From: Szymon Harabasz Date: Tue, 29 Sep 2020 17:44:37 +0200 Subject: [PATCH 1/3] A possibility to make bins with weighted averages added. This allows to make entires of the type: { value: 1.5, low: 1.0, high:2.0 }. Such a feature is necessary to submit data from figures like Fig. 6 in: https://doi.org/10.1103/PhysRevC.80.044905 --- hepdata_lib/__init__.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/hepdata_lib/__init__.py b/hepdata_lib/__init__.py index 0b8c9d9a..82ea59b8 100644 --- a/hepdata_lib/__init__.py +++ b/hepdata_lib/__init__.py @@ -53,11 +53,12 @@ class Variable(object): # pylint: disable=too-many-instance-attributes # Eight is reasonable in this case. - def __init__(self, name, is_independent=True, is_binned=True, units="", values=None): + def __init__(self, name, is_independent=True, is_binned=True, has_weighted_bins=False, units="", values=None): # pylint: disable=too-many-arguments self.name = name self.is_independent = is_independent self.is_binned = is_binned + self.has_weighted_bins = has_weighted_bins self.qualifiers = [] self.units = units # needed to make pylint happy, see https://github.com/PyCQA/pylint/issues/409 @@ -85,6 +86,17 @@ def values(self, value_list): # All good self._values = [(float(x[0]), float(x[1])) for x in value_list] + elif self.has_weighted_bins: + # Check that the input is well-formed + try: + assert all([len(x) == 3 for x in value_list]) + except (AssertionError, TypeError, ValueError): + raise ValueError("For Variables with weighted bins, values should be tuples of length three: \ + (bin's weighted mean, lower bin edge, upper bin edge)." + ) + + # All good + self._values = [(float(x[0]), float(x[1]), float(x[2])) for x in value_list] else: # Check that the input is well-formed try: @@ -95,11 +107,14 @@ def values(self, value_list): def scale_values(self, factor): """Multiply each value by constant factor. Also applies to uncertainties.""" - if not self.is_binned: - self.values = [factor * x for x in self.values] - else: + if self.is_binned: self.values = [(factor * x[0], factor * x[1]) for x in self.values] + elif self.has_weighted_bins: + self.values = [(factor * x[0], factor * x[1], factor * x[2]) + for x in self.values] + else: + self.values = [factor * x for x in self.values] for unc in self.uncertainties: unc.scale_values(factor) @@ -169,6 +184,13 @@ def make_dict(self): self.digits) valuedict["high"] = helpers.relative_round(self._values[i][1], self.digits) + elif self.has_weighted_bins: + valuedict["value"] = helpers.relative_round(self._values[i][0], + self.digits) + valuedict["low"] = helpers.relative_round(self._values[i][1], + self.digits) + valuedict["high"] = helpers.relative_round(self._values[i][2], + self.digits) else: valuedict["value"] = helpers.relative_round(self._values[i], self.digits) From 4d1d141f70a3301b180d10a7dc511a1b0bc01505 Mon Sep 17 00:00:00 2001 From: Szymon Harabasz Date: Mon, 5 Oct 2020 11:28:43 +0200 Subject: [PATCH 2/3] Fixed whitespaces and line lengths --- hepdata_lib/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hepdata_lib/__init__.py b/hepdata_lib/__init__.py index 82ea59b8..88e944d0 100644 --- a/hepdata_lib/__init__.py +++ b/hepdata_lib/__init__.py @@ -53,7 +53,8 @@ class Variable(object): # pylint: disable=too-many-instance-attributes # Eight is reasonable in this case. - def __init__(self, name, is_independent=True, is_binned=True, has_weighted_bins=False, units="", values=None): + def __init__(self, name, is_independent=True, is_binned=True, has_weighted_bins=False, + units="", values=None): # pylint: disable=too-many-arguments self.name = name self.is_independent = is_independent @@ -91,7 +92,8 @@ def values(self, value_list): try: assert all([len(x) == 3 for x in value_list]) except (AssertionError, TypeError, ValueError): - raise ValueError("For Variables with weighted bins, values should be tuples of length three: \ + raise ValueError("For Variables with weighted bins, values should be tuples \ + of length three: \ (bin's weighted mean, lower bin edge, upper bin edge)." ) @@ -186,7 +188,7 @@ def make_dict(self): self.digits) elif self.has_weighted_bins: valuedict["value"] = helpers.relative_round(self._values[i][0], - self.digits) + self.digits) valuedict["low"] = helpers.relative_round(self._values[i][1], self.digits) valuedict["high"] = helpers.relative_round(self._values[i][2], From 7645112ff52980340a082cc8b37a4aafdbffa0b1 Mon Sep 17 00:00:00 2001 From: Szymon Harabasz Date: Mon, 5 Oct 2020 11:56:53 +0200 Subject: [PATCH 3/3] Second try to sastisfy pylint --- hepdata_lib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hepdata_lib/__init__.py b/hepdata_lib/__init__.py index 88e944d0..59ae0b71 100644 --- a/hepdata_lib/__init__.py +++ b/hepdata_lib/__init__.py @@ -53,7 +53,7 @@ class Variable(object): # pylint: disable=too-many-instance-attributes # Eight is reasonable in this case. - def __init__(self, name, is_independent=True, is_binned=True, has_weighted_bins=False, + def __init__(self, name, is_independent=True, is_binned=True, has_weighted_bins=False, units="", values=None): # pylint: disable=too-many-arguments self.name = name