scico.numpy.util¶
Utility functions for working with jax arrays and BlockArrays.
Functions
|
Return a string providing information about an array. |
|
Convert an array representation of a namedtuple back to a namedtuple. |
|
Compute the result of broadcasting on array shapes. |
|
Construct the corresponding complex dtype for a given real dtype. |
|
Return the name of a dtype. |
|
Determine the shape of an array after indexing/slicing. |
|
Check if input is of type |
|
Check if input is of type |
|
Determine whether a sequence of shapes could be a |
|
Determine whether a sequence of shapes can be collapsed. |
|
Determine whether a dtype is complex. |
|
Check if input is a list/tuple containing at least one list/tuple. |
|
Determine whether a dtype is real. |
Determine whether an object is a scalar or is scalar-equivalent. |
|
|
Determine the shape of an array after indexing/slicing. |
|
Convert a namedtuple to an array. |
|
Return x/y, with 0 instead of |
|
Normalize axes to a sequence and optionally ensure correctness. |
|
Construct the corresponding real dtype for a given complex dtype. |
|
Construct a representation of array or blockarray shape and dtype. |
|
Compute array size corresponding to a specified shape. |
|
Determine the length of an array axis after indexing. |
|
Convert a list of namedtuples to namedtuple of lists. |
|
Convert a namedtuple of lists/arrays to a list of namedtuples. |
- scico.numpy.util.transpose_ntpl_of_list(ntpl)[source]¶
Convert a namedtuple of lists/arrays to a list of namedtuples.
- Parameters:
ntpl (
NamedTuple) – Named tuple object to be transposed.- Return type:
- Returns:
List of namedtuple objects.
- scico.numpy.util.transpose_list_of_ntpl(ntlist)[source]¶
Convert a list of namedtuples to namedtuple of lists.
- Parameters:
ntpl – List of namedtuple objects to be transposed.
- Return type:
- Returns:
Named tuple of lists.
- scico.numpy.util.namedtuple_to_array(ntpl)[source]¶
Convert a namedtuple to an array.
Convert a
collections.namedtupleobject to anumpy.ndarrayobject that can be saved usingnumpy.savez.- Parameters:
ntpl (
NamedTuple) – Named tuple object to be converted to ndarray.- Return type:
- Returns:
Array representation of input named tuple.
- scico.numpy.util.array_to_namedtuple(array)[source]¶
Convert an array representation of a namedtuple back to a namedtuple.
Convert a
numpy.ndarrayobject constructed bynamedtuple_to_arrayback to the originalcollections.namedtuplerepresentation.- Parameters:
by (Array representation of named tuple constructed) –
namedtuple_to_array.- Return type:
- Returns:
Named tuple object with the same name and fields as the original named tuple object provided to
namedtuple_to_array.
- scico.numpy.util.normalize_axes(axes, shape=None, default=None, sort=False)[source]¶
Normalize axes to a sequence and optionally ensure correctness.
Normalize axes to a tuple or list and (optionally) ensure that entries refer to axes that exist in shape.
- Parameters:
axes (
Union[int,Tuple[int,...],List[int],None]) – User specification of one or more axes: int, list, tuple, orNone. Negative values count from the last to the first axis.shape (
Optional[Tuple[int,...]]) – The shape of the array of which axes are being specified. If notNone, axes is checked to make sure its entries refer to axes that exist in shape.default (
Optional[List[int]]) – Default value to return if axes isNone. By default, tuple(range(len(shape))).sort (
bool) – IfTrue, sort the returned axis indices.
- Return type:
- Returns:
Tuple or list of axes (never an int, never
None). The output will only be a list if the input is a list or if the input isNoneand defaults is a list.
- scico.numpy.util.slice_length(length, idx)[source]¶
Determine the length of an array axis after indexing.
Determine the length of an array axis after slicing. An exception is raised if the indexing expression is an integer that is out of bounds for the specified axis length. A value of
Noneis returned for valid integer indexing expressions as an indication that the corresponding axis shape is an empty tuple; this value should be converted to a unit integer if the axis size is required.- Parameters:
- Return type:
- Returns:
Length of indexed/sliced axis.
- Raises:
ValueError – If idx is an integer index that is out bounds for the axis length or if the type of idx is not one of Ellipsis, int, or slice.
- scico.numpy.util.indexed_shape(shape, idx)[source]¶
Determine the shape of an array after indexing/slicing.
The indexed shape is determined by replicating the observed effects of NumPy/JAX array indexing/slicing syntax. It is significantly faster than
jax_indexed_shape, and has a minimal memory footprint in all circumstances.- Parameters:
idx (
ArrayIndex) – Indexing expression (singleton or tuple of Ellipsis, int, slice, orNone(np.newaxis)).
- Return type:
- Returns:
Shape of indexed/sliced array.
- Raises:
ValueError – If any element of idx is not one of Ellipsis,
int –
index is out bounds for the corresponding axis length. –
- scico.numpy.util.jax_indexed_shape(shape, idx)[source]¶
Determine the shape of an array after indexing/slicing.
The indexed shape is determined by constructing and indexing an array of the appropriate shape, relying on
jax.jitto avoid memory allocation. It is potentially more reliable thanindexed_shapebecause the indexing/slicing calculations are referred to JAX, but is significantly slower, and will involved potentially significant memory allocations if JIT is disabled, e.g. for debugging purposes.
- scico.numpy.util.no_nan_divide(x, y)[source]¶
Return x/y, with 0 instead of
NaNwhere y is 0.- Parameters:
x (
Union[BlockArray,Array]) – Numerator.y (
Union[BlockArray,Array]) – Denominator.
- Return type:
Union[BlockArray,Array]- Returns:
x / y with 0 wherever y == 0.
- scico.numpy.util.array_info(x)[source]¶
Return a string providing information about an array.
- Parameters:
x (
Union[BlockArray,Array]) – A numpy or jax array or scicoBlockArray.- Return type:
- Returns:
A string containing information on the array.
- Raises:
TypeError – If the array is not of a recognized type.
- scico.numpy.util.shape_to_size(shape)[source]¶
Compute array size corresponding to a specified shape.
Compute array size corresponding to a specified shape, which may be nested, i.e. corresponding to a
BlockArray.
- scico.numpy.util.is_array(x)[source]¶
Check if input is of type
jax.Arrayornumpy.ndarray.Check if input is an array, of type
jax.Arrayornumpy.ndarray.
- scico.numpy.util.is_arraylike(x)[source]¶
Check if input is of type
jax.typing.ArrayLike.isinstance(x, jax.typing.ArrayLike) does not work in Python < 3.10, see https://jax.readthedocs.io/en/latest/jax.typing.html#jax-typing-best-practices.
- scico.numpy.util.is_nested(x)[source]¶
Check if input is a list/tuple containing at least one list/tuple.
- Parameters:
x (
Any) – Object to be tested.- Return type:
- Returns:
Trueif x is a list/tuple containing at least one list/tuple,Falseotherwise.
Example
>>> is_nested([1, 2, 3]) False >>> is_nested([(1,2), (3,)]) True >>> is_nested([[1, 2], 3]) True
- scico.numpy.util.is_collapsible(shapes)[source]¶
Determine whether a sequence of shapes can be collapsed.
Return
Trueif the a list of shapes represent arrays that can be stacked, i.e., they are all the same.
- scico.numpy.util.is_blockable(shapes)[source]¶
Determine whether a sequence of shapes could be a
BlockArrayshape.Return
Trueif the sequence of shapes represent arrays that can be combined into aBlockArray, i.e., none are nested.
- scico.numpy.util.shape_dtype_rep(shape, dtype)[source]¶
Construct a representation of array or blockarray shape and dtype.
Construct a representation of array or block array shape and dtype that is suitable for both jax arrays and scico blockarrays.
- Parameters:
- Return type:
Union[ShapeDtypeStruct,BlockArray]- Returns:
A
jax.ShapeDtypeStructor aBlockArraycontaining objects of typejax.ShapeDtypeStruct.
- scico.numpy.util.broadcast_nested_shapes(shape_a, shape_b)[source]¶
Compute the result of broadcasting on array shapes.
Compute the result of applying a broadcasting binary operator to (block) arrays with (possibly nested) shapes shape_a and shape_b. Extends
numpy.broadcast_shapesto also support the nested tuple shapes ofBlockArrays.- Parameters:
- Return type:
- Returns:
A (possibly nested) shape tuple.
Example
>>> broadcast_nested_shapes(((1, 1, 3), (2, 3, 1)), ((2, 3,), (2, 1, 4))) ((1, 2, 3), (2, 3, 4))
- scico.numpy.util.real_dtype(dtype)[source]¶
Construct the corresponding real dtype for a given complex dtype.
Construct the corresponding real dtype for a given complex dtype, e.g. the real dtype corresponding to
complex64isfloat32.- Parameters:
dtype (
DType) – A complex numpy or scico.numpy dtype (e.g.complex64,complex128).- Return type:
- Returns:
The real dtype corresponding to the input dtype
- scico.numpy.util.complex_dtype(dtype)[source]¶
Construct the corresponding complex dtype for a given real dtype.
Construct the corresponding complex dtype for a given real dtype, e.g. the complex dtype corresponding to
float32iscomplex64.
- scico.numpy.util.dtype_name(dtype)[source]¶
Return the name of a dtype.
Construct a string representation of a dtype name.