Skip to content

Commit

Permalink
[python] Connect resizers to Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkerl committed Oct 6, 2024
1 parent ae4eb45 commit 61ce7cd
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 5 deletions.
24 changes: 22 additions & 2 deletions apis/python/src/tiledbsoma/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ def tiledbsoma_has_upgraded_domain(self) -> bool:
"""
return self._handle.tiledbsoma_has_upgraded_domain

def resize_soma_joinid_shape(self, newshape: int) -> None:
def resize_soma_joinid_shape(
self, newshape: int, check_only: bool = False
) -> Tuple[bool, str]:
"""Increases the shape of the dataframe on the ``soma_joinid`` index
column, if it indeed is an index column, leaving all other index columns
as-is. If the ``soma_joinid`` is not an index column, no change is made.
Expand All @@ -427,7 +429,25 @@ def resize_soma_joinid_shape(self, newshape: int) -> None:
domain: in that case please call ``tiledbsoma_upgrade_domain`` (WIP for
1.15).
"""
self._handle._handle.resize_soma_joinid_shape(newshape)
if check_only:
return cast(

Check warning on line 433 in apis/python/src/tiledbsoma/_dataframe.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dataframe.py#L433

Added line #L433 was not covered by tests
Tuple[bool, str], self._handle._handle.can_resize_soma_joinid_shape(newshape)
)
else:
self._handle._handle.resize_soma_joinid_shape(newshape)
return (True, "")

def upgrade_soma_joinid_shape(
self, newshape: int, check_only: bool = False
) -> Tuple[bool, str]:
"""XXX TO WRITE"""
if check_only:
return cast(

Check warning on line 445 in apis/python/src/tiledbsoma/_dataframe.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dataframe.py#L444-L445

Added lines #L444 - L445 were not covered by tests
Tuple[bool, str], self._handle._handle.can_upgrade_soma_joinid_shape(newshape)
)
else:
self._handle._handle.upgrade_soma_joinid_shape(newshape)
return (True, "")

Check warning on line 450 in apis/python/src/tiledbsoma/_dataframe.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_dataframe.py#L449-L450

Added lines #L449 - L450 were not covered by tests

def __len__(self) -> int:
"""Returns the number of rows in the dataframe. Same as ``df.count``."""
Expand Down
10 changes: 8 additions & 2 deletions apis/python/src/tiledbsoma/_sparse_nd_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,20 @@ def read(

return SparseNDArrayRead(sr, self, coords)

def resize(self, newshape: Sequence[Union[int, None]]) -> None:
def resize(
self, newshape: Sequence[Union[int, None]], dry_run: bool = False
) -> Tuple[bool, str]:
"""Increases the shape of the array as specfied. Raises an error if the new
shape is less than the current shape in any dimension. Raises an error if
the new shape exceeds maxshape in any dimension. Raises an error if the
array doesn't already have a shape: in that case please call
tiledbsoma_upgrade_shape.
"""
self._handle.resize(newshape)
if dry_run:
return self._handle.tiledbsoma_can_resize(newshape)

Check warning on line 309 in apis/python/src/tiledbsoma/_sparse_nd_array.py

View check run for this annotation

Codecov / codecov/patch

apis/python/src/tiledbsoma/_sparse_nd_array.py#L309

Added line #L309 was not covered by tests
else:
self._handle.resize(newshape)
return (True, "")

def tiledbsoma_upgrade_shape(self, newshape: Sequence[Union[int, None]]) -> None:
"""Allows the array to have a resizeable shape as described in the TileDB-SOMA
Expand Down
54 changes: 54 additions & 0 deletions apis/python/src/tiledbsoma/_tdb_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
"""Not implemented for DataFrame."""
raise NotImplementedError

def tiledbsoma_can_resize(
self, newshape: Sequence[Union[int, None]]
) -> Tuple[bool, str]:
"""Not implemented for DataFrame."""
raise NotImplementedError

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L466 was not covered by tests

def tiledbsoma_upgrade_shape(self, newshape: Sequence[Union[int, None]]) -> None:
"""Not implemented for DataFrame."""
raise NotImplementedError
Expand All @@ -467,6 +473,18 @@ def resize_soma_joinid_shape(self, newshape: int) -> None:
"""Only implemented for DataFrame."""
raise NotImplementedError

def can_resize_soma_joinid(self, newshape: int) -> Tuple[bool, str]:
"""Only implemented for DataFrame."""
raise NotImplementedError

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L478 was not covered by tests

def upgrade_soma_joinid_shape(self, newshape: int) -> None:
"""Only implemented for DataFrame."""
raise NotImplementedError

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L482 was not covered by tests

def can_upgrade_soma_joinid(self, newshape: int) -> Tuple[bool, str]:
"""Only implemented for DataFrame."""
raise NotImplementedError

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L486 was not covered by tests


