Repository navigation
Conversation
The index strategies in the tests generated tuple indices independently of the shape of the array being indexed, so a tuple with more than a few arguments was almost never a valid index for that shape, and the tests mostly checked that IndexError was raised for them. Tuples is now built from the shared short_shapes shape, so that it is usually a valid index for an array with that shape. Some of the time it is instead a valid index with arbitrary arguments inserted, or a tuple of completely arbitrary arguments as before, so every combination of index and shape can still be generated. Similarly, integer_arrays and the integers in ndindices are now usually in bounds for the first axis of the shape. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
When index has more than one integer array, each one gives a mask of the elements that are in the corresponding slice of self, and the masks are combined with a logical and. Each mask was checked for being empty, but the combined mask was not, so if the masks were each nonempty but did not overlap, as_subindex returned an index that selects nothing instead of raising ValueError. This was inconsistent with as_subindex in the other direction, which does raise ValueError for the same indices. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
Slice.as_subindex(IntegerArray) returns a boolean mask of the elements of the array that are in the slice, which flattens the dimensions of the array. But IntegerArray.as_subindex(Slice) keeps the shape of the array when every element is in the slice, so that a[j][i.as_subindex(j)] == a[i]. This meant that the two directions, a[j][i.as_subindex(j)] and a[i][j.as_subindex(i)], gave arrays with different shapes whenever the integer array had more than one dimension and was entirely inside the slice. Now if every element of the array is in the slice, there is nothing to mask, and as_subindex returns an index that selects everything without flattening: an empty Tuple for Slice.as_subindex(IntegerArray), and full slices over the dimensions of the arrays for Tuple.as_subindex. This also means that Tuple.as_subindex no longer returns a mask together with an integer array in this case. A mask and an integer array in the same index are broadcast together, which raised IndexError if they could not be broadcast. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
When a slice in self lines up with an integer array in index,
as_subindex gives a boolean mask of the elements of the array that are
in the slice. If self also has an integer array along another axis, the
result has both the mask and an integer array. These index different
axes independently, so the result should select every combination of
the two. But a boolean array in an index is equivalent to its nonzero()
integer arrays, which NumPy broadcasts together with the other integer
arrays in the index. So the result either selected the wrong elements
(only the broadcasted combinations), or, if the arrays could not be
broadcast together, the Tuple constructor raised IndexError.
For example, for a = np.arange(9).reshape((3, 3)),
i = Tuple(slice(0, 2), [0, 1])
j = Tuple([0, 1, 2])
a[j.raw][i.as_subindex(j).raw]
gave [0, 4] instead of [[0, 1], [3, 4]].
The mask is now replaced with its nonzero() arrays, with extra
dimensions added so that they broadcast against the integer arrays as an
outer index.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
When idx2 had an integer array, the test only checked that the unique elements of a[idx2][subindex] were the elements in both a[idx1] and a[idx2]. This did not catch a subindex that left out some of the elements of a[idx2] that are in a[idx1], as long as each distinct element was still there at least once. The only other check compares against as_subindex in the other direction, which had the same bug. Now the test checks how many times each element is in the result: once for each time it is in a[idx1] and each time it is in a[idx2]. The test also never checked the shape of the result, except against as_subindex in the other direction. Now if idx2 only has slices and a[idx1] is a subarray of a[idx2], it checks that a[idx2][subindex] is exactly a[idx1]. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
as_subindex() is only implemented for some types of indices, so for about 80% of the pairs of indices generated from ndindices it raised NotImplementedError, and the test returned without checking anything. Only about 10% of the examples got as far as comparing a result against an array, and almost none of those had an array index. The test now uses its own strategies for the two indices. Most of the time, these are valid indices for the shape that only have the types of indices that as_subindex() is implemented for (no ellipses, newaxes, or boolean scalars, slices with a positive step, and no negative indices), and such that for each axis at least one of the two indices has a slice. The rest of the time they are from ndindices as before. To do this, the strategy for valid tuple indices now has options for which types of indices to generate. The shape is now always short_shapes, since that is the shape that the indices are generated to be valid for. Previously it was an unrelated integer part of the time. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
The test returned without checking anything if as_subindex() raised NotImplementedError. This applied to the explicit examples too, so an example could silently stop testing anything, and the test would not notice if as_subindex() stopped being implemented for some input. The test no longer catches NotImplementedError from idx1.as_subindex(idx2). Instead, the strategy for idx2 rejects any index that it isn't implemented for, in the same way that ndindices rejects indices that ndindex() does not support. An explicit example that as_subindex() isn't implemented for now fails the test unless it is marked with .xfail(raises=NotImplementedError), in which case it is required to raise NotImplementedError. NotImplementedError is still allowed for as_subindex() in the other direction, which the test only uses as an additional check. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
…sions as_subindex() now gives an index that selects everything instead of an all True boolean mask, because the mask flattens the dimensions of the integer arrays that it masks. But if the arrays only have one dimension, there is nothing to flatten, and the two give the same result. So only do it for arrays with more than one dimension, so that the result for 1-D arrays is the same as it was before. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
a[j][i.as_subindex(j)] can only be equal to a[i] if a[i] is a subset of a[j], not the other way around. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
as_subindex() does not handle integer indices that are close to sys.maxsize. Adding to them or multiplying them overflows, which gives an IndexError, a NumPy overflow warning, or a wrong result, depending on the type of the index. An integer that large is never a valid index for the arrays in the tests, so just don't generate them for this test. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
This is the case where every element of a 1-D integer array in the index is in the corresponding slice, and there is an integer array along another axis. The mask for the 1-D array is not replaced with a slice, so it has to be combined with the integer array as an outer index. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
The test only checked that a[idx2][subindex] is exactly a[idx1] when a[idx1] is not empty, because for an empty array there is no way to tell from its elements whether it is a subarray of a[idx2]. But if idx2 selects all of a, then a[idx1] is always a subarray of it, so the shape of an empty result can be checked in that case. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
The two examples that as_subindex() is not implemented for were marked as expected failures of the Hypothesis test. But that allows NotImplementedError from anywhere in the test, including from constructing the indices. Test them in a separate test instead, which only allows it from the call to as_subindex(). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
a[i] being a subset of a[j] is not enough for a[j][i.as_subindex(j)] to be equal to a[i]. If j has an integer array, a[j] can have the elements of a[i] in a different order or more than once, and if j has an integer, a[j] has fewer dimensions than a[i]. It is only guaranteed if j only has slices and a[i] is not empty (an empty a[i] can have a different shape). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
…arrays
If every element of a 1-D integer array in the index is in the
corresponding slice of self, as_subindex() gives an all True mask for
it. If self also has an integer array along another axis, the mask is
combined with it as an outer index. But this is not possible if there is
a slice in between them, because Tuple does not support array indices
that are separated by slices, so as_subindex() raised
NotImplementedError. For example,
Tuple(slice(0, 2), slice(0, 3), [0, 2]).as_subindex(
Tuple([0, 1], slice(0, 3), slice(0, 4)))
In this case, use a full slice instead of the mask, as is already done
for masks with more than one dimension. A slice selects the same
elements as the all True mask, and it can be separated from the integer
array by another slice. The mask is still used when there are no integer
arrays, so that those results are the same as they were before.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012mm28VZGuXHayDrhjyLs4w
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug in #258 was present for years, but the tests never found it because it requires a rare combination of shapes and tuples. Now shape and tuple strategies are more likely to create indices and shapes that are actually compatible with one another.