def anyarray( draw, min_dims: int = 0, max_dims: int = 2, include_complex_numbers: bool = True, dtype: Optional[np.dtype] = None, ): if dtype is None: if include_complex_numbers: dtype = one_of( integer_dtypes(), floating_dtypes(), complex_number_dtypes() ) else: dtype = one_of(integer_dtypes(), floating_dtypes()) arr = draw( arrays( dtype=dtype, shape=array_shapes(min_dims=min_dims, max_dims=max_dims), ) ) assume(not np.any(np.isnan(arr))) assume(np.all(np.isfinite(arr))) return arr
def test_generate_arbitrary_indices(data): min_size = data.draw(st.integers(0, 10), "min_size") max_size = data.draw(st.none() | st.integers(min_size, min_size + 10), "max_size") unique = data.draw(st.booleans(), "unique") dtype = data.draw( st.one_of( npst.boolean_dtypes(), npst.integer_dtypes(endianness="="), npst.floating_dtypes(endianness="="), npst.complex_number_dtypes(endianness="="), npst.datetime64_dtypes(endianness="="), npst.timedelta64_dtypes(endianness="="), ).filter(supported_by_pandas), "dtype", ) pass_elements = data.draw(st.booleans(), "pass_elements") converted_dtype = pandas.Index([], dtype=dtype).dtype try: inferred_dtype = pandas.Index([data.draw(npst.from_dtype(dtype)) ]).dtype if pass_elements: elements = npst.from_dtype(dtype) dtype = None else: elements = None index = data.draw( pdst.indexes( elements=elements, dtype=dtype, min_size=min_size, max_size=max_size, unique=unique, )) except Exception as e: if type(e).__name__ == "OutOfBoundsDatetime": # See https://github.com/HypothesisWorks/hypothesis-python/pull/826 reject() else: raise if dtype is None: assert index.dtype == inferred_dtype else: assert index.dtype == converted_dtype if unique: assert len(set(index.values)) == len(index)
from hypothesis import given import hypothesis.strategies as st import hypothesis.extra.numpy as hnp import numpy as np import pytest from mygrad import Tensor from mygrad.nnet.initializers import normal @given(dtype=hnp.unsigned_integer_dtypes() | hnp.integer_dtypes() | hnp.complex_number_dtypes()) def test_normal_dtype_validation(dtype): with pytest.raises(ValueError): normal(1, dtype=dtype) @given(std=st.floats(-1000, 0, exclude_max=True)) def test_normal_std_validation(std): with pytest.raises(ValueError): normal(1, std=std) _array_shapes = ((1000000, ), (1000, 100, 10), (10, 10, 10, 10, 10, 10)) @given( shape=st.sampled_from(_array_shapes), mean=st.floats(-100, 100), std=st.floats(0, 5), )
([None, 1.0, 2.0], float, [np.nan, 1.0, 2.0]), ([None, 1.0, 2j], complex, [np.nan * 1j, 1.0, 2j]), ], ) def test_infer_dtype_from_mixed(values, dst_type, expected): values = np.asarray(values, dtype=object) array = _infer_data_type(values) assert array.dtype == dst_type assert_array_equal(array, expected) @given( values=hynp.arrays( dtype=hynp.floating_dtypes() | hynp.integer_dtypes() | hynp.complex_number_dtypes(), shape=hynp.array_shapes(), elements=integers(0, 2 ** 7 - 1), ), dst_type=hynp.floating_dtypes() | hynp.integer_dtypes() | hynp.complex_number_dtypes(), ) @pytest.mark.parametrize("as_iterable", [list, tuple, np.asarray]) def test_dtype_keyword(as_iterable, values, dst_type): array = _infer_data_type(as_iterable(values), dtype=dst_type) assert array.dtype == dst_type assert_array_equal(array, values) @given(
r" It is \(1\+1j\) of type " r"<class 'complex'>"): Arrays(min_value=1 + 1j) with pytest.raises(TypeError, match=r"max_value must be a real number. " r"It is \(1\+1j\) of type " r"<class 'complex'>"): Arrays(max_value=1 + 1j) with pytest.raises(TypeError, match=r'Setting min_value or max_value is ' r'not supported for complex ' r'validators'): Arrays(max_value=1, valid_types=(np.complexfloating, )) @given(dtype=complex_number_dtypes()) def test_complex(dtype): a = Arrays(valid_types=(np.complexfloating, )) a.validate(np.arange(10, dtype=dtype)) def test_complex_subtypes(): """Test that specifying a specific complex subtype works as expected""" a = Arrays(valid_types=(np.complex64, )) a.validate(np.arange(10, dtype=np.complex64)) with pytest.raises(TypeError, match=r"is not any of " r"\(<class 'numpy.complex64'>,\)" r" it is complex128"): a.validate(np.arange(10, dtype=np.complex128))
# in a future version, np.diagonal will return a writable view diag = np.diagonal(lower).copy() diag[np.abs(diag) < 1e-3] = 1 lower[np.diag_indices_from(lower)] = diag return lower @ lower.T # Pre-defined strategies some_shape = hn.array_shapes(max_dims=4, max_side=20) simple_shape = hn.array_shapes(max_dims=2, max_side=10) some_dtype = st.one_of( # hn.boolean_dtypes(), hn.integer_dtypes(), hn.unsigned_integer_dtypes(), hn.floating_dtypes(), hn.complex_number_dtypes(), # hn.datetime64_dtypes(), # hn.timedelta64_dtypes(), ) real_dtype = st.one_of( # hn.boolean_dtypes(), hn.integer_dtypes(), hn.unsigned_integer_dtypes(), hn.floating_dtypes(), # hn.complex_number_dtypes(), # hn.datetime64_dtypes(), # hn.timedelta64_dtypes(), ) some_array = hn.arrays(dtype=some_dtype, shape=some_shape) # ----------------------------------------------------------------------------- # Creation routines
)) else: rtol = 0 atol = 0 np_tst.assert_allclose( toeplitz_op.dot(test), mat_result, rtol=rtol, atol=atol ) @pytest.mark.parametrize("toep_cls", OPERATOR_LIST) @given( arrays( shared( complex_number_dtypes(sizes=COMPLEX_SIZES, endianness="="), key="dtype" ), shared(integers(min_value=1, max_value=MAX_ARRAY), key="nrows"), elements=builds( complex, floats(allow_infinity=False, allow_nan=False, width=32), floats(allow_infinity=False, allow_nan=False, width=32), ), ).filter(lambda x: np.all(np.isfinite(x))), arrays( shared( complex_number_dtypes(sizes=COMPLEX_SIZES, endianness="="), key="dtype" ), shared(integers(min_value=1, max_value=MAX_ARRAY), key="ncols"),