scipp.squeeze#

scipp.squeeze(x, dim=None)#

Remove dimensions of length 1.

This is equivalent to indexing the squeezed dimensions with index 0, that is squeeze(x, ['x', 'y']) is equivalent to x['x', 0]['y', 0].

Parameters
Returns

Same type as input – Input with length-1 dimensions removed.

Raises

scipp.DimensionError – If a dimension in dim does not have length 1.

Examples

>>> v = sc.arange('a', 3).fold('a', {'x': 1, 'y': 3, 'z': 1})
>>> v
<scipp.Variable> (x: 1, y: 3, z: 1)      int64  [dimensionless]  [0, 1, 2]
>>> sc.squeeze(v)
<scipp.Variable> (y: 3)      int64  [dimensionless]  [0, 1, 2]
>>> sc.squeeze(v, 'z')
<scipp.Variable> (x: 1, y: 3)      int64  [dimensionless]  [0, 1, 2]
>>> sc.squeeze(v, ['x', 'z'])
<scipp.Variable> (y: 3)      int64  [dimensionless]  [0, 1, 2]

Coordinates for squeezed dimensions are turned into attributes:

>>> da = sc.DataArray(v, coords={'x': sc.arange('x', 1),
...                              'y': sc.arange('y', 3)})
>>> da
<scipp.DataArray>
Dimensions: Sizes[x:1, y:3, z:1, ]
Coordinates:
  x                           int64  [dimensionless]  (x)  [0]
  y                           int64  [dimensionless]  (y)  [0, 1, 2]
Data:
                              int64  [dimensionless]  (x, y, z)  [0, 1, 2]
>>> sc.squeeze(da)
<scipp.DataArray>
Dimensions: Sizes[y:3, ]
Coordinates:
  y                           int64  [dimensionless]  (y)  [0, 1, 2]
Data:
                              int64  [dimensionless]  (y)  [0, 1, 2]
Attributes:
  x                           int64  [dimensionless]  ()  0