Pretty output for cov and grad in json

This commit is contained in:
Simon Kuberski 2021-12-14 15:54:52 +01:00
parent b7bc508dcf
commit 76b483d730

View file

@ -49,6 +49,22 @@ def create_json_string(ol, description='', indent=1):
def __str__(self): def __str__(self):
return self.__repr__() return self.__repr__()
class Floatlist:
def __init__(self, li):
self.li = list(li)
def __repr__(self):
s = '['
for i in range(len(self.li)):
if i > 0:
s += ', '
s += '%1.15e' % (self.li[i])
s += ']'
return s
def __str__(self):
return self.__repr__()
def _gen_data_d_from_list(ol): def _gen_data_d_from_list(ol):
dl = [] dl = []
for name in ol[0].mc_names: for name in ol[0].mc_names:
@ -76,13 +92,14 @@ def create_json_string(ol, description='', indent=1):
ed = {} ed = {}
ed['id'] = name ed['id'] = name
ed['layout'] = str(ol[0].covobs[name].cov.shape).lstrip('(').rstrip(')').rstrip(',') ed['layout'] = str(ol[0].covobs[name].cov.shape).lstrip('(').rstrip(')').rstrip(',')
ed['cov'] = list(np.ravel(ol[0].covobs[name].cov)) ed['cov'] = Floatlist(np.ravel(ol[0].covobs[name].cov))
ncov = ol[0].covobs[name].cov.shape[0] ncov = ol[0].covobs[name].cov.shape[0]
ed['grad'] = [] ed['grad'] = []
for i in range(ncov): for i in range(ncov):
ed['grad'].append([]) ed['grad'].append([])
for o in ol: for o in ol:
ed['grad'][-1].append(o.covobs[name].grad[i][0]) ed['grad'][-1].append(o.covobs[name].grad[i][0])
ed['grad'][-1] = Floatlist(ed['grad'][-1])
dl.append(ed) dl.append(ed)
return dl return dl
@ -174,9 +191,9 @@ def create_json_string(ol, description='', indent=1):
deltas = False deltas = False
split = s.split('\n') split = s.split('\n')
for i in range(len(split)): for i in range(len(split)):
if '"deltas":' in split[i]: if '"deltas":' in split[i] or '"cov":' in split[i] or '"grad":' in split[i]:
deltas = True deltas = True
elif deltas: if deltas:
split[i] = split[i].replace('"[', '[').replace(']"', ']') split[i] = split[i].replace('"[', '[').replace(']"', ']')
if split[i][-1] == ']': if split[i][-1] == ']':
deltas = False deltas = False