brainhops._core.proxy
Lazy arrays that read their values only when they are needed.
A proxy stands for an array whose values lie elsewhere, usually in a
file, and reads them only when NumPy converts it with np.asarray, or
when a computation, such as proxy + 1 or np.mean(proxy), needs
them. Indexing a proxy, transposing it or reshaping it returns another
proxy, which reads only the values that it needs. The design is
described in the memo spec/design/array-proxies.md.
ArrayProxyProtocol states what a consumer of a proxy may rely on,
and ArrayProxy is the base class of every proxy of brainhops. The
proxies fall in three groups:
- a
StoredProxypresents an array as its storage holds it, and reads it one box at a time. The proxy of each file format is one, and so is aPermutedProxy, which presents the axes of another stored proxy in another order, and aReshapedProxy, which presents the values of another proxy in another shape; - an
IndexedProxypresents a part of a stored proxy, which an index selects. Indexing a stored proxy returns one; - a
ScaledProxypresents the values of another proxy multiplied by a factor and shifted by an offset, as many formats store integers that stand for real values.
Indexing and transposing keep these layers in a fixed order. A scaled
proxy holds an indexed proxy, or a stored one, and an indexed proxy
holds a stored proxy, so a chain of indexes never builds a chain of
proxies. A reshape that cannot be done otherwise starts the order
again: a reshaped proxy is a stored proxy that holds any other proxy,
and reshaping it again reshapes that proxy. Each layer keeps the proxy
below it as base, and the root of every proxy is the stored proxy
that a reader built, at the bottom.
Classes
ArrayProxyProtocol
Bases: Protocol
What a consumer may rely on when it receives an array or a proxy.
A NumPy array, a Dask array, a nibabel ArrayProxy and an
ArrayProxy all satisfy the protocol. Indexing a proxy returns
another proxy without reading anything, whereas indexing a NumPy
array or a nibabel proxy returns a NumPy array. A consumer that needs
the values of an index must therefore call np.asarray on the
result.
ArrayProxy
Bases: NDArrayOperatorsMixin, Magic
A lazy array, which reads its values when they are needed.
A proxy is the object that a reader keeps instead of the voxels of a
file, so that the voxels are read only when they are needed, and
only the part of them that is needed. Indexing the proxy, as in
proxy[index], and transposing it return another proxy without
reading anything. The values are read when NumPy converts the proxy
with np.asarray, or when Dask computes the array that
to_dask returns.
The class defines what every proxy has: its shape, the type of its
values, the blocks in which it prefers to be read, and the
conversions to NumPy, CuPy and Dask. A subclass defines shape,
dtype, __getitem__, transpose and __array__. A file format
derives its proxy from StoredProxy, which implements the last
three from a method that reads a box.
The class is a Magic class without fields, so
that a subclass declares its state as fields and has its constructor
built from them. The fields of a subclass convert their arguments to
their types, and its representation shows the fields that do not
hold their defaults. The methods that a subclass defines are
abstract, since the metaclass of Magic is that of an abstract base
class, so a subclass that forgets one cannot be built.
A proxy can also be used where an array is expected, and it reads
its values only when a computation needs them. The operators of
Python, such as proxy + 1 or proxy == 0, the functions of NumPy,
such as np.mean(proxy), and the methods of a NumPy array, such as
proxy.sum(), all compute on the values. Asking for the shape, the
type of the values or a transposition reads nothing, through the
proxy or through NumPy, as in np.shape(proxy), and so does a
reshape, which returns another proxy. A computation runs in the
backend that promote_backends of brainhops._core.backends
chooses for its operands: next to a CuPy array, the proxy is read
onto the GPU, next to a Dask array, it becomes a Dask array that
reads nothing yet, and alone, it becomes what
to_backend makes of
it. The values that a computation reads are not kept, so a second
computation reads them again. A proxy is read-only: assigning values
into it, or using it as the output of a computation, raises a
TypeError. Like a NumPy array, a proxy cannot be hashed.
Attributes
shape
abstractmethod
property
shape: tuple[int, ...]
The shape of the array that the proxy presents.
chunks
property
chunks: tuple[int, ...] | None
The block shape that the proxy prefers, or None.
The blocks are the units that the storage reads at once, such as
the chunks of a Zarr array, the tiles of a slide or the slices
of a stack. None means that the storage has no preference.
root
abstractmethod
property
root: StoredProxy
The stored proxy that a reader built, which the values come from.
An index, a permutation or a scaling of a proxy keeps the proxy
below it as base, and its root is the root of base. A writer
compares the file of the root with the file that it is about to
write, so that it does not empty a file that the array still
reads.
Methods:
__getitem__
abstractmethod
__getitem__(index: Any) -> ArrayProxy
Return a proxy that selects a part of the values.
A proxy is indexed lazily with integers, slices of any step, the
ellipsis, None, and one array of integers along one axis, and
the result is what NumPy gives for the same index. Indexing with
several arrays, or with a boolean mask, would select points
rather than a box, so it raises an IndexError that asks for
np.asarray(proxy)[index] instead.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
transpose
abstractmethod
transpose(*axes: Any) -> ArrayProxy
Return a proxy with its axes in another order, as NumPy does.
Without an argument, or with None, the order of the axes is
reversed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
__array__
abstractmethod
Read the values.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
StoredProxy
Bases: ArrayProxy
A lazy array that presents an array as its storage holds it.
The proxy of a file format derives from this class. It describes the
stored array with two properties, shape and dtype, and reads a
box of it with one method, _read(box). It may also define
chunks, the block shape that the storage prefers. The base class
provides the rest.
Indexing a stored proxy returns an IndexedProxy, which holds
the stored proxy and the index. A read of the indexed proxy calls
_read with the smallest box with a positive step that holds the
selected values, and then applies in memory the negative steps, the
arrays of positions and the new axes, so that _read never sees
them. An index that selects every value in order returns the stored
proxy itself.
Transposing a stored proxy returns a PermutedProxy, which is
itself a stored proxy. A subclass that can present its axes in
another order without moving any value, as
[StridedProxy][brainhops._core.strided.StridedProxy] does by
permuting its strides, overrides transpose to do so.
A stored proxy that a reader built is the one that every indexed proxy reads through, so a handle that it opens on its first read, such as an open store, is opened once and shared by all of them.
Attributes
shape
abstractmethod
property
shape: tuple[int, ...]
The shape of the array that the proxy presents.
chunks
property
chunks: tuple[int, ...] | None
The block shape that the proxy prefers, or None.
The blocks are the units that the storage reads at once, such as
the chunks of a Zarr array, the tiles of a slide or the slices
of a stack. None means that the storage has no preference.
Methods:
__getitem__
__getitem__(index: Any) -> ArrayProxy
Return a proxy that selects a part of the values.
The result is an IndexedProxy of this proxy, or this proxy
itself when the index selects every value in order.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
transpose
transpose(*axes: Any) -> StoredProxy
Return the proxy with its axes in another order.
The result is a PermutedProxy of this proxy, or this proxy
itself when the permutation changes nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
StoredProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
__array__
Read all the values.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
PermutedProxy
magic
Bases: StoredProxy
A stored proxy that presents the axes of another one in another order.
Transposing a StoredProxy returns a permuted proxy, so that an
IndexedProxy never has to reorder axes itself: transposing an
indexed proxy transposes the stored proxy that it holds. Axis i of
the permuted proxy is axis perm[i] of base.
Transposing a permuted proxy composes the two permutations and
transposes base once, so permuted proxies never nest. A read
reorders the box for base, reads it, and transposes the values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Any
|
The stored proxy whose axes are permuted. |
required |
perm
|
tuple[int, ...]
|
The axis of |
required |
Attributes
chunks
property
chunks: tuple[int, ...] | None
The block shape of base, in the order of the permutation.
Methods:
transpose
transpose(*axes: Any) -> StoredProxy
Return base transposed by the two permutations, composed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
StoredProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
__getitem__
__getitem__(index: Any) -> ArrayProxy
Return a proxy that selects a part of the values.
The result is an IndexedProxy of this proxy, or this proxy
itself when the index selects every value in order.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
__array__
Read all the values.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
NewAxis
Bases: NamedTuple
An axis that an IndexedProxy presents but that is not stored.
None in an index adds an axis of length one, which holds a single
value. A later index may keep that value, remove it, or repeat it,
so the entry keeps the positions of the value that are presented:
a slice of range(1) keeps the value or removes it, and an array
of zeros repeats it.
IndexedProxy
magic
Bases: ArrayProxy
A lazy array that presents a part of a stored proxy.
Indexing a StoredProxy returns an indexed proxy, which holds
the stored proxy as base and the index as selection. The
selection is an index in the form that NumPy accepts, normalized so
that it has one entry for each axis of base, in order:
- an integer is a position, and the axis is dropped;
- a slice has an integer start and step, and its stop is an integer
or
None. It keeps the positions that NumPy keeps for it; - an array of integers keeps those positions, in that order.
A NewAxis entry is inserted wherever the proxy presents an
axis that base does not store, as None does in an index.
Indexing an indexed proxy composes the index with the selection, and
transposing it transposes base and moves the entries of the
selection with their axes. The result holds the same stored proxy,
or a PermutedProxy of it, so a chain of indexes never builds a
chain of proxies, and every read goes through the stored proxy that
a reader built. An index that selects every value of base in order
returns base itself.
A read calls the _read method of base with the smallest box with
a positive step that holds the selected values. The negative steps,
the arrays of positions and the new axes are then applied in memory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Any
|
The stored proxy whose values are selected. |
required |
selection
|
tuple[Any, ...]
|
One entry for each axis of |
required |
Attributes
selection
instance-attribute
selection: tuple[Any, ...]
One entry for each axis of base, in order, with a NewAxis
entry for each axis that is not stored.
chunks
property
chunks: tuple[int, ...] | None
The block shape that the proxy prefers, or None.
The blocks of base are carried through the selection. Along an
axis that a step subsamples, a block holds as many values as the
stored block covers, and a new axis has blocks of one value.
Methods:
__getitem__
__getitem__(index: Any) -> ArrayProxy
Return a proxy that selects a part of the values.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
transpose
transpose(*axes: Any) -> ArrayProxy
Return a proxy with its axes in another order, as NumPy does.
The entries of the selection move with their axes, and the
stored axes of base are transposed to follow them, so that the
selection stays in the order of the axes of the stored proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
__array__
Read the selected values.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
ScaledProxy
magic
Bases: ArrayProxy
A lazy array of the values of another proxy, scaled.
Many formats store integers that stand for real values, together
with a factor and an offset: the value is slope * stored + inter.
The factor and the offset may be single numbers, or arrays that
broadcast against the stored array, such as one factor for each
volume. A scaled proxy applies them to the values of base, which
reads the stored integers.
Indexing or transposing the proxy does the same to base and to
the factors, so that only the selected values are read and scaled.
An axis of length one of a factor stays of length one, so the
factors never grow to the shape of the values.
The type of the scaled values depends on the stored type only: it is the floating-point type that NumPy gives for the stored type and a float32, so that integers of 8 or 16 bits and float32 values give float32, and other integers and float64 values give float64. The factors are cast to that type before they are applied, so a factor of a wider type neither widens the result nor makes a wider copy of the values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Any
|
The proxy of the values before scaling. |
required |
slope
|
ndarray | None
|
The factor, a number or an array that broadcasts against the shape of |
None
|
inter
|
ndarray | None
|
The offset, a number or an array that broadcasts against the shape of |
None
|
Attributes
slope
class-attribute
instance-attribute
slope: ndarray | None = None
The factor, a number or an array that broadcasts against the shape
of base, or None.
inter
class-attribute
instance-attribute
inter: ndarray | None = None
The offset, a number or an array that broadcasts against the shape
of base, or None.
dtype
property
dtype: dtype
The type of the scaled values, which the stored type decides.
It is the floating-point type that NumPy gives for the stored type and a float32, whatever the type of the factors.
Methods:
__getitem__
__getitem__(index: Any) -> ScaledProxy
Return the scaled proxy of a part of the values.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
transpose
transpose(*axes: Any) -> ScaledProxy
Return the scaled proxy of the transposed values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
ScaledProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
__array__
Read the values of base and scale them.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
ReshapedProxy
magic
Bases: StoredProxy
A lazy array of the values of another proxy, in another shape.
The values are those of base, placed in the new shape in C order,
as np.reshape places them. A reshaped proxy is indexed and
transposed as the proxy of a file is, and its root is the root of
base. Reshaping it again reshapes base.
A read gets the rows of base that hold the selected values: along
each group of axes that the reshape splits or merges, at most one
extra row on each side.
ArrayProxy.reshape
returns a reshaped proxy when the reshape does more than add or
remove axes of length one, unless base is a strided proxy whose
values NumPy could reshape without copying them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Any
|
The proxy whose values are presented in another shape. |
required |
shape
|
tuple[int, ...]
|
The new shape, given as |
required |
Attributes
chunks
property
chunks: tuple[int, ...] | None
The block shape that the proxy prefers, or None.
The blocks are the units that the storage reads at once, such as
the chunks of a Zarr array, the tiles of a slide or the slices
of a stack. None means that the storage has no preference.
Methods:
__getitem__
__getitem__(index: Any) -> ArrayProxy
Return a proxy that selects a part of the values.
The result is an IndexedProxy of this proxy, or this proxy
itself when the index selects every value in order.
Raises:
| Type | Description |
|---|---|
IndexError
|
If an index is out of bounds, if there are more indices than dimensions, or if the index selects points rather than a box. |
TypeError
|
If an element of the index has an unsupported type. |
transpose
transpose(*axes: Any) -> StoredProxy
Return the proxy with its axes in another order.
The result is a PermutedProxy of this proxy, or this proxy
itself when the permutation changes nothing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*axes
|
int or sequence of int
|
The permutation, given as integers or as one sequence. Axis
|
()
|
Returns:
| Type | Description |
|---|---|
StoredProxy
|
The transposed proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the axes are not a permutation of the axes of the proxy. |
reshape
reshape(
*shape: Any, order: str = "C", copy: bool | None = None
) -> ArrayProxy
Return a proxy of the values in another shape, as NumPy does.
Nothing is read. A reshape that only adds or removes axes of
length one is an index of the proxy. A strided proxy whose
values NumPy could reshape without copying them gives another
strided proxy (see
[StridedProxy][brainhops._core.strided.StridedProxy]). Any
other reshape gives a ReshapedProxy, which reads the
values that a selection needs, and at most a few extra rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*shape
|
int or sequence of int
|
The new shape, given as integers or as one sequence. One
length may be |
()
|
order
|
(C, F)
|
The order in which the values are read and placed: the last axis varies fastest in C order, and the first one in Fortran order. |
"C"
|
copy
|
bool
|
Accepted for compatibility with |
None
|
Returns:
| Type | Description |
|---|---|
ArrayProxy
|
The reshaped proxy. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the new shape does not hold as many values as the proxy,
or |
__array__
Read all the values.
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
to_numpy
to_numpy() -> ndarray
to_cupy
Read the values of the proxy into a CuPy array on the GPU.
Returns:
| Type | Description |
|---|---|
ndarray
|
The values. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If CuPy is not installed. |
to_backend
Convert the proxy as a backend setting says.
The settings are described in brainhops.backends.
Under "native", and under "auto", a proxy of at most the
threshold of "auto" is read with NumPy and a larger one becomes
a Dask array.
A Dask array has the blocks that
to_dask gives, and
reading it reads only the blocks that it needs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str or module
|
The setting. By default, the current setting is used. |
None
|
Returns:
| Type | Description |
|---|---|
array
|
A NumPy, CuPy or Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If a package that the setting needs is not installed. |
to_dask
Return a Dask array that reads the proxy block by block.
The blocks are the chunks of the proxy when the storage
prefers some, and the default blocks of Dask otherwise. Dask
receives the type of the values through its meta argument, so
it does not read the proxy to learn it. With a CUDA device, each
block is moved to the GPU once it is read, which gives the
setting "dask+cupy". The functions of Dask, such as
dask.array.transpose, call this method under the name
to_dask_array when they receive a proxy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
str or int
|
The device for the chunks. |
"cpu"
|
**kwargs
|
dict
|
Options passed to |
{}
|
Returns:
| Type | Description |
|---|---|
Array
|
The lazy Dask array. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Dask is not installed, or CuPy when a CUDA device is requested. |
__array_ufunc__
Apply a universal function of NumPy, such as np.add.
The operators of Python, such as proxy + 1, call this method
too. The proxies among the operands are
converted to the backend of the computation, and so are the
arrays of other backends. When the computation writes into the
arrays given as out, it runs in their backend. Otherwise, the
backend is the one that promote_backends chooses for the
operands.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ufunc
|
ufunc
|
The universal function. |
required |
method
|
str
|
The method of the function, such as |
required |
*inputs
|
Any
|
The operands. |
()
|
**kwargs
|
Any
|
The options of the function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |
Raises:
| Type | Description |
|---|---|
TypeError
|
If a proxy is given as |
__array_function__
__array_function__(
func: Callable,
types: Collection[type],
args: tuple[Any, ...],
kwargs: dict[str, Any],
) -> Any
Apply a function of NumPy, such as np.concatenate.
When the computation runs in NumPy, the implementation of NumPy
receives the proxies themselves. It asks them for what it needs,
so that np.shape reads nothing, np.transpose and
np.reshape return proxies, and np.mean calls the method of
the proxy, which reads the values. In any other backend, the
proxies and the arrays of other backends are converted to the
backend that promote_backends chooses for them, and the
function of that backend runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
callable
|
The function of NumPy. |
required |
types
|
collection of type
|
The types of the operands that define |
required |
args
|
tuple
|
The positional arguments of the call. |
required |
kwargs
|
dict
|
The keyword arguments of the call. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result, or |