sporco.array

Functions operating on numpy arrays etc.

Functions

ntpl2array(ntpl)

Convert a namedtuple to an array.

array2ntpl(arr)

Convert an array representation of a namedtuple back to a namedtuple.

transpose_ntpl_list(lst)

Transpose a list of named tuple objects (of the same type) into a named tuple of lists.

zpad(x, pd, ax)

Zero-pad array x with pd = (leading, trailing) zeros on axis ax.

zdivide(x, y)

Return x/y, with 0 instead of NaN where y is 0.

promote16(u[, fn])

Promote np.float16 arguments to np.float32 dtype.

atleast_nd(n, u)

Append axes to an array so that it is n dimensional.

split(u[, axis])

Split an array into a list of arrays on the specified axis.

rolling_window(x, wsz[, wnm, pad])

Construct a rolling window view of the input array.

subsample_array(x, step[, pad, mode])

Construct a subsampled view of the input array.

extract_blocks(img, blksz[, stpsz])

Extract blocks from an ndarray signal into an ndarray.

average_blocks(blks, imgsz[, stpsz])

Average blocks together from an ndarray to reconstruct ndarray signal.

combine_blocks(blks, imgsz[, stpsz, fn])

Combine blocks from an ndarray to reconstruct ndarray signal.


Function Descriptions

sporco.array.ntpl2array(ntpl)[source]

Convert a namedtuple to an array.

Convert a collections.namedtuple object to a numpy.ndarray object that can be saved using numpy.savez.

Parameters:
ntplcollections.namedtuple object

Named tuple object to be converted to ndarray

Returns:
arrndarray

Array representation of input named tuple

sporco.array.array2ntpl(arr)[source]

Convert an array representation of a namedtuple back to a namedtuple.

Convert a numpy.ndarray object constructed by ntpl2array back to the original collections.namedtuple representation.

Parameters:
arrndarray

Array representation of named tuple constructed by ntpl2array

Returns:
ntplcollections.namedtuple object

Named tuple object with the same name and fields as the original named typle object provided to ntpl2array

sporco.array.transpose_ntpl_list(lst)[source]

Transpose a list of named tuple objects (of the same type) into a named tuple of lists.

Parameters:
lstlist of collections.namedtuple object

List of named tuple objects of the same type

Returns:
ntplcollections.namedtuple object

Named tuple object with each entry consisting of a list of the corresponding fields of the named tuple objects in list lst

sporco.array.zpad(x, pd, ax)[source]

Zero-pad array x with pd = (leading, trailing) zeros on axis ax.

Parameters:
xarray_like

Array to be padded

pdtuple

Sequence of two ints (leading,trailing) specifying number of zeros for padding

axint

Axis to be padded

Returns:
xparray_like

Padded array

sporco.array.zdivide(x, y)[source]

Return x/y, with 0 instead of NaN where y is 0.

Parameters:
xarray_like

Numerator

yarray_like

Denominator

Returns:
zndarray

Quotient x/y

sporco.array.promote16(u, fn=None, *args, **kwargs)[source]

Promote np.float16 arguments to np.float32 dtype.

Utility function for use with functions that do not support arrays of dtype np.float16. This function has two distinct modes of operation. If called with only the u parameter specified, the returned value is either u itself if u is not of dtype np.float16, or u promoted to np.float32 dtype if it is. If the function parameter fn is specified then u is conditionally promoted as described above, passed as the first argument to function fn, and the returned values are converted back to dtype np.float16 if u is of that dtype. Note that if parameter fn is specified, it may not be be specified as a keyword argument if it is followed by any non-keyword arguments.

Parameters:
uarray_like

Array to be promoted to np.float32 if it is of dtype np.float16

fnfunction or None, optional (default None)

Function to be called with promoted u as first parameter and *args and **kwargs as additional parameters

*args

Variable length list of arguments for function fn

**kwargs

Keyword arguments for function fn

Returns:
upndarray

Conditionally dtype-promoted version of u if fn is None, or value(s) returned by fn, converted to the same dtype as u, if fn is a function

