From 860852b4d8631acf609f4e693c2d50d55eed58f6 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 9 Dec 2021 13:25:06 +0000 Subject: [PATCH 1/3] test: tests slightly extended to improve coverage. --- tests/covobs_test.py | 1 + tests/obs_test.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/covobs_test.py b/tests/covobs_test.py index beedb54c..513fb351 100644 --- a/tests/covobs_test.py +++ b/tests/covobs_test.py @@ -11,6 +11,7 @@ def test_covobs(): name = 'Covariance' co = pe.cov_Obs(val, cov, name) co.gamma_method() + co.details() assert (co.dvalue == np.sqrt(cov)) assert (co.value == val) diff --git a/tests/obs_test.py b/tests/obs_test.py index 8fbfa43d..7f7a459c 100644 --- a/tests/obs_test.py +++ b/tests/obs_test.py @@ -46,8 +46,9 @@ def test_Obs_exceptions(): my_obs.gamma_method(tau_exp=2.3) my_obs.gamma_method() my_obs.details() + my_obs.plot_rep_dist() - my_obs += pe.Obs([np.random.rand(6)], ['name2|r1']) + my_obs += pe.Obs([np.random.rand(6)], ['name2|r1'], idl=[[1, 3, 4, 5, 6, 7]]) my_obs += pe.Obs([np.random.rand(6)], ['name2|r2']) my_obs.gamma_method() my_obs.details() @@ -475,6 +476,7 @@ def test_irregular_error_propagation(): pe.Obs([np.random.rand(6)], ['t'], idl=[[4, 18, 27, 29, 57, 80]]), pe.Obs([np.random.rand(50)], ['t'], idl=[list(range(1, 26)) + list(range(50, 100, 2))])] for obs1 in obs_list: + obs1.details() for obs2 in obs_list: assert obs1 == (obs1 / obs2) * obs2 assert obs1 == (obs1 * obs2) / obs2 From b49c707127e0837a3ec78b65bf4b65062bd9a21d Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 9 Dec 2021 15:49:01 +0000 Subject: [PATCH 2/3] refactor: created function import_json_string --- pyerrors/input/json.py | 71 ++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/pyerrors/input/json.py b/pyerrors/input/json.py index ce2e2fed..deda0d9e 100644 --- a/pyerrors/input/json.py +++ b/pyerrors/input/json.py @@ -203,20 +203,18 @@ def dump_to_json(ol, fname, description='', indent=1, gz=True): fp.close() -def load_json(fname, verbose=True, gz=True, full_output=False): - """Import a list of Obs or structures containing Obs from a .json.gz file. +def import_json_string(json_string, verbose=True, full_output=False): + """Reconstruct a list of Obs or structures containing Obs from a json string. The following structures are supported: Obs, list, numpy.ndarray If the list contains only one element, it is unpacked from the list. Parameters ---------- - fname : str - Filename of the input file. + json_string : str + json string containing the data. verbose : bool Print additional information that was written to the file. - gz : bool - If True, assumes that data is gzipped. If False, assumes JSON file. full_output : bool If True, a dict containing auxiliary information and the data is returned. If False, only the data is returned. @@ -281,35 +279,22 @@ def load_json(fname, verbose=True, gz=True, full_output=False): ret[-1].tag = taglist[i] return np.reshape(ret, layout) - if not fname.endswith('.json') and not fname.endswith('.gz'): - fname += '.json' - if gz: - if not fname.endswith('.gz'): - fname += '.gz' - with gzip.open(fname, 'r') as fin: - d = json.loads(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()) - - prog = d.get('program', '') - version = d.get('version', '') - who = d.get('who', '') - date = d.get('date', '') - host = d.get('host', '') + 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', '') 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 = d.get('description', '') + description = json_string.get('description', '') if description and verbose: print() print('Description: ', description) - obsdata = d['obsdata'] + obsdata = json_string['obsdata'] ol = [] for io in obsdata: if io['type'] == 'Obs': @@ -335,3 +320,37 @@ def load_json(fname, verbose=True, gz=True, full_output=False): ol = ol[0] return ol + + +def load_json(fname, verbose=True, gz=True, full_output=False): + """Import a list of Obs or structures containing Obs from a .json.gz file. + + The following structures are supported: Obs, list, numpy.ndarray + If the list contains only one element, it is unpacked from the list. + + Parameters + ---------- + fname : str + Filename of the input file. + verbose : bool + Print additional information that was written to the file. + gz : bool + If True, assumes that data is gzipped. If False, assumes JSON file. + full_output : bool + If True, a dict containing auxiliary information and the data is returned. + If False, only the data is returned. + """ + if not fname.endswith('.json') and not fname.endswith('.gz'): + fname += '.json' + if gz: + if not fname.endswith('.gz'): + fname += '.gz' + with gzip.open(fname, 'r') as fin: + d = json.loads(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()) + + return import_json_string(d, verbose, full_output) From ca303c93e99a1512eccafbd7d75d774f5038d617 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Thu, 9 Dec 2021 15:59:53 +0000 Subject: [PATCH 3/3] test: tests for json io extended --- tests/io_test.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/io_test.py b/tests/io_test.py index 7bb80cc0..79b61c61 100644 --- a/tests/io_test.py +++ b/tests/io_test.py @@ -41,6 +41,9 @@ def test_jsonio(): os.remove(fname + '.json.gz') + for o, r in zip(ol, rl): + assert np.all(o == r) + for i in range(len(rl)): if isinstance(ol[i], pe.Obs): o = ol[i] - rl[i] @@ -57,6 +60,9 @@ def test_jsonio(): rl = jsonio.load_json(fname, gz=False, full_output=True) - assert(description == rl['description']) - os.remove(fname + '.json') + + for o, r in zip(ol, rl['obsdata']): + assert np.all(o == r) + + assert(description == rl['description'])