mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
dobs: Zeros are written if config is not part of the Obs, these are ignored on input
This commit is contained in:
parent
e34d49d88d
commit
dc5116aa2f
2 changed files with 27 additions and 26 deletions
|
@ -397,7 +397,7 @@ def read_pobs(fname, full_output=False, gz=True, separator_insertion=None):
|
||||||
|
|
||||||
|
|
||||||
# this is based on Mattia Bruno's implementation at https://github.com/mbruno46/pyobs/blob/master/pyobs/IO/xml.py
|
# 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):
|
def import_dobs_string(content, full_output=False, separator_insertion=True):
|
||||||
"""Import a list of Obs from a string in the Zeuthen dobs format.
|
"""Import a list of Obs from a string in the Zeuthen dobs format.
|
||||||
|
|
||||||
Tags are not written or recovered automatically.
|
Tags are not written or recovered automatically.
|
||||||
|
@ -406,9 +406,6 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
----------
|
----------
|
||||||
content : str
|
content : str
|
||||||
XML string containing the data
|
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, possibly with vanishing entries.
|
|
||||||
full_output : bool
|
full_output : bool
|
||||||
If True, a dict containing auxiliary information and the data is returned.
|
If True, a dict containing auxiliary information and the data is returned.
|
||||||
If False, only the data is returned as list.
|
If False, only the data is returned as list.
|
||||||
|
@ -507,7 +504,11 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
|
|
||||||
for name in names:
|
for name in names:
|
||||||
for i in range(len(deltad[name])):
|
for i in range(len(deltad[name])):
|
||||||
deltad[name][i] = np.array(deltad[name][i]) + mean[i]
|
tmp = np.zeros_like(deltad[name][i])
|
||||||
|
for j in range(len(deltad[name][i])):
|
||||||
|
if deltad[name][i][j] != 0.:
|
||||||
|
tmp[j] = deltad[name][i][j] + mean[i]
|
||||||
|
deltad[name][i] = tmp
|
||||||
|
|
||||||
res = []
|
res = []
|
||||||
for i in range(len(mean)):
|
for i in range(len(mean)):
|
||||||
|
@ -516,11 +517,19 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
obs_names = []
|
obs_names = []
|
||||||
for name in names:
|
for name in names:
|
||||||
h = np.unique(deltad[name][i])
|
h = np.unique(deltad[name][i])
|
||||||
if len(h) == 1 and np.all(h == mean[i]) and noempty:
|
if len(h) == 1 and np.all(h == mean[i]):
|
||||||
continue
|
continue
|
||||||
deltas.append(deltad[name][i])
|
repdeltas = []
|
||||||
obs_names.append(name)
|
repidl = []
|
||||||
idl.append(idld[name])
|
for j in range(len(deltad[name][i])):
|
||||||
|
if deltad[name][i][j] != 0.:
|
||||||
|
repdeltas.append(deltad[name][i][j])
|
||||||
|
repidl.append(idld[name][j])
|
||||||
|
if len(repdeltas) > 0:
|
||||||
|
obs_names.append(name)
|
||||||
|
deltas.append(repdeltas)
|
||||||
|
idl.append(repidl)
|
||||||
|
|
||||||
res.append(Obs(deltas, obs_names, idl=idl))
|
res.append(Obs(deltas, obs_names, idl=idl))
|
||||||
res[-1]._value = mean[i]
|
res[-1]._value = mean[i]
|
||||||
_check(len(e_names) == ne)
|
_check(len(e_names) == ne)
|
||||||
|
@ -528,13 +537,10 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
cnames = list(covd.keys())
|
cnames = list(covd.keys())
|
||||||
for i in range(len(res)):
|
for i in range(len(res)):
|
||||||
new_covobs = {name: Covobs(0, covd[name], name, grad=gradd[name][i]) for name in cnames}
|
new_covobs = {name: Covobs(0, covd[name], name, grad=gradd[name][i]) for name in cnames}
|
||||||
if noempty:
|
for name in cnames:
|
||||||
for name in cnames:
|
if np.all(new_covobs[name].grad == 0):
|
||||||
if np.all(new_covobs[name].grad == 0):
|
del new_covobs[name]
|
||||||
del new_covobs[name]
|
cnames_loc = list(new_covobs.keys())
|
||||||
cnames_loc = list(new_covobs.keys())
|
|
||||||
else:
|
|
||||||
cnames_loc = cnames
|
|
||||||
for name in cnames_loc:
|
for name in cnames_loc:
|
||||||
res[i].names.append(name)
|
res[i].names.append(name)
|
||||||
res[i].shape[name] = 1
|
res[i].shape[name] = 1
|
||||||
|
@ -546,8 +552,6 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
res[i].tag = symbol[i]
|
res[i].tag = symbol[i]
|
||||||
if res[i].tag == 'None':
|
if res[i].tag == 'None':
|
||||||
res[i].tag = None
|
res[i].tag = None
|
||||||
if not noempty:
|
|
||||||
_check(len(res[0].covobs.keys()) == nc)
|
|
||||||
if full_output:
|
if full_output:
|
||||||
retd = {}
|
retd = {}
|
||||||
tool = file_origin.get('tool', None)
|
tool = file_origin.get('tool', None)
|
||||||
|
@ -568,7 +572,7 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insertion=True):
|
def read_dobs(fname, full_output=False, gz=True, separator_insertion=True):
|
||||||
"""Import a list of Obs from an xml.gz file in the Zeuthen dobs format.
|
"""Import a list of Obs from an xml.gz file in the Zeuthen dobs format.
|
||||||
|
|
||||||
Tags are not written or recovered automatically.
|
Tags are not written or recovered automatically.
|
||||||
|
@ -577,9 +581,6 @@ def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insert
|
||||||
----------
|
----------
|
||||||
fname : str
|
fname : str
|
||||||
Filename of the input file.
|
Filename of the input file.
|
||||||
noemtpy : bool
|
|
||||||
If True, ensembles with no contribution to the Obs are not included.
|
|
||||||
If False, ensembles are included as written in the file.
|
|
||||||
full_output : bool
|
full_output : bool
|
||||||
If True, a dict containing auxiliary information and the data is returned.
|
If True, a dict containing auxiliary information and the data is returned.
|
||||||
If False, only the data is returned as list.
|
If False, only the data is returned as list.
|
||||||
|
@ -615,7 +616,7 @@ def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insert
|
||||||
with open(fname, 'r') as fin:
|
with open(fname, 'r') as fin:
|
||||||
content = fin.read()
|
content = fin.read()
|
||||||
|
|
||||||
return import_dobs_string(content, noempty, full_output, separator_insertion=separator_insertion)
|
return import_dobs_string(content, full_output, separator_insertion=separator_insertion)
|
||||||
|
|
||||||
|
|
||||||
def _dobsdict_to_xmlstring(d):
|
def _dobsdict_to_xmlstring(d):
|
||||||
|
@ -782,7 +783,7 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
|
||||||
o = obsl[oi]
|
o = obsl[oi]
|
||||||
if repname in o.idl:
|
if repname in o.idl:
|
||||||
if counters[oi] < 0:
|
if counters[oi] < 0:
|
||||||
num = offsets[oi]
|
num = 0
|
||||||
if num == 0:
|
if num == 0:
|
||||||
data += '0 '
|
data += '0 '
|
||||||
else:
|
else:
|
||||||
|
@ -798,7 +799,7 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
|
||||||
if counters[oi] >= len(o.idl[repname]):
|
if counters[oi] >= len(o.idl[repname]):
|
||||||
counters[oi] = -1
|
counters[oi] = -1
|
||||||
else:
|
else:
|
||||||
num = offsets[oi]
|
num = 0
|
||||||
if num == 0:
|
if num == 0:
|
||||||
data += '0 '
|
data += '0 '
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -339,7 +339,7 @@ def test_dobsio():
|
||||||
|
|
||||||
dobsio.write_dobs(ol, fname, 'TEST')
|
dobsio.write_dobs(ol, fname, 'TEST')
|
||||||
|
|
||||||
rl = dobsio.read_dobs(fname, noempty=True)
|
rl = dobsio.read_dobs(fname)
|
||||||
os.remove(fname + '.xml.gz')
|
os.remove(fname + '.xml.gz')
|
||||||
[o.gamma_method() for o in rl]
|
[o.gamma_method() for o in rl]
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue