pack#

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

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

The magnitudes of every array-like leaf of a are stripped of their units and concatenated into a single, one-dimensional, dimensionless named_arrays.ScalarArray along axis, in a deterministic traversal order. Containers (dict, list, tuple, dataclasses instances) and composite named arrays (e.g. named_arrays.AbstractVectorArray) are traversed recursively; non-numeric values (e.g. None, str) are ignored.

This is the inverse of unpack(). Together they bridge the gap between the named, united arrays used throughout this package and the flat, dimensionless float vectors expected by optimizers such as those in scipy.optimize. The units, axis names, and structure dropped here are restored by unpack() from a prototype, so the pair round-trips: na.unpack(na.pack(a), a) == a.

Parameters:
  • a (Any) – The structure to flatten. May be a named array, a (possibly nested) dict, list, tuple, or dataclasses instance of named arrays, or any other value.

  • axis (str) – The name of the logical axis of the flattened result.

Return type:

ScalarArray

See also

unpack

Reconstruct a structure from a flattened array and a prototype.

Examples

import astropy.units as u
import named_arrays as na

params = na.CartesianNdVectorArray({
    "yaw": 1 * u.deg,
    "roll": 2 * u.deg,
    "defocus": -3 * u.mm,
})

na.pack(params)
ScalarArray(
    ndarray=[ 1.,  2., -3.],
    axes=('pack',),
)