def struct_array(draw: st.DataObject, subdtype: st.SearchStrategy[np.dtype] = some_dtype, shape: st.SearchStrategy[Tuple[int, ...]] = some_shape, **kwargs) -> st.SearchStrategy[np.ndarray]: """Strategy for structured arrays (1-level only)""" dtype = draw(hn.array_dtypes(subdtype, **kwargs)) if dtype.names is not None: hy.assume(all(valid_field(x) for x in dtype.names)) return draw(hn.arrays(dtype, shape))
def test_array_dtypes_may_have_field_titles(): find_any(nps.array_dtypes(), lambda dt: len(dt.fields) > len(dt.names))
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 == "="
if sys.version_info[:2] >= (3, 7): # pragma: no branch _global_type_lookup[re.Match] = (st.text().map( lambda c: re.match(".", c, flags=re.DOTALL)).filter(bool)) _global_type_lookup[re.Pattern] = st.builds(re.compile, st.sampled_from(["", b""])) if sys.version_info[:2] >= (3, 9): # pragma: no cover # subclass of MutableMapping, and in Python 3.9 we resolve to a union # which includes this... but we don't actually ever want to build one. _global_type_lookup[os._Environ] = st.just(os.environ) try: # pragma: no cover import numpy as np from hypothesis.extra.numpy import array_dtypes, array_shapes, arrays, scalar_dtypes _global_type_lookup[np.dtype] = array_dtypes() _global_type_lookup[np.ndarray] = arrays(scalar_dtypes(), array_shapes(max_dims=2)) except ImportError: pass _global_type_lookup.update({ # Note: while ByteString notionally also represents the bytearray and # memoryview types, it is a subclass of Hashable and those types are not. # We therefore only generate the bytes type. typing.ByteString: st.binary(), collections.abc.ByteString: st.binary(), # TODO: SupportsAbs and SupportsRound should be covariant, ie have functions. typing.SupportsAbs:
@given(similar_vectors_and_matching_covariances()) @settings( deadline=None, verbosity=Verbosity.verbose, suppress_health_check=[ *settings.default.suppress_health_check, HealthCheck.function_scoped_fixture, ], ) @pytest.mark.slow def test_display_vectors_and_covariances_comparison(monkeypatch, valid_inputs): monkeypatch.setattr(plt, "show", lambda: None, raising=True) plot_vectors_and_covariances_comparison(**valid_inputs) @given(hnp.arrays(dtype=hnp.array_dtypes(), shape=hnp.array_shapes())) def test_is_2d_square_matrix(array): assert is_2d_square_matrix(array) == (len(array.shape) == 2 and array.shape[0] == array.shape[1]) @given(hnp.arrays(dtype=hnp.array_dtypes(), shape=hnp.array_shapes())) def test_is_2d_matrix(array): assert is_2d_matrix(array) == (len(array.shape) == 2) @given(hnp.arrays(dtype=hnp.array_dtypes(), shape=hnp.array_shapes())) def test_is_vector(array): assert is_vector(array) == (len(array.shape) == 1)
| hynp.complex_number_dtypes(), ) def test_object_arrays(values, dst_type): assume( not np.issubdtype(values.dtype, np.complexfloating) or np.issubdtype(dst_type, np.complexfloating) ) values = np.asarray(values, dtype=object) array = _infer_data_type(values, dtype=dst_type) assert array.dtype == dst_type assert_array_equal(array, values) @given( values=hynp.arrays( dtype=hynp.complex_number_dtypes(), shape=hynp.array_shapes(), elements=integers(0, 8), ), dst_type=hynp.floating_dtypes() | hynp.integer_dtypes(), ) def test_from_complex(values, dst_type): array = _infer_data_type(values, dtype=dst_type) assert_array_equal(array.real, values.real) @given(values=hynp.arrays(dtype=hynp.array_dtypes(), shape=hynp.array_shapes())) def test_array_not_copied(values): array = _infer_data_type(values, dtype=values.dtype) assert array is values
def test_minimise_scalar_dtypes(): assert minimal(nps.scalar_dtypes()) == np.dtype(u'bool') def test_minimise_nested_types(): assert minimal(nps.nested_dtypes()) == np.dtype(u'bool') def test_minimise_array_strategy(): smallest = minimal(nps.arrays( nps.nested_dtypes(max_itemsize=settings.default.buffer_size // 3**3), nps.array_shapes(max_dims=3, max_side=3))) assert smallest.dtype == np.dtype(u'bool') and not smallest.any() @given(nps.array_dtypes(allow_subarrays=False)) def test_can_turn_off_subarrays(dt): for field, _ in dt.fields.values(): assert field.shape == () @pytest.mark.parametrize('byteorder', ['<', '>']) @given(data=st.data()) def test_can_restrict_endianness(data, byteorder): dtype = data.draw(nps.integer_dtypes(byteorder, sizes=(16, 32, 64))) if byteorder == ('<' if sys.byteorder == 'little' else '>'): assert dtype.byteorder == '=' else: assert dtype.byteorder == byteorder
try: # pragma: no cover import numpy as np from hypothesis.extra.numpy import ( arrays, array_shapes, scalar_dtypes, array_dtypes, from_dtype, integer_dtypes, unsigned_integer_dtypes, ) _global_type_lookup.update( { np.dtype: array_dtypes(), np.ndarray: arrays(scalar_dtypes(), array_shapes(max_dims=2)), } ) except ImportError: # pragma: no cover np = None try: import typing except ImportError: # pragma: no cover pass else: _global_type_lookup.update( { typing.ByteString: st.binary() | st.binary().map(bytearray), # Reversible is somehow a subclass of Hashable, so we tuplize it.