scipp.fold
scipp.fold¶
- scipp.fold(x, dim, sizes=None, dims=None, shape=None)¶
Fold a single dimension of a variable or data array into multiple dims.
- Parameters
x (VariableLike) – Variable or DataArray to fold.
dim (str) – A single dim label that will be folded into more dims.
sizes (Optional[Dict[str, int]]) – A dict mapping new dims to new shapes.
dims (Optional[Union[List[str], Tuple[str]]]) – A list of new dims labels.
shape (Optional[Sequence[int]]) – A list of new dim shapes.
- Raises
If the volume of the old shape is not equal to the volume of the new shape.
- Returns
Variable or DataArray with requested dimension labels and shape.
- Return type
VariableLike
Examples:
>>> v = sc.arange('x', 6) >>> v <scipp.Variable> (x: 6) int64 [dimensionless] [0, 1, ..., 4, 5] >>> sc.fold(v, dim='x', sizes={'y': 2, 'z': 3}) <scipp.Variable> (y: 2, z: 3) int64 [dimensionless] [0, 1, ..., 4, 5] >>> sc.fold(v, dim='x', sizes={'y': 2, 'z': 3}).values array([[0, 1, 2], [3, 4, 5]])
>>> sc.fold(v, dim='x', dims=['y', 'z'], shape=[2, 3]) <scipp.Variable> (y: 2, z: 3) int64 [dimensionless] [0, 1, ..., 4, 5]
>>> a = sc.DataArray(0.1 * sc.arange('x', 6), coords={'x': sc.arange('x', 6)}) >>> sc.fold(a, dim='x', sizes={'y': 2, 'z': 3}) <scipp.DataArray> Dimensions: Sizes[y:2, z:3, ] Coordinates: x int64 [dimensionless] (y, z) [0, 1, ..., 4, 5] Data: float64 [dimensionless] (y, z) [0, 0.1, ..., 0.4, 0.5] >>> sc.fold(a, dim='x', sizes={'y': 2, 'z': 3}).data.values array([[0. , 0.1, 0.2], [0.3, 0.4, 0.5]]) >>> sc.fold(a, dim='x', sizes={'y': 2, 'z': 3}).coords['x'].values array([[0, 1, 2], [3, 4, 5]])