| import numpy as np |
|
|
| import tiledb |
|
|
| |
| _inttypes = (int, np.integer) |
|
|
|
|
| def _tiledb_datetime_extent(begin, end): |
| """ |
| Returns the integer extent of a datetime range. |
| |
| :param begin: beginning of datetime range |
| :type begin: numpy.datetime64 |
| :param end: end of datetime range |
| :type end: numpy.datetime64 |
| :return: Extent of range, returned as an integer number of time units |
| :rtype: int |
| """ |
| extent = end - begin + 1 |
| date_unit = np.datetime_data(extent.dtype)[0] |
| one = np.timedelta64(1, date_unit) |
| |
| return int(extent / one) |
|
|
|
|
| def index_as_tuple(idx): |
| """Forces scalar index objects to a tuple representation""" |
| if isinstance(idx, tuple): |
| return idx |
| return (idx,) |
|
|
|
|
| def replace_ellipsis(ndim: int, idx: tuple): |
| """ |
| Replace indexing ellipsis object with slice objects to match the number |
| of dimensions. |
| """ |
| |
| n_ellip = sum(1 for i in idx if i is Ellipsis) |
| if n_ellip > 1: |
| raise IndexError("an index can only have a single ellipsis ('...')") |
| elif n_ellip == 1: |
| n = len(idx) |
| if (n - 1) >= ndim: |
| |
| idx = tuple(i for i in idx if i is not Ellipsis) |
| else: |
| |
| |
| left = idx.index(Ellipsis) |
| right = n - (left + 1) |
| new_idx = idx[:left] + ((slice(None),) * (ndim - (n - 1))) |
| if right: |
| new_idx += idx[-right:] |
| idx = new_idx |
| idx_ndim = len(idx) |
| if idx_ndim < ndim: |
| idx += (slice(None),) * (ndim - idx_ndim) |
| if len(idx) > ndim: |
| raise IndexError("too many indices for array") |
| return idx |
|
|
|
|
| def replace_scalars_slice(dom, idx: tuple): |
| """Replace scalar indices with slice objects""" |
| new_idx, drop_axes = [], [] |
| for i in range(dom.ndim): |
| dim = dom.dim(i) |
| dim_idx = idx[i] |
| if np.isscalar(dim_idx): |
| drop_axes.append(i) |
| if isinstance(dim_idx, _inttypes): |
| start = int(dim_idx) |
| if start < 0: |
| start += int(dim.domain[1]) + 1 |
| stop = start + 1 |
| else: |
| start = dim_idx |
| stop = dim_idx |
| new_idx.append(slice(start, stop, None)) |
| else: |
| new_idx.append(dim_idx) |
| return tuple(new_idx), tuple(drop_axes) |
|
|
|
|
| def check_for_floats(selection): |
| """ |
| Check if a selection object contains floating point values |
| |
| :param selection: selection object |
| :return: True if selection contains floating point values |
| :rtype: bool |
| """ |
| if isinstance(selection, float): |
| return True |
| if isinstance(selection, slice): |
| if isinstance(selection.start, float) or isinstance(selection.stop, float): |
| return True |
| elif isinstance(selection, tuple): |
| for s in selection: |
| if check_for_floats(s): |
| return True |
| return False |
|
|
|
|
| def index_domain_subarray(array, dom, idx: tuple): |
| """ |
| Return a numpy array representation of the tiledb subarray buffer |
| for a given domain and tuple of index slices |
| """ |
| ndim = dom.ndim |
| if len(idx) != ndim: |
| raise IndexError( |
| "number of indices does not match domain rank: " |
| "(got {!r}, expected: {!r})".format(len(idx), ndim) |
| ) |
|
|
| subarray = list() |
| for r in range(ndim): |
| |
| dim = dom.dim(r) |
| dim_dtype = dim.dtype |
|
|
| if array.mode == "r" and ( |
| np.issubdtype(dim_dtype, np.str_) or np.issubdtype(dim_dtype, np.bytes_) |
| ): |
| |
| ned = array.nonempty_domain() |
| (dim_lb, dim_ub) = ned[r] if ned else (None, None) |
| else: |
| (dim_lb, dim_ub) = dim.domain |
|
|
| dim_slice = idx[r] |
| if not isinstance(dim_slice, slice): |
| raise IndexError("invalid index type: {!r}".format(type(dim_slice))) |
|
|
| start, stop, step = dim_slice.start, dim_slice.stop, dim_slice.step |
|
|
| if np.issubdtype(dim_dtype, np.str_) or np.issubdtype(dim_dtype, np.bytes_): |
| if start is None or stop is None: |
| if start is None: |
| start = dim_lb |
| if stop is None: |
| stop = dim_ub |
| elif not isinstance(start, (str, bytes)) or not isinstance( |
| stop, (str, bytes) |
| ): |
| raise tiledb.TileDBError( |
| f"Non-string range '({start},{stop})' provided for string dimension '{dim.name}'" |
| ) |
| subarray.append((start, stop)) |
| continue |
|
|
| if step and array.schema.sparse: |
| raise IndexError("steps are not supported for sparse arrays") |
|
|
| |
| is_datetime = dim_dtype.kind == "M" |
|
|
| |
| if start is not None and stop is not None: |
| if type(start) != type(stop): |
| promoted_dtype = np.promote_types(type(start), type(stop)) |
| start = np.array(start, dtype=promoted_dtype, ndmin=1)[0] |
| stop = np.array(stop, dtype=promoted_dtype, ndmin=1)[0] |
|
|
| if start is not None: |
| if is_datetime and not isinstance(start, np.datetime64): |
| raise IndexError( |
| "cannot index datetime dimension with non-datetime interval" |
| ) |
| |
| if np.issubdtype(dim_dtype, np.integer): |
| if isinstance(start, (np.float32, np.float64)): |
| raise IndexError( |
| "cannot index integral domain dimension with floating point slice" |
| ) |
| elif not isinstance(start, _inttypes): |
| raise IndexError( |
| "cannot index integral domain dimension with non-integral slice (dtype: {})".format( |
| type(start) |
| ) |
| ) |
| |
| if not is_datetime and start < 0: |
| start += int(dim_ub) + 1 |
| if start < dim_lb: |
| |
| |
| |
| raise IndexError("index out of bounds <todo>") |
| else: |
| start = dim_lb |
| if stop is not None: |
| if is_datetime and not isinstance(stop, np.datetime64): |
| raise IndexError( |
| "cannot index datetime dimension with non-datetime interval" |
| ) |
| |
| if np.issubdtype(dim_dtype, np.integer): |
| if isinstance(start, (np.float32, np.float64)): |
| raise IndexError( |
| "cannot index integral domain dimension with floating point slice" |
| ) |
| elif not isinstance(start, _inttypes): |
| raise IndexError( |
| "cannot index integral domain dimension with non-integral slice (dtype: {})".format( |
| type(start) |
| ) |
| ) |
| if not is_datetime and stop < 0: |
| stop = np.int64(stop) + dim_ub |
| if stop > dim_ub: |
| |
| |
| if is_datetime: |
| stop = dim_ub |
| else: |
| stop = int(dim_ub) + 1 |
| else: |
| if np.issubdtype(dim_dtype, np.floating) or is_datetime: |
| stop = dim_ub |
| else: |
| stop = int(dim_ub) + 1 |
|
|
| if np.issubdtype(type(stop), np.floating): |
| |
| start = dim_dtype.type(start) |
| stop = dim_dtype.type(stop) |
| subarray.append((start, stop)) |
| elif is_datetime: |
| |
| |
| start = start.astype(dim_dtype) |
| stop = stop.astype(dim_dtype) |
| subarray.append((start, stop)) |
| elif np.issubdtype(type(stop), np.integer): |
| |
| subarray.append((start, int(stop) - 1)) |
| else: |
| raise IndexError( |
| "domain indexing is defined for integral and floating point values" |
| ) |
| return subarray |
|
|