MCPcopy
hub / github.com/numpy/numpy / normalize_axis_tuple

Function normalize_axis_tuple

numpy/_core/numeric.py:1427–1481  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1425
1426@set_module("numpy.lib.array_utils")
1427def normalize_axis_tuple(axis, ndim, argname=None, allow_duplicate=False):
1428 """
1429 Normalizes an axis argument into a tuple of non-negative integer axes.
1430
1431 This handles shorthands such as ``1`` and converts them to ``(1,)``,
1432 as well as performing the handling of negative indices covered by
1433 `normalize_axis_index`.
1434
1435 By default, this forbids axes from being specified multiple times.
1436
1437 Used internally by multi-axis-checking logic.
1438
1439 Parameters
1440 ----------
1441 axis : int, iterable of int
1442 The un-normalized index or indices of the axis.
1443 ndim : int
1444 The number of dimensions of the array that `axis` should be normalized
1445 against.
1446 argname : str, optional
1447 A prefix to put before the error message, typically the name of the
1448 argument.
1449 allow_duplicate : bool, optional
1450 If False, the default, disallow an axis from being specified twice.
1451
1452 Returns
1453 -------
1454 normalized_axes : tuple of int
1455 The normalized axis index, such that `0 <= normalized_axis < ndim`
1456
1457 Raises
1458 ------
1459 AxisError
1460 If any axis provided is out of range
1461 ValueError
1462 If an axis is repeated
1463
1464 See also
1465 --------
1466 normalize_axis_index : normalizing a single scalar axis
1467 """
1468 # Optimization to speed-up the most common cases.
1469 if not isinstance(axis, (tuple, list)):
1470 try:
1471 axis = [operator.index(axis)]
1472 except TypeError:
1473 pass
1474 # Going via an iterator directly is slower than via list comprehension.
1475 axis = tuple(normalize_axis_index(ax, ndim, argname) for ax in axis)
1476 if not allow_duplicate and len(set(axis)) != len(axis):
1477 if argname:
1478 raise ValueError(f'repeated axis in `{argname}` argument')
1479 else:
1480 raise ValueError('repeated axis')
1481 return axis
1482
1483
1484def _moveaxis_dispatcher(a, source, destination):

Callers 3

vector_normFunction · 0.85
averageFunction · 0.85
compress_ndFunction · 0.85

Calls 1

indexMethod · 0.45

Tested by 7

test_partitionMethod · 0.64
test_partition_cdtypeMethod · 0.64
test_partition_intFunction · 0.64
test_partition_fpFunction · 0.64

Used in the wild real call sites across dependent graphs

searching dependent graphs…