armi.bookkeeping.db.jaggedArray module

Tooling to help flatten jagged (non rectangular) data into rectangular arrays.

The goal here is to support jagged data for NumPy arrays to be written into the ARMI databases.

class armi.bookkeeping.db.jaggedArray.JaggedArray(jaggedData, paramName)[source]

Bases: object

Take a list of numpy arrays or lists and flatten them into a single 1D array.

This implementation can preserve the structure of a multi-dimensional numpy array by storing the dimensions in self.shapes and then re-populating a numpy array of that shape from the flattened 1D array. However, it can only preserve one layer of jaggedness in a list of lists (or other iterables). For example, a list of tuples with varying lengths can be flattened and reconstituted exactly. But, if a list of lists of tuples is passed in, the tuples in that final layer of nesting will all be flattened to a single 1D numpy array after a round trip. No structure is retained from nested lists of jagged lists or tuples.

JaggedArray constructor.

Parameters:
  • jaggedData (list of np.ndarray) – A list of numpy arrays (or lists or tuples) to be flattened into a single array

  • paramName (str) – The name of the parameter represented by this data

static flatten(x)[source]

Recursively flatten an iterable (list, tuple, or numpy.ndarray).

xlist, tuple, np.ndarray

An iterable. Can be a nested iterable in which the elements themselves are also iterable.

classmethod fromH5(data, offsets, shapes, nones, dtype, paramName)[source]

Create a JaggedArray instance from an HDF5 dataset.

The JaggedArray is stored in HDF5 as a flat 1D array with accompanying attributes of “offsets” and “shapes” to define how to reconstitute the original data.

Parameters:
  • data (np.ndarray) – A flattened 1D numpy array read in from an HDF5 file

  • offsets (np.ndarray) – Offset indices for the zeroth element of each constituent array

  • shapes (np.ndarray) – The shape of each constituent array

  • nones (np.ndarray) – The location of Nones

  • dtype (np.dtype) – The data type for the array

  • paramName (str) – The name of the parameter represented by this data

Returns:

obj

Return type:

JaggedArray An instance of JaggedArray populated with the input data

tolist()[source]

Alias for unpack() to make this class respond like a np.ndarray.

unpack()[source]

Unpack a JaggedArray object into a list of arrays.

Returns:

unpackedJaggedData – List of numpy arrays with varying dimensions (i.e., jagged arrays)

Return type:

list of np.ndarray