sporco.array.atleast_nd(n, u)[source]

Append axes to an array so that it is n dimensional.

If the input array has fewer than n dimensions, append singleton dimensions so that it is n dimensional. Note that the interface differs substantially from that of numpy.atleast_3d etc.

Parameters:
nint

Minimum number of required dimensions

uarray_like

Input array

Returns:
vndarray

Output array with at least n dimensions

sporco.array.split(u, axis=0)[source]

Split an array into a list of arrays on the specified axis.

Split an array into a list of arrays on the specified axis. The length of the list is the shape of the array on the specified axis, and the corresponding axis is removed from each entry in the list. This function does not have the same behaviour as numpy.split.

Parameters:
uarray_like

Input array

axisint, optional (default 0)

Axis on which to split the input array

Returns:
vlist of ndarray

List of arrays

sporco.array.rolling_window(x, wsz, wnm=None, pad='wrap')[source]

Construct a rolling window view of the input array.

Use numpy.lib.stride_tricks.as_strided to construct a view of the input array that represents different positions of a rolling window as additional axes of the array. If the number of shifts requested is such that the window extends beyond the boundary of the input array, it is padded before the view is constructed. For example, if x is 4 x 5 array, the output of y = rolling_window(x, (3, 3)) is a 3 x 3 x 2 x 3 array, with the first window position indexed as y[..., 0, 0].

Parameters:
xndarray

Input array

wsztuple

Window size

wnmtuple, optional (default None)

Number of shifts of window on each axis. If None, the number of shifts is set so that the end sample in the array is also the end sample in the final window position.

padstring, optional (default ‘wrap’)

A pad mode specification for numpy.pad

Returns:
xwndarray

An array of shape wsz + wnm representing all requested shifts of the window within the input array

sporco.array.subsample_array(x, step, pad=False, mode='reflect')[source]

Construct a subsampled view of the input array.

Use numpy.lib.stride_tricks.as_strided to construct a view of the input array that represents a subsampling of the array by the specified step, with different offsets of the subsampling as additional axes of the array. If the input array shape is not evenly divisible by the subsampling step, it is padded before the view is constructed. For example, if x is 6 x 6 array, the output of y = subsample_array(x, (2, 2)) is a 2 x 2 x 3 x 3 array, with the first subsampling offset indexed as y[0, 0].

Parameters:
xndarray

Input array

steptuple

Subsampling step size

padbool, optional (default False)

Flag indicating whether the input array should be padded when its size is not integer divisible by the step size

modestring, optional (default ‘reflect’)

A pad mode specification for numpy.pad

Returns:
xsndarray

An array representing different subsampling offsets in the input array

sporco.array.extract_blocks(img, blksz, stpsz=None)[source]

Extract blocks from an ndarray signal into an ndarray.

Parameters:
imgndarray or tuple of ndarrays

nd array of images, or tuple of images

blksztuple

tuple of block sizes, blocks are taken starting from the first index of img

stpsztuple, optional (default None, corresponds to steps of 1)

tuple of step sizes between neighboring blocks

Returns:
blksndarray

image blocks

sporco.array.average_blocks(blks, imgsz, stpsz=None)[source]

Average blocks together from an ndarray to reconstruct ndarray signal.

Parameters:
blksndarray

Array of blocks of a signal

imgsztuple

Tuple of the signal size

stpsztuple, optional (default None, corresponds to steps of 1)

Tuple of step sizes between neighboring blocks

Returns:
imgsndarray

Reconstructed signal, unknown pixels are returned as np.nan

sporco.array.combine_blocks(blks, imgsz, stpsz=None, fn=<function median>)[source]

Combine blocks from an ndarray to reconstruct ndarray signal.

Parameters:
blksndarray

Array of blocks of a signal

imgsztuple

Tuple of the signal size

stpsztuple, optional (default None, corresponds to steps of 1)

Tuple of step sizes between neighboring blocks

fnfunction, optional (default np.median)

Function used to resolve multivalued cells

Returns:
imgsndarray

Reconstructed signal, unknown pixels are returned as np.nan