MCPcopy Create free account
hub / github.com/dask/dask / coarsen

Function coarsen

dask/array/chunk.py:85–139  ·  view source on GitHub ↗

Coarsen array by applying reduction to fixed size neighborhoods Parameters ---------- reduction: function Function like np.sum, np.mean, etc... x: np.ndarray Array to be coarsened axes: dict Mapping of axis to coarsening factor Examples --------

(reduction, x, axes, trim_excess=False, **kwargs)

Source from the content-addressed store, hash-verified

83
84
85def coarsen(reduction, x, axes, trim_excess=False, **kwargs):
86 """Coarsen array by applying reduction to fixed size neighborhoods
87
88 Parameters
89 ----------
90 reduction: function
91 Function like np.sum, np.mean, etc...
92 x: np.ndarray
93 Array to be coarsened
94 axes: dict
95 Mapping of axis to coarsening factor
96
97 Examples
98 --------
99 >>> x = np.array([1, 2, 3, 4, 5, 6])
100 >>> coarsen(np.sum, x, {0: 2})
101 array([ 3, 7, 11])
102 >>> coarsen(np.max, x, {0: 3})
103 array([3, 6])
104
105 Provide dictionary of scale per dimension
106
107 >>> x = np.arange(24).reshape((4, 6))
108 >>> x
109 array([[ 0, 1, 2, 3, 4, 5],
110 [ 6, 7, 8, 9, 10, 11],
111 [12, 13, 14, 15, 16, 17],
112 [18, 19, 20, 21, 22, 23]])
113
114 >>> coarsen(np.min, x, {0: 2, 1: 3})
115 array([[ 0, 3],
116 [12, 15]])
117
118 You must avoid excess elements explicitly
119
120 >>> x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
121 >>> coarsen(np.min, x, {0: 3}, trim_excess=True)
122 array([1, 4])
123 """
124 # Insert singleton dimensions if they don't exist already
125 for i in range(x.ndim):
126 if i not in axes:
127 axes[i] = 1
128
129 if trim_excess:
130 ind = tuple(
131 slice(0, -(d % axes[i])) if d % axes[i] else slice(None, None)
132 for i, d in enumerate(x.shape)
133 )
134 x = x[ind]
135
136 # (10, 10) -> (5, 2, 5, 2)
137 newshape = tuple(concat([(x.shape[i] // axes[i], axes[i]) for i in range(x.ndim)]))
138
139 return reduction(x.reshape(newshape), axis=tuple(range(1, x.ndim * 2, 2)), **kwargs)
140
141
142def trim(x, axes=None):

Callers 1

test_coarsenFunction · 0.90

Calls 3

reshapeMethod · 0.80
reductionFunction · 0.70
concatFunction · 0.50

Tested by 1

test_coarsenFunction · 0.72