fix: import_json_string now correctly reconstructs obs from string, test

added
This commit is contained in:
Fabian Joswig 2021-12-10 10:37:50 +00:00
parent ca4548b105
commit 2ce73fb3f2
2 changed files with 19 additions and 10 deletions

View file

@ -279,22 +279,24 @@ def import_json_string(json_string, verbose=True, full_output=False):
ret[-1].tag = taglist[i]
return np.reshape(ret, layout)
prog = json_string.get('program', '')
version = json_string.get('version', '')
who = json_string.get('who', '')
date = json_string.get('date', '')
host = json_string.get('host', '')
json_dict = json.loads(json_string)
prog = json_dict.get('program', '')
version = json_dict.get('version', '')
who = json_dict.get('who', '')
date = json_dict.get('date', '')
host = json_dict.get('host', '')
if prog and verbose:
print('Data has been written using %s.' % (prog))
if version and verbose:
print('Format version %s' % (version))
if np.any([who, date, host] and verbose):
print('Written by %s on %s on host %s' % (who, date, host))
description = json_string.get('description', '')
description = json_dict.get('description', '')
if description and verbose:
print()
print('Description: ', description)
obsdata = json_string['obsdata']
obsdata = json_dict['obsdata']
ol = []
for io in obsdata:
if io['type'] == 'Obs':
@ -346,11 +348,11 @@ def load_json(fname, verbose=True, gz=True, full_output=False):
if not fname.endswith('.gz'):
fname += '.gz'
with gzip.open(fname, 'r') as fin:
d = json.loads(fin.read().decode('utf-8'))
d = fin.read().decode('utf-8')
else:
if fname.endswith('.gz'):
warnings.warn("Trying to read from %s without unzipping!" % fname, UserWarning)
with open(fname, 'r', encoding='utf-8') as fin:
d = json.loads(fin.read())
d = fin.read()
return import_json_string(d, verbose, full_output)

View file

@ -1,4 +1,4 @@
import pyerrors.obs as pe
import pyerrors as pe
import pyerrors.input.json as jsonio
import numpy as np
import os
@ -66,3 +66,10 @@ def test_jsonio():
assert np.all(o == r)
assert(description == rl['description'])
def test_json_string_reconstruction():
my_obs = pe.Obs([np.random.rand(100)], ['name'])
json_string = pe.input.json.create_json_string(my_obs)
reconstructed_obs = pe.input.json.import_json_string(json_string)
assert my_obs == reconstructed_obs