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(npst.scalar_dtypes(), "dtype") assume(supported_by_pandas(dtype)) # Pandas bug: https://github.com/pandas-dev/pandas/pull/14916 until 0.20; # then int64 indexes are inferred from uint64 values. assume(dtype.kind != "u") 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: if pandas.__version__ >= "0.19": assert index.dtype == inferred_dtype else: assert index.dtype == converted_dtype if unique: assert len(set(index.values)) == len(index)
def column_strategy(draw): name = draw(st.none() | st.text()) dtype = draw(npst.scalar_dtypes().filter(supported_by_pandas)) pass_dtype = not draw(st.booleans()) if pass_dtype: pass_elements = not draw(st.booleans()) else: pass_elements = True if pass_elements: elements = npst.from_dtype(dtype) else: elements = None unique = draw(st.booleans()) fill = st.nothing() if draw(st.booleans()) else None return pdst.column( name=name, dtype=dtype, unique=unique, fill=fill, elements=elements)
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(npst.scalar_dtypes(), 'dtype') assume(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)
def test_minimise_scalar_dtypes(): assert minimal(nps.scalar_dtypes()) == np.dtype(u'bool')
def test_minimise_scalar_dtypes(): assert minimal(nps.scalar_dtypes()) == np.dtype("bool")
import numpy as np from hypothesis.extra.numpy import ( arrays, array_shapes, scalar_dtypes, nested_dtypes, from_dtype, integer_dtypes, unsigned_integer_dtypes, ) _global_type_lookup.update({ np.dtype: nested_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. # See also the discussion at https://bugs.python.org/issue39046 typing.Reversible:
@given(array_shapes()) def test_can_generate_array_shapes(shape): assert isinstance(shape, tuple) assert all(isinstance(i, int) for i in shape) @given(st.integers(1, 10), st.integers(0, 9), st.integers(1), st.integers(0)) def test_minimise_array_shapes(min_dims, dim_range, min_side, side_range): smallest = minimal(array_shapes(min_dims, min_dims + dim_range, min_side, min_side + side_range)) assert len(smallest) == min_dims and all(k == min_side for k in smallest) @given(scalar_dtypes()) def test_can_generate_scalar_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nested_dtypes()) def test_can_generate_compound_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nested_dtypes(max_itemsize=settings.default.buffer_size // 10), st.data()) def test_infer_strategy_from_dtype(dtype, data): # Given a dtype assert isinstance(dtype, np.dtype) # We can infer a strategy
@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) @given(st.integers(1, 10), st.integers(0, 9), st.integers(1), 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 + dim_range, min_side, min_side + side_range)) assert len(smallest) == min_dims and all(k == min_side for k in smallest) @given(nps.scalar_dtypes()) def test_can_generate_scalar_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nps.nested_dtypes()) def test_can_generate_compound_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nps.nested_dtypes(max_itemsize=settings.default.buffer_size // 10), st.data()) def test_infer_strategy_from_dtype(dtype, data): # Given a dtype assert isinstance(dtype, np.dtype) # We can infer a strategy
smallest = minimal( nps.array_shapes( min_dims, min_dims + dim_range, min_side, min_side + side_range ) ) assert len(smallest) == min_dims and all(k == min_side for k in smallest) @pytest.mark.parametrize( "kwargs", [dict(min_side=100), dict(min_dims=15), dict(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) @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)
@given(lists(_st_shape, min_size=0, max_size=5), real_scalar_dtypes(), data()) def test_elements_tuple_of_arrays(shapes, dtype, data): choices = data.draw(real_from_dtype(dtype)) elements = sampled_from(choices) S = gu._tuple_of_arrays(shapes, dtype, elements=elements) X = data.draw(S) validate_elements(X, choices=choices, dtype=dtype) @given( gu.gufunc_args( "(1),(1),(1),()->()", dtype=["object", "object", "object", "bool"], elements=[_st_shape, scalar_dtypes(), just(None), booleans()], min_side=1, max_dims_extra=1, ), data(), ) def test_bcast_tuple_of_arrays(args, data): """Now testing broadcasting of tuple_of_arrays, kind of crazy since it uses gufuncs to test itself. Some awkwardness here since there are a lot of corner cases when dealing with object types in the numpy extension. For completeness, should probably right a function like this for the other functions, but there always just pass dtype, elements, unique to `_tuple_of_arrays` anyway, so this should be pretty good. """ shapes, dtype, elements, unique = args
# # 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 http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import from hypothesis import given from hypothesis.extra import numpy as npst from tests.common.utils import checks_deprecated_behaviour from hypothesis.strategies import data, sampled_from @given(data(), npst.arrays( dtype=npst.scalar_dtypes(), shape=npst.array_shapes(max_dims=1) )) def test_can_sample_1D_numpy_array_without_warning(data, arr): data.draw(sampled_from(arr)) @checks_deprecated_behaviour @given(data(), npst.arrays( dtype=npst.scalar_dtypes(), shape=npst.array_shapes(min_dims=2, max_dims=5) )) def test_sampling_multi_dimensional_arrays_is_deprecated(data, arr): data.draw(sampled_from(arr))
@given(nps.array_shapes()) def test_can_generate_array_shapes(shape): assert isinstance(shape, tuple) assert all(isinstance(i, int) for i in shape) @given(st.integers(1, 10), st.integers(0, 9), st.integers(1), 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 + dim_range, min_side, min_side + side_range)) assert len(smallest) == min_dims and all(k == min_side for k in smallest) @given(nps.scalar_dtypes()) def test_can_generate_scalar_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nps.nested_dtypes()) def test_can_generate_compound_dtypes(dtype): assert isinstance(dtype, np.dtype) @given(nps.nested_dtypes(max_itemsize=settings.default.buffer_size // 10), st.data()) def test_infer_strategy_from_dtype(dtype, data): # Given a dtype assert isinstance(dtype, np.dtype) # We can infer a strategy
# obtain one at http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import import numpy as np from hypothesis import given, assume from hypothesis.extra import numpy as npst from tests.common.utils import checks_deprecated_behaviour from hypothesis.strategies import data, sampled_from @given(data(), npst.arrays( dtype=npst.scalar_dtypes(), shape=npst.array_shapes(max_dims=1) )) def test_can_sample_1D_numpy_array_without_warning(data, arr): elem = data.draw(sampled_from(arr)) try: assume(not np.isnan(elem)) except TypeError: pass assert elem in arr @checks_deprecated_behaviour @given(data(), npst.arrays( dtype=npst.scalar_dtypes(), shape=npst.array_shapes(min_dims=2, max_dims=5)
# 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 from __future__ import absolute_import, division, print_function from hypothesis import given from hypothesis.errors import InvalidArgument from hypothesis.extra import numpy as npst from hypothesis.strategies import data, sampled_from from tests.common.utils import fails_with @given( data(), npst.arrays(dtype=npst.scalar_dtypes(), shape=npst.array_shapes(max_dims=1)) ) def test_can_sample_1D_numpy_array_without_warning(data, arr): data.draw(sampled_from(arr)) @fails_with(InvalidArgument) @given( data(), npst.arrays( dtype=npst.scalar_dtypes(), shape=npst.array_shapes(min_dims=2, max_dims=5) ), ) def test_sampling_multi_dimensional_arrays_is_deprecated(data, arr): data.draw(sampled_from(arr))
import pytest from hypothesis import given import hypothesis.strategies as st import hypothesis.extra.numpy as st_np import numpy as np import pyarrow as pa from ..context import serialization_context # Fails, most concerningly, on: # array([1], dtype=uint16) # array([256], dtype=uint16) @pytest.mark.xfail @given( st_np.arrays(dtype=st_np.scalar_dtypes(), shape=st.one_of(st.just(0), st_np.array_shapes()))) def test_plain_roundtrip(arr): cereal = pa.serialize(arr, context=serialization_context) buf = cereal.to_buffer() decereal = pa.deserialize(buf, context=serialization_context) assert arr.dtype == decereal.dtype.newbyteorder( arr.dtype.byteorder) # arrow may change endianness np.testing.assert_array_equal(arr, decereal)
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
def test_can_create_a_series_of_any_dtype(data): dtype = np.dtype(data.draw(npst.scalar_dtypes())) assume(supported_by_pandas(dtype)) series = data.draw(pdst.series(dtype=dtype)) assert series.dtype == pandas.Series([], dtype=dtype).dtype
def test_can_draw_shapeless_from_scalars(data): dt = data.draw(nps.scalar_dtypes()) result = data.draw(nps.arrays(dtype=dt, shape=())) assert isinstance(result, dt.type)
df = pd.DataFrame({ 'key': np.arange(0, 5000), 'count': rng.integers(0, 1000, 5000), 'score': rng.normal(10, 2, 5000) }) dump(df, file) df2 = load(file) assert all(df2.columns == df.columns) for c in df2.columns: assert all(df2[c] == df[c]) @given(arrays(scalar_dtypes(), st.integers(500, 10000))) def test_compress_many_arrays(a): "Pickle random NumPy arrays" assume(not any(np.isnan(a))) with TemporaryDirectory('.test', 'binpickle') as path: file = Path(path) / 'data.bpk' with BinPickler.compressed(file) as w: w.dump(a) with BinPickleFile(file) as bpf: assert not bpf.find_errors() assert len(bpf.entries) in (1, 2) a2 = bpf.load() assert len(a2) == len(a)
def test_minimise_scalar_dtypes(): assert minimal(scalar_dtypes()) == np.dtype(u'bool')
_global_type_lookup[type] = st.sampled_from( [type(None)] + sorted(_global_type_lookup, key=str)) 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({ typing.ByteString: st.binary() | st.binary().map(bytearray), # Reversible is somehow a subclass of Hashable, so we tuplize it. # See also the discussion at https://bugs.python.org/issue39046 typing.Reversible: st.lists(st.integers()).map(tuple), # TODO: SupportsAbs and SupportsRound should be covariant, ie have functions. typing.SupportsAbs: st.one_of( st.booleans(),
)) 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(
def posvel_arrays(draw): s = draw(array_shapes()) dtype = draw(scalar_dtypes()) pos = draw(arrays(dtype=dtype, shape=(3, ) + s)) vel = draw(arrays(dtype=dtype, shape=(3, ) + s)) return pos, vel
def posvel_arrays_broadcastable(draw): s, s_pos, s_vel = draw(broadcastable_subshapes(array_shapes())) dtype = draw(scalar_dtypes()) pos = draw(arrays(dtype=dtype, shape=(3, ) + tuple(s_pos))) vel = draw(arrays(dtype=dtype, shape=(3, ) + tuple(s_vel))) return pos, vel, (3, ) + s
# 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 http://mozilla.org/MPL/2.0/. # # END HEADER from __future__ import division, print_function, absolute_import from hypothesis import given from hypothesis.extra import numpy as npst from tests.common.utils import checks_deprecated_behaviour from hypothesis.strategies import data, sampled_from @given(data(), npst.arrays(dtype=npst.scalar_dtypes(), shape=npst.array_shapes(max_dims=1))) def test_can_sample_1D_numpy_array_without_warning(data, arr): data.draw(sampled_from(arr)) @checks_deprecated_behaviour @given(data(), npst.arrays(dtype=npst.scalar_dtypes(), shape=npst.array_shapes(min_dims=2, max_dims=5))) def test_sampling_multi_dimensional_arrays_is_deprecated(data, arr): data.draw(sampled_from(arr))
int: st.integers().filter(lambda x: isinstance(x, int)), long: st.integers().map(long) # noqa }) try: from hypothesis.extra.pytz import timezones _global_type_lookup[datetime.tzinfo] = timezones() except ImportError: # pragma: no cover pass try: # pragma: no cover import numpy as np from hypothesis.extra.numpy import \ arrays, array_shapes, scalar_dtypes, nested_dtypes _global_type_lookup.update({ np.dtype: nested_dtypes(), np.ndarray: arrays(scalar_dtypes(), array_shapes(max_dims=2)), }) except ImportError: # pragma: no cover pass try: import typing except ImportError: # pragma: no cover pass else: _global_type_lookup.update({ typing.ByteString: st.binary(), typing.io.BinaryIO: st.builds(io.BytesIO, st.binary()), # type: ignore typing.io.TextIO: st.builds(io.StringIO, st.text()), # type: ignore typing.Reversible: st.lists(st.integers()), typing.SupportsAbs: st.complex_numbers(),
from hypothesis.extra.numpy import ( arrays, scalar_dtypes, unsigned_integer_dtypes, datetime64_dtypes, floating_dtypes, integer_dtypes, ) from hypothesis import HealthCheck from superintendent.queueing import _features_to_array guaranteed_dtypes = one_of( scalar_dtypes(), unsigned_integer_dtypes(), datetime64_dtypes(), floating_dtypes(), integer_dtypes(), ) @composite def dataframe(draw): n_cols = draw(integers(min_value=1, max_value=20)) dtypes = draw( lists( sampled_from([float, int, str]), min_size=n_cols, max_size=n_cols ) )
from hypothesis.extra.pandas import column, data_frames from hypothesis.strategies import ( booleans, composite, floats, integers, lists, one_of, text, tuples, ) from superintendent.distributed.queueing import DatabaseQueue guaranteed_dtypes = one_of( np_strategies.scalar_dtypes(), np_strategies.unsigned_integer_dtypes(), np_strategies.datetime64_dtypes(), np_strategies.floating_dtypes(), np_strategies.integer_dtypes(), ) @composite def dataframe(draw): n_cols = draw(integers(min_value=1, max_value=20)) dtypes = draw( lists( one_of( np_strategies.floating_dtypes(), np_strategies.integer_dtypes(),