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

Function pad

dask/array/creation.py:1256–1303  ·  view source on GitHub ↗
(array, pad_width, mode="constant", **kwargs)

Source from the content-addressed store, hash-verified

1254
1255@derived_from(np)
1256def pad(array, pad_width, mode="constant", **kwargs):
1257 array = asarray(array)
1258
1259 pad_width = expand_pad_value(array, pad_width)
1260
1261 if callable(mode):
1262 return pad_udf(array, pad_width, mode, **kwargs)
1263
1264 # Make sure that no unsupported keywords were passed for the current mode
1265 allowed_kwargs = {
1266 "empty": [],
1267 "edge": [],
1268 "wrap": [],
1269 "constant": ["constant_values"],
1270 "linear_ramp": ["end_values"],
1271 "maximum": ["stat_length"],
1272 "mean": ["stat_length"],
1273 "median": ["stat_length"],
1274 "minimum": ["stat_length"],
1275 "reflect": ["reflect_type"],
1276 "symmetric": ["reflect_type"],
1277 }
1278 try:
1279 unsupported_kwargs = set(kwargs) - set(allowed_kwargs[mode])
1280 except KeyError as e:
1281 raise ValueError(f"mode '{mode}' is not supported") from e
1282 if unsupported_kwargs:
1283 raise ValueError(
1284 "unsupported keyword arguments for mode '{}': {}".format(
1285 mode, unsupported_kwargs
1286 )
1287 )
1288
1289 if mode in {"maximum", "mean", "median", "minimum"}:
1290 stat_length = kwargs.get("stat_length", tuple((n, n) for n in array.shape))
1291 return pad_stats(array, pad_width, mode, stat_length)
1292 elif mode == "constant":
1293 kwargs.setdefault("constant_values", 0)
1294 return pad_edge(array, pad_width, mode, **kwargs)
1295 elif mode == "linear_ramp":
1296 kwargs.setdefault("end_values", 0)
1297 return pad_edge(array, pad_width, mode, **kwargs)
1298 elif mode in {"edge", "empty"}:
1299 return pad_edge(array, pad_width, mode)
1300 elif mode in ["reflect", "symmetric", "wrap"]:
1301 return pad_reuse(array, pad_width, mode, **kwargs)
1302
1303 raise RuntimeError("unreachable")

Callers 1

diagFunction · 0.85

Calls 8

asarrayFunction · 0.90
expand_pad_valueFunction · 0.85
pad_udfFunction · 0.85
setClass · 0.85
pad_statsFunction · 0.85
pad_edgeFunction · 0.85
pad_reuseFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected