Skip to content

Commit

Permalink
Simplify inheritance of Array class from CloudArray
Browse files Browse the repository at this point in the history
  • Loading branch information
kounelisagis committed Jan 15, 2025
1 parent e1f57bd commit e23d31c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 37 deletions.
29 changes: 6 additions & 23 deletions tiledb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .consolidation_plan import ConsolidationPlan
from .ctx import Config, Ctx, default_ctx, scope_ctx
from .dataframe_ import from_csv, from_pandas, open_dataframe
from .dense_array import DenseArrayImpl
from .dense_array import DenseArray
from .dimension import Dim
from .dimension_label import DimLabel
from .dimension_label_schema import DimLabelSchema
Expand Down Expand Up @@ -93,7 +93,7 @@
from .query import Query
from .query_condition import QueryCondition
from .schema_evolution import ArraySchemaEvolution
from .sparse_array import SparseArrayImpl
from .sparse_array import SparseArray
from .stats import (
stats_disable,
stats_dump,
Expand All @@ -107,6 +107,10 @@
__version__ = version.version
group_create = Group.create

# backwards compatibility
SparseArrayImpl = SparseArray
DenseArrayImpl = DenseArray

# Note: we use a modified namespace packaging to allow continuity of existing TileDB-Py imports.
# Therefore, 'tiledb/__init__.py' must *only* exist in this package.
# Furthermore, in sub-packages, the `find_packages` helper will not work at the
Expand All @@ -118,24 +122,3 @@
#
# Note: 'pip -e' in particular will not work without this declaration:
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

# If tiledb.cloud is installed, add CloudArray methods to TileDB arrays
try:
from tiledb.cloud.cloudarray import CloudArray
except ImportError:

class DenseArray(DenseArrayImpl):
pass

class SparseArray(SparseArrayImpl):
pass

else:

class DenseArray(DenseArrayImpl, CloudArray):
pass

class SparseArray(SparseArrayImpl, CloudArray):
pass

del CloudArray
25 changes: 16 additions & 9 deletions tiledb/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,14 @@ def preload_array(uri, mode, key, timestamp, ctx=None):
return lt.Array(ctx, uri, query_type, (ts_start, ts_end))


class Array:
# If tiledb.cloud is installed, add CloudArray methods to TileDB Arrays
try:
from tiledb.cloud.cloudarray import CloudArray
except ImportError:
CloudArray = object


class Array(CloudArray):
"""Base class for TileDB array objects.
Defines common properties/functionality for the different array types. When
Expand Down Expand Up @@ -373,14 +380,14 @@ def create(cls, uri, schema, key=None, overwrite=False, ctx=None):
)
ctx = ctx or default_ctx()

from .dense_array import DenseArrayImpl
from .sparse_array import SparseArrayImpl
from .dense_array import DenseArray
from .sparse_array import SparseArray

if issubclass(cls, DenseArrayImpl) and schema.sparse:
if issubclass(cls, DenseArray) and schema.sparse:
raise ValueError(
"Array.create `schema` argument must be a dense schema for DenseArray and subclasses"
)
if issubclass(cls, SparseArrayImpl) and not schema.sparse:
if issubclass(cls, SparseArray) and not schema.sparse:
raise ValueError(
"Array.create `schema` argument must be a sparse schema for SparseArray and subclasses"
)
Expand Down Expand Up @@ -416,17 +423,17 @@ def load_typed(cls, uri, mode="r", key=None, timestamp=None, attr=None, ctx=None
if ctx is None:
ctx = default_ctx()

from .dense_array import DenseArrayImpl
from .sparse_array import SparseArrayImpl
from .dense_array import DenseArray
from .sparse_array import SparseArray

tmp_array = preload_array(uri, mode, key, timestamp, ctx)

if tmp_array._schema()._array_type == lt.ArrayType.SPARSE:
return SparseArrayImpl(
return SparseArray(
uri, mode, key, timestamp, attr, ctx, preloaded_array=tmp_array
)
else:
return DenseArrayImpl(
return DenseArray(
uri, mode, key, timestamp, attr, ctx, preloaded_array=tmp_array
)

Expand Down
4 changes: 2 additions & 2 deletions tiledb/dense_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .subarray import Subarray


class DenseArrayImpl(Array):
class DenseArray(Array):
"""Class representing a dense TileDB array.
Inherits properties and methods of :py:class:`tiledb.Array`.
Expand Down Expand Up @@ -565,7 +565,7 @@ def _setitem_impl(self, selection, val, nullmaps: dict):
values.append(val)
else:
dtype = self.schema.attr(self.view_attr).dtype
with DenseArrayImpl(
with DenseArray(
self.uri, "r", ctx=tiledb.Ctx(self.ctx.config())
) as readable:
current = readable[selection]
Expand Down
4 changes: 2 additions & 2 deletions tiledb/domain_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ def __getitem__(self, idx):
"order must be 'C' (TILEDB_ROW_MAJOR), 'F' (TILEDB_COL_MAJOR), or 'G' (TILEDB_GLOBAL_ORDER)"
)

if isinstance(self.array, tiledb.sparse_array.SparseArrayImpl):
if isinstance(self.array, tiledb.sparse_array.SparseArray):
return self.array._read_sparse_subarray(
subarray, attr_names, attr_cond, layout
)
elif isinstance(self.array, tiledb.dense_array.DenseArrayImpl):
elif isinstance(self.array, tiledb.dense_array.DenseArray):
return self.array._read_dense_subarray(
subarray, attr_names, attr_cond, layout, coords
)
Expand Down
2 changes: 1 addition & 1 deletion tiledb/sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def _setitem_impl_sparse(self, selection, val, nullmaps: dict):
)


class SparseArrayImpl(Array):
class SparseArray(Array):
"""Class representing a sparse TileDB array (internal).
Inherits properties and methods of :py:class:`tiledb.Array`.
Expand Down
4 changes: 4 additions & 0 deletions tiledb/subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,7 @@ def shape(self) -> Optional[Sequence[tuple]]:
return None
shape = self._shape(self._ctx)
return tuple(length for length in shape)

def dump(self):
"""Prints the subarray ranges to stdout."""
self._dump(self._ctx)

0 comments on commit e23d31c

Please sign in to comment.