scipp.flatten#

scipp.flatten(x, dims=None, to=None)#

Flatten multiple dimensions of a variable or data array into a single dimension. If dims is omitted, then we flatten all of the inputs dimensions into a single dim.

Parameters
Raises

If the bin edge coordinates cannot be stitched back together.

Returns

Union[Variable, DataArray, Dataset] – Variable or DataArray with requested dimension labels and shape.

Examples:

>>> v = sc.array(dims=['x', 'y'], values=np.arange(6).reshape(2, 3))
>>> v
<scipp.Variable> (x: 2, y: 3)      int64  [dimensionless]  [0, 1, ..., 4, 5]
>>> sc.flatten(v, to='u')
<scipp.Variable> (u: 6)      int64  [dimensionless]  [0, 1, ..., 4, 5]
>>> sc.flatten(v, dims=['x', 'y'], to='u')
<scipp.Variable> (u: 6)      int64  [dimensionless]  [0, 1, ..., 4, 5]
>>> v = sc.array(dims=['x', 'y', 'z'], values=np.arange(24).reshape(2, 3, 4))
>>> v
<scipp.Variable> (x: 2, y: 3, z: 4)      int64  [dimensionless]  [0, 1, ..., 22, 23]
>>> sc.flatten(v, to='u')
<scipp.Variable> (u: 24)      int64  [dimensionless]  [0, 1, ..., 22, 23]
>>> sc.flatten(v, dims=['x', 'y'], to='u')
<scipp.Variable> (u: 6, z: 4)      int64  [dimensionless]  [0, 1, ..., 22, 23]
>>> sc.flatten(v, dims=['y', 'z'], to='u')
<scipp.Variable> (x: 2, u: 12)      int64  [dimensionless]  [0, 1, ..., 22, 23]
>>> a = sc.DataArray(0.1 * sc.array(dims=['x', 'y'], values=np.arange(6).reshape(2, 3)),
...        coords={'x': sc.arange('x', 2),
...                'y': sc.arange('y', 3),
...                'xy': sc.array(dims=['x', 'y'],
...                               values=np.arange(6).reshape(2, 3))})
>>> a
<scipp.DataArray>
Dimensions: Sizes[x:2, y:3, ]
Coordinates:
  x                           int64  [dimensionless]  (x)  [0, 1]
  xy                          int64  [dimensionless]  (x, y)  [0, 1, ..., 4, 5]
  y                           int64  [dimensionless]  (y)  [0, 1, 2]
Data:
                            float64  [dimensionless]  (x, y)  [0, 0.1, ..., 0.4, 0.5]
>>> sc.flatten(a, to='u')
<scipp.DataArray>
Dimensions: Sizes[u:6, ]
Coordinates:
  x                           int64  [dimensionless]  (u)  [0, 0, ..., 1, 1]
  xy                          int64  [dimensionless]  (u)  [0, 1, ..., 4, 5]
  y                           int64  [dimensionless]  (u)  [0, 1, ..., 1, 2]
Data:
                            float64  [dimensionless]  (u)  [0, 0.1, ..., 0.4, 0.5]