Package: hypothesis-gufunc
This project is experimental and the APIs are not considered stable.OnlyPython>=3.6is officially supported, but older versions of Python likely work as well.This package includes support for strategies which generate arguments to
functions that follow the numpy general universal function API. So, it can
automatically generate the matrices with shapes that follow the shape
constraints. For example, to generate test inputs fornp.dot, one can use,importnumpyasnpfromhypothesisimportgivenfromhypothesis.strategiesimportfloatsfromhypothesis_gufunc.gufuncimportgufunc_argseasy_floats=floats(min_value=-10,max_value=10)@given(gufunc_args('(m,n),(n,p)->(m,p)',dtype=np.float_,elements=easy_floats))deftest_np_dot(args):x,y=argsassertnp.allclose(np.dot(x,y),np.dot(y.T,x.T).T)test_np_dot()# Run the testWe also allow for adding extra dimensions that follow the numpy broadcasting
conventions. This allows one to test that the broadcasting follows the
vectorization conventions:@given(gufunc_args('(m,n),(n,p)->(m,p)',dtype=np.float_,elements=easy_floats,max_dims_extra=3))deftest_np_matmul(args):x,y=argsf_vec=np.vectorize(np.matmul,signature='(m,n),(n,p)->(m,p)',otypes=[np.float_])assertnp.allclose(np.matmul(x,y),f_vec(x,y))test_np_matmul()# Run the testProvidingmax_dims_extra=3gives up to 3 broadcast compatible dimensions on each of the arguments.Quick Start/InstallationSimply install with pip:pip install hypothesis-gufuncIf one would like the same pinned requirements we use during testing, then one can install from the repo with:git clone git@github.com:uber/hypothesis-gufunc.git
cd hypothesis-gufunc
pip install -r requirements/base.txt
pip install -e .Running the TestsThe tests for this package can be run by first doing acdto its root directory and then./test.shThe script creates a conda environment using the requirements found inrequirements/test.txt.Hypothesis for XarrayThis package also contains an extension to hypothesis to generate xarray data structures.To install the package with the xarray dependencies install it with pip aspip install hypothesis-gufunc[xarray]Once installed, one can generate a data array as follows:fromhypothesis.strategiesimportintegers,listsfromhypothesis_gufunc.extra.xrimportfixed_dataarraysS=fixed_dataarrays(("a","b"),coords_st={"a":lists(integers(0,3))})S.example()Here,coords_stallows one to specify a custom strategy for the coordinates on a per-dimension basis. Likewise, if
one has known coordinates one can callfixed_coords_dataarrays; ordataarraysif one wants both the dimensions and coordinates determined by the
strategy.The package also has the ability to generate a dataset:fromhypothesis_gufunc.extra.xrimportfixed_datasetsS=fixed_datasets({5:("a","b"),"bar":("b"),"baz":()},coords_st={"a":lists(integers(0,3))})S.example()One can usefixed_coords_datasetswhen the coordinates are determined; or simplydatasetsto have both the dimensions and coordinates generated.LinksThesourceis hosted on GitHub.Thedocumentationis hosted at Read the Docs.The mainhypothesis project.A description of the numpyGeneralized Universal Function API.Likewise, the numpy broadcasting rules are describedhere.Thexarrayproject describes data arrays and datasets.LicenseThis project is licensed under the Apache 2 License - see the LICENSE file for details.