Skip to content

Commit

Permalink
[python] Support for current domain on dense arrays (#3179)
Browse files Browse the repository at this point in the history
Co-authored-by: nguyenv <vivian@tiledb.com>
  • Loading branch information
johnkerl and nguyenv committed Oct 16, 2024
1 parent 15eed72 commit 245deb0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
52 changes: 41 additions & 11 deletions apis/python/src/tiledbsoma/_dense_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from ._arrow_types import pyarrow_to_carrow_type
from ._common_nd_array import NDArray
from ._exception import SOMAError, map_exception_for_create
from ._flags import NEW_SHAPE_FEATURE_FLAG_ENABLED
from ._tdb_handles import DenseNDArrayWrapper
from ._types import OpenTimestamp, Slice
from ._util import dense_indices_to_shape
Expand Down Expand Up @@ -103,15 +104,43 @@ def create(
for dim_idx, dim_shape in enumerate(shape):
dim_name = f"soma_dim_{dim_idx}"
pa_field = pa.field(dim_name, pa.int64())
dim_capacity, dim_extent = cls._dim_capacity_and_extent(
dim_name,
dim_shape,
TileDBCreateOptions.from_platform_config(platform_config),
)
index_column_schema.append(pa_field)
# TODO: support current domain for dense arrays once we have core support.
# https://github.com/single-cell-data/TileDB-SOMA/issues/2955

if NEW_SHAPE_FEATURE_FLAG_ENABLED and clib.embedded_version_triple() >= (
2,
27,
0,
):
dim_capacity, dim_extent = cls._dim_capacity_and_extent(

Check warning on line 113 in apis/python/src/tiledbsoma/_dense_nd_array.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dense_nd_array.py#L113

Added line #L113 was not covered by tests
dim_name,
# The user specifies current domain -- this is the max domain
# which is taken from the max ranges for the dim datatype.
# We pass None here to detect those.
None,
TileDBCreateOptions.from_platform_config(platform_config),
)

if dim_shape == 0:
raise ValueError("DenseNDArray shape slots must be at least 1")
if dim_shape is None:
dim_shape = dim_capacity

Check warning on line 125 in apis/python/src/tiledbsoma/_dense_nd_array.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dense_nd_array.py#L122-L125

Added lines #L122 - L125 were not covered by tests

index_column_data[pa_field.name] = [

Check warning on line 127 in apis/python/src/tiledbsoma/_dense_nd_array.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dense_nd_array.py#L127

Added line #L127 was not covered by tests
0,
dim_capacity - 1,
dim_extent,
0,
dim_shape - 1,
]

else:
dim_capacity, dim_extent = cls._dim_capacity_and_extent(
dim_name,
dim_shape,
TileDBCreateOptions.from_platform_config(platform_config),
)

index_column_data[pa_field.name] = [0, dim_capacity - 1, dim_extent]
index_column_schema.append(pa_field)

index_column_info = pa.RecordBatch.from_pydict(
index_column_data, schema=pa.schema(index_column_schema)
Expand Down Expand Up @@ -309,9 +338,10 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
"""Supported for ``SparseNDArray``; scheduled for implementation for
``DenseNDArray`` in TileDB-SOMA 1.15
"""
# TODO: support current domain for dense arrays once we have core support.
# https://github.com/single-cell-data/TileDB-SOMA/issues/2955
raise NotImplementedError()
if clib.embedded_version_triple() >= (2, 27, 0):
self._handle.resize(newshape)

Check warning on line 342 in apis/python/src/tiledbsoma/_dense_nd_array.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dense_nd_array.py#L342

Added line #L342 was not covered by tests
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

@classmethod
def _dim_capacity_and_extent(
Expand Down
14 changes: 8 additions & 6 deletions apis/python/src/tiledbsoma/_tdb_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,19 +619,21 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
"""Supported for ``SparseNDArray``; scheduled for implementation for
``DenseNDArray`` in TileDB-SOMA 1.15
"""
# TODO: support current domain for dense arrays once we have core support.
# https://github.com/single-cell-data/TileDB-SOMA/issues/2955
raise NotImplementedError()
if clib.embedded_version_triple() >= (2, 27, 0):
self._handle.resize(newshape)

Check warning on line 623 in apis/python/src/tiledbsoma/_tdb_handles.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_tdb_handles.py#L622-L623

Added lines #L622 - L623 were not covered by tests
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

Check warning on line 625 in apis/python/src/tiledbsoma/_tdb_handles.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_tdb_handles.py#L625

Added line #L625 was not covered by tests

def tiledbsoma_can_resize(
self, newshape: Sequence[Union[int, None]]
) -> StatusAndReason:
"""Supported for ``SparseNDArray``; scheduled for implementation for
``DenseNDArray`` in TileDB-SOMA 1.15.
"""
# TODO: support current domain for dense arrays once we have core support.
# https://github.com/single-cell-data/TileDB-SOMA/issues/2955
raise NotImplementedError()
if clib.embedded_version_triple() >= (2, 27, 0):
return cast(StatusAndReason, self._handle.tiledbsoma_can_resize(newshape))

Check warning on line 634 in apis/python/src/tiledbsoma/_tdb_handles.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_tdb_handles.py#L633-L634

Added lines #L633 - L634 were not covered by tests
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

Check warning on line 636 in apis/python/src/tiledbsoma/_tdb_handles.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_tdb_handles.py#L636

Added line #L636 was not covered by tests


class SparseNDArrayWrapper(SOMAArrayWrapper[clib.SOMASparseNDArray]):
Expand Down
12 changes: 8 additions & 4 deletions apis/python/tests/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ def test_sparse_nd_array_basics(
(ok, msg) = snda.resize(new_shape, check_only=True)


## Pending 2.27 timeframe for dense support for current domain, including resize
## https://github.com/single-cell-data/TileDB-SOMA/issues/2955
def test_dense_nd_array_basics(tmp_path):
uri = tmp_path.as_posix()
shape = (100, 200)
Expand All @@ -212,12 +210,18 @@ def test_dense_nd_array_basics(tmp_path):
assert dnda.non_empty_domain() == ((0, 0), (0, 0))

with tiledbsoma.DenseNDArray.open(uri, "w") as dnda:
with pytest.raises(NotImplementedError):
if tiledbsoma.pytiledbsoma.embedded_version_triple() >= (2, 27, 0):
dnda.resize((300, 400))
else:
with pytest.raises(NotImplementedError):
dnda.resize((300, 400))

with tiledbsoma.DenseNDArray.open(uri) as dnda:
assert dnda.non_empty_domain() == ((0, 0), (0, 0))
assert dnda.shape == (100, 200)
if tiledbsoma.pytiledbsoma.embedded_version_triple() >= (2, 27, 0):
assert dnda.shape == (300, 400)
else:
assert dnda.shape == (100, 200)


@pytest.mark.parametrize(
Expand Down
3 changes: 0 additions & 3 deletions libtiledbsoma/test/unit_soma_dense_ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ TEST_CASE("SOMADenseNDArray: platform_config", "[SOMADenseNDArray]") {
auto index_columns = helper::create_column_index_info(dim_infos);

if (use_current_domain) {
// Setting a current domain on a TileDB dense array is not (yet)
// supported
// https://github.com/single-cell-data/TileDB-SOMA/issues/2955
if (helper::have_dense_current_domain_support()) {
SOMADenseNDArray::create(
uri,
Expand Down

0 comments on commit 245deb0

Please sign in to comment.