cf.Data.coarsen

Data.coarsen(reduction, axes, trim_excess=False, inplace=False)[source]

Coarsen the data.

Coarsen the data by applying the reduction function to combine the elements within fixed-size neighbourhoods.

Added in version 3.20.0.

Parameters:
reduction: function

The function with which to coarsen the data.

axes: dict

Define how large to set the coarsening neighbourhood for each axis. A dictionary key is an integer axis position, with corresponding value giving the integer size of the coarsening neighbourhood for that axis. Unspecified axes are not coarsened, which is equivalent to providing a coarsening neighbourhood of 1.

Example:

Coarsen the axis in position 1 by combining every 4 elements: {1: 4}

Example:

Coarsen the first axis by combining every 3 elements, and the last axis by combining every 4 elements: {0: 3, -1: 4}

trim_excess: bool, optional

If True then omit a partially-full neighbourhood at the end of a coarsened axis. If False (the default) then an exception is raised if there are any partially-filled neighbourhoods.

inplace: bool, optional

If True then do the operation in-place and return None.

Returns:
Data or None

The coarsened data, or None if the operation was in-place.

Examples

>>> import numpy as np
>>> d = cf.Data(np.arange(24).reshape((4, 6)))
>>> print(d.array)
[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
>>> e = d.coarsen(np.min, {0: 2, 1: 3})
>>> print(e.array)
[[ 0  3]
 [12 15]]
>>> e = d.coarsen(np.max, {-1: 5}, trim_excess=True)
>>> print(e.array)
[[ 4]
 [10]
 [16]
 [22]]
>>> e = d.coarsen(np.max, {-1: 5}, trim_excess=False)
ValueError: Coarsening factors {1: 5} do not align with array shape (4, 6).