forked from HypothesisWorks/hypothesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gen_data.py
1151 lines (965 loc) · 37.1 KB
/
test_gen_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2020 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
#
# END HEADER
import sys
from functools import reduce
from itertools import zip_longest
import numpy as np
import pytest
from hypothesis import HealthCheck, assume, given, note, settings, strategies as st
from hypothesis.errors import InvalidArgument, Unsatisfiable
from hypothesis.extra import numpy as nps
from tests.common.debug import find_any, minimal
from tests.common.utils import fails_with, flaky
ANY_SHAPE = nps.array_shapes(min_dims=0, max_dims=32, min_side=0, max_side=32)
ANY_NONZERO_SHAPE = nps.array_shapes(min_dims=0, max_dims=32, min_side=1, max_side=32)
@given(nps.arrays(float, ()))
def test_empty_dimensions_are_arrays(x):
assert isinstance(x, np.ndarray)
assert x.dtype.kind == "f"
@given(nps.arrays(float, (1, 0, 1)))
def test_can_handle_zero_dimensions(x):
assert x.shape == (1, 0, 1)
@given(nps.arrays("uint32", (5, 5)))
def test_generates_unsigned_ints(x):
assert (x >= 0).all()
@given(nps.arrays(int, (1,)))
def test_assert_fits_in_machine_size(x):
pass
def test_generates_and_minimizes():
assert (minimal(nps.arrays(float, (2, 2))) == np.zeros(shape=(2, 2))).all()
def test_can_minimize_large_arrays():
x = minimal(
nps.arrays("uint32", 100),
lambda x: np.any(x) and not np.all(x),
timeout_after=60,
)
assert np.logical_or(x == 0, x == 1).all()
assert np.count_nonzero(x) in (1, len(x) - 1)
@flaky(max_runs=50, min_passes=1)
def test_can_minimize_float_arrays():
x = minimal(nps.arrays(float, 50), lambda t: t.sum() >= 1.0)
assert x.sum() in (1, 50)
class Foo:
pass
foos = st.tuples().map(lambda _: Foo())
def test_can_create_arrays_of_composite_types():
arr = minimal(nps.arrays(object, 100, elements=foos))
for x in arr:
assert isinstance(x, Foo)
@given(st.lists(st.integers()), st.data())
def test_can_create_zero_dim_arrays_of_lists(x, data):
arr = data.draw(nps.arrays(object, (), elements=st.just(x)))
assert arr.shape == ()
assert arr.dtype == np.dtype(object)
assert arr.item() == x
def test_can_create_arrays_of_tuples():
arr = minimal(
nps.arrays(object, 10, elements=st.tuples(st.integers(), st.integers())),
lambda x: all(t0 != t1 for t0, t1 in x),
)
assert all(a in ((1, 0), (0, 1)) for a in arr)
@given(nps.arrays(object, (2, 2), elements=st.tuples(st.integers())))
def test_does_not_flatten_arrays_of_tuples(arr):
assert isinstance(arr[0][0], tuple)
@given(
nps.arrays(object, (2, 2), elements=st.lists(st.integers(), min_size=1, max_size=1))
)
def test_does_not_flatten_arrays_of_lists(arr):
assert isinstance(arr[0][0], list)
@given(nps.array_shapes())
def test_can_generate_array_shapes(shape):
assert isinstance(shape, tuple)
assert all(isinstance(i, int) for i in shape)
@settings(deadline=None, max_examples=10)
@given(st.integers(0, 10), st.integers(0, 9), st.integers(0), st.integers(0))
def test_minimise_array_shapes(min_dims, dim_range, min_side, side_range):
smallest = minimal(
nps.array_shapes(
min_dims=min_dims,
max_dims=min_dims + dim_range,
min_side=min_side,
max_side=min_side + side_range,
)
)
assert len(smallest) == min_dims and all(k == min_side for k in smallest)
@pytest.mark.parametrize(
"kwargs", [{"min_side": 100}, {"min_dims": 15}, {"min_dims": 32}]
)
def test_interesting_array_shapes_argument(kwargs):
nps.array_shapes(**kwargs).example()
@given(nps.scalar_dtypes())
def test_can_generate_scalar_dtypes(dtype):
assert isinstance(dtype, np.dtype)
@settings(max_examples=100)
@given(
nps.nested_dtypes(
subtype_strategy=st.one_of(
nps.scalar_dtypes(), nps.byte_string_dtypes(), nps.unicode_string_dtypes()
)
)
)
def test_can_generate_compound_dtypes(dtype):
assert isinstance(dtype, np.dtype)
@settings(max_examples=100)
@given(
nps.nested_dtypes(
subtype_strategy=st.one_of(
nps.scalar_dtypes(), nps.byte_string_dtypes(), nps.unicode_string_dtypes()
)
).flatmap(lambda dt: nps.arrays(dtype=dt, shape=1))
)
def test_can_generate_data_compound_dtypes(arr):
# This is meant to catch the class of errors which prompted PR #2085
assert isinstance(arr, np.ndarray)
@given(nps.nested_dtypes())
def test_np_dtype_is_idempotent(dtype):
assert dtype == np.dtype(dtype)
def test_minimise_scalar_dtypes():
assert minimal(nps.scalar_dtypes()) == np.dtype("bool")
def test_minimise_nested_types():
assert minimal(nps.nested_dtypes()) == np.dtype("bool")
def test_minimise_array_strategy():
smallest = minimal(
nps.arrays(
nps.nested_dtypes(max_itemsize=200),
nps.array_shapes(max_dims=3, max_side=3),
)
)
assert smallest.dtype == np.dtype("bool") and not smallest.any()
@given(nps.array_dtypes(allow_subarrays=False))
def test_can_turn_off_subarrays(dt):
for name in dt.names:
assert dt.fields[name][0].shape == ()
def test_array_dtypes_may_have_field_titles():
find_any(nps.array_dtypes(), lambda dt: len(dt.fields) > len(dt.names))
@pytest.mark.parametrize("byteorder", ["<", ">"])
@given(data=st.data())
def test_can_restrict_endianness(data, byteorder):
dtype = data.draw(nps.integer_dtypes(endianness=byteorder, sizes=(16, 32, 64)))
if byteorder == ("<" if sys.byteorder == "little" else ">"):
assert dtype.byteorder == "="
else:
assert dtype.byteorder == byteorder
@given(nps.integer_dtypes(sizes=8))
def test_can_specify_size_as_an_int(dt):
assert dt.itemsize == 1
@given(st.data())
def test_can_draw_arrays_from_scalars(data):
dt = data.draw(nps.scalar_dtypes())
result = data.draw(nps.arrays(dtype=dt, shape=()))
assert isinstance(result, np.ndarray)
assert result.dtype == dt
@given(st.data())
def test_can_cast_for_arrays(data):
# Note: this only passes with castable datatypes, certain dtype
# combinations will result in an error if numpy is not able to cast them.
dt_elements = np.dtype(data.draw(st.sampled_from(["bool", "<i2", ">i2"])))
dt_desired = np.dtype(
data.draw(st.sampled_from(["<i2", ">i2", "float32", "float64"]))
)
result = data.draw(
nps.arrays(
dtype=dt_desired, elements=nps.from_dtype(dt_elements), shape=(1, 2, 3)
)
)
assert isinstance(result, np.ndarray)
assert result.dtype == dt_desired
@given(nps.arrays(dtype="int8", shape=st.integers(0, 20), unique=True))
def test_array_values_are_unique(arr):
assert len(set(arr)) == len(arr)
def test_cannot_generate_unique_array_of_too_many_elements():
strat = nps.arrays(dtype=int, elements=st.integers(0, 5), shape=10, unique=True)
with pytest.raises(Unsatisfiable):
strat.example()
@given(
nps.arrays(
elements=st.just(0.0),
dtype=float,
fill=st.just(np.nan),
shape=st.integers(0, 20),
unique=True,
)
)
def test_array_values_are_unique_high_collision(arr):
assert (arr == 0.0).sum() <= 1
@given(nps.arrays(dtype="int8", shape=(4,), elements=st.integers(0, 3), unique=True))
def test_generates_all_values_for_unique_array(arr):
# Ensures that the "reject already-seen element" branch is covered
assert len(set(arr)) == len(arr)
def test_may_fill_with_nan_when_unique_is_set():
find_any(
nps.arrays(
dtype=float,
elements=st.floats(allow_nan=False),
shape=10,
unique=True,
fill=st.just(np.nan),
),
lambda x: np.isnan(x).any(),
)
@given(
nps.arrays(
dtype=float,
elements=st.floats(allow_nan=False),
shape=10,
unique=True,
fill=st.just(np.nan),
)
)
def test_is_still_unique_with_nan_fill(xs):
assert len(set(xs)) == len(xs)
@fails_with(InvalidArgument)
@given(
nps.arrays(
dtype=float,
elements=st.floats(allow_nan=False),
shape=10,
unique=True,
fill=st.just(0.0),
)
)
def test_may_not_fill_with_non_nan_when_unique_is_set(arr):
pass
@fails_with(InvalidArgument)
@given(nps.arrays(dtype="U", shape=10, unique=True, fill=st.just("")))
def test_may_not_fill_with_non_nan_when_unique_is_set_and_type_is_not_number(arr):
pass
@pytest.mark.parametrize("fill", [False, True])
@fails_with(InvalidArgument)
@given(st.data())
def test_overflowing_integers_are_deprecated(fill, data):
kw = {"elements": st.just(300)}
if fill:
kw = {"elements": st.nothing(), "fill": kw["elements"]}
arr = data.draw(nps.arrays(dtype="int8", shape=(1,), **kw))
assert arr[0] == (300 % 256)
@pytest.mark.parametrize("fill", [False, True])
@pytest.mark.parametrize(
"dtype,strat",
[
("float16", st.floats(min_value=65520, allow_infinity=False)),
("float32", st.floats(min_value=10 ** 40, allow_infinity=False)),
(
"complex64",
st.complex_numbers(min_magnitude=10 ** 300, allow_infinity=False),
),
("U1", st.text(min_size=2, max_size=2)),
("S1", st.binary(min_size=2, max_size=2)),
],
)
@fails_with(InvalidArgument)
@given(data=st.data())
def test_unrepresentable_elements_are_deprecated(fill, dtype, strat, data):
if fill:
kw = {"elements": st.nothing(), "fill": strat}
else:
kw = {"elements": strat}
arr = data.draw(nps.arrays(dtype=dtype, shape=(1,), **kw))
try:
# This is a float or complex number, and has overflowed to infinity,
# triggering our deprecation for overflow.
assert np.isinf(arr[0])
except TypeError:
# We tried to call isinf on a string. The string was generated at
# length two, then truncated by the dtype of size 1 - deprecation
# again. If the first character was \0 it is now the empty string.
assert len(arr[0]) <= 1
@given(nps.arrays(dtype="float16", shape=(1,)))
def test_inferred_floats_do_not_overflow(arr):
pass
@given(nps.arrays(dtype="float16", shape=10, elements={"min_value": 0, "max_value": 1}))
def test_inferred_floats_can_be_constrained_at_low_width(arr):
assert (arr >= 0).all()
assert (arr <= 1).all()
@given(
nps.arrays(
dtype="float16",
shape=10,
elements={
"min_value": 0,
"max_value": 1,
"exclude_min": True,
"exclude_max": True,
},
)
)
def test_inferred_floats_can_be_constrained_at_low_width_excluding_endpoints(arr):
assert (arr > 0).all()
assert (arr < 1).all()
@given(
nps.arrays(
dtype="float16",
shape=10,
unique=True,
elements=st.integers(1, 9),
fill=st.just(np.nan),
)
)
def test_unique_array_with_fill_can_use_all_elements(arr):
assume(len(set(arr)) == arr.size)
@given(nps.arrays(dtype="uint8", shape=25, unique=True, fill=st.nothing()))
def test_unique_array_without_fill(arr):
# This test covers the collision-related branchs for fully dense unique arrays.
# Choosing 25 of 256 possible elements means we're almost certain to see colisions
# thanks to the 'birthday paradox', but finding unique elemennts is still easy.
assume(len(set(arr)) == arr.size)
@given(ndim=st.integers(0, 5), data=st.data())
def test_mapped_positive_axes_are_unique(ndim, data):
min_size = data.draw(st.integers(0, ndim), label="min_size")
max_size = data.draw(st.integers(min_size, ndim), label="max_size")
axes = data.draw(
nps.valid_tuple_axes(ndim, min_size=min_size, max_size=max_size), label="axes"
)
assert len(set(axes)) == len({i if 0 < i else ndim + i for i in axes})
@given(ndim=st.integers(0, 5), data=st.data())
def test_length_bounds_are_satisfied(ndim, data):
min_size = data.draw(st.integers(0, ndim), label="min_size")
max_size = data.draw(st.integers(min_size, ndim), label="max_size")
axes = data.draw(
nps.valid_tuple_axes(ndim, min_size=min_size, max_size=max_size), label="axes"
)
assert min_size <= len(axes) <= max_size
@given(shape=nps.array_shapes(), data=st.data())
def test_axes_are_valid_inputs_to_sum(shape, data):
x = np.zeros(shape, dtype="uint8")
axes = data.draw(nps.valid_tuple_axes(ndim=len(shape)), label="axes")
np.sum(x, axes)
@settings(deadline=None, max_examples=10)
@given(ndim=st.integers(0, 3), data=st.data())
def test_minimize_tuple_axes(ndim, data):
min_size = data.draw(st.integers(0, ndim), label="min_size")
max_size = data.draw(st.integers(min_size, ndim), label="max_size")
smallest = minimal(nps.valid_tuple_axes(ndim, min_size=min_size, max_size=max_size))
assert len(smallest) == min_size and all(k > -1 for k in smallest)
@settings(deadline=None, max_examples=10)
@given(ndim=st.integers(0, 3), data=st.data())
def test_minimize_negative_tuple_axes(ndim, data):
min_size = data.draw(st.integers(0, ndim), label="min_size")
max_size = data.draw(st.integers(min_size, ndim), label="max_size")
smallest = minimal(
nps.valid_tuple_axes(ndim, min_size=min_size, max_size=max_size),
lambda x: all(i < 0 for i in x),
)
assert len(smallest) == min_size
@given(nps.broadcastable_shapes((), min_side=0, max_side=0, min_dims=0, max_dims=0))
def test_broadcastable_empty_shape(shape):
assert shape == ()
@settings(deadline=None, suppress_health_check=[HealthCheck.too_slow])
@given(shape=ANY_SHAPE, data=st.data())
def test_broadcastable_shape_bounds_are_satisfied(shape, data):
min_dims = data.draw(st.integers(0, 32), label="min_dims")
max_dims = data.draw(st.none() | st.integers(min_dims, 32), label="max_dims")
min_side = data.draw(st.integers(0, 3), label="min_side")
max_side = data.draw(st.none() | st.integers(min_side, 6), label="max_side")
try:
bshape = data.draw(
nps.broadcastable_shapes(
shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
),
label="bshape",
)
except InvalidArgument:
assume(False)
assert False, "unreachable"
if max_dims is None:
max_dims = max(len(shape), min_dims) + 2
if max_side is None:
max_side = max(tuple(shape[::-1][:max_dims]) + (min_side,)) + 2
assert isinstance(bshape, tuple) and all(isinstance(s, int) for s in bshape)
assert min_dims <= len(bshape) <= max_dims
assert all(min_side <= s <= max_side for s in bshape)
@settings(deadline=None)
@given(num_shapes=st.integers(1, 4), base_shape=ANY_SHAPE, data=st.data())
def test_mutually_broadcastable_shape_bounds_are_satisfied(
num_shapes, base_shape, data
):
min_dims = data.draw(st.integers(0, 32), label="min_dims")
max_dims = data.draw(
st.one_of(st.none(), st.integers(min_dims, 32)), label="max_dims"
)
min_side = data.draw(st.integers(0, 3), label="min_side")
max_side = data.draw(
st.one_of(st.none(), st.integers(min_side, 6)), label="max_side"
)
try:
shapes, result = data.draw(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
),
label="shapes, result",
)
except InvalidArgument:
assume(False)
assert False, "unreachable"
if max_dims is None:
max_dims = max(len(base_shape), min_dims) + 2
if max_side is None:
max_side = max(tuple(base_shape[::-1][:max_dims]) + (min_side,)) + 2
assert isinstance(shapes, tuple)
assert isinstance(result, tuple)
assert all(isinstance(s, int) for s in result)
for bshape in shapes:
assert isinstance(bshape, tuple) and all(isinstance(s, int) for s in bshape)
assert min_dims <= len(bshape) <= max_dims
assert all(min_side <= s <= max_side for s in bshape)
def _draw_valid_bounds(data, shape, max_dims, permit_none=True):
if max_dims == 0 or not shape:
return 0, None
smallest_side = min(shape[::-1][:max_dims])
min_strat = (
st.sampled_from([1, smallest_side])
if smallest_side > 1
else st.just(smallest_side)
)
min_side = data.draw(min_strat, label="min_side")
largest_side = max(max(shape[::-1][:max_dims]), min_side)
if permit_none:
max_strat = st.one_of(st.none(), st.integers(largest_side, largest_side + 2))
else:
max_strat = st.integers(largest_side, largest_side + 2)
max_side = data.draw(max_strat, label="max_side")
return min_side, max_side
def _broadcast_two_shapes(shape_a: nps.Shape, shape_b: nps.Shape) -> nps.Shape:
result = []
for a, b in zip_longest(reversed(shape_a), reversed(shape_b), fillvalue=1):
if a != b and (a != 1) and (b != 1):
raise ValueError(
"shapes %r and %r are not broadcast-compatible" % (shape_a, shape_b)
)
result.append(a if a != 1 else b)
return tuple(reversed(result))
def _broadcast_shapes(*shapes):
"""Returns the shape resulting from broadcasting the
input shapes together.
Raises ValueError if the shapes are not broadcast-compatible"""
assert len(shapes)
return reduce(_broadcast_two_shapes, shapes, ())
@settings(deadline=None, max_examples=500)
@given(
shapes=st.lists(
nps.array_shapes(min_dims=0, min_side=0, max_dims=4, max_side=4), min_size=1
)
)
def test_broadcastable_shape_util(shapes):
"""Ensures that `_broadcast_shapes` raises when fed incompatible shapes,
and ensures that it produces the true broadcasted shape"""
if len(shapes) == 1:
assert _broadcast_shapes(*shapes) == shapes[0]
return
arrs = [np.zeros(s, dtype=np.uint8) for s in shapes]
try:
broadcast_out = np.broadcast_arrays(*arrs)
except ValueError:
with pytest.raises(ValueError):
_broadcast_shapes(*shapes)
return
broadcasted_shape = _broadcast_shapes(*shapes)
assert broadcast_out[0].shape == broadcasted_shape
@settings(deadline=None, max_examples=200)
@given(shape=ANY_NONZERO_SHAPE, data=st.data())
def test_broadcastable_shape_has_good_default_values(shape, data):
# This test ensures that default parameters can always produce broadcast-compatible shapes
broadcastable_shape = data.draw(
nps.broadcastable_shapes(shape), label="broadcastable_shapes"
)
# error if drawn shape for b is not broadcast-compatible
_broadcast_shapes(shape, broadcastable_shape)
@settings(deadline=None, max_examples=200)
@given(base_shape=ANY_SHAPE, num_shapes=st.integers(1, 10), data=st.data())
def test_mutually_broadcastableshapes_has_good_default_values(
num_shapes, base_shape, data
):
# This test ensures that default parameters can always produce broadcast-compatible shapes
shapes, result = data.draw(
nps.mutually_broadcastable_shapes(num_shapes=num_shapes, base_shape=base_shape),
label="shapes, result",
)
assert len(shapes) == num_shapes
# raises if shapes are not mutually-compatible
assert result == _broadcast_shapes(base_shape, *shapes)
@settings(deadline=None)
@given(min_dims=st.integers(0, 32), shape=ANY_SHAPE, data=st.data())
def test_broadcastable_shape_can_broadcast(min_dims, shape, data):
max_dims = data.draw(st.none() | st.integers(min_dims, 32), label="max_dims")
min_side, max_side = _draw_valid_bounds(data, shape, max_dims)
broadcastable_shape = data.draw(
nps.broadcastable_shapes(
shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
),
label="broadcastable_shapes",
)
# error if drawn shape for b is not broadcast-compatible
_broadcast_shapes(shape, broadcastable_shape)
@settings(deadline=None)
@given(
num_shapes=st.integers(1, 10),
min_dims=st.integers(0, 32),
base_shape=ANY_SHAPE,
data=st.data(),
)
def test_mutually_broadcastable_shape_can_broadcast(
num_shapes, min_dims, base_shape, data
):
max_dims = data.draw(st.none() | st.integers(min_dims, 32), label="max_dims")
min_side, max_side = _draw_valid_bounds(data, base_shape, max_dims)
shapes, result = data.draw(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
),
label="shapes, result",
)
# error if drawn shapes are not mutually broadcast-compatible
assert result == _broadcast_shapes(base_shape, *shapes)
@settings(deadline=None, max_examples=10)
@given(min_dims=st.integers(0, 32), shape=ANY_SHAPE, data=st.data())
def test_minimize_broadcastable_shape(min_dims, shape, data):
# Ensure aligned dimensions of broadcastable shape minimizes to `(1,) * min_dims`
max_dims = data.draw(st.none() | st.integers(min_dims, 32), label="max_dims")
min_side, max_side = _draw_valid_bounds(data, shape, max_dims, permit_none=False)
smallest = minimal(
nps.broadcastable_shapes(
shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
)
)
note(f"(smallest): {smallest}")
n_leading = max(len(smallest) - len(shape), 0)
n_aligned = max(len(smallest) - n_leading, 0)
expected = [min_side] * n_leading + [
1 if min_side <= 1 <= max_side else i for i in shape[len(shape) - n_aligned :]
]
assert tuple(expected) == smallest
@settings(deadline=None, max_examples=50)
@given(
num_shapes=st.integers(1, 3),
min_dims=st.integers(0, 5),
base_shape=nps.array_shapes(min_dims=0, max_dims=3, min_side=0, max_side=5),
data=st.data(),
)
def test_minimize_mutually_broadcastable_shape(num_shapes, min_dims, base_shape, data):
# Ensure aligned dimensions of broadcastable shape minimizes to `(1,) * min_dims`
max_dims = data.draw(st.none() | st.integers(min_dims, 5), label="max_dims")
min_side, max_side = _draw_valid_bounds(
data, base_shape, max_dims, permit_none=False
)
if num_shapes > 1:
# shrinking gets a little bit hairy when we have empty axes
# and multiple num_shapes
assume(min_side > 0)
note("(min_side, max_side): {}".format((min_side, max_side)))
smallest_shapes, result = minimal(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
)
)
note("(smallest_shapes, result): {}".format((smallest_shapes, result)))
assert len(smallest_shapes) == num_shapes
assert result == _broadcast_shapes(base_shape, *smallest_shapes)
for smallest in smallest_shapes:
n_leading = max(len(smallest) - len(base_shape), 0)
n_aligned = max(len(smallest) - n_leading, 0)
expected = [min_side] * n_leading + [
1 if min_side <= 1 <= max_side else i
for i in base_shape[len(base_shape) - n_aligned :]
]
assert tuple(expected) == smallest
@settings(deadline=None)
@given(max_dims=st.integers(4, 6), data=st.data())
def test_broadcastable_shape_adjusts_max_dim_with_explicit_bounds(max_dims, data):
# Ensures that `broadcastable_shapes` limits itself to satisfiable dimensions
# Broadcastable values can only be drawn for dims 0-3 for these shapes
shape = data.draw(st.sampled_from([(5, 3, 2, 1), (0, 3, 2, 1)]), label="shape")
broadcastable_shape = data.draw(
nps.broadcastable_shapes(
shape, min_side=2, max_side=3, min_dims=3, max_dims=max_dims
),
label="broadcastable_shapes",
)
assert len(broadcastable_shape) == 3
# error if drawn shape for b is not broadcast-compatible
_broadcast_shapes(shape, broadcastable_shape)
@settings(deadline=None)
@given(
max_side=st.sampled_from([3, None]),
min_dims=st.integers(0, 4),
num_shapes=st.integers(1, 3),
data=st.data(),
)
def test_mutually_broadcastable_shape_adjusts_max_dim_with_default_bounds(
max_side, min_dims, num_shapes, data
):
# Ensures that `mutually_broadcastable_shapes` limits itself to satisfiable dimensions
# when a default `max_dims` is derived.
base_shape = data.draw(
st.sampled_from([(5, 3, 2, 1), (0, 3, 2, 1)]), label="base_shape"
)
try:
shapes, result = data.draw(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=2,
max_side=max_side,
min_dims=min_dims,
),
label="shapes, result",
)
except InvalidArgument:
# There is no satisfiable `max_dims` for us to tune
assert min_dims == 4 and (max_side == 3 or base_shape[0] == 0)
return
if max_side == 3 or base_shape[0] == 0:
assert all(len(s) <= 3 for s in shapes)
elif min_dims == 4:
assert all(4 <= len(s) for s in shapes)
# error if drawn shape for b is not broadcast-compatible
assert len(shapes) == num_shapes
assert result == _broadcast_shapes(base_shape, *shapes)
@settings(deadline=None, max_examples=10)
@given(min_dims=st.integers(0, 32), min_side=st.integers(2, 3), data=st.data())
def test_broadcastable_shape_shrinking_with_singleton_out_of_bounds(
min_dims, min_side, data
):
max_dims = data.draw(st.none() | st.integers(min_dims, 32), label="max_dims")
max_side = data.draw(st.none() | st.integers(min_side, 6), label="max_side")
shape = data.draw(st.integers(1, 4).map(lambda n: n * (1,)), label="shape")
smallest = minimal(
nps.broadcastable_shapes(
shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
)
)
assert smallest == (min_side,) * min_dims
@settings(deadline=None, max_examples=50)
@given(
num_shapes=st.integers(1, 4),
min_dims=st.integers(0, 4),
min_side=st.integers(2, 3),
data=st.data(),
)
def test_mutually_broadcastable_shapes_shrinking_with_singleton_out_of_bounds(
num_shapes, min_dims, min_side, data
):
"""Ensures that shapes minimize to `(min_side,) * min_dims` when singleton dimensions
are disallowed."""
max_dims = data.draw(st.none() | st.integers(min_dims, 4), label="max_dims")
max_side = data.draw(
st.one_of(st.none(), st.integers(min_side, 6)), label="max_side"
)
ndims = data.draw(st.integers(1, 4), label="ndim")
base_shape = (1,) * ndims
smallest_shapes, result = minimal(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=min_side,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
)
)
note("(smallest_shapes, result): {}".format((smallest_shapes, result)))
assert len(smallest_shapes) == num_shapes
assert result == _broadcast_shapes(base_shape, *smallest_shapes)
for smallest in smallest_shapes:
assert smallest == (min_side,) * min_dims
@given(
num_shapes=st.integers(1, 4),
min_dims=st.integers(1, 32),
max_side=st.integers(1, 6),
data=st.data(),
)
def test_mutually_broadcastable_shapes_only_singleton_is_valid(
num_shapes, min_dims, max_side, data
):
"""Ensures that, when all aligned base-shape dim sizes are larger
than ``max_side``, only singletons can be drawn"""
max_dims = data.draw(st.integers(min_dims, 32), label="max_dims")
base_shape = data.draw(
nps.array_shapes(min_side=max_side + 1, min_dims=1), label="base_shape"
)
input_shapes, result = data.draw(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=1,
max_side=max_side,
min_dims=min_dims,
max_dims=max_dims,
),
label="input_shapes, result",
)
assert len(input_shapes) == num_shapes
assert result == _broadcast_shapes(base_shape, *input_shapes)
for shape in input_shapes:
assert all(i == 1 for i in shape[-len(base_shape) :])
@settings(deadline=None)
@given(
shape=nps.array_shapes(min_dims=0, max_dims=3, min_side=0, max_side=5),
max_dims=st.integers(0, 6),
data=st.data(),
)
def test_broadcastable_shape_can_generate_arbitrary_ndims(shape, max_dims, data):
# ensures that generates shapes can possess any length in [min_dims, max_dims]
desired_ndim = data.draw(st.integers(0, max_dims), label="desired_ndim")
min_dims = data.draw(
st.one_of(st.none(), st.integers(0, desired_ndim)), label="min_dims"
)
# check default arg behavior too
kwargs = {"min_dims": min_dims} if min_dims is not None else {}
find_any(
nps.broadcastable_shapes(shape, min_side=0, max_dims=max_dims, **kwargs),
lambda x: len(x) == desired_ndim,
settings(max_examples=10 ** 6),
)
@settings(deadline=None)
@given(
num_shapes=st.integers(1, 3),
base_shape=nps.array_shapes(min_dims=0, max_dims=3, min_side=0, max_side=5),
max_dims=st.integers(0, 4),
data=st.data(),
)
def test_mutually_broadcastable_shapes_can_generate_arbitrary_ndims(
num_shapes, base_shape, max_dims, data
):
# ensures that each generated shape can possess any length in [min_dims, max_dims]
desired_ndims = data.draw(
st.lists(st.integers(0, max_dims), min_size=num_shapes, max_size=num_shapes),
label="desired_ndims",
)
min_dims = data.draw(
st.one_of(st.none(), st.integers(0, min(desired_ndims))), label="min_dims"
)
# check default arg behavior too
kwargs = {"min_dims": min_dims} if min_dims is not None else {}
find_any(
nps.mutually_broadcastable_shapes(
num_shapes=num_shapes,
base_shape=base_shape,
min_side=0,
max_dims=max_dims,
**kwargs,
),
lambda x: {len(s) for s in x.input_shapes} == set(desired_ndims),
settings(max_examples=10 ** 6),
)
@settings(deadline=None)
@given(
shape=nps.array_shapes(min_dims=1, min_side=1),
dtype=st.one_of(nps.unsigned_integer_dtypes(), nps.integer_dtypes()),
data=st.data(),
)
def test_advanced_integer_index_is_valid_with_default_result_shape(shape, dtype, data):
index = data.draw(nps.integer_array_indices(shape, dtype=dtype))
x = np.zeros(shape)
out = x[index] # raises if the index is invalid
assert not np.shares_memory(x, out) # advanced indexing should not return a view
assert all(dtype == x.dtype for x in index)
@settings(deadline=None)
@given(
shape=nps.array_shapes(min_dims=1, min_side=1),
min_dims=st.integers(0, 3),
min_side=st.integers(0, 3),
dtype=st.one_of(nps.unsigned_integer_dtypes(), nps.integer_dtypes()),
data=st.data(),
)
def test_advanced_integer_index_is_valid_and_satisfies_bounds(
shape, min_dims, min_side, dtype, data
):
max_side = data.draw(st.integers(min_side, min_side + 2), label="max_side")
max_dims = data.draw(st.integers(min_dims, min_dims + 2), label="max_dims")
index = data.draw(
nps.integer_array_indices(
shape,
result_shape=nps.array_shapes(
min_dims=min_dims,
max_dims=max_dims,
min_side=min_side,
max_side=max_side,
),
dtype=dtype,
)
)
x = np.zeros(shape)
out = x[index] # raises if the index is invalid
assert all(min_side <= s <= max_side for s in out.shape)
assert min_dims <= out.ndim <= max_dims
assert not np.shares_memory(x, out) # advanced indexing should not return a view
assert all(dtype == x.dtype for x in index)
@settings(deadline=None)
@given(
shape=nps.array_shapes(min_dims=1, min_side=1),