class DataFrameWrapper(SOMAArrayWrapper[clib.SOMADataFrame]):
"""Wrapper around a Pybind11 SOMADataFrame handle."""
Expand Down Expand Up @@ -525,6 +543,26 @@ def resize_soma_joinid_shape(self, newshape: int) -> None:
"""
self._handle.resize_soma_joinid_shape(newshape)

def can_resize_soma_joinid(self, newshape: int) -> Tuple[bool, str]:
"""Increases the shape of the dataframe on the ``soma_joinid`` index
column, if it indeed is an index column, leaving all other index columns
as-is. If the ``soma_joinid`` is not an index column, no change is made.
This is a special case of ``upgrade_domain`` (WIP for 1.15), but simpler
to keystroke, and handles the most common case for dataframe domain
expansion. Raises an error if the dataframe doesn't already have a
domain: in that case please call ``tiledbsoma_upgrade_domain`` (WIP for
1.15).
"""
return cast(Tuple[bool, str], self._handle.can_resize_soma_joinid(newshape))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L556 was not covered by tests

def upgrade_soma_joinid_shape(self, newshape: int) -> None:
"""XXX TO WRITE"""
self._handle.upgrade_soma_joinid_shape(newshape)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L560 was not covered by tests

def can_upgrade_soma_joinid(self, newshape: int) -> Tuple[bool, str]:
"""XXX TO WRITE"""
return cast(Tuple[bool, str], self._handle.can_upgrade_soma_joinid(newshape))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L564 was not covered by tests


class PointCloudDataFrameWrapper(SOMAArrayWrapper[clib.SOMAPointCloudDataFrame]):
"""Wrapper around a Pybind11 SOMAPointCloudDataFrame handle."""
Expand Down Expand Up @@ -563,6 +601,16 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
# https://github.com/single-cell-data/TileDB-SOMA/issues/2955
raise NotImplementedError()

def tiledbsoma_can_resize(
self, newshape: Sequence[Union[int, None]]
) -> Tuple[bool, str]:
"""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()

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L612 was not covered by tests


class SparseNDArrayWrapper(SOMAArrayWrapper[clib.SOMASparseNDArray]):
"""Wrapper around a Pybind11 SparseNDArrayWrapper handle."""
Expand Down Expand Up @@ -593,6 +641,12 @@ def resize(self, newshape: Sequence[Union[int, None]]) -> None:
"""
self._handle.resize(newshape)

def tiledbsoma_can_resize(
self, newshape: Sequence[Union[int, None]]
) -> Tuple[bool, str]:
"""XXX WRITE ME"""
return cast(Tuple[bool, str], self._handle.can_resize(newshape))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L648 was not covered by tests

def tiledbsoma_upgrade_shape(self, newshape: Sequence[Union[int, None]]) -> None:
"""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
Expand Down
36 changes: 36 additions & 0 deletions apis/python/src/tiledbsoma/soma_dataframe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,42 @@ void load_soma_dataframe(py::module& m) {
throw TileDBSOMAError(e.what());
}
},
"newshape"_a)

.def(
"can_resize_soma_joinid_shape",
[](SOMADataFrame& sdf, int64_t newshape) {
try {
return sdf.can_resize_soma_joinid_shape(
newshape, "can_resize_soma_joinid_shape");
} catch (const std::exception& e) {
throw TileDBSOMAError(e.what());
}
},
"newshape"_a)

.def(
"upgrade_soma_joinid_shape",
[](SOMADataFrame& sdf, int64_t newshape) {
try {
sdf.upgrade_soma_joinid_shape(
newshape, "upgrade_soma_joinid_shape");
} catch (const std::exception& e) {
throw TileDBSOMAError(e.what());
}
},
"newshape"_a)

.def(
"can_upgrade_soma_joinid_shape",
[](SOMADataFrame& sdf, int64_t newshape) {
try {
return sdf.can_upgrade_soma_joinid_shape(
newshape, "can_upgrade_soma_joinid_shape");
} catch (const std::exception& e) {
throw TileDBSOMAError(e.what());
}
},
"newshape"_a);
}
} // namespace libtiledbsomacpp
11 changes: 11 additions & 0 deletions apis/python/src/tiledbsoma/soma_sparse_ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ void load_soma_sparse_ndarray(py::module& m) {
},
"newshape"_a)

.def(
"can_resize",
[](SOMAArray& array, const std::vector<int64_t>& newshape) {
try {
return array.can_resize(newshape, "can_resize");
} catch (const std::exception& e) {
throw TileDBSOMAError(e.what());
}
},
"newshape"_a)

.def(
"tiledbsoma_upgrade_shape",
[](SOMAArray& array, const std::vector<int64_t>& newshape) {
Expand Down
2 changes: 1 addition & 1 deletion libtiledbsoma/src/soma/soma_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
* This file defines the SOMAArray class.
*/

#include "soma_array.h"
#include <tiledb/array_experimental.h>
#include "../utils/logger.h"
#include "../utils/util.h"
#include "soma_array.h"
namespace tiledbsoma {
using namespace tiledb;

Expand Down

0 comments on commit 61ce7cd

Please sign in to comment.