array_like

Functions and classes for array-like objects, implementing common numpy array features for datasets or nested sequences, while trying to avoid copying data.

Classes:

  • DatasetView: Similar to a numpy view, to access a h5py dataset as if it was transposed, without casting it into a numpy array (this lets h5py handle reading the data from the file into memory, as needed).

  • ListOfImages: Similar to a numpy view, to access a list of 2D numpy arrays as if it was a 3D array (possibly transposed), without casting it into a numpy array.

Functions:

is_array(obj)[source]

Return True if object implements necessary attributes to be considered similar to a numpy array.

Attributes needed are “shape”, “dtype”, “__getitem__” and “__array__”.

Parameters

obj – Array-like object (numpy array, h5py dataset…)

Returns

boolean

is_list_of_arrays(obj)[source]

Return True if object is a sequence of numpy arrays, e.g. a list of images as 2D arrays.

Parameters

obj – list of arrays

Returns

boolean

is_nested_sequence(obj)[source]

Return True if object is a nested sequence.

A simple 1D sequence is considered to be a nested sequence.

Numpy arrays and h5py datasets are not considered to be nested sequences.

To test if an object is a nested sequence in a more general sense, including arrays and datasets, use:

is_nested_sequence(obj) or is_array(obj)
Parameters

obj – nested sequence (numpy array, h5py dataset…)

Returns

boolean

get_shape(array_like)[source]

Return shape of an array like object.

In case the object is a nested sequence but not an array or dataset (list of lists, tuples…), the size of each dimension is assumed to be uniform, and is deduced from the length of the first sequence.

Parameters

array_like – Array like object: numpy array, hdf5 dataset, multi-dimensional sequence

Returns

Shape of array, as a tuple of integers

get_dtype(array_like)[source]

Return dtype of an array like object.

In the case of a nested sequence, the type of the first value is inspected.

Parameters

array_like – Array like object: numpy array, hdf5 dataset, multi-dimensional nested sequence

Returns

numpy dtype of object

get_concatenated_dtype(arrays)[source]

Return dtype of array resulting of concatenation of a list of arrays (without actually concatenating them).

Parameters

arrays – list of numpy arrays

Returns

resulting dtype after concatenating arrays

class ListOfImages(images, transposition=None)[source]

This class provides a way to access values and slices in a stack of images stored as a list of 2D numpy arrays, without creating a 3D numpy array first.

A transposition can be specified, as a 3-tuple of dimensions in the wanted order. For example, to transpose from xyz (0, 1, 2) into yzx, the transposition tuple is (1, 2, 0)

All the 2D arrays in the list must have the same shape.

The global dtype of the stack of images is the one that would be obtained by casting the list of 2D arrays into a 3D numpy array.

Parameters
  • images – list of 2D numpy arrays, or ListOfImages object

  • transposition – Tuple of dimension numbers in the wanted order

images = None

List of images

shape = None

Tuple of array dimensions

dtype = None

Data-type of the global array

ndim = None

Number of array dimensions

size = None

Number of elements in the array.

transposition = None

List of dimension indices, in an order depending on the specified transposition. By default this is simply [0, …, self.ndim], but it can be changed by specifying a different transposition parameter at initialization.

Use transpose(), to create a new ListOfImages with a different transposition.

transpose(transposition=None)[source]

Return a re-ordered (dimensions permutated) ListOfImages.

The returned object refers to the same images but with a different transposition.

Parameters

transposition (List[int]) – List/tuple of dimension numbers in the wanted order. If None (default), reverse the dimensions.

Returns

new ListOfImages object

property T

Same as self.transpose()

Returns

DatasetView with dimensions reversed.

min()[source]
Returns

Global minimum value

max()[source]
Returns

Global maximum value

class DatasetView(dataset, transposition=None)[source]

This class provides a way to transpose a dataset without casting it into a numpy array. This way, the dataset in a file need not necessarily be integrally read into memory to view it in a different transposition.

Note

The performances depend a lot on the way the dataset was written to file. Depending on the chunking strategy, reading a complete 2D slice in an unfavorable direction may still require the entire dataset to be read from disk.

Parameters
  • dataset – h5py dataset

  • transposition – List of dimensions sorted in the order of transposition (relative to the original h5py dataset)

dataset = None

original dataset

shape = None

Tuple of array dimensions

dtype = None

Data-type of the array’s element

ndim = None

Number of array dimensions

size = None

Number of elements in the array.

transposition = None

List of dimension indices, in an order depending on the specified transposition. By default this is simply [0, …, self.ndim], but it can be changed by specifying a different transposition parameter at initialization.

Use transpose(), to create a new DatasetView with a different transposition.

transpose(transposition=None)[source]

Return a re-ordered (dimensions permutated) DatasetView.

The returned object refers to the same dataset but with a different transposition.

Parameters

transposition (List[int]) – List of dimension numbers in the wanted order. If None (default), reverse the dimensions.

Returns

Transposed DatasetView

property T

Same as self.transpose()

Returns

DatasetView with dimensions reversed.