Skip to content

computation

batcharray.computation

Contain the computation models.

Note

max, min and sum have the same names as Python builtins. Use them through the namespace (cmpt.max(arr)) and avoid from batcharray.computation import *, which would shadow the builtins.

batcharray.computation.ArrayComputationModel

Bases: BaseComputationModel[ndarray]

Implement a computation model for numpy.ndarrays.

batcharray.computation.AutoComputationModel

Bases: BaseComputationModel[ArrayT]

Implement a computation model that automatically finds the right computation model based on the array type.

Note

concatenate selects the computation model from the type of the first array. With mixed np.ndarray / np.ma.MaskedArray inputs, a masked array first keeps the masks, but a plain array first silently drops the masks of the masked arrays that follow (their data is used as is). Convert the arrays to a common type first if this matters.

Example
>>> import numpy as np
>>> from batcharray.computation import AutoComputationModel
>>> comp_model = AutoComputationModel()
>>> arrays = [
...     np.array([[0, 1, 2], [4, 5, 6]]),
...     np.array([[10, 11, 12], [13, 14, 15]]),
... ]
>>> out = comp_model.concatenate(arrays, axis=0)
>>> out
array([[ 0,  1,  2],
       [ 4,  5,  6],
       [10, 11, 12],
       [13, 14, 15]])

batcharray.computation.AutoComputationModel.add_computation_model classmethod

add_computation_model(
    array_type: type[ndarray],
    comp_model: BaseComputationModel[ArrayT],
    exist_ok: bool = False,
) -> None

Add a computation model for a given array type.

Parameters:

Name Type Description Default
array_type type[ndarray]

The array type.

required
comp_model BaseComputationModel[ArrayT]

The computation model to use for the given array type.

required
exist_ok bool

If False, RuntimeError is raised if the data type already exists. This parameter should be set to True to overwrite the computation model for an array type.

False

Raises:

Type Description
RuntimeError

if a computation model is already registered for the array type and exist_ok=False.

Example
>>> import numpy as np
>>> from batcharray.computation import AutoComputationModel, ArrayComputationModel
>>> AutoComputationModel.add_computation_model(
...     np.ndarray, ArrayComputationModel(), exist_ok=True
... )

batcharray.computation.AutoComputationModel.find_computation_model classmethod

find_computation_model(
    array_type: type[ndarray],
) -> BaseComputationModel[ArrayT]

Find the computation model associated to an array type.

Parameters:

Name Type Description Default
array_type type[ndarray]

The array type.

required

Returns:

Type Description
BaseComputationModel[ArrayT]

The computation model associated to the array type.

Example
>>> import numpy as np
>>> from batcharray.computation import AutoComputationModel
>>> AutoComputationModel.find_computation_model(np.ndarray)
ArrayComputationModel()
>>> AutoComputationModel.find_computation_model(np.ma.MaskedArray)
MaskedArrayComputationModel()

batcharray.computation.AutoComputationModel.has_computation_model classmethod

has_computation_model(array_type: type[ndarray]) -> bool

Indicate if a computation model is registered for the given array type.

Parameters:

Name Type Description Default
array_type type[ndarray]

The array type.

required

Returns:

Type Description
bool

True if a computation model is registered, otherwise False.

Example
>>> import numpy as np
>>> from batcharray.computation import AutoComputationModel
>>> AutoComputationModel.has_computation_model(np.ndarray)
True
>>> AutoComputationModel.has_computation_model(str)
False

batcharray.computation.BaseComputationModel

Bases: ABC, Generic[ArrayT]

Define the base class for computation models.

This class provides the interface that all computation models must implement. It can be extended to create custom computation models for different array types.

batcharray.computation.BaseComputationModel.argmax abstractmethod

