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

[python] Run upgrade-shapes on notebook example experiments #3289

Merged
merged 3 commits into from
Nov 4, 2024
Merged
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added apis/python/notebooks/data/pbmc3k_raw.h5ad
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
802 changes: 519 additions & 283 deletions apis/python/notebooks/tutorial_exp_query.ipynb

Large diffs are not rendered by default.

2,245 changes: 1,178 additions & 1,067 deletions apis/python/notebooks/tutorial_soma_append_mode.ipynb

Large diffs are not rendered by default.

162 changes: 81 additions & 81 deletions apis/python/notebooks/tutorial_soma_objects.ipynb

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion apis/python/src/tiledbsoma/_dense_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from ._exception import SOMAError, map_exception_for_create
from ._flags import DENSE_ARRAYS_CAN_HAVE_CURRENT_DOMAIN, NEW_SHAPE_FEATURE_FLAG_ENABLED
from ._tdb_handles import DenseNDArrayWrapper
from ._types import OpenTimestamp, Slice
from ._types import OpenTimestamp, Slice, StatusAndReason
from ._util import dense_indices_to_shape
from .options._soma_tiledb_context import (
SOMATileDBContext,
Expand Down Expand Up @@ -361,6 +361,22 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

def tiledbsoma_upgrade_shape(
self, newshape: Sequence[Union[int, None]], check_only: bool = False
) -> StatusAndReason:
"""Allows the array to have a resizeable shape as described in the TileDB-SOMA
1.15 release notes. Raises an error if the new shape exceeds maxshape in
any dimension. Raises an error if the array already has a shape.
"""
if NEW_SHAPE_FEATURE_FLAG_ENABLED and DENSE_ARRAYS_CAN_HAVE_CURRENT_DOMAIN:
if check_only:
return self._handle.tiledbsoma_can_upgrade_shape(newshape)
else:
self._handle.tiledbsoma_upgrade_shape(newshape)
return (True, "")
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

@classmethod
def _dim_capacity_and_extent(
cls,
Expand Down
18 changes: 18 additions & 0 deletions apis/python/src/tiledbsoma/_tdb_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,24 @@ def tiledbsoma_can_resize(
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

def tiledbsoma_upgrade_shape(self, newshape: Sequence[Union[int, None]]) -> None:
"""Wrapper-class internals"""
if DENSE_ARRAYS_CAN_HAVE_CURRENT_DOMAIN:
self._handle.tiledbsoma_upgrade_shape(newshape)
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")

def tiledbsoma_can_upgrade_shape(
self, newshape: Sequence[Union[int, None]]
) -> StatusAndReason:
"""Wrapper-class internals"""
if DENSE_ARRAYS_CAN_HAVE_CURRENT_DOMAIN:
return cast(
StatusAndReason, self._handle.tiledbsoma_can_upgrade_shape(newshape)
)
else:
raise NotImplementedError("Not implemented for libtiledbsoma < 2.27.0")


class SparseNDArrayWrapper(SOMAArrayWrapper[clib.SOMASparseNDArray]):
"""Wrapper around a Pybind11 SparseNDArrayWrapper handle."""
Expand Down
18 changes: 16 additions & 2 deletions apis/python/src/tiledbsoma/io/shaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class SizingArgs(TypedDict):
output_handle: Printable


def _find_old_sparse_ndarray_bounds(
snda: tiledbsoma.SparseNDArray,
) -> Tuple[Tuple[int, int], ...]:
# New arrays (created by tiledbsoma 1.15 and above) will have the new shape.
# Older will have used_shape ...
# ... except _really_ old won't even have that.
try:
return snda.used_shape()
except tiledbsoma.SOMAError:
return snda.non_empty_domain()


def show_experiment_shapes(
uri: str,
*,
Expand Down Expand Up @@ -257,7 +269,8 @@ def _leaf_visitor_show_shapes(

elif isinstance(item, tiledbsoma.SparseNDArray):
_print_leaf_node_banner("SparseNDArray", node_name, item.uri, args)
_bannerize(args, "used_shape", item.used_shape())
####_bannerize(args, "used_shape", item.used_shape())
_bannerize(args, "used_shape", _find_old_sparse_ndarray_bounds(item))
_bannerize(args, "shape", item.shape)
_bannerize(args, "maxshape", item.maxshape)
_bannerize(args, "upgraded", item.tiledbsoma_has_upgraded_shape)
Expand Down Expand Up @@ -306,7 +319,8 @@ def _leaf_visitor_upgrade(
print(" Already upgraded", file=args["output_handle"])

elif isinstance(item, tiledbsoma.SparseNDArray):
used_shape = item.used_shape()
#### used_shape = item.used_shape()
used_shape = _find_old_sparse_ndarray_bounds(item)
new_shape = tuple(e[1] + 1 for e in used_shape)

_print_leaf_node_banner("SparseNDArray", node_name, item.uri, args)
Expand Down
12 changes: 12 additions & 0 deletions apis/python/tests/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,18 @@ def test_dense_nd_array_basics(tmp_path):
else:
assert dnda.shape == (100, 200)

if (
tiledbsoma._flags.DENSE_ARRAYS_CAN_HAVE_CURRENT_DOMAIN
and tiledbsoma._flags.NEW_SHAPE_FEATURE_FLAG_ENABLED
):
with tiledbsoma.DenseNDArray.open(uri) as dnda:
ok, msg = dnda.tiledbsoma_upgrade_shape((600, 700), check_only=True)
assert not ok
assert (
msg
== "tiledbsoma_can_upgrade_shape: array already has a shape: please use resize"
)


@pytest.mark.parametrize(
"soma_joinid_domain",
Expand Down
Loading