Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A possibility to make bins with weighted averages added. #146

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions hepdata_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ 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
Expand Down Expand Up @@ -85,6 +87,18 @@ 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:
Expand All @@ -95,11 +109,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)
Expand Down Expand Up @@ -169,6 +186,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)
Expand Down