def test_sparse_array_not_dense(self): ctx = t.Ctx() dom = t.Domain(ctx, t.Dim(ctx, domain=(1, 8), tile=2), t.Dim(ctx, domain=(1, 8), tile=2)) att = t.Attr(ctx, "val", dtype='f8') T = t.SparseArray(ctx, self.path("foo"), domain=dom, attrs=(att, )) T.dump() self.assertTrue(T.name == self.path("foo")) self.assertTrue(T.sparse)
def test_simple_1d_sparse_vector(self): ctx = t.Ctx() dom = t.Domain(ctx, t.Dim(ctx, domain=(0, 3), tile=4, dtype=int)) att = t.Attr(ctx, dtype=int) T = t.SparseArray(ctx, self.path("foo"), domain=dom, attrs=(att, )) values = np.array([3, 4]) T[[1, 2]] = values assert_array_equal(T[[1, 2]], values)
def test_sparse_unordered_fp_domain(self): ctx = t.Ctx() dom = t.Domain( ctx, t.Dim(ctx, "x", domain=(0.0, 10.0), tile=2.0, dtype=float)) attr = t.Attr(ctx, dtype=float) T = t.SparseArray(ctx, self.path("foo"), domain=dom, attrs=(attr, )) values = np.array([3.3, 2.7]) T[[4.2, 2.5]] = values assert_array_equal(T[[2.5, 4.2]], values[::-1])
def test_subarray(self): ctx = t.Ctx() dom = t.Domain(ctx, t.Dim(ctx, "x", domain=(1, 10000), tile=100, dtype=int)) att = t.Attr(ctx, "", dtype=float) T = t.SparseArray(ctx, self.path("foo"), domain=dom, attrs=(att, )) self.assertIsNone(T.nonempty_domain()) T[[50, 60, 100]] = [1.0, 2.0, 3.0] self.assertEqual(((50, 100), ), T.nonempty_domain()) # retrieve just valid coordinates in subarray T[40:60] assert_array_equal(T[40:61]["coords"]["x"], [50, 60])
def test_multiple_attributes(self): ctx = t.Ctx() dom = t.Domain(ctx, t.Dim(ctx, domain=(1, 10), tile=10, dtype=int), t.Dim(ctx, domain=(1, 10), tile=10, dtype=int)) attr_int = t.Attr(ctx, "ints", dtype=int) attr_float = t.Attr(ctx, "floats", dtype="float") T = t.SparseArray(ctx, self.path("foo"), domain=dom, attrs=( attr_int, attr_float, )) I = np.array([1, 1, 1, 2, 3, 3, 3, 4]) J = np.array([1, 2, 4, 3, 1, 6, 7, 5]) V_ints = np.array([0, 1, 2, 3, 4, 6, 7, 5]) V_floats = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 6.0, 7.0, 5.0]) V = {"ints": V_ints, "floats": V_floats} T[I, J] = V R = T[I, J] assert_array_equal(V["ints"], R["ints"]) assert_array_equal(V["floats"], R["floats"]) # check error attribute does not exist # TODO: should this be an attribute error? V["foo"] = V["ints"].astype(np.int8) with self.assertRaises(t.TileDBError): T[I, J] = V # check error ncells length V["ints"] = V["ints"][1:2].copy() with self.assertRaises(AttributeError): T[I, J] = V