Helper function for padding boundaries with statistics from the array. In cases where the padding requires computations of statistics from part or all of the array, this function helps compute those statistics as requested and then adds those statistics onto the boundaries of the a
(array, pad_width, mode, stat_length)
| 1145 | |
| 1146 | |
| 1147 | def pad_stats(array, pad_width, mode, stat_length): |
| 1148 | """ |
| 1149 | Helper function for padding boundaries with statistics from the array. |
| 1150 | |
| 1151 | In cases where the padding requires computations of statistics from part |
| 1152 | or all of the array, this function helps compute those statistics as |
| 1153 | requested and then adds those statistics onto the boundaries of the array. |
| 1154 | """ |
| 1155 | |
| 1156 | if mode == "median": |
| 1157 | raise NotImplementedError("`pad` does not support `mode` of `median`.") |
| 1158 | |
| 1159 | stat_length = expand_pad_value(array, stat_length) |
| 1160 | |
| 1161 | result = np.empty(array.ndim * (3,), dtype=object) |
| 1162 | for idx in np.ndindex(result.shape): |
| 1163 | axes = [] |
| 1164 | select = [] |
| 1165 | pad_shape = [] |
| 1166 | pad_chunks = [] |
| 1167 | for d, (i, s, c, w, l) in enumerate( |
| 1168 | zip(idx, array.shape, array.chunks, pad_width, stat_length) |
| 1169 | ): |
| 1170 | if i < 1: |
| 1171 | axes.append(d) |
| 1172 | select.append(slice(None, l[0], None)) |
| 1173 | pad_shape.append(w[0]) |
| 1174 | pad_chunks.append(w[0]) |
| 1175 | elif i > 1: |
| 1176 | axes.append(d) |
| 1177 | select.append(slice(s - l[1], None, None)) |
| 1178 | pad_shape.append(w[1]) |
| 1179 | pad_chunks.append(w[1]) |
| 1180 | else: |
| 1181 | select.append(slice(None)) |
| 1182 | pad_shape.append(s) |
| 1183 | pad_chunks.append(c) |
| 1184 | |
| 1185 | axes = tuple(axes) |
| 1186 | select = tuple(select) |
| 1187 | pad_shape = tuple(pad_shape) |
| 1188 | pad_chunks = tuple(pad_chunks) |
| 1189 | |
| 1190 | result_idx = array[select] |
| 1191 | if axes: |
| 1192 | if mode == "maximum": |
| 1193 | result_idx = result_idx.max(axis=axes, keepdims=True) |
| 1194 | elif mode == "mean": |
| 1195 | result_idx = result_idx.mean(axis=axes, keepdims=True) |
| 1196 | elif mode == "minimum": |
| 1197 | result_idx = result_idx.min(axis=axes, keepdims=True) |
| 1198 | |
| 1199 | result_idx = broadcast_to(result_idx, pad_shape, chunks=pad_chunks) |
| 1200 | |
| 1201 | if mode == "mean": |
| 1202 | if np.issubdtype(array.dtype, np.integer): |
| 1203 | result_idx = rint(result_idx) |
| 1204 | result_idx = result_idx.astype(array.dtype) |
no test coverage detected
searching dependent graphs…