mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-03-15 06:40:24 +01:00
Feat/str format (#170)
* feat: _format_uncertainty now accepts a significance. * feat: __format__ now accepts significance. * feat: explicit + sign added to format. * fix: - option removed from format specifier. * feat: space format added.
This commit is contained in:
parent
a214465413
commit
be061fdc23
2 changed files with 31 additions and 4 deletions
|
@ -695,6 +695,15 @@ class Obs:
|
|||
def __str__(self):
|
||||
return _format_uncertainty(self.value, self._dvalue)
|
||||
|
||||
def __format__(self, format_type):
|
||||
my_str = _format_uncertainty(self.value, self._dvalue,
|
||||
significance=int(float(format_type.replace("+", "").replace("-", ""))))
|
||||
for char in ["+", " "]:
|
||||
if format_type.startswith(char):
|
||||
if my_str[0] != "-":
|
||||
my_str = char + my_str
|
||||
return my_str
|
||||
|
||||
def __hash__(self):
|
||||
hash_tuple = (np.array([self.value]).astype(np.float32).data.tobytes(),)
|
||||
hash_tuple += tuple([o.astype(np.float32).data.tobytes() for o in self.deltas.values()])
|
||||
|
@ -969,17 +978,21 @@ class CObs:
|
|||
return 'CObs[' + str(self) + ']'
|
||||
|
||||
|
||||
def _format_uncertainty(value, dvalue):
|
||||
def _format_uncertainty(value, dvalue, significance=2):
|
||||
"""Creates a string of a value and its error in paranthesis notation, e.g., 13.02(45)"""
|
||||
if dvalue == 0.0:
|
||||
return str(value)
|
||||
if not isinstance(significance, int):
|
||||
raise TypeError("significance needs to be an integer.")
|
||||
if significance < 1:
|
||||
raise ValueError("significance needs to be larger than zero.")
|
||||
fexp = np.floor(np.log10(dvalue))
|
||||
if fexp < 0.0:
|
||||
return '{:{form}}({:2.0f})'.format(value, dvalue * 10 ** (-fexp + 1), form='.' + str(-int(fexp) + 1) + 'f')
|
||||
return '{:{form}}({:1.0f})'.format(value, dvalue * 10 ** (-fexp + significance - 1), form='.' + str(-int(fexp) + significance - 1) + 'f')
|
||||
elif fexp == 0.0:
|
||||
return '{:.1f}({:1.1f})'.format(value, dvalue)
|
||||
return f"{value:.{significance - 1}f}({dvalue:1.{significance - 1}f})"
|
||||
else:
|
||||
return '{:.0f}({:2.0f})'.format(value, dvalue)
|
||||
return f"{value:.{max(0, int(significance - fexp - 1))}f}({dvalue:2.{max(0, int(significance - fexp - 1))}f})"
|
||||
|
||||
|
||||
def _expand_deltas(deltas, idx, shape, gapsize):
|
||||
|
|
|
@ -1213,3 +1213,17 @@ def test_nan_obs():
|
|||
o.idl['test'] = [1, 5] + list(range(7, 2002, 2))
|
||||
no = np.NaN * o
|
||||
no.gamma_method()
|
||||
|
||||
|
||||
def test_format_uncertainty():
|
||||
assert pe.obs._format_uncertainty(0.548, 0.248976, 4) == '0.5480(2490)'
|
||||
assert pe.obs._format_uncertainty(0.548, 2.48497, 2) == '0.5(2.5)'
|
||||
assert pe.obs._format_uncertainty(0.548, 2.48497, 4) == '0.548(2.485)'
|
||||
assert pe.obs._format_uncertainty(0.548, 20078.3, 9) == '0.5480(20078.3000)'
|
||||
|
||||
|
||||
def test_format():
|
||||
o1 = pe.pseudo_Obs(0.348, 0.0123, "test")
|
||||
assert o1.__format__("+3") == '+0.3480(123)'
|
||||
assert o1.__format__("+2") == '+0.348(12)'
|
||||
assert o1.__format__(" 2") == ' 0.348(12)'
|
||||
|
|
Loading…
Add table
Reference in a new issue