-
Notifications
You must be signed in to change notification settings - Fork 45
Multi-dimensional arrays support #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
shared, tuples as hypotheses_tuples, | ||
floats, just, composite, one_of, none, | ||
booleans) | ||
from hypothesis.extra.numpy import mutually_broadcastable_shapes | ||
from hypothesis_array import mutually_broadcastable_shapes, get_strategies_namespace | ||
from hypothesis import assume | ||
|
||
from .pytest_helpers import nargs | ||
|
@@ -16,10 +16,14 @@ | |
integer_or_boolean_dtype_objects, dtype_objects) | ||
from ._array_module import (ones, full, float32, float64, bool as bool_dtype, _UndefinedStub) | ||
from . import _array_module | ||
from ._array_module import mod as xp | ||
|
||
from .function_stubs import elementwise_functions | ||
|
||
|
||
xps = get_strategies_namespace(xp) | ||
|
||
|
||
# Set this to True to not fail tests just because a dtype isn't implemented. | ||
# If no compatible dtype is implemented for a given test, the test will fail | ||
# with a hypothesis health check error. Note that this functionality will not | ||
|
@@ -89,11 +93,11 @@ def tuples(elements, *, min_size=0, max_size=None, unique_by=None, unique=False) | |
return lists(elements, min_size=min_size, max_size=max_size, | ||
unique_by=unique_by, unique=unique).map(tuple) | ||
|
||
shapes = tuples(integers(0, 10)).filter(lambda shape: prod(shape) < MAX_ARRAY_SIZE) | ||
shapes = xps.array_shapes(min_side=0).filter(lambda shape: prod(shape) < MAX_ARRAY_SIZE) | ||
|
||
# Use this to avoid memory errors with NumPy. | ||
# See https://github.com/numpy/numpy/issues/15753 | ||
shapes = tuples(integers(0, 10)).filter( | ||
shapes = xps.array_shapes(min_side=0).filter( | ||
lambda shape: prod([i for i in shape if i]) < MAX_ARRAY_SIZE) | ||
|
||
two_mutually_broadcastable_shapes = mutually_broadcastable_shapes(num_shapes=2)\ | ||
|
@@ -121,8 +125,17 @@ def two_broadcastable_shapes(draw, shapes=shapes): | |
|
||
nonbroadcastable_ones_array_two_args = hypotheses_tuples(ones_arrays, ones_arrays) | ||
|
||
# TODO: Generate general arrays here, rather than just scalars. | ||
numeric_arrays = builds(full, just((1,)), floats()) | ||
floating_arrays = xps.arrays(dtype=xps.floating_dtypes(), shape=xps.array_shapes()) | ||
|
||
@composite | ||
def broadcastable_floating_array_pairs(draw): | ||
dtype = draw(xps.floating_dtypes()) | ||
broadcastable_shapes = draw(xps.mutually_broadcastable_shapes(2, min_dims=1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why min_dims=1? We definitely want to make sure dimension 0 arrays are tested. Actually, can't you use two_mutually_broadcastable_shapes defined above here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea why I did this heh. |
||
shape1, shape2 = broadcastable_shapes.input_shapes | ||
assume(len(shape1) >= len(shape2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this restriction here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh this goes back to the issue I was having with the special case tests generating indices which (mostly) worked only when the first array had ndims There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Yeah, there's no need to test broadcasting in the special cases tests (it's already tested separately in test_broadcasting.py), and it just makes building a mask more complicated, so we should just generate arrays of the same shape. |
||
array1 = draw(xps.arrays(dtype=dtype, shape=shape1)) | ||
array2 = draw(xps.arrays(dtype=dtype, shape=shape2)) | ||
return array1, array2 | ||
|
||
@composite | ||
def scalars(draw, dtypes, finite=False): | ||
|
@@ -227,3 +240,5 @@ def multiaxis_indices(draw, shapes): | |
extra = draw(lists(one_of(integer_indices(sizes), slices(sizes)), min_size=0, max_size=3)) | ||
res += extra | ||
return tuple(res) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we instead want a function
broadcastable_array_pairs
(ormutually_broadcastable_array_pairs
), which takes a dtype strategy as input. This will be similar to thearray_scalars
function in this file, which I expect this strategy will replace most instances of. The specific dtype combinations can then be set asfloating_arrays = broadcastable_array_pairs(floating_dtypes)
and so on. See the stuff at the top of test_elementwise.py (which should probably be moved to this file).