def test_ExtendedDict(self): " ExtendedDict " # check ExtendedDict() and refill_buf p = BufferDict() p['a'] = 1. p['b'] = [2., 3.] p['log(c)'] = 0. p['sqrt(d)'] = [5., 6.] p['erfinv(e)'] = [[33.]] newp = ExtendedDict(p) for i in range(2): for k in p: assert np.all(p[k] == newp[k]) assert newp['c'] == np.exp(newp['log(c)']) assert np.all(newp['d'] == np.square(newp['sqrt(d)'])) assert np.all(newp['e'] == gv.erf(newp['erfinv(e)'])) assert np.all(p.buf == newp.stripped_buf) p.buf[:] = [10., 20., 30., 1., 2., 3., 4.] newp.buf = np.array(p.buf.tolist() + i * [114., 135., 4223., 0.7]) # trim redundant keys oldp = trim_redundant_keys(newp) assert 'c' not in oldp assert 'd' not in oldp assert np.all(oldp.buf == p.buf) # nonredundant keys assert set(nonredundant_keys(newp.keys())) == set(p.keys()) # stripkey for ks, f, k in [ ('aa', np.exp, 'log(aa)'), ('aa', np.square, 'sqrt(aa)'), ]: assert (ks, f) == ExtendedDict.stripkey(k) # addparentheses pvar = BufferDict() pvar['a'] = p['a'] pvar['b'] = p['b'] pvar['logc'] = p['log(c)'] pvar['sqrtd'] = p['sqrt(d)'] pvar['erfinv(e)'] = p['erfinv(e)'] pvar = add_parameter_parentheses(pvar) for k in p: assert k in pvar assert np.all(p[k] == pvar[k]) for k in pvar: assert k in p pvar = add_parameter_parentheses(pvar) for k in p: assert k in pvar assert np.all(p[k] == pvar[k]) for k in pvar: assert k in p pvar['log(c(23))'] = 1.2 pvar = ExtendedDict(pvar) assert 'c(23)' in pvar assert 'log(c(23))' in pvar
def test_b_update(self): """ b.add(dict(..)) """ global b, bkeys, bvalues, bslices, bbuf nb = BufferDict() nb.update(b) for k in bkeys: self.assert_arraysequal(nb[k], b[k]) nb[k] += 10. self.assert_arraysequal(nb[k], b[k] + 10.)
def test_dump_gvar(self): b = BufferDict(dict(a=gv.gvar(1, 2), b=[gv.gvar(3, 4), gv.gvar(5, 6)])) b[0, 1] = b['a'] + 10 * b['b'][0] for use_json in [True, False]: sb = b.dumps(use_json=use_json) c = BufferDict.loads(sb, use_json=use_json) for k in b: self.assert_gvclose(b[k], c[k], rtol=1e-6) self.assert_gvclose((c[0, 1] - 10 * c['b'][0]) / c['a'], gv.gvar(1.0, 0.0), rtol=1e-6)
def test_uniform(self): " BufferDict.uniform " b = BufferDict() BufferDict.uniform('f', 0., 1.) for fw, w in [(0, 0.5), (-1., 0.15865525393145707), (1, 1 - 0.15865525393145707)]: b['f(s)'] = fw fmt = '{:.6f}' self.assertEqual(fmt.format(b['s']), fmt.format(w)) b['f(a)'] = 4 * [fw] self.assertEqual(str(b['a']), str(np.array(4 * [w]))) BufferDict.del_distribution('f')
def test_dtype(self): """ BufferDict(d, dtype=X) """ global b ib = BufferDict(b, dtype=np.intp) self.assertTrue(np.all(ib.buf == b.buf)) self.assertTrue(isinstance(ib.buf[0], np.intp)) ib = BufferDict([(k, b[k]) for k in b], dtype=np.intp) self.assertTrue(np.all(ib.buf == b.buf)) self.assertTrue(isinstance(ib.buf[0], np.intp)) ob = BufferDict([], dtype=object) ob[0] = 1 ob[0] = gv.gvar('1(1)')
def test_del(self): """ del b[k] """ global b for k in b: newb = BufferDict(b) keys = list(b.keys()) del newb[k] keys.remove(k) self.assertEqual(keys, list(newb.keys())) size = np.size(b[k]) self.assertEqual(len(b.buf) - size, len(newb.buf)) for l in newb: self.assertTrue(np.all(newb[l] == b[l]))
def test_keys(self): """ b.keys """ global b, bkeys self.assertSequenceEqual(list(b), bkeys) # check unusual keys bb = BufferDict() bb[('a', 1)] = 2. bb[(3, 4, 5)] = 3. bb[('a', 'b')] = 22. bb[0] = 2. bb['log(c)'] = 5. bb['c'] = 75. self.assertSequenceEqual(list(bb.keys()), [('a', 1), (3, 4, 5), ('a', 'b'), 0, 'log(c)', 'c'])
def test_dump(self): global b b[0, 1] = 2. for use_json in [True, False]: sb = b.dumps(use_json=use_json) c = BufferDict.loads(sb, use_json=use_json) for k in b: self.assert_arraysequal(b[k], c[k])
def test_gvar_dump_load_buffer_dict(self): """Dumps gvar to db, reads it off and checks if gvars are equal.""" a1 = gvar(1, 2) a2 = gvar(2, 3) a = BufferDict(**{"a1": a1, "a2": a2, "a1/a2": a1 / a2}) ExampleTable(a=a).save() a_stored = ExampleTable.objects.first().a assert_allclose(a, a_stored)
def setUp(self): global b, bkeys, bvalues, bslices, bbuf b = BufferDict() bkeys = ['scalar', 'vector', 'tensor'] bvalues = [0., np.array([1., 2.]), np.array([[3., 4.], [5., 6.]])] bslices = [0, slice(1, 3, None), slice(3, 7, None)] bbuf = np.arange(7.) b['scalar'] = 0. b['vector'] = [1., 2.] b['tensor'] = [[3., 4.], [5., 6.]]
def test_bufferdict_b(self): """ BufferDict(b) """ global b, bkeys, bvalues, bslices, bbuf nb = BufferDict(b) for k in bkeys: self.assert_arraysequal(nb[k], b[k]) nb[k] += 10. self.assert_arraysequal(nb[k], b[k] + 10.) nb = BufferDict(nb, buf=b.flat) for k in bkeys: self.assert_arraysequal(nb[k], b[k]) self.assertEqual(b.size, nb.size) b.flat[-1] = 130. self.assertEqual(nb.flat[-1], 130.) with self.assertRaises(ValueError): nb = BufferDict(b, buf=nb.flat[:-1]) nb = BufferDict(b, keys=reversed(bkeys)) nbkeys = list(nb.keys()) self.assertEqual(nbkeys, list(reversed(bkeys)))
def test_pickle_gvar(self): b = BufferDict() b['a'] = gv.gvar(1,2) b['b'] = [gv.gvar(3,4), gv.gvar(5,6)] b['b'] += gv.gvar(1, 1) b['c'] = gv.gvar(10,1) sb = pckl.dumps(b) c = pckl.loads(sb) self.assertEqual(str(b), str(c)) self.assertEqual(str(gv.evalcov(b)), str(gv.evalcov(c))) # no uncorrelated bits b['a'] += b['c'] sb = pckl.dumps(b) c = pckl.loads(sb) self.assertEqual(str(b), str(c)) self.assertEqual(str(gv.evalcov(b)), str(gv.evalcov(c)))
def test_pickle_gvar(self): b = BufferDict(dict(a=gv.gvar(1, 2), b=[gv.gvar(3, 4), gv.gvar(5, 6)])) sb = pckl.dumps(b) c = pckl.loads(sb) for k in b: self.assert_gvclose(b[k], c[k], rtol=1e-6)
def test_extension_mapping(self): " BufferDict extension and mapping properties " p = BufferDict() p['a'] = 1. p['b'] = [2., 3.] p['log(c)'] = 0. p['sqrt(d)'] = [5., 6.] p['erfinv(e)'] = [[33.]] newp = BufferDict(p) for i in range(2): for k in p: assert np.all(p[k] == newp[k]) assert newp['c'] == np.exp(newp['log(c)']) assert np.all(newp['d'] == np.square(newp['sqrt(d)'])) assert np.all(newp['e'] == gv.erf(newp['erfinv(e)'])) assert np.all(p.buf == newp.buf) p.buf[:] = [10., 20., 30., 1., 2., 3., 4.] newp.buf = np.array(p.buf.tolist()) self.assertEqual(gv.get_dictkeys(p, ['c', 'a', 'log(c)', 'e', 'd']), ['log(c)', 'a', 'log(c)', 'erfinv(e)', 'sqrt(d)']) self.assertEqual( [gv.dictkey(p, k) for k in ['c', 'a', 'log(c)', 'e', 'd']], ['log(c)', 'a', 'log(c)', 'erfinv(e)', 'sqrt(d)']) self.assertTrue(gv.BufferDict.has_dictkey(p, 'a')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'b')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'c')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'd')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'e')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'log(c)')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'sqrt(d)')) self.assertTrue(gv.BufferDict.has_dictkey(p, 'erfinv(e)')) self.assertTrue(not gv.BufferDict.has_dictkey(p, 'log(a)')) self.assertTrue(not gv.BufferDict.has_dictkey(p, 'sqrt(b)')) self.assertEqual(list(p), ['a', 'b', 'log(c)', 'sqrt(d)', 'erfinv(e)']) np.testing.assert_equal(list(p.values()), [10.0, [20., 30.], 1.0, [2., 3.], [[4.]]]) self.assertEqual(p.get('c'), p['c']) # tracking? self.assertAlmostEqual(p['c'], np.exp(1)) self.assertAlmostEqual(p['log(c)'], 1.) p['log(c)'] = 2. self.assertAlmostEqual(p['c'], np.exp(2)) self.assertAlmostEqual(p['log(c)'], 2.) p['a'] = 12. self.assertAlmostEqual(p['c'], np.exp(2)) self.assertAlmostEqual(p['log(c)'], 2.) self.assertEqual( list(p), ['a', 'b', 'log(c)', 'sqrt(d)', 'erfinv(e)'], ) # the rest is not so important # trim redundant keys oldp = trim_redundant_keys(newp) assert 'c' not in oldp assert 'd' not in oldp assert np.all(oldp.buf == newp.buf) # nonredundant keys assert set(nonredundant_keys(newp.keys())) == set(p.keys()) # stripkey for ks, f, k in [ ('aa', np.exp, 'log(aa)'), ('aa', np.square, 'sqrt(aa)'), ]: assert (ks, f) == gv._bufferdict._stripkey(k) # addparentheses pvar = BufferDict() pvar['a'] = p['a'] pvar['b'] = p['b'] pvar['logc'] = p['log(c)'] pvar['sqrtd'] = p['sqrt(d)'] pvar['erfinv(e)'] = p['erfinv(e)'] pvar = add_parameter_parentheses(pvar) for k in p: assert k in pvar assert np.all(p[k] == pvar[k]) for k in pvar: assert k in p pvar = add_parameter_parentheses(pvar) for k in p: assert k in pvar assert np.all(p[k] == pvar[k]) for k in pvar: assert k in p pvar['log(c(23))'] = 1.2 pvar = BufferDict(pvar) assert 'c(23)' not in pvar assert 'log(c(23))' in pvar self.assertAlmostEqual(gv.exp(pvar['log(c(23))']), pvar['c(23)'])
def test_arithmetic(self): a = BufferDict(a=1., b=[2., 3.]) b = dict(b=[20., 30.], a=10.) self.assertEqual(str(a + b), str(BufferDict(a=11., b=[22., 33.]))) self.assertEqual(str(b + a), str(BufferDict(a=11., b=[22., 33.]))) self.assertEqual(str(a - b), str(BufferDict(a=-9., b=[-18., -27.]))) self.assertEqual(str(b - a), str(BufferDict(a=9., b=[18., 27.]))) self.assertEqual(str(a * 2), str(BufferDict(a=2., b=[4., 6.]))) self.assertEqual(str(2 * a), str(BufferDict(a=2., b=[4., 6.]))) self.assertEqual(str(a / 0.5), str(BufferDict(a=2., b=[4., 6.]))) a = BufferDict(a=1., b=[2., 3.]) a += b self.assertEqual(str(a), str(BufferDict(a=11., b=[22., 33.]))) a = BufferDict(a=1., b=[2., 3.]) a -= b self.assertEqual(str(a), str(BufferDict(a=-9., b=[-18., -27.]))) a = BufferDict(a=1., b=[2., 3.]) a *= 2 self.assertEqual(str(a), str(BufferDict(a=2., b=[4., 6.]))) a = BufferDict(a=1., b=[2., 3.]) a /= 0.5 self.assertEqual(str(a), str(BufferDict(a=2., b=[4., 6.]))) a = BufferDict(a=1., b=[2., 3.]) self.assertEqual(str(+a), str(BufferDict(a=1., b=[2., 3.]))) self.assertEqual(str(-a), str(BufferDict(a=-1., b=[-2., -3.]))) b = gv.gvar(dict(b=['20(1)', '30(1)'], a='10(1)')) self.assertEqual( str(a + b), str(gv.gvar(BufferDict(a='11(1)', b=['22(1)', '33(1)']))))