xarray.testing.strategies.variables

Contents

xarray.testing.strategies.variables#

xarray.testing.strategies.variables(*, array_strategy_fn=None, dims=None, dtype=one_of(one_of(one_of(integer_dtypes(endianness='='), unsigned_integer_dtypes(endianness='=')), floating_dtypes(endianness='=')), complex_number_dtypes(endianness='=')), attrs=recursive(dictionaries(keys=text(alphabet=characters(max_codepoint=383, categories=['L', 'N']), max_size=5), values=one_of(one_of(one_of(none(), booleans()), text(alphabet=characters(max_codepoint=383, categories=['L', 'N']), max_size=5)), arrays(dtype=scalar_dtypes(), shape=array_shapes(max_dims=2, max_side=2)))), lambda children: ..., max_leaves=3))#

Generates arbitrary xarray.Variable objects.

Follows the basic signature of the xarray.Variable constructor, but allows passing alternative strategies to generate either numpy-like array data or dimensions. Also allows specifying the shape or dtype of the wrapped array up front.

Passing nothing will generate a completely arbitrary Variable (containing a numpy array).

Requires the hypothesis package to be installed.

Parameters:
  • array_strategy_fn (Callable which returns a strategy generating array-likes, optional) – Callable must only accept shape and dtype kwargs, and must generate results consistent with its input. If not passed the default is to generate a small numpy array with one of the supported_dtypes.

  • dims (Strategy for generating the dimensions, optional) – Can either be a strategy for generating a sequence of string dimension names, or a strategy for generating a mapping of string dimension names to integer lengths along each dimension. If provided as a mapping the array shape will be passed to array_strategy_fn. Default is to generate arbitrary dimension names for each axis in data.

  • dtype (Strategy which generates np.dtype objects, optional) – Will be passed in to array_strategy_fn. Default is to generate any scalar dtype using supported_dtypes. Be aware that this default set of dtypes includes some not strictly allowed by the array API standard.

  • attrs (Strategy which generates dicts, optional) – Default is to generate a nested attributes dictionary containing arbitrary strings, booleans, integers, Nones, and numpy arrays.

Returns:

variable_strategy – Strategy for generating xarray.Variable objects.

Raises:

ValueError – If a custom array_strategy_fn returns a strategy which generates an example array inconsistent with the shape & dtype input passed to it.

Examples

Generate completely arbitrary Variable objects backed by a numpy array:

>>> variables().example()  
<xarray.Variable (żō: 3)>
array([43506,   -16,  -151], dtype=int32)
>>> variables().example()  
<xarray.Variable (eD: 4, ğŻżÂĕ: 2, T: 2)>
array([[[-10000000., -10000000.],
        [-10000000., -10000000.]],
       [[-10000000., -10000000.],
        [        0., -10000000.]],
       [[        0., -10000000.],
        [-10000000.,        inf]],
       [[       -0., -10000000.],
        [-10000000.,        -0.]]], dtype=float32)
Attributes:
    śřĴ:      {'ĉ': {'iĥf': array([-30117,  -1740], dtype=int16)}}

Generate only Variable objects with certain dimension names:

>>> variables(dims=st.just(["a", "b"])).example()  
<xarray.Variable (a: 5, b: 3)>
array([[       248, 4294967295, 4294967295],
       [2412855555, 3514117556, 4294967295],
       [       111, 4294967295, 4294967295],
       [4294967295, 1084434988,      51688],
       [     47714,        252,      11207]], dtype=uint32)

Generate only Variable objects with certain dimension names and lengths:

>>> variables(dims=st.just({"a": 2, "b": 1})).example()  
<xarray.Variable (a: 2, b: 1)>
array([[-1.00000000e+007+3.40282347e+038j],
       [-2.75034266e-225+2.22507386e-311j]])