Refusals¶
A Refused answer carries a Refusal with three fields. kind is a stable
identifier of the check that failed, message is the reason in words, and
raises is the name of the exception NumPy raises for the call.
This page lists every kind in version 0.1. A kind is safe to branch on. A
message is written by shaperules, is not NumPy's message, and may be reworded
between versions.
AxisError is numpy.exceptions.AxisError, a subclass of both ValueError
and IndexError.
Broadcasting and the matrix product¶
| Kind | Raises | Answered by | Meaning |
|---|---|---|---|
broadcast |
ValueError |
broadcast, broadcast_pair |
Along one axis, two sizes are never equal and neither can be 1. |
mismatch |
ValueError |
match_pair |
Two sizes that must be equal never are. |
matmul-scalar |
ValueError |
matmul |
An operand has no axes. NumPy's matrix product takes no scalars. |
matmul-core |
ValueError |
matmul |
The last axis of the first operand never equals the last-but-one of the second. |
matmul-batch |
ValueError |
matmul |
The axes before the last two do not broadcast. |
Axes¶
| Kind | Raises | Answered by | Meaning |
|---|---|---|---|
axis-out-of-range |
AxisError |
reduce, reduce_shape, arg_reduce, cumulative, transpose, swapaxes, moveaxis, squeeze, expand_dims, concatenate, stack, normalize_axis, normalize_axes |
An axis is not in the range the number of axes allows. |
axis-repeated |
ValueError |
reduce, reduce_shape, transpose, moveaxis, squeeze, expand_dims, normalize_axes |
The same axis is named twice. |
axis-not-integer |
TypeError |
arg_reduce, reduce_shape |
argmax and argmin were given a tuple of axes. They take one. |
reduce-empty |
ValueError |
reduce with needs_elements=True, reduce_shape, arg_reduce |
The axes reduced hold no element, and the reduction has no value for an empty input. |
average-empty |
ZeroDivisionError |
reduce_shape for average |
The result has no elements, and np.average divides by their number. |
transpose-count |
ValueError |
transpose |
The number of axes given is not the number of axes of the array. |
moveaxis-count |
ValueError |
moveaxis |
source and destination name different numbers of axes. |
squeeze-size |
ValueError |
squeeze |
An axis named for removal has a size that is never 1. |
stack has one case where axis-out-of-range carries ValueError in place
of AxisError. NumPy compares the shapes of the arrays before it looks at the
axis. When the axis is out of range and the shapes are not proven equal, NumPy
raises either ValueError for the shapes or AxisError for the axis,
depending on the sizes. AxisError is a ValueError, so ValueError is the
class that is right for every assignment.
Reshaping and joining¶
| Kind | Raises | Answered by | Meaning |
|---|---|---|---|
reshape-size |
ValueError |
reshape |
The number of elements never matches the shape asked for, or -1 has no one value. |
reshape-two-unknowns |
ValueError |
reshape |
More than one size of the new shape is -1. |
join-empty |
ValueError |
concatenate, stack |
No array was given. |
join-scalar |
ValueError |
concatenate |
An array has no axes and so cannot be concatenated. |
join-rank |
ValueError |
concatenate |
The arrays have different numbers of axes. |
join-mismatch |
ValueError |
concatenate |
The arrays differ along an axis that is not the one joined. |
stack-mismatch |
ValueError |
stack |
The arrays do not all have one shape. |
Indexing¶
| Kind | Raises | Answered by | Meaning |
|---|---|---|---|
index-out-of-bounds |
IndexError |
index |
An integer index is outside its axis for every assignment. |
index-too-many |
IndexError |
index |
The key names more axes than the array has. |
index-ellipsis |
IndexError |
index |
The key holds more than one .... |
Element types¶
| Kind | Raises | Answered by | Meaning |
|---|---|---|---|
no-loop |
TypeError |
ufunc_dtype, ufunc_dtypes, reduce_dtype |
NumPy has no loop of the ufunc, or no definition of the reduction, for these element types. |
One example of each¶
Each call below gets the refusal named on its line. The test suite runs this block, so the list cannot drift from the code without a failure.
>>> import shaperules as sr
>>> refused = [
... sr.broadcast((3, 4), (5,)),
... sr.match_pair("n", "n + 1"),
... sr.matmul((), (3, 4)),
... sr.matmul((2, 3), (4, 5)),
... sr.matmul((2, 3, "n"), (4, "n", 5)),
... sr.reduce((4, 5), axis=2),
... sr.reduce((4, 5), axis=(0, 0)),
... sr.arg_reduce((4, 5), axis=(0, 1)),
... sr.reduce((0, 5), axis=0, needs_elements=True),
... sr.reduce_shape("average", (0, 5), axis=1),
... sr.transpose((4, 5), (0,)),
... sr.moveaxis((4, 5, 6), (0, 1), 2),
... sr.squeeze((1, 5), axis=1),
... sr.reshape((6,), (4,)),
... sr.reshape((6,), (-1, -1)),
... sr.concatenate([]),
... sr.concatenate([(), ()]),
... sr.concatenate([(3, 4), (3,)]),
... sr.concatenate([(3, 4), (3, 5)]),
... sr.stack([(3, 4), (3, 5)]),
... sr.index((3, 4), 5),
... sr.index((3, 4), (0, 0, 0)),
... sr.index((3, 4), (..., ...)),
... sr.ufunc_dtype("subtract", "bool", "bool"),
... ]
>>> for answer in refused:
... print(f"{answer.refusal.kind:22}{answer.refusal.raises}")
broadcast ValueError
mismatch ValueError
matmul-scalar ValueError
matmul-core ValueError
matmul-batch ValueError
axis-out-of-range AxisError
axis-repeated ValueError
axis-not-integer TypeError
reduce-empty ValueError
average-empty ZeroDivisionError
transpose-count ValueError
moveaxis-count ValueError
squeeze-size ValueError
reshape-size ValueError
reshape-two-unknowns ValueError
join-empty ValueError
join-scalar ValueError
join-rank ValueError
join-mismatch ValueError
stack-mismatch ValueError
index-out-of-bounds IndexError
index-too-many IndexError
index-ellipsis IndexError
no-loop TypeError
The case of stack described above:
>>> sr.stack([("n", 3), ("n", 3)], axis=5).refusal.raises
'AxisError'
>>> sr.stack([("n", 3), ("m", 3)], axis=5).refusal.raises
'ValueError'
Reading a refusal¶
>>> answer = sr.squeeze((1, 5), axis=1)
>>> answer.refusal.kind
'squeeze-size'
>>> print(answer.refusal)
squeeze: axis 1 of (1, 5) has size 5, not 1
A refusal about one axis also carries that axis, for a caller that writes
its own message. For axis-out-of-range and axis-repeated it is the axis
as the caller wrote it. For squeeze-size it is the index of the axis whose
size is not 1. For every other kind it is None.
>>> sr.reduce((4, 5), axis=(0, -3)).refusal.axis
-3
>>> sr.transpose((4, 5), (1, 1)).refusal.axis
1
>>> sr.squeeze((4, 1, 5), axis=-1).refusal.axis
2
>>> sr.broadcast((3,), (4,)).refusal.axis is None
True
unwrap turns a refusal into a ShapeRefused exception that carries it. See
The three answers.