xdas.virtual.Selection#

class xdas.virtual.Selection(shape)[source]#

Used to perform lazy selection.

It is usefull when dealing with lazy array to avoid loading unneccessary data. It must be initialized with the shape of the underlying array. It allows to track the succesive slice or single element selections made along the different dimensions of the array. Once the overall selection must be aaplied, the get_indexer method can be called to retreive a tuple of slice and/or int that can be applied to the array.

Parameters:

shape (tuple of int) – The shape of the array to slice lazily.

shape#

The shape of the array after selection is applied.

Type:

tuple of int

ndim#

The dimensionality of the array after selection is applied.

Type:

int

get_indexer()[source]#

Retrun a tuple of slice or int that can be applied to the array to effectively perform the selection

Examples

>>> import numpy as np
>>> from xdas.virtual import Selection

Selection is meant to be used on lazy arrays but here we show an example on a in-memory array for simplicity:

>>> arr = np.arange(2 * 3 * 5).reshape(2, 3, 5)

Successive indexing can be computed on the flight:

>>> expected = arr[0][:, 1:-1][::2]

Or it can be delayed up to the point where the user is done with indexing:

>>> sel = Selection(arr.shape)
>>> sel = sel[0][:, 1:-1][::2]  # Successive selections
>>> slc = sel.get_indexer()
>>> assert np.array_equal(arr[slc], expected)

The shape of the the resulting array can be known in advance:

>>> assert sel.shape == expected.shape
__init__(shape)[source]#

Methods

__init__(shape)

get_indexer()

Return a tuple of slices/ints that materialises the accumulated selections.

Attributes

ndim

Number of dimensions remaining after selections.

shape

Shape of the array after all selections are applied.