bug: Fixed dobs I/O, similarly to JSON I/O

This commit is contained in:
Simon Kuberski 2022-05-19 19:18:53 +02:00
parent a799bd1f7c
commit d7a8721ef6
2 changed files with 22 additions and 15 deletions

View file

@ -379,10 +379,6 @@ def read_pobs(fname, full_output=False, gz=True, separator_insertion=None):
return res
# Reading (and writing) dobs is not yet working properly:
# we have to loop over root[2:] because each entry is a dobs
# But maybe this is just a problem with Ben's implementation
# this is based on Mattia Bruno's implementation at https://github.com/mbruno46/pyobs/blob/master/pyobs/IO/xml.py
def import_dobs_string(content, noempty=False, full_output=False, separator_insertion=True):
"""Import a list of Obs from a string in the Zeuthen dobs format.
@ -501,7 +497,7 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
obs_names.append(name)
idl.append(idld[name])
res.append(Obs(deltas, obs_names, idl=idl))
print(mean, 'vs', res)
res[-1]._value = mean[i]
_check(len(e_names) == ne)
cnames = list(covd.keys())
@ -743,16 +739,22 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
ad['layout'] = layout
data = ''
counters = [0 for o in obsl]
offsets = [o.r_values[repname] - o.value if repname in o.r_values else 0 for o in obsl]
print(repname, offsets)
for ci in idx:
data += '%d ' % ci
for oi in range(len(obsl)):
o = obsl[oi]
if repname in o.idl:
if counters[oi] < 0:
data += '0 '
num = offsets[oi]
if num == 0:
data += '0 '
else:
data += '%1.16e ' % (num)
continue
if o.idl[repname][counters[oi]] == ci:
num = o.deltas[repname][counters[oi]]
num = o.deltas[repname][counters[oi]] + offsets[oi]
if num == 0:
data += '0 '
else:
@ -761,7 +763,11 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
if counters[oi] >= len(o.idl[repname]):
counters[oi] = -1
else:
data += '0 '
num = offsets[oi]
if num == 0:
data += '0 '
else:
data += '%1.16e ' % (num)
else:
data += '0 '
data += '\n'

View file

@ -323,15 +323,16 @@ def test_dobsio():
o5 /= co2[0]
o5.tag = 2 * otag
tt1 = pe.Obs([np.random.rand(100)], ['t|r1'], idl=[range(2, 202, 2)])
tt2 = pe.Obs([np.random.rand(100)], ['t|r2'], idl=[range(2, 202, 2)])
tt1 = pe.Obs([np.random.rand(100), np.random.rand(100)], ['t|r1', 't|r2'], idl=[range(2, 202, 2), range(22, 222, 2)])
tt3 = pe.Obs([np.random.rand(102)], ['qe|r1'])
tt = tt1 + tt2 + tt3
tt = tt1 + tt3
tt.tag = 'Test Obs: Ä'
ol = [o2, o3, o4, do, o5, tt]
tt4 = pe.Obs([np.random.rand(100), np.random.rand(100)], ['t|r1', 't|r2'], idl=[range(1, 101, 1), range(2, 202, 2)])
ol = [o2, o3, o4, do, o5, tt, tt4, np.log(tt4 / o5**2), np.exp(o5 + np.log(co3 / tt3 + o4) / tt)]
print(ol)
fname = 'test_rw'
@ -341,9 +342,6 @@ def test_dobsio():
os.remove(fname + '.xml.gz')
[o.gamma_method() for o in rl]
for i in range(len(ol)):
assert (ol[i] - rl[i].is_zero())
for i in range(len(ol)):
if isinstance(ol[i], pe.Obs):
o = ol[i] - rl[i]
@ -353,3 +351,6 @@ def test_dobsio():
for j in range(len(or1)):
o = or1[j] - or2[j]
assert(o.is_zero())
if isinstance(ol[i], pe.Obs):
for name in ol[i].r_values:
assert(np.isclose(ol[i].r_values[name], rl[i].r_values[name]))