Skip to content

brainhops.backends

Computational backends.

brainhops supports multiple computational backends, which allow users to make optimal user of their computational infrastructure (threads, processes, GPUs, computing clusters, etc.).

Backends can be set globally with set_backend or locally with the backend context manager. The setting decides in which backend a computation runs and where its result lives, for example when an image is resliced or when the lazy array of a file is summed. Reading the data of an object does not convert it: data returns the array that the object stores, and the to_* and as_* methods convert it on request. Under the default setting, "native", a computation runs where its operands live, and operands that live in different backends are promoted to one backend (see promote_backends in brainhops._core.backends):

  • if one of the operands is a Dask array, the computation is lazy;
  • if one of the operands is on a GPU, the computation runs on the GPU;
  • otherwise, the computation runs with NumPy.

The lazy array of a file counts as the array that "auto" would make of it.

The following backend settings are supported:

Setting On CPU or GPU memory Eagerly or lazily
"native" as the array is as the array is
"auto" CPU memory depends on array size
"auto+cupy" GPU memory depends on array size
"numpy" CPU memory eagerly
"cupy" GPU memory eagerly
"dask" CPU memory lazily, in NumPy chunks
"dask+cupy" GPU memory lazily, in CuPy chunks

When dask is installed and the "auto" setting is selected, arrays that are larger than get_auto_threshold bytes are promoted to Dask arrays, while smaller arrays are kept as NumPy arrays.

The "auto+cupy" setting behaves similarly to "auto", but returns either a CuPy array, or a Dask array with CuPy chunks.

A setting can be set by a module (numpy, cupy or dask.array) instead of a name.

Functions:

backend

backend(
    backend: BackendLike | None = None,
    threshold: int | None = None,
) -> Generator[Backend, None, None]

Select a backend setting inside a with block.

The previous setting and threshold are restored on exit. Without an argument, or with None, the current setting is kept, whereas backend("native") selects the setting that converts nothing.

Example

with backend("cupy"):
    x = np.arange(3)
    y = to_backend(x)

Parameters:

Name Type Description Default
backend (native, numpy, cupy, dask[+cupy], auto[+cupy])

The setting to select. By default, the current setting is kept.

"native"
threshold int

The number of bytes up to which "auto" keeps arrays at once. By default, the current threshold is kept.

None

Yields:

Type Description
{native, numpy, cupy, dask[+cupy], auto[+cupy]}

The setting in use.

backends

backends() -> tuple[ArrayBackend, ...]

Return the names of the installed backends, most preferred first.

A backend counts as installed when its array package is installed. Interpolation runs with brainhops._ext.interpol, which needs nothing else.

get_auto_threshold

get_auto_threshold() -> int

Return the number of bytes up to which "auto" keeps arrays at once.

get_backend

get_backend() -> Backend

Return the current backend setting.

set_auto_threshold

set_auto_threshold(nbytes: int | None = None) -> None

Set the number of bytes up to which "auto" keeps arrays at once.

Raises:

Type Description
ValueError

If the number is negative.

set_backend

set_backend(backend: BackendLike | None = None) -> None

Set the current backend setting, given by name or by module.

Passing the result of [array_backend][brainhops._core.backends.array_backend] selects the backend of an array. The installed backends are listed by backends.

Raises:

Type Description
ValueError

If the argument is not a setting.

ImportError

If a package that the setting needs is not installed.