Merge pull request #147 from fjosw/fix/non_overlapping_cnfgs

Fix non overlapping configurations
This commit is contained in:
Fabian Joswig 2023-02-02 15:15:04 +00:00 committed by GitHub
commit 2e66f0323a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 127 additions and 30 deletions

View file

@ -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
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.
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
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
If True, a dict containing auxiliary information and the data is returned.
If False, only the data is returned as list.
@ -457,7 +454,6 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
_check(dobs[4].tag == "ne")
ne = int(dobs[4].text.strip())
_check(dobs[5].tag == "nc")
nc = int(dobs[5].text.strip())
idld = {}
deltad = {}
@ -507,7 +503,11 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
for name in names:
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 = []
for i in range(len(mean)):
@ -516,11 +516,19 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
obs_names = []
for name in names:
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
deltas.append(deltad[name][i])
obs_names.append(name)
idl.append(idld[name])
repdeltas = []
repidl = []
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[-1]._value = mean[i]
_check(len(e_names) == ne)
@ -528,13 +536,10 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
cnames = list(covd.keys())
for i in range(len(res)):
new_covobs = {name: Covobs(0, covd[name], name, grad=gradd[name][i]) for name in cnames}
if noempty:
for name in cnames:
if np.all(new_covobs[name].grad == 0):
del new_covobs[name]
cnames_loc = list(new_covobs.keys())
else:
cnames_loc = cnames
for name in cnames:
if np.all(new_covobs[name].grad == 0):
del new_covobs[name]
cnames_loc = list(new_covobs.keys())
for name in cnames_loc:
res[i].names.append(name)
res[i].shape[name] = 1
@ -546,8 +551,6 @@ def import_dobs_string(content, noempty=False, full_output=False, separator_inse
res[i].tag = symbol[i]
if res[i].tag == 'None':
res[i].tag = None
if not noempty:
_check(len(res[0].covobs.keys()) == nc)
if full_output:
retd = {}
tool = file_origin.get('tool', None)
@ -568,7 +571,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=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.
Tags are not written or recovered automatically.
@ -577,9 +580,6 @@ def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insert
----------
fname : str
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
If True, a dict containing auxiliary information and the data is returned.
If False, only the data is returned as list.
@ -615,7 +615,7 @@ def read_dobs(fname, noempty=False, full_output=False, gz=True, separator_insert
with open(fname, 'r') as fin:
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):
@ -782,7 +782,7 @@ def create_dobs_string(obsl, name, spec='dobs v1.0', origin='', symbol=[], who=N
o = obsl[oi]
if repname in o.idl:
if counters[oi] < 0:
num = offsets[oi]
num = 0
if num == 0:
data += '0 '
else:
@ -798,7 +798,7 @@ 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:
num = offsets[oi]
num = 0
if num == 0:
data += '0 '
else:

View file

@ -1097,7 +1097,7 @@ def _expand_deltas_for_merge(deltas, idx, shape, new_idx):
ret = np.zeros(new_idx[-1] - new_idx[0] + 1)
for i in range(shape):
ret[idx[i] - new_idx[0]] = deltas[i]
return np.array([ret[new_idx[i] - new_idx[0]] for i in range(len(new_idx))])
return np.array([ret[new_idx[i] - new_idx[0]] for i in range(len(new_idx))]) * len(new_idx) / len(idx)
def derived_observable(func, data, array_mode=False, **kwargs):