Changed some details regarding dobs implementation

This commit is contained in:
Simon Kuberski 2022-05-02 15:12:07 +02:00
parent 44b268fcf9
commit 729a61db61
2 changed files with 40 additions and 30 deletions

View file

@ -89,7 +89,7 @@ def create_pobs_string(obsl, name, spec='', origin='', symbol=[], enstag=None):
"""Export a list of Obs or structures containing Obs to an xml string
according to the Zeuthen pobs format.
Tags are not written or recovered automatically.
Tags are not written or recovered automatically. The separator | is removed from the replica names.
Parameters
----------
@ -175,7 +175,7 @@ def write_pobs(obsl, fname, name, spec='', origin='', symbol=[], enstag=None, gz
"""Export a list of Obs or structures containing Obs to a .xml.gz file
according to the Zeuthen pobs format.
Tags are not written or recovered automatically.
Tags are not written or recovered automatically. The separator | is removed from the replica names.
Parameters
----------
@ -195,7 +195,7 @@ def write_pobs(obsl, fname, name, spec='', origin='', symbol=[], enstag=None, gz
enstag : str
Enstag that is written to pobs. If None, the ensemble name is used.
gz : bool
If True, the output is a gzipped json. If False, the output is a json file.
If True, the output is a gzipped xml. If False, the output is an xml file.
"""
pobsstring = create_pobs_string(obsl, name, spec, origin, symbol, enstag)
@ -303,7 +303,8 @@ def read_pobs(fname, full_output=False, gz=True, separator_insertion=None):
separatior_insertion: str or int
str: replace all occurences of "separator_insertion" within the replica names
by "|%s" % (separator_insertion) when constructing the names of the replica.
int: Insert the separator "|" at position separator_insertion
int: Insert the separator "|" at the position given by separator_insertion.
None (default): Replica names remain unchanged.
"""
if not fname.endswith('.xml') and not fname.endswith('.gz'):
@ -383,7 +384,7 @@ def read_pobs(fname, full_output=False, gz=True, separator_insertion=None):
# 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=None):
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.
Tags are not written or recovered automatically.
@ -394,14 +395,17 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
XML string containing the data
noemtpy : bool
If True, ensembles with no contribution to the Obs are not included.
If False, ensembles are included as written in the file.
If False, ensembles are included as written in the file, possibly with vanishing entries.
full_output : bool
If True, a dict containing auxiliary information and the data is returned.
If False, only the data is returned as list.
separatior_insertion: str or int
separatior_insertion: str, int or bool
str: replace all occurences of "separator_insertion" within the replica names
by "|%s" % (separator_insertion) when constructing the names of the replica.
int: Insert the separator "|" at position separator_insertion
int: Insert the separator "|" at the position given by separator_insertion.
True (default): separator "|" is inserted after len(ensname), assuming that the
ensemble name is a prefix to the replica name.
None or False: No separator is inserted.
"""
root = et.fromstring(content)
@ -450,7 +454,9 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
R = int(dobs[k][1].text.strip())
for i in range(2, 2 + R):
deltas, rname, idx = _import_rdata(dobs[k][i])
if separator_insertion is None:
if separator_insertion is None or False:
pass
elif separator_insertion is True:
if rname.startswith(ename):
rname = rname[:len(ename)] + '|' + rname[len(ename):]
elif isinstance(separator_insertion, int):
@ -541,7 +547,7 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
return res
def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insertion=None):
def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insertion=True):
"""Import a list of Obs from an xml.gz file in the Zeuthen dobs format.
Tags are not written or recovered automatically.
@ -558,10 +564,13 @@ def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insert
If False, only the data is returned as list.
gz : bool
If True, assumes that data is gzipped. If False, assumes XML file.
separatior_insertion: str or int
separatior_insertion: str, int or bool
str: replace all occurences of "separator_insertion" within the replica names
by "|%s" % (separator_insertion) when constructing the names of the replica.
int: Insert the separator "|" at position separator_insertion
int: Insert the separator "|" at the position given by separator_insertion.
True (default): separator "|" is inserted after len(ensname), assuming that the
ensemble name is a prefix to the replica name.
None or False: No separator is inserted.
"""
if not fname.endswith('.xml') and not fname.endswith('.gz'):
@ -647,7 +656,7 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
"""Generate the string for the export of a list of Obs or structures containing Obs
to a .xml.gz file according to the Zeuthen dobs format.
Tags are not written or recovered automatically.
Tags are not written or recovered automatically. The separator |is removed from the replica names.
Parameters
----------
@ -820,7 +829,7 @@ def write_dobs(obsl, fname, name, spec='dobs v1.0', origin='', symbol=[], who=No
"""Export a list of Obs or structures containing Obs to a .xml.gz file
according to the Zeuthen dobs format.
Tags are not written or recovered automatically.
Tags are not written or recovered automatically. The separator | is removed from the replica names.
Parameters
----------

View file

@ -255,16 +255,16 @@ def test_renorm_deriv_of_corr(tmp_path):
def test_dobsio():
o = pe.pseudo_Obs(1.0, .2, 'one')
o1 = pe.pseudo_Obs(1.5, .2, 'one')
o = pe.pseudo_Obs(1.0, .2, 'one|r1')
o1 = pe.pseudo_Obs(1.5, .2, 'one|r1')
ol = [o, o1]
fname = 'test_rw'
dobsio.write_pobs(ol, fname, 'Testobs')
rl = dobsio.read_pobs(fname)
for o, r in zip(ol, rl):
assert np.all(o == r)
for i in range(len(ol)):
assert (ol[i] - rl[i].is_zero())
od = {
'obsdata': ol,
@ -276,8 +276,9 @@ def test_dobsio():
dobsio.write_pobs(ol, fname, od['name'], od['spec'], od['origin'], od['symbol'])
rd = dobsio.read_pobs(fname, full_output=True)
for o, r in zip(od['obsdata'], rd['obsdata']):
assert np.all(o == r)
for i in range(len(od['obsdata'])):
assert (od['obsdata'][i] - rd['obsdata'][i].is_zero())
assert(od['spec'] == rd['description']['spec'])
assert(od['origin'] == rd['description']['origin'])
assert(od['name'] == rd['description']['name'])
@ -286,14 +287,16 @@ def test_dobsio():
dobsio.write_dobs(ol, fname, 'Testobs')
rl = dobsio.read_dobs(fname)
for o, r in zip(ol, rl):
assert np.all(o == r)
for i in range(len(ol)):
assert (ol[i] - rl[i].is_zero())
dobsio.write_dobs(ol, fname, od['name'], od['spec'], od['origin'], od['symbol'])
rd = dobsio.read_dobs(fname, full_output=True)
os.remove(fname + '.xml.gz')
for i in range(len(od['obsdata'])):
assert (od['obsdata'][i] - rd['obsdata'][i].is_zero())
for o, r in zip(od['obsdata'], rd['obsdata']):
assert np.all(o == r)
assert(od['spec'] == rd['description']['spec'])
assert(od['origin'] == rd['description']['origin'])
assert(od['name'] == rd['description']['name'])
@ -312,12 +315,11 @@ def test_dobsio():
o5 = pe.pseudo_Obs(0.8, .1, 'two|r2')
co2 = pe.cov_Obs([1, 2], [[.12, .004], [.004, .02]], 'cov2')
o5 /= co2[0]
#o3 /= co2[1]
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)])
tt3 = pe.Obs([np.random.rand(102)], ['qe'])
tt3 = pe.Obs([np.random.rand(102)], ['qe|r1'])
tt = tt1 + tt2 + tt3
@ -330,12 +332,11 @@ def test_dobsio():
dobsio.write_dobs(ol, fname, 'TEST')
rl = dobsio.read_dobs(fname, noempty=True)
os.remove(fname + '.xml.gz')
[o.gamma_method() for o in rl]
#os.remove(fname + '.xml.gz')
for o, r in zip(ol, rl):
assert np.all(o == r)
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):