Normalizes an axis argument into a tuple of non-negative integer axes. This handles shorthands such as ``1`` and converts them to ``(1,)``, as well as performing the handling of negative indices covered by `normalize_axis_index`. By default, this forbids axes from being specif
(axis, ndim, argname=None, allow_duplicate=False)
| 1328 | |
| 1329 | |
| 1330 | def normalize_axis_tuple(axis, ndim, argname=None, allow_duplicate=False): |
| 1331 | """ |
| 1332 | Normalizes an axis argument into a tuple of non-negative integer axes. |
| 1333 | |
| 1334 | This handles shorthands such as ``1`` and converts them to ``(1,)``, |
| 1335 | as well as performing the handling of negative indices covered by |
| 1336 | `normalize_axis_index`. |
| 1337 | |
| 1338 | By default, this forbids axes from being specified multiple times. |
| 1339 | |
| 1340 | Used internally by multi-axis-checking logic. |
| 1341 | |
| 1342 | .. versionadded:: 1.13.0 |
| 1343 | |
| 1344 | Parameters |
| 1345 | ---------- |
| 1346 | axis : int, iterable of int |
| 1347 | The un-normalized index or indices of the axis. |
| 1348 | ndim : int |
| 1349 | The number of dimensions of the array that `axis` should be normalized |
| 1350 | against. |
| 1351 | argname : str, optional |
| 1352 | A prefix to put before the error message, typically the name of the |
| 1353 | argument. |
| 1354 | allow_duplicate : bool, optional |
| 1355 | If False, the default, disallow an axis from being specified twice. |
| 1356 | |
| 1357 | Returns |
| 1358 | ------- |
| 1359 | normalized_axes : tuple of int |
| 1360 | The normalized axis index, such that `0 <= normalized_axis < ndim` |
| 1361 | |
| 1362 | Raises |
| 1363 | ------ |
| 1364 | AxisError |
| 1365 | If any axis provided is out of range |
| 1366 | ValueError |
| 1367 | If an axis is repeated |
| 1368 | |
| 1369 | See also |
| 1370 | -------- |
| 1371 | normalize_axis_index : normalizing a single scalar axis |
| 1372 | """ |
| 1373 | # Optimization to speed-up the most common cases. |
| 1374 | if type(axis) not in (tuple, list): |
| 1375 | try: |
| 1376 | axis = [operator.index(axis)] |
| 1377 | except TypeError: |
| 1378 | pass |
| 1379 | # Going via an iterator directly is slower than via list comprehension. |
| 1380 | axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis]) |
| 1381 | if not allow_duplicate and len(set(axis)) != len(axis): |
| 1382 | if argname: |
| 1383 | raise ValueError('repeated axis in `{}` argument'.format(argname)) |
| 1384 | else: |
| 1385 | raise ValueError('repeated axis') |
| 1386 | return axis |
| 1387 |