Skip to content

Commit

Permalink
Make changes according to review
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenv committed Feb 23, 2024
1 parent b1de479 commit 6e47da4
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 54 deletions.
4 changes: 2 additions & 2 deletions apis/python/src/tiledbsoma/_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def read(

handle = self._handle._handle

context = handle.ctx()
context = handle.context()
if platform_config is not None:
config = context.tiledb_config.copy()
config.update(platform_config or {})
Expand All @@ -353,7 +353,7 @@ def read(
sr = clib.SOMADataFrame.open(
uri=handle.uri,
mode=clib.OpenMode.read,
ctx=context,
context=context,
column_names=column_names or [],
result_order=_util.to_clib_result_order(result_order),
timestamp=ts,
Expand Down
57 changes: 29 additions & 28 deletions apis/python/src/tiledbsoma/_tdb_handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,39 @@ def open(
open_mode = clib.OpenMode.read if mode == "r" else clib.OpenMode.write
timestamp_ms = context._open_timestamp_ms(timestamp)

try:
soma_object = clib.SOMAObject.open(
uri, open_mode, context.native_context, (0, timestamp_ms)
)

if open_mode == clib.OpenMode.read and soma_object.type == "SOMADataFrame":
return DataFrameWrapper._from_soma_object(soma_object, context)

# This object still uses tiledb-py and must be handled below
if soma_object.type in (
"SOMADataFrame",
"SOMASparseNDArray",
"SOMADenseNDArray",
):
return ArrayWrapper.open(uri, mode, context, timestamp)
if soma_object.type in ("SOMACollection", "SOMAExperiment", "SOMAMeasurement"):
return GroupWrapper.open(uri, mode, context, timestamp)
except (SOMAError, RuntimeError):
# TODO on Linux this throws a SOMAError but on macOS a RuntimeError
pass

# We avoid needing to create a tiledb.Ctx() unless necessary
# This SOMAObject does not have metadata information
# associated with it yet and requires tiledb.object_type to check

obj_type = tiledb.object_type(uri, ctx=context.tiledb_ctx)
# if there is not a valid SOMAObject at the given URI, this
# returns None
soma_object = clib.SOMAObject.open(
uri, open_mode, context.native_context, (0, timestamp_ms)
)

# Avoid creating a TileDB-Py Ctx unless necessary
obj_type = (
soma_object.type
if soma_object is not None
else tiledb.object_type(uri, ctx=context.tiledb_ctx)
)

if not obj_type:
raise DoesNotExistError(f"{uri!r} does not exist")

if obj_type == "array":
if open_mode == clib.OpenMode.read and obj_type == "SOMADataFrame":
return DataFrameWrapper._from_soma_object(soma_object, context)

if obj_type in (
"SOMADataFrame",
"SOMASparseNDArray",
"SOMADenseNDArray",
"array",
):
return ArrayWrapper.open(uri, mode, context, timestamp)
if obj_type == "group":

if obj_type in (
"SOMACollection",
"SOMAExperiment",
"SOMAMeasurement",
"group",
):
return GroupWrapper.open(uri, mode, context, timestamp)

# Invalid object
Expand Down
13 changes: 7 additions & 6 deletions apis/python/src/tiledbsoma/options/_soma_tiledb_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,16 @@ def timestamp(self) -> Optional[datetime.datetime]:
@property
def native_context(self) -> clib.SOMAContext:
"""The C++ SOMAContext for this SOMA context."""
if self._native_context is None:
self._native_context = clib.SOMAContext(
{k: str(self.tiledb_config[k]) for k in self.tiledb_config}
)
return self._native_context
with self._lock:
if self._native_context is None:
self._native_context = clib.SOMAContext(
{k: str(self.tiledb_config[k]) for k in self.tiledb_config}
)
return self._native_context

@property
def tiledb_ctx(self) -> tiledb.Ctx:
"""The TileDB Context for this SOMA context."""
"""The TileDB-Py Context for this SOMA context."""
with self._lock:
if self._tiledb_ctx is None:
if self._initial_config is None:
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsoma/soma_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void load_soma_array(py::module &m) {
auto pa_schema_import = pa.attr("Schema").attr("_import_from_c");
return pa_schema_import(py::capsule(reader.arrow_schema().get()));
})
.def("ctx", &SOMAArray::ctx)
.def("context", &SOMAArray::ctx)

// After this are short functions expected to be invoked when the coords
// are Python list/tuple, or NumPy arrays. Arrow arrays are in this
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsoma/soma_collection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ void load_soma_collection(py::module &m) {
py::class_<SOMAExperiment, SOMAGroup, SOMAObject>(m, "SOMAExperiment");
py::class_<SOMAMeasurement, SOMAGroup, SOMAObject>(m, "SOMAMeasurement");
}
}
}
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsoma/soma_dataframe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void load_soma_dataframe(py::module &m) {
std::optional<std::pair<uint64_t, uint64_t>>>(&SOMADataFrame::open),
"uri"_a,
"mode"_a,
"ctx"_a,
"context"_a,
py::kw_only(),
"column_names"_a = py::none(),
"result_order"_a = ResultOrder::automatic,
Expand Down
2 changes: 1 addition & 1 deletion apis/python/src/tiledbsoma/soma_group.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ void load_soma_group(py::module &m) {
py::class_<SOMAGroup, SOMAObject>(m, "SOMAGroup")
.def_property_readonly("type", &SOMAGroup::type);
}
}
}
32 changes: 18 additions & 14 deletions apis/python/src/tiledbsoma/soma_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ void load_soma_object(py::module &m) {
OpenMode mode,
std::shared_ptr<SOMAContext> ctx,
std::optional<std::pair<uint64_t, uint64_t>> timestamp) -> py::object {
auto obj = SOMAObject::open(uri, mode, ctx, timestamp);
if (obj->type() == "SOMADataFrame")
return py::cast(dynamic_cast<SOMADataFrame&>(*obj));
else if (obj->type() == "SOMASparseNDArray")
return py::cast(dynamic_cast<SOMASparseNDArray&>(*obj));
else if (obj->type() == "SOMADenseNDArray")
return py::cast(dynamic_cast<SOMADenseNDArray&>(*obj));
else if (obj->type() == "SOMACollection")
return py::cast(dynamic_cast<SOMACollection&>(*obj));
else if (obj->type() == "SOMAExperiment")
return py::cast(dynamic_cast<SOMAExperiment&>(*obj));
else if (obj->type() == "SOMAMeasurement")
return py::cast(dynamic_cast<SOMAMeasurement&>(*obj));
TPY_ERROR_LOC("Invalid SOMAObject");
try{
auto obj = SOMAObject::open(uri, mode, ctx, timestamp);
if (obj->type() == "SOMADataFrame")
return py::cast(dynamic_cast<SOMADataFrame&>(*obj));
else if (obj->type() == "SOMASparseNDArray")
return py::cast(dynamic_cast<SOMASparseNDArray&>(*obj));
else if (obj->type() == "SOMADenseNDArray")
return py::cast(dynamic_cast<SOMADenseNDArray&>(*obj));
else if (obj->type() == "SOMACollection")
return py::cast(dynamic_cast<SOMACollection&>(*obj));
else if (obj->type() == "SOMAExperiment")
return py::cast(dynamic_cast<SOMAExperiment&>(*obj));
else if (obj->type() == "SOMAMeasurement")
return py::cast(dynamic_cast<SOMAMeasurement&>(*obj));
return py::none();
}catch(...){
return py::none();
}
})
.def_property_readonly("type", &SOMAObject::type);

Expand Down

0 comments on commit 6e47da4

Please sign in to comment.