File size: 5,709 Bytes
2c3c408 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | # %%
import numpy as np
import tiledb
from .common import DiskTestCase, assert_array_equal, assert_equal
class DomainIndexingSparseTest(DiskTestCase):
def test_int_domain_indexing(self):
path = self.path("int_domain_indexing")
dom = tiledb.Domain(
tiledb.Dim(name="x", domain=(-10, 10), tile=1, dtype=np.int64)
)
schema = tiledb.ArraySchema(
domain=dom, sparse=True, attrs=[tiledb.Attr(name="a", dtype=np.float64)]
)
tiledb.SparseArray.create(path, schema)
X = np.arange(-10, 11, step=1)
val = np.random.rand(len(X))
with tiledb.SparseArray(path, mode="w") as A:
A[X] = val
with tiledb.SparseArray(path) as A:
assert_array_equal(A.domain_index[X[0]]["a"], val[0])
assert_array_equal(A.domain_index[X[-1]]["a"], val[-1])
assert_array_equal(A.domain_index[X[0] : X[-1]]["a"], val[:])
# sanity check
assert_array_equal(A.domain_index[X[0] : X[-1]]["x"], X[:])
def test_fp_domain_indexing(self):
array_path = self.path("test_domain_idx")
# test case from https://github.com/TileDB-Inc/TileDB-Py/issues/201
tile = 1
dom = tiledb.Domain(
tiledb.Dim(name="x", domain=(-89.75, 89.75), tile=tile, dtype=np.float64),
tiledb.Dim(name="y", domain=(-179.75, 179.75), tile=tile, dtype=np.float64),
tiledb.Dim(name="z", domain=(157498, 157863), tile=tile, dtype=np.float64),
)
schema = tiledb.ArraySchema(
domain=dom, sparse=True, attrs=[tiledb.Attr(name="data", dtype=np.float64)]
)
tiledb.SparseArray.create(array_path, schema)
# fake data
X = np.linspace(-89.75, 89.75, 359)
Y = np.linspace(-179.75, 179.75, 359)
Z = np.linspace(157498, 157857, 359)
# data = np.random.rand(*map(lambda x: x[0], (X.shape, Y.shape, Z.shape)))
data = np.random.rand(X.shape[0])
with tiledb.SparseArray(array_path, mode="w") as A:
A[X, Y, Z] = data
with tiledb.SparseArray(array_path) as A:
# check direct slicing
assert_array_equal(A.domain_index[X[0], Y[0], Z[0]]["data"], data[0])
# check small slice ranges
tmp = A.domain_index[
X[0] : np.nextafter(X[0], 0),
Y[0] : np.nextafter(Y[0], 0),
Z[0] : np.nextafter(Z[0], Z[0] + 1),
]
assert_array_equal(tmp["data"], data[0])
# check slicing last element
tmp = A.domain_index[X[-1], Y[-1], Z[-1]]
assert_array_equal(tmp["data"], data[-1])
# check slice range multiple components
tmp = A.domain_index[X[1] : X[2], Y[1] : Y[2], Z[1] : Z[2]]
assert_array_equal(tmp["data"], data[1:3])
# check an interior point
coords = X[145], Y[145], Z[145]
tmp = A.domain_index[coords]
assert_array_equal(tmp["x"], X[145])
assert_array_equal(tmp["data"], data[145])
# check entire domain
tmp = A.domain_index[X[0] : X[-1], Y[0] : Y[-1], Z[0] : Z[-1]]
assert_array_equal(tmp["data"], data[:])
# check entire domain
# TODO uncomment if vectorized indexing is available
# coords = np.array([X,Y,Z]).transpose().flatten()
# tmp = A.domain_index[X,Y,Z]
# assert_array_equal(
# tmp['data'],
# data[:]
# )
def test_fp_domain_count(self):
array_path = self.path("test_domain_count")
tile = 1
dom = tiledb.Domain(
tiledb.Dim(name="x", domain=(0.0, 2.0), tile=tile, dtype=np.float64),
tiledb.Dim(name="y", domain=(0.0, 2.0), tile=tile, dtype=np.float64),
)
schema = tiledb.ArraySchema(
domain=dom, sparse=True, attrs=[tiledb.Attr(name="data", dtype=np.float64)]
)
tiledb.SparseArray.create(array_path, schema)
# fake data
X = [1.0]
Y = [1.0]
data = [1.0]
with tiledb.SparseArray(array_path, mode="w") as A:
A[X, Y] = data
with tiledb.SparseArray(array_path) as A:
# check direct slicing
assert_array_equal(A.domain_index[X[0], Y[0]]["data"], data[0])
# check counting by slice
assert_equal(A.domain_index[0:2.0, 0:1.0]["x"].shape[0], 1)
assert_equal(A.domain_index[0:2.0, 0:1.0]["y"].shape[0], 1)
assert_equal(A.domain_index[0:2.0, np.nextafter(1.0, 2.0)]["x"].shape[0], 0)
assert_equal(A.domain_index[0:2.0, np.nextafter(1.0, 2.0)]["y"].shape[0], 0)
class DomainIndexingDenseTest(DiskTestCase):
def test_int_domain_indexing(self):
path = self.path("dense_int_domain_indexing")
dom = tiledb.Domain(
tiledb.Dim(name="x", domain=(0, 10), tile=1, dtype=np.int64)
)
schema = tiledb.ArraySchema(
domain=dom, sparse=False, attrs=[tiledb.Attr(name="a", dtype=np.float64)]
)
tiledb.DenseArray.create(path, schema)
X = np.arange(0, 11, step=1)
val = np.random.rand(len(X))
with tiledb.DenseArray(path, mode="w") as A:
A[:] = val
with tiledb.DenseArray(path) as A:
assert_array_equal(A.domain_index[X[0]]["a"], val[0])
assert_array_equal(A.domain_index[X[-1]]["a"], val[-1])
assert_array_equal(A.domain_index[X[0] : X[-1]]["a"], val[:])
# sanity check
assert_array_equal(A.domain_index[X[0] : X[-1]]["x"], X[:])
|