Skip to content

Commit 8bf4f2d

Browse files
committed
Fix and add test for multi_index against dense array
1 parent 7d56242 commit 8bf4f2d

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

tiledb/multirange_indexing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def mr_dense_result_shape(
9494

9595
new_shape = []
9696
for i, subranges in enumerate(ranges):
97-
if subranges:
97+
if isinstance(subranges, np.ndarray):
98+
total_length = len(subranges)
99+
new_shape.append(np.uint64(total_length))
100+
elif subranges not in (None, ()):
98101
total_length = sum(abs(stop - start) + 1 for start, stop in subranges)
99102
new_shape.append(np.uint64(total_length))
100103
elif base_shape is not None:

tiledb/tests/test_multi_index.py

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -820,20 +820,22 @@ def test_multi_index_timing(self):
820820
assert "py.getitem_time.pandas_index_update_time :" in internal_stats
821821
tiledb.stats_disable()
822822

823-
# parametrize dtype and sparse
824-
@pytest.mark.parametrize(
825-
"dim_dtype",
826-
[
827-
np.int64,
828-
np.uint64,
829-
np.int32,
830-
np.uint32,
831-
np.int16,
832-
np.uint16,
833-
np.int8,
834-
np.uint8,
835-
],
836-
)
823+
824+
# parametrize dtype and sparse
825+
@pytest.mark.parametrize(
826+
"dim_dtype",
827+
[
828+
np.int64,
829+
np.uint64,
830+
np.int32,
831+
np.uint32,
832+
np.int16,
833+
np.uint16,
834+
np.int8,
835+
np.uint8,
836+
],
837+
)
838+
class TestMultiIndexND(DiskTestCase):
837839
def test_multi_index_ndarray(self, dim_dtype):
838840
# TODO support for dense?
839841
sparse = True # ndarray indexing currently only supported for sparse
@@ -865,3 +867,65 @@ def test_multi_index_ndarray(self, dim_dtype):
865867
assert_dict_arrays_equal(
866868
A.multi_index[coords.tolist()], A.multi_index[coords]
867869
)
870+
assert_dict_arrays_equal(
871+
A.multi_index[coords.tolist()], A.multi_index[coords]
872+
)
873+
874+
def test_multi_index_ndarray_2d(self, dim_dtype):
875+
sparse = False
876+
877+
path = self.path(f"test_multi_index_ndarray_2d")
878+
879+
ncells = 10
880+
ext = ncells - 1
881+
if sparse:
882+
data = np.arange(ext)
883+
else:
884+
data = np.arange(ext**2).reshape(ext, ext)
885+
d1_coords = np.arange(ext)
886+
d2_coords = np.arange(ext, 0, -1)
887+
888+
# use negative range for sparse
889+
if sparse and np.issubdtype(dim_dtype, np.signedinteger):
890+
d1_coords -= 4
891+
892+
d1 = tiledb.Dim(
893+
name="d1", domain=(d1_coords.min(), d1_coords.max()), dtype=dim_dtype
894+
)
895+
d2 = tiledb.Dim(
896+
name="d2", domain=(d2_coords.min(), d2_coords.max()), dtype=dim_dtype
897+
)
898+
dom = tiledb.Domain([d1, d2])
899+
900+
att = tiledb.Attr(dtype=np.int8)
901+
schema = tiledb.ArraySchema(domain=dom, attrs=(att,), sparse=sparse)
902+
tiledb.Array.create(path, schema)
903+
904+
with tiledb.open(path, "w") as A:
905+
if sparse:
906+
A[d1_coords.tolist(), d2_coords.tolist()] = {"": data}
907+
else:
908+
A[:] = data
909+
# raise ValueError("Test only support sparse")
910+
911+
with tiledb.open(path) as A:
912+
assert_dict_arrays_equal(
913+
A.multi_index[d1_coords.tolist(), d2_coords.tolist()],
914+
A.multi_index[d1_coords, d2_coords],
915+
)
916+
917+
# note: np.flip below because coords are in reverse order, which is how
918+
# tiledb will return the results for the first query, but not second
919+
assert_dict_arrays_equal(
920+
A.multi_index[d1_coords.tolist(), np.flip(d2_coords.tolist())],
921+
A.multi_index[d1_coords, :],
922+
)
923+
924+
slc = slice(0, ncells - 1, 2)
925+
assert_dict_arrays_equal(
926+
A.multi_index[d1_coords[slc].tolist(), :],
927+
A.multi_index[d1_coords[slc], :],
928+
)
929+
assert_dict_arrays_equal(
930+
A.multi_index[:, d2_coords[slc]], A.multi_index[:, d2_coords[slc]]
931+
)

0 commit comments

Comments
 (0)