Scientific Hypothesis (for NumPy)¶
The centerpiece is the
arrays strategy, which generates arrays with
any dtype, shape, and contents you can specify or give a strategy for.
To make this as useful as possible, strategies are provided to generate array
shapes and generate all kinds of fixed-size or compound dtypes.
arrays(dtype, shape, elements=None)¶
dtype may be any valid input to
np.dtypeobjects), or a strategy that generates such values. shape may be an integer >= 0, a tuple of length >= of such integers, or a strategy that generates such values.
Arrays of specified dtype and shape are generated for example like this:
>>> import numpy as np >>> arrays(np.int8, (2, 3)).example() array([[-8, 6, 3], [-6, 4, 6]], dtype=int8)
If elements is None, Hypothesis infers a strategy based on the dtype, which may give any legal value (including eg
NaNfor floats). If you have more specific requirements, you can supply your own elements strategy - see What you can generate and how.
>>> import numpy as np >>> from hypothesis.strategies import floats >>> arrays(np.float, 3, elements=floats(0, 1)).example() array([ 0.88974794, 0.77387938, 0.1977879 ])
Hypothesis works really well with NumPy, but is designed for small data. The default entropy is 8192 bytes - it is impossible to draw an example where
example_array.nbytesis greater than
settings.default.buffer_size. See the settings documentation if you need to increase this value, but be aware that Hypothesis may take much longer to produce a minimal failure case.
array_shapes(min_dims=1, max_dims=3, min_side=1, max_side=10)¶
Return a strategy for array shapes (tuples of int >= 1).
Return a strategy that can return any non-flexible scalar dtype.
Return a strategy for unsigned integer dtypes.
endianness may be
=for native byte order, or
?to allow either byte order. This argument only applies to dtypes of more than one byte.
sizes must be a collection of integer sizes in bits. The default (8, 16, 32, 64) covers the full range of sizes.
Return a strategy for signed integer dtypes.
endianness and sizes are treated as for unsigned_integer_dtypes.
Return a strategy for floating-point dtypes.
sizes is the size in bits of floating-point number. Some machines support 96- or 128-bit floats, but these are not generated by default.
Larger floats (96 and 128 bit real parts) are not supported on all platforms and therefore disabled by default. To generate these dtypes, include these values in the sizes argument.
Return a strategy complex-number dtypes.
sizes is the total size in bits of a complex number, which consists of two floats. Complex halfs (a 16-bit real part) are not supported by numpy and will not be generated by this strategy.
Return a strategy for datetime64 dtypes, with various precisions from year to attosecond.
Return a strategy for timedelta64 dtypes, with various precisions from year to attosecond.
Return a strategy for generating bytestring dtypes, of various lengths and byteorder.
Return a strategy for generating unicode string dtypes, of various lengths and byteorder.
Return a strategy for generating array (compound) dtypes, with members drawn from the given subtype strategy.
nested_dtypes(subtype_strategy=scalar_dtypes(), max_leaves=10, max_itemsize=None)¶
Return the most-general dtype strategy.
Elements drawn from this strategy may be simple (from the subtype_strategy), or several such values drawn from array_dtypes with
allow_subarrays=True. Subdtypes in an array dtype may be nested to any depth, subject to the max_leaves argument.