Skip to content

Commit 79145af

Browse files
committed
Add to_dict and test it
1 parent 799d927 commit 79145af

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/dmu/stats/measurement.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,18 @@ def __contains__(self, variable : str) -> bool:
5454
'''
5555

5656
return variable in self.data
57+
# ----------------------
58+
def to_dict(self) -> dict[str, float]:
59+
'''
60+
Returns
61+
-------------
62+
Dictionary mapping names of quatities to numerical values.
63+
E.g. given quantity x, entries x and x_error are created
64+
'''
65+
result = dict()
66+
for name, (value, error) in self.data.items():
67+
result[name ] = value
68+
result[f'{name}_error'] = error
69+
70+
return result
5771
# ----------------------------------------------

tests/stats/test_measurement.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,16 @@ def test_print():
2929

3030
ms.__repr__()
3131
ms.__str__()
32+
# ----------------------
33+
def test_to_dict():
34+
'''
35+
Tests to_dict method
36+
'''
37+
data = {'a' : (1., 1.), 'b' : (2., 1.)}
38+
39+
ms = Measurement(data=data)
40+
res = ms.to_dict()
41+
42+
assert res == {
43+
'a' : 1., 'a_error' : 1.,
44+
'b' : 2., 'b_error' : 1.}

0 commit comments

Comments
 (0)