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
aare stripped of their units and concatenated into a single, one-dimensional, dimensionlessnamed_arrays.ScalarArrayalongaxis, in a deterministic traversal order. Containers (dict,list,tuple,dataclassesinstances) 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 inscipy.optimize. The units, axis names, and structure dropped here are restored byunpack()from a prototype, so the pair round-trips:na.unpack(na.pack(a), a) == a.- Parameters:
- Return type:
See also
unpackReconstruct 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',), )