def test_relative_round(self): '''Test behavior of Variable.scale_values function''' # Some values are mapped onto themselves eigenvalues = [np.inf, 0, 'astring'] for prec in range(10): for eig in eigenvalues: self.assertTrue(relative_round(eig, prec) == eig) self.assertTrue(np.isnan(relative_round(np.nan, prec))) # Test format is # (original value, number of digits, rounded value) values = [ (1.23456, 1, 1.0), (12.3456, 1, 10), (12.3456, 3, 12.3), (12.3456, 5, 12.346), ] for original, digits, result in values: rounded = relative_round(original, digits) self.assertTrue(rounded == result)
def make_dict(self): """ Return all data in this Variable as a dictionary. The dictionary structure follows the hepdata conventions, so that dumping this dictionary to YAML will give a file that hepdata can read. Uncertainties associated to this Variable are also written into the dictionary. This function is intended to be called internally by the Submission object. Except for debugging purposes, no user should have to call this function. """ tmp = {} tmp["header"] = {"name": self.name} if self.units: tmp["header"]["units"] = self.units if self.qualifiers: tmp["qualifiers"] = self.qualifiers tmp["values"] = [] for i in range(len(self._values)): valuedict = defaultdict(list) if self.is_binned: valuedict["low"] = helpers.relative_round( self._values[i][0], self.digits) valuedict["high"] = helpers.relative_round( self._values[i][1], self.digits) 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 }) tmp["values"].append(valuedict) return tmp
def make_dict(self): """ Return all data in this Variable as a dictionary. The dictionary structure follows the hepdata conventions, so that dumping this dictionary to YAML will give a file that hepdata can read. Uncertainties associated to this Variable are also written into the dictionary. This function is intended to be called internally by the Submission object. Except for debugging purposes, no user should have to call this function. """ tmp = {} tmp["header"] = {"name": self.name} if self.units: tmp["header"]["units"] = self.units if self.qualifiers: tmp["qualifiers"] = self.qualifiers tmp["values"] = [] nonzero_uncs = helpers.any_uncertainties_nonzero( self.uncertainties, size=len(self._values) ) for i in range(len(self._values)): valuedict = defaultdict(list) if self.is_binned: valuedict["low"] = helpers.relative_round(self._values[i][0], self.digits) valuedict["high"] = helpers.relative_round(self._values[i][1], self.digits) else: valuedict["value"] = helpers.relative_round(self._values[i], self.digits) # 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