unpack#

named_arrays.unpack(a, prototype, axis='pack')#

Reconstruct a structure from a flattened array and a prototype.

This is the inverse of pack(). The flat values in a are distributed, in order, back onto the numeric leaves of a copy of prototype, restoring each leaf’s units, axis names, and type from the prototype. Non-numeric parts of the structure are copied from prototype unchanged. Thus na.unpack(na.pack(a), a) == a.

Parameters:
  • a (AbstractScalarArray | ArrayLike) – The flattened values, for example the result of pack() or the x returned by a scipy.optimize routine.

  • prototype (PrototypeT) – A structure with the same layout as the one that produced a, supplying the units, axes, types, and traversal order used to rebuild it.

  • axis (str) – The name of the logical axis of a if it is a named array.

Return type:

PrototypeT

See also

pack

Flatten the numeric leaves of a structure into a 1D array.

Examples

import numpy as np
import astropy.units as u
import named_arrays as na

prototype = na.CartesianNdVectorArray({
    "yaw": 0 * u.deg,
    "roll": 0 * u.deg,
    "defocus": 0 * u.mm,
})

na.unpack(na.ScalarArray(np.array([1.0, 2.0, -3.0]), axes="pack"), prototype)
CartesianNdVectorArray(components={'yaw': <Quantity 1. deg>, 'roll': <Quantity 2. deg>, 'defocus': <Quantity -3. mm>})