deeplake.core.index
- class deeplake.core.index.IndexEntry(value: Union[int, slice, Tuple[int, ...]] = slice(None, None, None))
- __getitem__(item: Union[int, slice, Tuple[int, ...]])
Combines the given
item
and this IndexEntry. Returns a new IndexEntry representing the composition of the two.Examples
>>> IndexEntry()[0:100] IndexEntry(slice(0, 100, None))
>>> IndexEntry()[100:200][5] IndexEntry(105)
>>> IndexEntry()[(0, 1, 2, 3)] IndexEntry((0, 1, 2, 3))
>>> IndexEntry()[1, 2, 3] IndexEntry((0, 1, 2, 3))
- Parameters
item – The desired sub-index to be composed with this IndexEntry. Can be an int, a slice, or a tuple of ints.
- Returns
The new IndexEntry object.
- Return type
- Raises
TypeError – An integer IndexEntry should not be indexed further.
- __init__(value: Union[int, slice, Tuple[int, ...]] = slice(None, None, None))
- __str__()
Return str(self).
- __weakref__
list of weak references to the object (if defined)
- downsample(factor: int, length: int)
Downsamples an IndexEntry by a given factor.
- Parameters
factor (int) – The factor by which to downsample.
length (int) – The length of the downsampled IndexEntry.
- Returns
The downsampled IndexEntry.
- Return type
- Raises
TypeError – If the IndexEntry cannot be downsampled.
- indices(length: int)
Generates the sequence of integer indices for a target of a given length.
- is_trivial()
Checks if an IndexEntry represents the entire slice
- length(parent_length: int) int
Returns the length of an IndexEntry given the length of the parent it is indexing.
Examples
>>> IndexEntry(slice(5, 10)).length(100) 5 >>> len(list(range(100))[5:10]) 5 >>> IndexEntry(slice(5, 100)).length(50) 45 >>> len(list(range(50))[5:100]) 45 >>> IndexEntry(0).length(10) 1
- Parameters
parent_length (int) – The length of the target that this IndexEntry is indexing.
- Returns
The length of the index if it were applied to a parent of the given length.
- Return type
int
- subscriptable()
Returns whether an IndexEntry can be further subscripted.
- validate(parent_length: int)
Checks that the index is not accessing values outside the range of the parent.
- class deeplake.core.index.Index(item: Union[int, slice, Tuple[int, ...], Index, List[IndexEntry]] = slice(None, None, None))
- __getitem__(item: Union[int, slice, List[int], Tuple[Union[int, slice, Tuple[int, ...]]], Index])
Returns a new Index representing a subscripting with the given item. Modeled after NumPy’s advanced integer indexing.
See: https://numpy.org/doc/stable/reference/arrays.indexing.html
Examples
>>> Index([5, slice(None)])[5] Index([5, 5])
>>> Index([5])[5:6] Index([5, slice(5, 6)])
>>> Index()[0, 1, 2:5, 3] Index([0, 1, slice(2, 5), 3])
>>> Index([slice(5, 6)])[(0, 1, 2:5, 3),] Index([(5, 1, slice(2, 5), 3)])
- Parameters
item – The contents of the subscript expression to add to this Index.
- Returns
The Index representing the result of the subscript operation.
- Return type
- Raises
TypeError – Given item should be another Index, or compatible with NumPy’s advanced integer indexing.
- __init__(item: Union[int, slice, Tuple[int, ...], Index, List[IndexEntry]] = slice(None, None, None))
Initializes an Index from an IndexValue, another Index, or the values from another Index.
Represents a list of IndexEntry objects corresponding to indexes into each axis of an ndarray.
- __repr__()
Return repr(self).
- __str__()
Return str(self).
- __weakref__
list of weak references to the object (if defined)
- apply(samples: List[ndarray])
Applies an Index to a list of ndarray samples with the same number of entries as the first entry in the Index.
- apply_squeeze(samples: List[ndarray])
Applies the primary axis of an Index to a list of ndarray samples. Will either return the list as given, or return the first sample.
- compose_at(item: Union[int, slice, Tuple[int, ...]], i: Optional[int] = None)
Returns a new Index representing the addition of an IndexValue, or the composition with a given axis.
Examples
>>> Index([slice(None), slice(None)]).compose_at(5) Index([slice(None), slice(None), 5])
>>> Index([slice(None), slice(5, 10), slice(None)]).compose_at(3, 1) Index([slice(None), 8, slice(None)])
- Parameters
item (IndexValue) – The value to append or compose with the Index.
i (int, optional) – The axis to compose with the given item. Defaults to None, meaning that the item will be appended instead.
- Returns
The result of the addition or composition.
- Return type
- downsample(factor: int, shape: Tuple[int, ...])
Downsamples an Index by the given factor.
- Parameters
factor (int) – The factor to downsample by.
shape (Tuple[int, ...]) – The shape of the downsampled data.
- Returns
The downsampled Index.
- Return type
- find_axis(offset: int = 0)
Returns the index for the nth subscriptable axis in the values of an Index.
- Parameters
offset (int) – The number of subscriptable axes to skip before returning. Defaults to 0, meaning that the first valid axis is returned.
- Returns
The index of the found axis, or None if no match is found.
- Return type
int
- is_trivial()
Checks if an Index is equivalent to the trivial slice [:], aka slice(None).
- length(parent_length: int)
Returns the primary length of an Index given the length of the parent it is indexing. See:
IndexEntry.length()
- validate(parent_length)
Checks that the index is not accessing values outside the range of the parent.
- deeplake.core.index.merge_slices(existing_slice: slice, new_slice: slice) slice
Compose two slice objects
Given an iterable x, the following should be equivalent:
x[existing_slice][new_slice] == x[merge_slices(existing_slice, new_slice)]
- Parameters
existing_slice (slice) – The existing slice to be restricted.
new_slice (slice) – The new slice to be applied to the existing slice.
- Returns
the composition of the given slices
- Return type
slice
- Raises
NotImplementedError – Composing slices with negative values is not supported. Negative indexing for slices is only supported for the first slice.
- deeplake.core.index.slice_at_int(s: slice, i: int)
Returns the
i
th element of a slices
.Examples
>>> slice_at_int(slice(None), 10) 10
>>> slice_at_int(slice(10, 20, 2), 3) 16
- Parameters
s (slice) – The slice to index into.
i (int) – The integer offset into the slice.
- Returns
The index corresponding to the offset into the slice.
- Return type
int
- Raises
NotImplementedError – Nontrivial slices should not be indexed with negative integers.
IndexError – If step is negative and start is not greater than stop.
- deeplake.core.index.slice_length(s: slice, parent_length: int) int
Returns the length of a slice given the length of its parent.