def test_merge_obs(): my_obs1 = pe.Obs([np.random.rand(100)], ['t']) my_obs2 = pe.Obs([np.random.rand(100)], ['q'], idl=[range(1, 200, 2)]) merged = pe.merge_obs([my_obs1, my_obs2]) diff = merged - my_obs2 - my_obs1 assert diff == -(my_obs1.value + my_obs2.value) / 2 with pytest.raises(Exception): pe.merge_obs([my_obs1, my_obs1]) my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') with pytest.raises(Exception): pe.merge_obs([my_obs1, my_covobs])
def test_merge_obs_r_values(): a1 = pe.pseudo_Obs(1.1, .1, 'a|1') a2 = pe.pseudo_Obs(1.2, .1, 'a|2') a = pe.merge_obs([a1, a2]) assert np.isclose(a.r_values['a|1'], a1.value) assert np.isclose(a.r_values['a|2'], a2.value) assert np.isclose(a.value, np.mean([a1.value, a2.value]))
def test_jsonio(): o = pe.pseudo_Obs(1.0, .2, 'one') o2 = pe.pseudo_Obs(0.5, .1, 'two|r1') o3 = pe.pseudo_Obs(0.5, .1, 'two|r2') o4 = pe.merge_obs([o2, o3]) otag = 'This has been merged!' o4.tag = otag do = o - .2 * o4 co1 = pe.cov_Obs(1., .123, 'cov1') co3 = pe.cov_Obs(4., .1 ** 2, 'cov3') do *= co1 / co3 do.tag = {'A': 2} o5 = pe.pseudo_Obs(0.8, .1, 'two|r2') co2 = pe.cov_Obs([1, 2], [[.12, .004], [.004, .02]], 'cov2') o5 /= co2[0] o3 /= co2[1] o5.tag = 2 * otag testl = [o3, o5] arr = np.array([o3, o5]) mat = np.array([[pe.pseudo_Obs(1.0, .1, 'mat'), pe.pseudo_Obs(0.3, .1, 'mat')], [pe.pseudo_Obs(0.2, .1, 'mat'), pe.pseudo_Obs(2.0, .4, 'mat')]]) mat[0][1].tag = ['This', 'is', 2, None] mat[1][0].tag = '{testt}' mat[1][1].tag = '[tag]' tt1 = pe.Obs([np.random.rand(100)], ['t|r1'], idl=[range(2, 202, 2)]) tt2 = pe.Obs([np.random.rand(100)], ['t|r2'], idl=[range(2, 202, 2)]) tt3 = pe.Obs([np.random.rand(102)], ['qe']) tt = tt1 + tt2 + tt3 tt.tag = 'Test Obs: Ä' ol = [o4, do, testl, mat, arr, np.array([o]), np.array([tt, tt]), [tt, tt], co1, co2, np.array(co2), co1 / co2[0]] fname = 'test_rw' jsonio.dump_to_json(ol, fname, indent=1, description='[I am a tricky description]') rl = jsonio.load_json(fname) os.remove(fname + '.json.gz') for o, r in zip(ol, rl): assert np.all(o == r) for i in range(len(ol)): if isinstance(ol[i], pe.Obs): o = ol[i] - rl[i] assert(o.is_zero()) assert(ol[i].tag == rl[i].tag) or1 = np.ravel(ol[i]) or2 = np.ravel(rl[i]) for j in range(len(or1)): o = or1[j] - or2[j] assert(o.is_zero()) description = {'I': {'Am': {'a': 'nested dictionary!'}}} jsonio.dump_to_json(ol, fname, indent=0, gz=False, description=description) rl = jsonio.load_json(fname, gz=False, full_output=True) os.remove(fname + '.json') for o, r in zip(ol, rl['obsdata']): assert np.all(o == r) assert(description == rl['description'])