def test_multi_dot(): for dim in [4, 6]: my_list = [] length = 1000 + np.random.randint(200) for i in range(dim**2): my_list.append( pe.Obs([np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2'])) my_array = pe.cov_Obs(1.0, 0.002, 'cov') * np.array(my_list).reshape( (dim, dim)) tt = pe.linalg.matmul( my_array, my_array, my_array, my_array) - my_array @ my_array @ my_array @ my_array for t, e in np.ndenumerate(tt): assert e.is_zero(), t my_list = [] length = 1000 + np.random.randint(200) for i in range(dim**2): my_list.append( pe.CObs( pe.Obs( [np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']), pe.Obs( [np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2']))) my_array = np.array(my_list).reshape( (dim, dim)) * pe.cov_Obs(1.0, 0.002, 'cov') tt = pe.linalg.matmul( my_array, my_array, my_array, my_array) - my_array @ my_array @ my_array @ my_array for t, e in np.ndenumerate(tt): assert e.is_zero(), t
def test_matmul_irregular_histories(): dim = 2 length = 500 standard_array = [] for i in range(dim**2): standard_array.append( pe.Obs([np.random.normal(1.1, 0.2, length)], ['ens1'])) standard_matrix = np.array(standard_array).reshape( (dim, dim)) * pe.cov_Obs(1.0, 0.002, 'cov') * pe.pseudo_Obs( 0.1, 0.002, 'qr') for idl in [range(1, 501, 2), range(250, 273), [2, 8, 19, 20, 78]]: irregular_array = [] for i in range(dim**2): irregular_array.append( pe.Obs([np.random.normal(1.1, 0.2, len(idl))], ['ens1'], idl=[idl])) irregular_matrix = np.array(irregular_array).reshape( (dim, dim)) * pe.cov_Obs( [1.0, 1.0], [[0.001, 0.0001], [0.0001, 0.002]], 'norm')[0] t1 = standard_matrix @ irregular_matrix t2 = pe.linalg.matmul(standard_matrix, irregular_matrix) assert np.all([o.is_zero() for o in (t1 - t2).ravel()]) assert np.all([o.is_merged for o in t1.ravel()]) assert np.all([o.is_merged for o in t2.ravel()])
def test_covobs_name_collision(): covobs = pe.cov_Obs(0.5, 0.002, 'test') my_obs = pe.pseudo_Obs(2.3, 0.2, 'test') with pytest.raises(Exception): summed_obs = my_obs + covobs covobs2 = pe.cov_Obs(0.3, 0.001, 'test') with pytest.raises(Exception): summed_obs = covobs + covobs2
def test_covobs_exceptions(): with pytest.raises(Exception): covobs = pe.cov_Obs(0.1, [[0.1, 0.2], [0.1, 0.2]], 'test') with pytest.raises(Exception): covobs = pe.cov_Obs(0.1, np.array([[0.1, 0.2], [0.1, 0.2]]), 'test') with pytest.raises(Exception): covobs = pe.cov_Obs([0.5, 0.1], np.array([[2, 1, 3], [1, 2, 3]]), 'test') with pytest.raises(Exception): covobs = pe.cov_Obs([0.5, 0.1], np.random.random((2, 2, 2)), 'test')
def test_covobs_overloading(): covobs = pe.cov_Obs([0.5, 0.5], np.array([[0.02, 0.02], [0.02, 0.02]]), 'test') assert (covobs[0] / covobs[1]) == 1 assert (covobs[0] - covobs[1]) == 0 my_obs = pe.pseudo_Obs(2.3, 0.2, 'obs') assert (my_obs * covobs[0] / covobs[1]) == my_obs covobs = pe.cov_Obs(0.0, 0.3, 'test') assert not covobs.is_zero()
def test_correlate(): my_obs1 = pe.Obs([np.random.rand(100)], ['t']) my_obs2 = pe.Obs([np.random.rand(100)], ['t']) corr1 = pe.correlate(my_obs1, my_obs2) corr2 = pe.correlate(my_obs2, my_obs1) assert corr1 == corr2 my_obs3 = pe.Obs([np.random.rand(100)], ['t'], idl=[range(2, 102)]) with pytest.raises(Exception): pe.correlate(my_obs1, my_obs3) my_obs4 = pe.Obs([np.random.rand(99)], ['t']) with pytest.raises(Exception): pe.correlate(my_obs1, my_obs4) my_obs5 = pe.Obs([np.random.rand(100)], ['t'], idl=[range(5, 505, 5)]) my_obs6 = pe.Obs([np.random.rand(100)], ['t'], idl=[range(5, 505, 5)]) corr3 = pe.correlate(my_obs5, my_obs6) assert my_obs5.idl == corr3.idl my_new_obs = pe.Obs([np.random.rand(100)], ['q3']) with pytest.raises(Exception): pe.correlate(my_obs1, my_new_obs) my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') with pytest.raises(Exception): pe.correlate(my_covobs, my_covobs) r_obs = pe.reweight(my_obs1, [my_obs1])[0] with pytest.warns(RuntimeWarning): pe.correlate(r_obs, r_obs)
def test_reweighting(): my_obs = pe.Obs([np.random.rand(1000)], ['t']) assert not my_obs.reweighted r_obs = pe.reweight(my_obs, [my_obs]) assert r_obs[0].reweighted r_obs2 = r_obs[0] * my_obs assert r_obs2.reweighted my_irregular_obs = pe.Obs([np.random.rand(500)], ['t'], idl=[range(1, 1001, 2)]) assert not my_irregular_obs.reweighted r_obs = pe.reweight(my_obs, [my_irregular_obs], all_configs=True) r_obs = pe.reweight(my_obs, [my_irregular_obs], all_configs=False) r_obs = pe.reweight(my_obs, [my_obs]) assert r_obs[0].reweighted r_obs2 = r_obs[0] * my_obs assert r_obs2.reweighted my_covobs = pe.cov_Obs(1.0, 0.003, 'cov') with pytest.raises(Exception): pe.reweight(my_obs, [my_covobs]) my_obs2 = pe.Obs([np.random.rand(1000)], ['t2']) with pytest.raises(Exception): pe.reweight(my_obs, [my_obs + my_obs2]) with pytest.raises(Exception): pe.reweight(my_irregular_obs, [my_obs])
def test_renorm_deriv_of_corr(tmp_path): c = pe.Corr([pe.pseudo_Obs(i, .1, 'test') for i in range(10)]) c *= pe.cov_Obs(1., .1, '#ren') c = c.deriv() pe.input.json.dump_to_json(c, (tmp_path / 'test').as_posix()) recover = pe.input.json.load_json((tmp_path / 'test').as_posix()) assert np.all([o == 0 for o in (c - recover)[1:-1]])
def test_thin(): c = pe.Corr([pe.pseudo_Obs(i, .1, 'test') for i in range(10)]) c *= pe.cov_Obs(1., .1, '#ren') thin = c.thin() thin.gamma_method() thin.fit(lambda a, x: a[0] * x) c.thin(offset=1) c.thin(3, offset=1)
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_covobs(): val = 1.123124 cov = .243423 name = 'Covariance' co = pe.cov_Obs(val, cov, name) co.gamma_method() co.details() assert (co.dvalue == np.sqrt(cov)) assert (co.value == val) do = 2 * co assert (do.covobs[name].grad[0] == 2) do = co * co assert (do.covobs[name].grad[0] == 2 * val) assert np.array_equal(do.covobs[name].cov, co.covobs[name].cov) pi = [16.7457, -19.0475] cov = [[3.49591, -6.07560], [-6.07560, 10.5834]] cl = pe.cov_Obs(pi, cov, 'rAP') pl = pe.misc.gen_correlated_data(pi, np.asarray(cov), 'rAPpseudo') def rAP(p, g0sq): return -0.0010666 * g0sq * (1 + np.exp(p[0] + p[1] / g0sq)) for g0sq in [1, 1.5, 1.8]: oc = rAP(cl, g0sq) oc.gamma_method() op = rAP(pl, g0sq) op.gamma_method() assert(np.isclose(oc.value, op.value, rtol=1e-14, atol=1e-14)) [o.gamma_method() for o in cl] assert(pe.covariance(cl[0], cl[1]) == cov[0][1]) assert(pe.covariance(cl[0], cl[1]) == cov[1][0]) do = cl[0] * cl[1] assert(np.array_equal(do.covobs['rAP'].grad, np.transpose([pi[1], pi[0]]).reshape(2, 1)))
def test_matmul(): for dim in [4, 6]: for const in [ 1, pe.cov_Obs([1.0, 1.0], [[0.001, 0.0001], [0.0001, 0.002]], 'norm')[1] ]: my_list = [] length = 100 + np.random.randint(200) for i in range(dim**2): my_list.append( pe.Obs( [np.random.rand(length), np.random.rand(length + 1)], ['t1', 't2'])) my_array = const * np.array(my_list).reshape((dim, dim)) tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array for t, e in np.ndenumerate(tt): assert e.is_zero(), t my_list = [] length = 100 + np.random.randint(200) for i in range(dim**2): my_list.append( pe.CObs( pe.Obs([ np.random.rand(length), np.random.rand(length + 1) ], ['t1', 't2']), pe.Obs([ np.random.rand(length), np.random.rand(length + 1) ], ['t1', 't2']))) my_array = np.array(my_list).reshape((dim, dim)) * const tt = pe.linalg.matmul(my_array, my_array) - my_array @ my_array for t, e in np.ndenumerate(tt): assert e.is_zero(), t
def test_irregular_matrix_inverse(): dim = 3 length = 500 for idl in [ range(8, 508, 10), range(250, 273), [2, 8, 19, 20, 78, 99, 828, 10548979] ]: irregular_array = [] for i in range(dim**2): irregular_array.append( pe.Obs([ np.random.normal(1.1, 0.2, len(idl)), np.random.normal(0.25, 0.1, 10) ], ['ens1', 'ens2'], idl=[idl, range(1, 11)])) irregular_matrix = np.array(irregular_array).reshape( (dim, dim)) * pe.cov_Obs(1.0, 0.002, 'cov') * pe.pseudo_Obs( 1.0, 0.002, 'ens2|r23') invertible_irregular_matrix = np.identity( dim) + irregular_matrix @ irregular_matrix.T inverse = pe.linalg.inv(invertible_irregular_matrix) assert np.allclose( np.linalg.inv( np.vectorize(lambda x: x.value)(invertible_irregular_matrix)) - np.vectorize(lambda x: x.value)(inverse), 0.0) check1 = pe.linalg.matmul(invertible_irregular_matrix, inverse) assert np.all( [o.is_zero() for o in (check1 - np.identity(dim)).ravel()]) check2 = invertible_irregular_matrix @ inverse assert np.all( [o.is_zero() for o in (check2 - np.identity(dim)).ravel()])
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'])
def test_least_squares(): dim = 10 + int(30 * np.random.rand()) x = np.arange(dim) y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim) yerr = 0.1 + 0.1 * np.random.rand(dim) oy = [] for i, item in enumerate(x): oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i))) def f(x, a, b): return a * np.exp(-b * x) popt, pcov = scipy.optimize.curve_fit(f, x, y, sigma=[o.dvalue for o in oy], absolute_sigma=True) def func(a, x): y = a[0] * np.exp(-a[1] * x) return y out = pe.least_squares(x, oy, func, expected_chisquare=True, resplot=True, qqplot=True) beta = out.fit_parameters str(out) repr(out) len(out) for i in range(2): beta[i].gamma_method(S=1.0) assert math.isclose(beta[i].value, popt[i], abs_tol=1e-5) assert math.isclose(pcov[i, i], beta[i].dvalue**2, abs_tol=1e-3) assert math.isclose(pe.covariance(beta[0], beta[1]), pcov[0, 1], abs_tol=1e-3) chi2_pyerrors = np.sum( ((f(x, *[o.value for o in beta]) - y) / yerr)**2) / (len(x) - 2) chi2_scipy = np.sum(((f(x, *popt) - y) / yerr)**2) / (len(x) - 2) assert math.isclose(chi2_pyerrors, chi2_scipy, abs_tol=1e-10) out = pe.least_squares(x, oy, func, const_par=[beta[1]]) assert ((out.fit_parameters[0] - beta[0]).is_zero()) assert ((out.fit_parameters[1] - beta[1]).is_zero()) oyc = [] for i, item in enumerate(x): oyc.append(pe.cov_Obs(y[i], yerr[i]**2, 'cov' + str(i))) outc = pe.least_squares(x, oyc, func) betac = outc.fit_parameters for i in range(2): betac[i].gamma_method(S=1.0) assert math.isclose(betac[i].value, popt[i], abs_tol=1e-5) assert math.isclose(pcov[i, i], betac[i].dvalue**2, abs_tol=1e-3) assert math.isclose(pe.covariance(betac[0], betac[1]), pcov[0, 1], abs_tol=1e-3)
def test_covobs_init(): covobs = pe.cov_Obs(0.5, 0.002, 'test') covobs = pe.cov_Obs([1, 2], [0.1, 0.2], 'test') covobs = pe.cov_Obs([1, 2], np.array([0.1, 0.2]), 'test') covobs = pe.cov_Obs([1, 2], [[0.1, 0.2], [0.1, 0.2]], 'test') covobs = pe.cov_Obs([1, 2], np.array([[0.1, 0.2], [0.1, 0.2]]), 'test')
def test_covobs_replica_separator(): with pytest.raises(Exception): covobs = pe.cov_Obs(0.5, 0.002, 'test|r2')
def test_total_least_squares(): dim = 10 + int(30 * np.random.rand()) x = np.arange(dim) + np.random.normal(0.0, 0.15, dim) xerr = 0.1 + 0.1 * np.random.rand(dim) y = 2 * np.exp(-0.06 * x) + np.random.normal(0.0, 0.15, dim) yerr = 0.1 + 0.1 * np.random.rand(dim) ox = [] for i, item in enumerate(x): ox.append(pe.pseudo_Obs(x[i], xerr[i], str(i))) oy = [] for i, item in enumerate(x): oy.append(pe.pseudo_Obs(y[i], yerr[i], str(i))) def f(x, a, b): return a * np.exp(-b * x) def func(a, x): y = a[0] * np.exp(-a[1] * x) return y data = RealData([o.value for o in ox], [o.value for o in oy], sx=[o.dvalue for o in ox], sy=[o.dvalue for o in oy]) model = Model(func) odr = ODR(data, model, [0, 0], partol=np.finfo(np.float64).eps) odr.set_job(fit_type=0, deriv=1) output = odr.run() out = pe.total_least_squares(ox, oy, func, expected_chisquare=True) beta = out.fit_parameters str(out) repr(out) len(out) for i in range(2): beta[i].gamma_method(S=1.0) assert math.isclose(beta[i].value, output.beta[i], rel_tol=1e-5) assert math.isclose( output.cov_beta[i, i], beta[i].dvalue**2, rel_tol=2.5e-1), str( output.cov_beta[i, i]) + ' ' + str(beta[i].dvalue**2) assert math.isclose(pe.covariance(beta[0], beta[1]), output.cov_beta[0, 1], rel_tol=2.5e-1) out = pe.total_least_squares(ox, oy, func, const_par=[beta[1]]) diff = out.fit_parameters[0] - beta[0] assert (diff / beta[0] < 1e-3 * beta[0].dvalue) assert ((out.fit_parameters[1] - beta[1]).is_zero()) oxc = [] for i, item in enumerate(x): oxc.append(pe.cov_Obs(x[i], xerr[i]**2, 'covx' + str(i))) oyc = [] for i, item in enumerate(x): oyc.append(pe.cov_Obs(y[i], yerr[i]**2, 'covy' + str(i))) outc = pe.total_least_squares(oxc, oyc, func) betac = outc.fit_parameters for i in range(2): betac[i].gamma_method(S=1.0) assert math.isclose(betac[i].value, output.beta[i], rel_tol=1e-3) assert math.isclose( output.cov_beta[i, i], betac[i].dvalue**2, rel_tol=2.5e-1), str( output.cov_beta[i, i]) + ' ' + str(betac[i].dvalue**2) assert math.isclose(pe.covariance(betac[0], betac[1]), output.cov_beta[0, 1], rel_tol=2.5e-1) outc = pe.total_least_squares(oxc, oyc, func, const_par=[betac[1]]) diffc = outc.fit_parameters[0] - betac[0] assert (diffc / betac[0] < 1e-3 * betac[0].dvalue) assert ((outc.fit_parameters[1] - betac[1]).is_zero()) outc = pe.total_least_squares(oxc, oy, func) betac = outc.fit_parameters for i in range(2): betac[i].gamma_method(S=1.0) assert math.isclose(betac[i].value, output.beta[i], rel_tol=1e-3) assert math.isclose( output.cov_beta[i, i], betac[i].dvalue**2, rel_tol=2.5e-1), str( output.cov_beta[i, i]) + ' ' + str(betac[i].dvalue**2) assert math.isclose(pe.covariance(betac[0], betac[1]), output.cov_beta[0, 1], rel_tol=2.5e-1) outc = pe.total_least_squares(oxc, oy, func, const_par=[betac[1]]) diffc = outc.fit_parameters[0] - betac[0] assert (diffc / betac[0] < 1e-3 * betac[0].dvalue) assert ((outc.fit_parameters[1] - betac[1]).is_zero())
def test_json_dict_io(): def check_dict_equality(d1, d2): def dict_check_obs(d1, d2): for k, v in d1.items(): if isinstance(v, dict): v = dict_check_obs(v, d2[k]) elif isinstance(v, list) and all( [isinstance(o, pe.Obs) for o in v]): for i in range(len(v)): assert ((v[i] - d2[k][i]).is_zero()) elif isinstance(v, list): v = list_check_obs(v, d2[k]) elif isinstance(v, pe.Obs): assert ((v - d2[k]).is_zero()) elif isinstance(v, pe.Corr): for i in range(v.T): assert ((v[i] - d2[k][i]).is_zero()) elif isinstance(v, np.ndarray): a1 = np.ravel(v) a2 = np.ravel(d2[k]) for i in range(len(a1)): assert ((a1[i] - a2[i]).is_zero()) def list_check_obs(l1, l2): for ei in range(len(l1)): e = l1[ei] if isinstance(e, list): e = list_check_obs(e, l2[ei]) elif isinstance(e, list) and all( [isinstance(o, pe.Obs) for o in e]): for i in range(len(e)): assert ((e[i] - l2[ei][i]).is_zero()) elif isinstance(e, dict): e = dict_check_obs(e, l2[ei]) elif isinstance(e, pe.Obs): assert ((e - l2[ei]).is_zero()) elif isinstance(e, pe.Corr): for i in range(e.T): assert ((e[i] - l2[ei][i]).is_zero()) elif isinstance(e, np.ndarray): a1 = np.ravel(e) a2 = np.ravel(l2[ei]) for i in range(len(a1)): assert ((a1[i] - a2[i]).is_zero()) dict_check_obs(d1, d2) return True od = { 'l': { 'a': pe.pseudo_Obs(1, .2, 'testa', samples=10), 'b': [ pe.pseudo_Obs(1.1, .1, 'test', samples=10), pe.pseudo_Obs(1.2, .1, 'test', samples=10), pe.pseudo_Obs(1.3, .1, 'test', samples=10) ], 'c': { 'd': 1, 'e': pe.pseudo_Obs(.2, .01, 'teste', samples=10), 'f': pe.Corr([ pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10) ]), 'g': np.reshape( np.asarray([ pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10), pe.pseudo_Obs(.1, .01, 'a', samples=10) ]), (2, 2)), } }, 's': { 'a': 'Infor123', 'b': ['Some', 'list'], 'd': pe.pseudo_Obs(.01, .001, 'testd', samples=10) * pe.cov_Obs(1, .01, 'cov1'), 'se': None, 'sf': 1.2, } } fname = 'test_rw' desc = 'This is a random description' with pytest.raises(Exception): jsonio.dump_dict_to_json(od, fname, description=desc, reps='|Test') jsonio.dump_dict_to_json(od, fname, description=desc, reps='TEST') nd = jsonio.load_json_dict(fname, full_output=True, reps='TEST') with pytest.raises(Exception): nd = jsonio.load_json_dict(fname, full_output=True) jsonio.dump_dict_to_json(od, fname, description=desc) nd = jsonio.load_json_dict(fname, full_output=True) assert (desc == nd['description']) assert (check_dict_equality(od, nd['obsdata'])) nd = jsonio.load_json_dict(fname, full_output=False) assert (check_dict_equality(od, nd)) nl = jsonio.load_json(fname, full_output=True) nl = jsonio.load_json(fname, full_output=False) with pytest.raises(Exception): jsonio.dump_dict_to_json(nl, fname, description=desc) od['k'] = 'DICTOBS2' with pytest.raises(Exception): jsonio.dump_dict_to_json(od, fname, description=desc) od['k'] = ['DICTOBS2'] with pytest.raises(Exception): jsonio.dump_dict_to_json(od, fname, description=desc) os.remove(fname + '.json.gz')