argmax(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ndarray

Return the array of indices of the maximum values along the given axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the argmax are computed. The default (None) is to compute the argmax along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ndarray

The array of indices of the maximum values along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.argmax(array, axis=0)
>>> out
array([4, 4])
>>> out = comp_model.argmax(array, axis=1)
>>> out
array([1, 1, 1, 1, 1])
>>> out = comp_model.argmax(array, axis=0, keepdims=True)
>>> out
array([[4, 4]])

batcharray.computation.BaseComputationModel.argmin abstractmethod

argmin(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ndarray

Return the array of indices of the minimum values along the given axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the argmin are computed. The default (None) is to compute the argmin along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ndarray

The array of indices of the minimum values along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.argmin(array, axis=0)
>>> out
array([0, 0])
>>> out = comp_model.argmin(array, axis=1)
>>> out
array([0, 0, 0, 0, 0])
>>> out = comp_model.argmin(array, axis=0, keepdims=True)
>>> out
array([[0, 0]])

batcharray.computation.BaseComputationModel.argsort abstractmethod

argsort(
    arr: ArrayT,
    axis: int | None = None,
    *,
    kind: SortKind | None = None,
) -> ndarray

Return the indices that sort an array along the given axis in ascending order by value.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which to sort. The default (None) is to sort along a flattened version of the array.

None
kind SortKind | None

Sorting algorithm. The default is quicksort. Note that both stable and mergesort use timsort under the covers and, in general, the actual implementation will vary with datatype. The mergesort option is retained for backwards compatibility.

None

Returns:

Type Description
ndarray

The indices that sort the array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[3, 5, 0, 2, 4], [4, 7, 8, 9, 5], [7, 5, 8, 9, 0]])
>>> out = comp_model.argsort(array, axis=0)
>>> out
array([[0, 0, 0, 0, 2],
       [1, 2, 1, 1, 0],
       [2, 1, 2, 2, 1]])
>>> out = comp_model.argsort(array, axis=1)
>>> out
array([[2, 3, 0, 4, 1],
       [0, 4, 1, 2, 3],
       [4, 1, 0, 2, 3]])

batcharray.computation.BaseComputationModel.concatenate abstractmethod

concatenate(
    arrays: Sequence[ArrayT],
    axis: int | None = None,
    *,
    dtype: DTypeLike | None = None,
) -> ArrayT

Concatenate a sequence of arrays along an existing axis.

Parameters:

Name Type Description Default
arrays Sequence[ArrayT]

The arrays to concatenate.

required
axis int | None

The axis along which the arrays will be joined. If axis is None, arrays are flattened before use.

None
dtype DTypeLike | None

If provided, the destination array will have this data type.

None

Returns:

Type Description
ArrayT

The concatenated array.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> arrays = [
...     np.array([[0, 1, 2], [4, 5, 6]]),
...     np.array([[10, 11, 12], [13, 14, 15]]),
... ]
>>> out = comp_model.concatenate(arrays, axis=0)
>>> out
array([[ 0,  1,  2],
       [ 4,  5,  6],
       [10, 11, 12],
       [13, 14, 15]])
>>> out = comp_model.concatenate(arrays, axis=1)
>>> out
array([[ 0,  1,  2, 10, 11, 12],
       [ 4,  5,  6, 13, 14, 15]])

batcharray.computation.BaseComputationModel.max abstractmethod

