Skip to content

Commit

Permalink
Merge pull request #161 from HEPData/2021-04-19_no_zero_errs
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasAlbert committed May 6, 2021
2 parents 2458da4 + ca6dc2a commit 319a6f4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
69 changes: 41 additions & 28 deletions hepdata_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ def make_dict(self):

tmp["values"] = []

nonzero_uncs = helpers.any_uncertainties_nonzero(
self.uncertainties,
size=len(self._values)
)
for i in range(len(self._values)):
valuedict = defaultdict(list)

Expand All @@ -178,25 +182,36 @@ def make_dict(self):
else:
valuedict["value"] = helpers.relative_round(self._values[i],
self.digits)

for unc in self.uncertainties:
if unc.is_symmetric:
valuedict['errors'].append({
"symerror":
helpers.relative_round(unc.values[i], self.digits),
"label":
unc.label
})
else:
valuedict['errors'].append({
"asymerror": {
"minus":
helpers.relative_round(unc.values[i][0], self.digits),
"plus":
helpers.relative_round(unc.values[i][1], self.digits)
},
"label": unc.label
})
# An uncertainty entry is only appended
# if at least one of the uncertainties is not zero.
if nonzero_uncs[i]:
for unc in self.uncertainties:
if unc.is_symmetric:
valuedict['errors'].append({
"symerror":
helpers.relative_round(unc.values[i], self.digits),
"label":
unc.label
})
else:
valuedict['errors'].append({
"asymerror": {
"minus":
helpers.relative_round(unc.values[i][0], self.digits),
"plus":
helpers.relative_round(unc.values[i][1], self.digits)
},
"label": unc.label
})
elif self.uncertainties:
print(
"Warning: omitting 'errors' since all uncertainties " \
"are zero for bin {} of variable '{}'.".format(i+1, self.name)
)
print(
"Note that bins with zero content should preferably " \
"be omitted completely from the HEPData table."
)
tmp["values"].append(valuedict)
return tmp

Expand Down Expand Up @@ -526,15 +541,13 @@ def create_files(self, outdir="."):
self.copy_files(outdir)

# Put everything into a tarfile
tar = tarfile.open("submission.tar.gz", "w:gz")
for yaml_file in helpers.find_all_matching(outdir, "*.yaml"):
tar.add(yaml_file)
for png_file in helpers.find_all_matching(outdir, "*.png"):
tar.add(png_file)
for additional in self.files_to_copy:
tar.add(os.path.join(outdir, os.path.basename(additional)))

tar.close()
with tarfile.open("submission.tar.gz", "w:gz") as tar:
for yaml_file in helpers.find_all_matching(outdir, "*.yaml"):
tar.add(yaml_file)
for png_file in helpers.find_all_matching(outdir, "*.png"):
tar.add(png_file)
for additional in self.files_to_copy:
tar.add(os.path.join(outdir, os.path.basename(additional)))


class Uncertainty(object):
Expand Down
2 changes: 1 addition & 1 deletion hepdata_lib/c_file_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def cfile(self, cfile):
"CFileReader: Input file is not a .C file (name does not end in .C)!"
)
if check_file_existence(cfile):
self._cfile = open(cfile, "r")
self._cfile = open(cfile, "r") # pylint: disable=consider-using-with
elif six.PY2:
if isinstance(cfile, file): # pylint:disable=E0602
self._cfile = cfile
Expand Down
41 changes: 29 additions & 12 deletions hepdata_lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ def execute_command(command):
:param command: Command to execute.
:type command: string
"""
proc = subprocess.Popen(
command,

subprocess_args = dict(
args=command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True)
exit_code = proc.wait()
if exit_code == 127:
print("Command does not exist:", command)
return False
if exit_code != 0:
result = ""
for line in proc.stderr:
result = result + line
raise RuntimeError(result)
universal_newlines=True
)
with subprocess.Popen(**subprocess_args) as proc:
exit_code = proc.wait()
if exit_code == 127:
print("Command does not exist:", command)
return False
if exit_code != 0:
result = ""
for line in proc.stderr:
result = result + line
raise RuntimeError(result)
return True

def find_all_matching(path, pattern):
Expand Down Expand Up @@ -247,6 +250,20 @@ def check_file_size(path_to_file, upper_limit=None, lower_limit=None):
raise RuntimeError("File too small: '{0}'. Minimal allowed value is {1} \
MB.".format(path_to_file, lower_limit))


def any_uncertainties_nonzero(uncertainties, size):
"""
Return a mask of bins where any of the uncertainties is nonzero.
"""
nonzero = np.zeros(size, dtype=bool)

for unc in uncertainties:
if unc.is_symmetric:
nonzero = nonzero | (np.array(unc.values) != 0)
else:
nonzero = nonzero | np.any((np.array(unc.values) != 0),axis=1)
return nonzero

def sanitize_value(value):
"""
Handle conversion of input types for internal storage.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def test_yaml_output(self):
testtxt = ("dependent_variables:\n- header:\n name: Y\n values:\n" +
" - value: 0.12\n - value: 0.22\nindependent_variables:\n" +
"- header:\n name: X\n values:\n - value: 1.2\n - value: 2.2\n")
testfile = open("output/testtable.yaml", 'r')
testyaml = testfile.read()
with open("output/testtable.yaml", 'r') as testfile:
testyaml = testfile.read()

self.assertEqual(str(testyaml), testtxt)
self.addCleanup(os.remove, "submission.tar.gz")
Expand Down

0 comments on commit 319a6f4

Please sign in to comment.