Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand test_sparse_string_domain for empty strings #917

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions tiledb/npbuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ using namespace tiledb;
namespace py = pybind11;
using namespace pybind11::literals;

#if PY_MAJOR_VERSION >= 3
class NumpyConvert {
private:
bool use_iter_ = false;
Expand Down Expand Up @@ -571,24 +570,19 @@ class NumpyConvert {
return py::make_tuple(data_np, offset_np);
}
};
#endif

py::tuple convert_np(
py::array input, bool allow_unicode, bool use_fallback = false) {
#if PY_MAJOR_VERSION >= 3
if (use_fallback) {
#endif
auto tiledb = py::module::import("tiledb");
auto libtiledb = tiledb.attr("libtiledb");
auto array_to_buffer = libtiledb.attr("array_to_buffer");
return array_to_buffer(input);
#if PY_MAJOR_VERSION >= 3
} else {
NumpyConvert cvt(input);
cvt.allow_unicode(allow_unicode);
return cvt.get();
}
#endif
}

}; // namespace tiledbpy
2 changes: 1 addition & 1 deletion tiledb/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,6 @@ def assert_captured(cap, expected):
assert expected in out


@pytest.fixture(scope="module", params=["hilbert", "row-major"])
@pytest.fixture(scope="module", params=["hilbert", "row-major", "col-major"])
def fx_sparse_cell_order(request):
yield request.param
47 changes: 40 additions & 7 deletions tiledb/tests/test_libtiledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,16 @@ def test_sparse_2d_varlen_int(self, fx_sparse_cell_order):

with tiledb.SparseArray(path) as A:
res = A[:]
if fx_sparse_cell_order == "col-major":
data = np.array(
[
np.array([2], dtype=np.int32),
np.array([1, 1], dtype=np.int32),
np.array([3, 3, 3], dtype=np.int32),
np.array([4], dtype=np.int32),
],
dtype="O",
)
assert_subarrays_equal(res[""], data)
assert_unordered_equal(res["__dim_0"], c1)
assert_unordered_equal(res["__dim_1"], c2)
Expand Down Expand Up @@ -2269,7 +2279,17 @@ def test_sparse_mixed_domain_uint_float64(self, fx_sparse_cell_order):
self.assertEqual(a_nonempty[0], (0, 49))
self.assertEqual(a_nonempty[1], (-100.0, 100.0))

def test_sparse_string_domain(self, fx_sparse_cell_order):
@pytest.mark.parametrize(
"coords, expected_ned, allows_duplicates",
[
([b"aa", b"bbb", b"c", b"dddd"], [b"aa", b"dddd"], False),
([b""], [b"", b""], True),
([b"", b"", b"", b""], [b"", b""], True),
],
)
def test_sparse_string_domain(
self, coords, expected_ned, allows_duplicates, fx_sparse_cell_order
):
path = self.path("sparse_string_domain")
dom = tiledb.Domain(tiledb.Dim(name="d", domain=(None, None), dtype=np.bytes_))
att = tiledb.Attr(name="a", dtype=np.int64)
Expand All @@ -2278,22 +2298,35 @@ def test_sparse_string_domain(self, fx_sparse_cell_order):
attrs=(att,),
sparse=True,
cell_order=fx_sparse_cell_order,
allows_duplicates=allows_duplicates,
capacity=10000,
)
tiledb.SparseArray.create(path, schema)

data = [1, 2, 3, 4]
coords = [b"aa", b"bbb", b"c", b"dddd"]
data = [1, 2, 3, 4][: len(coords)]

with tiledb.open(path, "w") as A:
A[coords] = data

with tiledb.open(path) as A:
ned = A.nonempty_domain()[0]
res = A[ned[0] : ned[1]]
assert_array_equal(res["a"], data)
self.assertEqual(set(res["d"]), set(coords))
self.assertEqual(A.nonempty_domain(), ((b"aa", b"dddd"),))
assert_array_equal(A.nonempty_domain(), ((tuple(expected_ned)),))

if not (
fx_sparse_cell_order in ("hilbert", "row-major", "col-major")
and allows_duplicates == True
):
assert_array_equal(A[ned[0] : ned[1]]["a"], data)
self.assertEqual(set(A[ned[0] : ned[1]]["d"]), set(coords))

if allows_duplicates and fx_sparse_cell_order != "hilbert":
res_u1 = A.query().multi_index[ned[0] : ned[1]]
assert_unordered_equal(res_u1["a"], data)
self.assertEqual(set(res_u1["d"]), set(coords))

res_u2 = A.query()[ned[0] : ned[1]]
assert_unordered_equal(res_u2["a"], data)
self.assertEqual(set(res_u2["d"]), set(coords))

def test_sparse_string_domain2(self, fx_sparse_cell_order):
path = self.path("sparse_string_domain2")
Expand Down
Loading