max(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the maximum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the maximum values are computed. The default (None) is to compute the maximum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

The maximum of the input array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.max(array, axis=0)
>>> out
array([8, 9])
>>> out = comp_model.max(array, axis=1)
>>> out
array([1, 3, 5, 7, 9])
>>> out = comp_model.max(array, axis=0, keepdims=True)
>>> out
array([[8, 9]])

batcharray.computation.BaseComputationModel.mean abstractmethod

mean(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the mean along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the means are computed. The default (None) is to compute the mean along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result. If the input contains integers or floats smaller than np.float64, then the output data-type is np.float64. Otherwise, the data-type of the output is the same as that of the input.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.mean(array, axis=0)
>>> out
array([4., 5.])
>>> out = comp_model.mean(array, axis=1)
>>> out
array([0.5, 2.5, 4.5, 6.5, 8.5])
>>> out = comp_model.mean(array, axis=0, keepdims=True)
>>> out
array([[4., 5.]])

batcharray.computation.BaseComputationModel.median abstractmethod

median(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the median along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the medians are computed. The default (None) is to compute the median along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result. If the input contains integers or floats smaller than np.float64, then the output data-type is np.float64. Otherwise, the data-type of the output is the same as that of the input.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.median(array, axis=0)
>>> out
array([4., 5.])
>>> out = comp_model.median(array, axis=1)
>>> out
array([0.5, 2.5, 4.5, 6.5, 8.5])
>>> out = comp_model.median(array, axis=0, keepdims=True)
>>> out
array([[4., 5.]])

batcharray.computation.BaseComputationModel.min abstractmethod

min(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the minimum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the minimum values are computed. The default (None) is to compute the minimum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

The minimum of the input array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.min(array, axis=0)
>>> out
array([0, 1])
>>> out = comp_model.min(array, axis=1)
>>> out
array([0, 2, 4, 6, 8])
>>> out = comp_model.min(array, axis=0, keepdims=True)
>>> out
array([[0, 1]])

batcharray.computation.BaseComputationModel.prod abstractmethod

prod(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the product along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the products are computed. The default (None) is to compute the product along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.prod(array, axis=0)
>>> out
array([  0, 945])
>>> out = comp_model.prod(array, axis=1)
>>> out
array([ 0,  6, 20, 42, 72])
>>> out = comp_model.prod(array, axis=0, keepdims=True)
>>> out
array([[  0, 945]])

batcharray.computation.BaseComputationModel.sort abstractmethod

sort(
    arr: ArrayT,
    axis: int | None = None,
    *,
    kind: SortKind | None = None,
) -> ArrayT

Sort the elements of the input array along the given axis in ascending order by value.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which to sort. The default (None) is to sort along a flattened version of the array.

None
kind SortKind | None

Sorting algorithm. The default is quicksort. Note that both stable and mergesort use timsort under the covers and, in general, the actual implementation will vary with datatype. The mergesort option is retained for backwards compatibility.

None

Returns:

Type Description
ArrayT

The sorted array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[3, 5, 0, 2, 4], [4, 7, 8, 8, 5], [8, 5, 8, 8, 0]])
>>> out = comp_model.sort(array, axis=0)
>>> out
array([[3, 5, 0, 2, 0],
       [4, 5, 8, 8, 4],
       [8, 7, 8, 8, 5]])
>>> out = comp_model.sort(array, axis=1)
>>> out
array([[0, 2, 3, 4, 5],
       [4, 5, 7, 8, 8],
       [0, 5, 8, 8, 8]])

batcharray.computation.BaseComputationModel.std abstractmethod

std(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
    ddof: int = 0,
) -> ArrayT

Return the standard deviation along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the standard deviations are computed. The default (None) is to compute the standard deviation along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False
ddof int

The delta degrees of freedom. The divisor is N - ddof.

0

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.std(array, axis=0)
>>> out
array([2.82842712, 2.82842712])
>>> out = comp_model.std(array, axis=1)
>>> out
array([0.5, 0.5, 0.5, 0.5, 0.5])
>>> out = comp_model.std(array, axis=0, keepdims=True)
>>> out
array([[2.82842712, 2.82842712]])

batcharray.computation.BaseComputationModel.sum abstractmethod

sum(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the sum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the sums are computed. The default (None) is to compute the sum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.sum(array, axis=0)
>>> out
array([20, 25])
>>> out = comp_model.sum(array, axis=1)
>>> out
array([ 1,  5,  9, 13, 17])
>>> out = comp_model.sum(array, axis=0, keepdims=True)
>>> out
array([[20, 25]])

batcharray.computation.BaseComputationModel.var abstractmethod

var(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
    ddof: int = 0,
) -> ArrayT

Return the variance along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the variances are computed. The default (None) is to compute the variance along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False
ddof int

The delta degrees of freedom. The divisor is N - ddof.

0

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = comp_model.var(array, axis=0)
>>> out
array([8., 8.])
>>> out = comp_model.var(array, axis=1)
>>> out
array([0.25, 0.25, 0.25, 0.25, 0.25])
>>> out = comp_model.var(array, axis=0, keepdims=True)
>>> out
array([[8., 8.]])

batcharray.computation.MaskedArrayComputationModel

Bases: BaseComputationModel[MaskedArray]

Implement a computation model for numpy.ma.MaskedArrays.

batcharray.computation.argmax

argmax(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ndarray

Return the array of indices of the maximum values along the given axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the argmax are computed. The default (None) is to compute the argmax along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ndarray

The array of indices of the maximum values along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import argmax
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = argmax(array, axis=0)
>>> out
array([4, 4])
>>> out = argmax(array, axis=1)
>>> out
array([1, 1, 1, 1, 1])
>>> out = argmax(array, axis=0, keepdims=True)
>>> out
array([[4, 4]])

batcharray.computation.argmin

argmin(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ndarray

Return the array of indices of the minimum values along the given axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the argmin are computed. The default (None) is to compute the argmin along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ndarray

The array of indices of the minimum values along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import argmin
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = argmin(array, axis=0)
>>> out
array([0, 0])
>>> out = argmin(array, axis=1)
>>> out
array([0, 0, 0, 0, 0])
>>> out = argmin(array, axis=0, keepdims=True)
>>> out
array([[0, 0]])

batcharray.computation.argsort

argsort(
    arr: ArrayT,
    axis: int | None = None,
    *,
    kind: SortKind | None = None,
) -> ndarray

Return the indices that sort an array along the given axis in ascending order by value.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which to sort. The default (None) is to sort along a flattened version of the array.

None
kind SortKind | None

Sorting algorithm. The default is quicksort. Note that both stable and mergesort use timsort under the covers and, in general, the actual implementation will vary with datatype. The mergesort option is retained for backwards compatibility.

None

Returns:

Type Description
ndarray

The indices that sort the array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import ArrayComputationModel
>>> comp_model = ArrayComputationModel()
>>> array = np.array([[3, 5, 0, 2, 4], [4, 7, 8, 9, 5], [7, 5, 8, 9, 0]])
>>> out = comp_model.argsort(array, axis=0)
>>> out
array([[0, 0, 0, 0, 2],
       [1, 2, 1, 1, 0],
       [2, 1, 2, 2, 1]])
>>> out = comp_model.argsort(array, axis=1)
>>> out
array([[2, 3, 0, 4, 1],
       [0, 4, 1, 2, 3],
       [4, 1, 0, 2, 3]])

batcharray.computation.concatenate

concatenate(
    arrays: Sequence[ArrayT],
    axis: int | None = None,
    *,
    dtype: DTypeLike | None = None,
) -> ArrayT

Concatenate a sequence of arrays along an existing axis.

Parameters:

Name Type Description Default
arrays Sequence[ArrayT]

The arrays to concatenate.

required
axis int | None

The axis along which the arrays will be joined. If axis is None, arrays are flattened before use.

None
dtype DTypeLike | None

If provided, the destination array will have this data type.

None

Returns:

Type Description
ArrayT

The concatenated array.

Example
>>> import numpy as np
>>> from batcharray.computation import concatenate
>>> arrays = [
...     np.array([[0, 1, 2], [4, 5, 6]]),
...     np.array([[10, 11, 12], [13, 14, 15]]),
... ]
>>> out = concatenate(arrays, axis=0)
>>> out
array([[ 0,  1,  2],
       [ 4,  5,  6],
       [10, 11, 12],
       [13, 14, 15]])
>>> out = concatenate(arrays, axis=1)
>>> out
array([[ 0,  1,  2, 10, 11, 12],
       [ 4,  5,  6, 13, 14, 15]])

batcharray.computation.max

max(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the maximum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the maximum values are computed. The default (None) is to compute the maximum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

The maximum of the input array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import max
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = max(array, axis=0)
>>> out
array([8, 9])
>>> out = max(array, axis=1)
>>> out
array([1, 3, 5, 7, 9])
>>> out = max(array, axis=0, keepdims=True)
>>> out
array([[8, 9]])

batcharray.computation.mean

mean(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the mean along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the means are computed. The default (None) is to compute the mean along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result. If the input contains integers or floats smaller than np.float64, then the output data-type is np.float64. Otherwise, the data-type of the output is the same as that of the input.

Example
>>> import numpy as np
>>> from batcharray.computation import mean
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = mean(array, axis=0)
>>> out
array([4., 5.])
>>> out = mean(array, axis=1)
>>> out
array([0.5, 2.5, 4.5, 6.5, 8.5])
>>> out = mean(array, axis=0, keepdims=True)
>>> out
array([[4., 5.]])

batcharray.computation.median

median(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the median along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the medians are computed. The default (None) is to compute the median along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result. If the input contains integers or floats smaller than np.float64, then the output data-type is np.float64. Otherwise, the data-type of the output is the same as that of the input.

Example
>>> import numpy as np
>>> from batcharray.computation import median
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = median(array, axis=0)
>>> out
array([4., 5.])
>>> out = median(array, axis=1)
>>> out
array([0.5, 2.5, 4.5, 6.5, 8.5])
>>> out = median(array, axis=0, keepdims=True)
>>> out
array([[4., 5.]])

batcharray.computation.min

min(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the minimum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the minimum values are computed. The default (None) is to compute the minimum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

The minimum of the input array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import min
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = min(array, axis=0)
>>> out
array([0, 1])
>>> out = min(array, axis=1)
>>> out
array([0, 2, 4, 6, 8])
>>> out = min(array, axis=0, keepdims=True)
>>> out
array([[0, 1]])

batcharray.computation.prod

prod(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the product along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the products are computed. The default (None) is to compute the product along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import prod
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = prod(array, axis=0)
>>> out
array([  0, 945])
>>> out = prod(array, axis=1)
>>> out
array([ 0,  6, 20, 42, 72])
>>> out = prod(array, axis=0, keepdims=True)
>>> out
array([[  0, 945]])

batcharray.computation.register_computation_models

register_computation_models() -> None

Register computation models to AutoComputationModel.

Example
>>> from batcharray.computation import AutoComputationModel, register_computation_models
>>> register_computation_models()
>>> comp_model = AutoComputationModel()
>>> comp_model
AutoComputationModel(
  ...
)

batcharray.computation.sort

sort(
    arr: ArrayT,
    axis: int | None = None,
    *,
    kind: SortKind | None = None,
) -> ArrayT

Sort the elements of the input array along the given axis in ascending order by value.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which to sort. The default (None) is to sort along a flattened version of the array.

None
kind SortKind | None

Sorting algorithm. The default is quicksort. Note that both stable and mergesort use timsort under the covers and, in general, the actual implementation will vary with datatype. The mergesort option is retained for backwards compatibility.

None

Returns:

Type Description
ArrayT

The sorted array along the given axis.

Example
>>> import numpy as np
>>> from batcharray.computation import sort
>>> array = np.array([[3, 5, 0, 2, 4], [4, 7, 8, 8, 5], [8, 5, 8, 8, 0]])
>>> out = sort(array, axis=0)
>>> out
array([[3, 5, 0, 2, 0],
       [4, 5, 8, 8, 4],
       [8, 7, 8, 8, 5]])
>>> out = sort(array, axis=1)
>>> out
array([[0, 2, 3, 4, 5],
       [4, 5, 7, 8, 8],
       [0, 5, 8, 8, 8]])

batcharray.computation.std

std(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
    ddof: int = 0,
) -> ArrayT

Return the standard deviation along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the standard deviations are computed. The default (None) is to compute the standard deviation along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False
ddof int

The delta degrees of freedom. The divisor is N - ddof.

0

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import std
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = std(array, axis=0)
>>> out
array([2.82842712, 2.82842712])
>>> out = std(array, axis=1)
>>> out
array([0.5, 0.5, 0.5, 0.5, 0.5])
>>> out = std(array, axis=0, keepdims=True)
>>> out
array([[2.82842712, 2.82842712]])

batcharray.computation.sum

sum(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
) -> ArrayT

Return the sum along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the sums are computed. The default (None) is to compute the sum along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import sum
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = sum(array, axis=0)
>>> out
array([20, 25])
>>> out = sum(array, axis=1)
>>> out
array([ 1,  5,  9, 13, 17])
>>> out = sum(array, axis=0, keepdims=True)
>>> out
array([[20, 25]])

batcharray.computation.var

var(
    arr: ArrayT,
    axis: int | None = None,
    *,
    keepdims: bool = False,
    ddof: int = 0,
) -> ArrayT

Return the variance along the specified axis.

Parameters:

Name Type Description Default
arr ArrayT

The input array.

required
axis int | None

Axis along which the variances are computed. The default (None) is to compute the variance along a flattened version of the array.

None
keepdims bool

If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

False
ddof int

The delta degrees of freedom. The divisor is N - ddof.

0

Returns:

Type Description
ArrayT

A new array holding the result.

Example
>>> import numpy as np
>>> from batcharray.computation import var
>>> array = np.array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
>>> out = var(array, axis=0)
>>> out
array([8., 8.])
>>> out = var(array, axis=1)
>>> out
array([0.25, 0.25, 0.25, 0.25, 0.25])
>>> out = var(array, axis=0, keepdims=True)
>>> out
array([[8., 8.]])