def test_set_subfield(): y = random_array(5, [('a', [('b', float)])]) x = lgp.StructuredArray(y) a = x['a'] x['a'] = random_array(a.shape, a.dtype) b = x['a']['b'] x['a']['b'] = random_array(b.shape, b.dtype)
def jit(self, deriv=0, nd=False): donesomething = False for kw in self.kwargs_list: if nd: x = self.random_x_nd(2, **kw) x = lgp.StructuredArray(x) dtype = np.result_type(*(x[name].dtype for name in x.dtype.names)) else: x = self.random_x(**kw) dtype = x.dtype if not np.issubdtype(dtype, np.number) and not dtype == bool: continue kernel = self.kernel_class(**kw) if kernel.derivable < deriv: continue if nd and kernel.maxdim < 2: continue d = (deriv, 'f0') if nd else deriv kernel = kernel.diff(d, d) cov1 = kernel(x[None, :], x[:, None]) cov2 = jax.jit(kernel)(x[None, :], x[:, None]) np.testing.assert_allclose(cov2, cov1, rtol=1e-6, atol=1e-5, equal_nan=False) donesomething = True if not donesomething: pytest.skip()
def test_readonly(): x = random_array((2, 3), '4f,d,?') x = lgp.StructuredArray(x) x = x[:, 1] with pytest.raises(ValueError): y0 = x['f0'] x['f0'] = random_array(y0.shape, y0.dtype)
def test_tree(): dtypes = [ [('f0', float)], 'f,f', 'f,d,f', 'f,O', 'S25,U10,?', [('a', 'f,f', (3, 1)), ('b', [('c', 'd,d'), ('e', '?', 15)])], ] shapes = [ (), (0,), (1,), (1, 0), (0, 1), (10,), (1, 2, 3), (2, 3, 4), ] for dtype, shape in itertools.product(dtypes, shapes): array1 = random_array(shape, dtype) array = lgp.StructuredArray(array1) children, aux_data = tree_util.tree_flatten(array) array = tree_util.tree_unflatten(aux_data, children) array2 = np.asarray(array) util.assert_equal(array1, array2)
def test_repr(): x = np.array( [[( 79.4607 , 34.16494104, True), (-122.71211 , 70.48242559, False)], [( -95.21081 , 80.3448 , False), ( 52.144142 , 57.35083659, False)], [( 97.3018 , -65.78833393, True), ( 88.46741 , -85.54110175, False)], [( -2.7272243, -16.86393124, False), ( 0.946183 , -50.12316733, False)], [( -19.002777 , -114.6684194 , True), ( -59.26707 , 87.90692279, False)]], dtype=[('f0', '<f4'), ('f1', '<f8'), ('f2', '?')] ) y = lgp.StructuredArray(x) s = """\ StructuredArray({ 'f0': array([[ 79.4607 , -122.71211 ], [ -95.21081 , 52.144142 ], [ 97.3018 , 88.46741 ], [ -2.7272243, 0.946183 ], [ -19.002777 , -59.26707 ]], dtype=float32), 'f1': array([[ 34.16494104, 70.48242559], [ 80.3448 , 57.35083659], [ -65.78833393, -85.54110175], [ -16.86393124, -50.12316733], [-114.6684194 , 87.90692279]]), 'f2': array([[ True, False], [False, False], [ True, False], [False, False], [ True, False]]), })""" assert repr(y) == s x = np.array( [(43.52967 , -258.21209855, False), ( 0.80091816, 80.99479392, False)], dtype=[('f0', '<f4'), ('f1', '<f8'), ('f2', '?')] ) y = lgp.StructuredArray(x) s = "StructuredArray({'f0': array([43.52967 , 0.80091816], dtype=float32), 'f1': array([-258.21209855, 80.99479392]), 'f2': array([False, False])})" assert repr(y) == s x = np.array( [([ 102.43267 , 25.42825 , -76.084114 , 39.536003 ], 85.10061608), ([ -6.0310173, -108.25371 , -20.205214 , -233.0124 ], -152.64632798)], dtype=[('f0', '<f4', (4,)), ('f1', '<f8')] ) y = lgp.StructuredArray(x) s = """\ StructuredArray({ 'f0': array([[ 102.43267 , 25.42825 , -76.084114 , 39.536003 ], [ -6.0310173, -108.25371 , -20.205214 , -233.0124 ]], dtype=float32), 'f1': array([ 85.10061608, -152.64632798]), })""" assert repr(y) == s x = np.zeros(5, []) y = lgp.StructuredArray(x) s = "StructuredArray({})" assert repr(y) == s
def test_ellipsis(): x = np.empty((2, 3), dtype=[('a', float, 4)]) y = lgp.StructuredArray(x) z = y[..., 0] assert z.shape == y.shape[:1] assert z['a'].shape == y.shape[:1] + x.dtype.fields['a'][0].shape
('label', int) ]) x['time'][0] = time delay = 20 x['time'][1] = time - delay x['label'][0] = 0 x['label'][1] = 1 label_names = ['gatti_comprati', 'gatti_morti'] function = lambda x: np.exp(-1/2 * ((x - 10) / 5)**2) data_error = 0.05 data_mean = function(x['time']) + data_error * np.random.randn(*x.shape) data_mean[1] += 0.02 * time data = gvar.gvar(data_mean, np.full_like(data_mean, data_error)) x = lgp.StructuredArray(x) def makegp(params): kernel = lgp.Cauchy(scale=params['time_scale'], dim='time', beta=2) kernel *= lgp.ExpQuad(scale=params['label_scale'], dim='label') gp = lgp.GP(kernel) x['time'] = jnp.array([time, time - params['delay']]) gp.addx(x, 'A') return gp start = systime.time() hyperprior = gvar.BufferDict({ 'log(time_scale)': gvar.log(gvar.gvar(10, 10)), 'log(label_scale)': gvar.log(gvar.gvar(10, 10)), 'delay': gvar.gvar(10, 20) }) params = lgp.empbayes_fit(hyperprior, makegp, {'A': data}, raises=False, jit=True).p