Example #1
0
# END HEADER

import sys
from functools import reduce
from itertools import zip_longest

import numpy as np
import pytest

from hypothesis import HealthCheck, assume, given, note, settings, strategies as st
from hypothesis.errors import InvalidArgument, Unsatisfiable
from hypothesis.extra import numpy as nps
from tests.common.debug import find_any, minimal
from tests.common.utils import fails_with, flaky

ANY_SHAPE = nps.array_shapes(min_dims=0, max_dims=32, min_side=0, max_side=32)
ANY_NONZERO_SHAPE = nps.array_shapes(min_dims=0,
                                     max_dims=32,
                                     min_side=1,
                                     max_side=32)


@given(nps.arrays(float, ()))
def test_empty_dimensions_are_arrays(x):
    assert isinstance(x, np.ndarray)
    assert x.dtype.kind == "f"


@given(nps.arrays(float, (1, 0, 1)))
def test_can_handle_zero_dimensions(x):
    assert x.shape == (1, 0, 1)
Example #2
0
def test_interesting_array_shapes_argument(kwargs):
    nps.array_shapes(**kwargs).example()
Example #3
0
    test_set = load_by_id(datasaver.run_id)
    expec_data = np.array([item for item in list_of_strings])
    actual_data = test_set.get_parameter_data()["p"]["p"]

    try:
        np.testing.assert_array_equal(actual_data, expec_data)
    finally:
        test_set.conn.close()


@settings(suppress_health_check=(HealthCheck.function_scoped_fixture, ),
          deadline=None)
@given(p_values=hypnumpy.arrays(dtype=hst.sampled_from(
    (hypnumpy.unicode_string_dtypes(), hypnumpy.byte_string_dtypes(),
     hypnumpy.timedelta64_dtypes(), hypnumpy.datetime64_dtypes())),
                                shape=hypnumpy.array_shapes()))
def test_string_and_date_data_in_array(experiment, p_values):
    p = qc.Parameter('p',
                     label='String parameter',
                     unit='',
                     get_cmd=None,
                     set_cmd=None,
                     initial_value=p_values)

    meas = Measurement(experiment)
    meas.register_parameter(p, paramtype='array')

    with meas.run() as datasaver:
        datasaver.add_result((p, p.get()))
    actual_data = datasaver.dataset.get_parameter_data()["p"]["p"]
    np.testing.assert_array_equal(actual_data,
Example #4
0
        lambda c: re.match(".", c, flags=re.DOTALL)).filter(bool))
    _global_type_lookup[re.Pattern] = st.builds(re.compile,
                                                st.sampled_from(["", b""]))
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({
    # Note: while ByteString notionally also represents the bytearray and
    # memoryview types, it is a subclass of Hashable and those types are not.
    # We therefore only generate the bytes type.
    typing.ByteString:
    st.binary(),
    collections.abc.ByteString:
    st.binary(),
    # TODO: SupportsAbs and SupportsRound should be covariant, ie have functions.
    typing.SupportsAbs:
    st.one_of(
        st.booleans(),
# 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))
    with the new kf cell interface.

    Parameters
    ----------
    module: nn.Module
        The torch module.

    Returns
    -------
    Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
        A callable function that odeint can consume.
    """
    return lambda t, z: module(t, z, torch.zeros(z.shape[:-1] + (0, )))


@given(x=tensor_strategy(shape=hnp.array_shapes(min_dims=1, max_dims=1)))
def test_quat_mul_simple(x: torch.Tensor):
    eye = torch.eye(x.shape[-1])
    assert_allclose_tensors(x.dot(x), quadratic_matmul(x, eye))


@given(x=tensor_strategy(shape=hnp.array_shapes(min_dims=1, max_dims=10)))
def test_quat_mul_simple_batch(x: torch.Tensor):
    eye = torch.eye(x.shape[-1]).expand(x.shape + (x.shape[-1], ))

    manual_dot = torch.sum(x * x, dim=-1)
    assert_allclose_tensors(manual_dot, quadratic_matmul(x, eye))


@given(B=st.integers(min_value=1, max_value=100),
       X=st.integers(min_value=1, max_value=100))
Example #7
0
import numpy as np
import eqtk

import hypothesis
import hypothesis.strategies as hs
import hypothesis.extra.numpy as hnp

# 1D arrays
array_shapes = hnp.array_shapes(min_dims=1,
                                max_dims=1,
                                min_side=2,
                                max_side=10)
arrays = hnp.arrays(np.float, array_shapes, elements=hs.floats(-100, 100))

# 2D matrices
array_shapes_2d = hnp.array_shapes(min_dims=2,
                                   max_dims=2,
                                   min_side=2,
                                   max_side=10)
arrays_2d = hnp.arrays(np.float,
                       array_shapes_2d,
                       elements=hs.floats(-100, 100))


@hypothesis.settings(
    deadline=None,
    max_examples=500,
    suppress_health_check=[hypothesis.HealthCheck.filter_too_much],
)
@hypothesis.given(arrays_2d)
def test_NK_formulation(N):
Example #8
0
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
Example #9
0
def test_minimise_array_strategy():
    smallest = minimal(nps.arrays(
        nps.nested_dtypes(max_itemsize=settings.default.buffer_size // 3**3),
        nps.array_shapes(max_dims=3, max_side=3)))
    assert smallest.dtype == np.dtype(u'bool') and not smallest.any()
Example #10
0
import hypothesis.extra.numpy as npst
import hypothesis.strategies as st
from hypothesis import given, settings

import xarray as xr

# Run for a while - arrays are a bigger search space than usual
settings.register_profile("ci", deadline=None)
settings.load_profile("ci")


an_array = npst.arrays(
    dtype=st.one_of(
        npst.unsigned_integer_dtypes(), npst.integer_dtypes(), npst.floating_dtypes()
    ),
    shape=npst.array_shapes(max_side=3),  # max_side specified for performance
)


@pytest.mark.slow
@given(st.data(), an_array)
def test_CFMask_coder_roundtrip(data, arr):
    names = data.draw(
        st.lists(st.text(), min_size=arr.ndim, max_size=arr.ndim, unique=True).map(
            tuple
        )
    )
    original = xr.Variable(names, arr)
    coder = xr.coding.variables.CFMaskCoder()
    roundtripped = coder.decode(coder.encode(original))
    xr.testing.assert_identical(original, roundtripped)
Example #11
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)
Example #12
0
    assert all(a in ((1, 0), (0, 1)) for a in arr)


@given(nps.arrays(object, (2, 2), st.tuples(st.integers())))
def test_does_not_flatten_arrays_of_tuples(arr):
    assert isinstance(arr[0][0], tuple)


@given(
    nps.arrays(object, (2, 2), st.lists(st.integers(), min_size=1, max_size=1))
)
def test_does_not_flatten_arrays_of_lists(arr):
    assert isinstance(arr[0][0], list)


@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):
#
# 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))
Example #14
0
def jd_arrays(draw, jd_values):
    s = draw(array_shapes())
    d = np.dtype([("jd1", float), ("jd2", float)])
    jdv = jd_values.map(lambda x: np.array(x, dtype=d))
    a = draw(arrays(d, s, elements=jdv))
    return a["jd1"], a["jd2"]
Example #15
0
    mygrad_func=var,
    true_func=_var,
    num_arrays=1,
    kwargs=dict(axis=partial(axis_arg, min_dim=1),
                keepdims=keepdims_arg,
                ddof=ddof_arg),
    vary_each_element=True,
    index_to_bnds={0: (-10, 10)},
)
def test_var_bkwd():
    pass


@given(x=tensors(
    dtype=np.float,
    shape=hnp.array_shapes(),
    elements=st.floats(allow_infinity=False, allow_nan=False),
))
def test_var_no_axis_fwd(x: mg.Tensor):
    o = mg.var(x, axis=())
    assert np.all(o == mg.zeros_like(x))


@given(x=tensors(
    dtype=np.float,
    shape=hnp.array_shapes(),
    elements=st.floats(allow_infinity=False, allow_nan=False),
    constant=False,
))
def test_var_no_axis_bkwrd(x: mg.Tensor):
    mg.var(x, axis=()).backward()
Example #16
0
def test_str_to_mjds_singleton_arrayobj():
    s = np.array(["41498.0"])[0]
    assert isinstance(s, str)
    jd1, jd2 = str_to_mjds(s)
    assert isinstance(jd1, float)
    assert isinstance(jd2, float)


def test_mjds_to_jds_singleton():
    jd1, jd2 = mjds_to_jds(40000, 0.0)
    assert isinstance(jd1, float)
    assert isinstance(jd2, float)


@given(arrays(np.object, array_shapes(), elements=mjd_strs()))
def test_str_to_mjds_array(s):
    i, f = str_to_mjds(s)
    assert np.shape(i) == np.shape(f) == np.shape(s)
    for i_i, f_i, s_i in np.nditer([i, f, s], flags=["refs_ok"]):
        assert i_i, f_i == str_to_mjds(s_i)


@given(
    one_of(
        array_pair(
            np.int,
            integers(min_value=40000, max_value=60000),
            np.float,
            floats(0, 1, allow_nan=False),
        ),
Example #17
0
def shapes(min_dims=0, max_dims=4, min_side=1, max_side=5):
    strategy = hnp.array_shapes(max(1, min_dims), max_dims, min_side, max_side)
    if min_dims < 1:
        strategy = hps.one_of(hps.just(()), strategy)
    return strategy
Example #18
0
def array_pair_broadcast(draw, dtype1, elements1, dtype2, elements2):
    s, s_a, s_b = draw(broadcastable_subshapes(array_shapes()))
    a = draw(arrays(dtype=dtype1, shape=s_a, elements=elements1))
    b = draw(arrays(dtype=dtype2, shape=s_b, elements=elements2))
    return s, a, b
Example #19
0
from .custom_strategies import valid_axes
from .utils.numerical_gradient import numerical_gradient_full
from .wrappers.uber import backprop_test_factory, fwdprop_test_factory


def test_input_validation():
    x = Tensor([[1, 2]])

    with raises(TypeError):
        transpose(x, (0, ), 1)


@settings(deadline=None)
@given(
    x=hnp.arrays(
        shape=hnp.array_shapes(max_side=4, max_dims=5),
        dtype=float,
        elements=st.floats(-10.0, 10.0),
    ),
    data=st.data(),
)
def test_transpose(x, data):
    axes = data.draw(
        valid_axes(x.ndim, min_dim=x.ndim, max_dim=x.ndim).map(
            lambda out: (out, ) if isinstance(out, int) else out),
        label="axes",
    )

    x_arr = Tensor(np.copy(x))

    o = transpose(x_arr, axes, constant=False)
Example #20
0

@given(
    shape=st.sampled_from(_array_shapes),
    mean=st.floats(-100, 100),
    std=st.floats(0, 5),
)
def test_normal_statistics(shape, mean, std):
    tensor = normal(shape, mean=mean, std=std)
    assert isinstance(tensor, Tensor)
    assert np.isclose(np.mean(tensor.data), mean, atol=1e-2)
    assert np.isclose(np.std(tensor.data), std, atol=1e-2)


@given(
    shape=hnp.array_shapes(min_dims=2),
    mean=st.floats(-100, 100),
    std=st.floats(0, 100),
    dtype=hnp.floating_dtypes(),
    constant=st.booleans(),
)
def test_normal(shape, mean, std, dtype, constant):
    tensor = normal(shape,
                    mean=Tensor(mean),
                    std=Tensor(std),
                    dtype=dtype,
                    constant=constant)
    assert tensor.shape == shape
    assert tensor.dtype == dtype
    assert tensor.constant == constant
Example #21
0
def gen_setitem(draw):
    a = draw(st.integers(min_value=2, max_value=5))
    shape = draw(array_shapes(min_dims=2, max_dims=2, min_side=a))
    index = draw(array_shapes(min_dims=2, max_dims=2, max_side=a - 1))

    return shape, index
Example #22
0
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:
Example #23
0
def gen_shape_data(draw):
    shape = draw(array_shapes(max_dims=3, min_side=5))
    data = draw(
        st.dictionaries(keys=st.integers(min_value=0, max_value=4), values=st.floats())
    )
    return shape, data
Example #24
0
def timestamps_values_uncertainties_kind(
    draw,
    min_count: Optional[int] = 2,
    max_count: Optional[int] = None,
    kind_tuple: Optional[Tuple[str]] = ("linear", "previous", "next",
                                        "nearest"),
    sorted_timestamps: Optional[bool] = True,
    extrapolate: Optional[Union[bool, str]] = False,
    restrict_fill_value: Optional[str] = None,
    restrict_fill_unc: Optional[str] = None,
    returnC: Optional[bool] = False,
    for_make_equidistant: Optional[bool] = False,
) -> Dict[str, Union[np.ndarray, str]]:
    """Set custom strategy for _hypothesis_ to draw desired input from

    Parameters
    ----------
        draw : callable
            this is a hypothesis internal callable to actually draw from provided
            strategies
        min_count : int, optional
            the minimum number of elements expected inside the arrays of timestamps
             (or frequencies), measurement values and associated uncertainties.
             (default = 2)
        max_count : int, optional
            the maximum number of elements expected inside the arrays of timestamps
            (or frequencies), measurement values and associated uncertainties
            (default is None)
        kind_tuple : tuple(str), optional
            the tuple of strings out of "linear", "previous", "next", "nearest",
            "spline", "least-squares" from which the strategy for the
            kind randomly chooses. Defaults to the valid options "linear",
            "previous", "next", "nearest"
        sorted_timestamps : bool, optional
            if True (default) the timestamps (or frequencies) are guaranteed to be in
            ascending order, if False they still might be by coincidence or not
        extrapolate : bool or str, optional
            If True the interpolation timestamps (or frequencies) are generated such
            that extrapolation is necessary by guarantying at least one of the
            interpolation timestamps (or frequencies) outside the original bounds
            and accordingly setting appropriate values for `fill_value` and
            `bounds_error = False`. If False (default) each element of t_new is
            guaranteed to lie within the range of t. Can be set to "above" or "below"
            to guarantee at least one element of t_new to lie either below or above
            the bounds of t.
        restrict_fill_value : str, optional
            String specifying the desired strategy for drawing a fill_value. One of
            "float", "tuple", "str", "nan" to guarantee either a float, a tuple of
            two floats, the string "extrapolate" or np.nan. (default is None)
        restrict_fill_unc : str, optional
            Same as fill_value, but just for the uncertainties. (default is None)
        returnC : bool, optional
            If True we request the sensitivities to be returned. If False (default) we
            do not request them.
        for_make_equidistant : bool, optional
            If True we return the expected parameters for calling `make_equidistant()`.
            If False (default) we return the expected parameters for calling
            `interp1d_unc()`.

    Returns
    -------
        A dict containing the randomly generated expected input parameters for
        `interp1d_unc()`
    """
    def draw_fill_values(strategy_spec: str):
        """Little helper to find proper strategy for efficient testing.

        Parameters
        ----------
            strategy_spec : str
                String specifying the desired strategy for drawing a fill_value. One of
                "float", "tuple", "str", "nan" to guarantee either a float, a tuple of
                two floats, the string "extrapolate" or np.nan.

        Returns
        -------
            The drawn sample to match desired fill_value.
        """
        float_strategy = st.floats(**float_generic_params)
        tuple_strategy = st.tuples(float_strategy, float_strategy)
        string_strategy = st.just("extrapolate")
        nan_strategy = st.just(np.nan)
        if strategy_spec == "float":
            fill_strategy = float_strategy
        elif strategy_spec == "tuple":
            fill_strategy = tuple_strategy
        elif strategy_spec == "str":
            fill_strategy = string_strategy
        elif strategy_spec == "nan":
            fill_strategy = nan_strategy
        else:
            fill_strategy = st.one_of(float_strategy, tuple_strategy,
                                      string_strategy, nan_strategy)
        return draw(fill_strategy)

    # Set the maximum absolute value for floats to be really unique in calculations.
    float_abs_max = 1e64
    # Set generic float parameters.
    float_generic_params = {
        "allow_nan": False,
        "allow_infinity": False,
    }
    # Set all common parameters for timestamps (or frequencies), measurements values
    # and associated uncertainties.
    shape_for_timestamps = hnp.array_shapes(max_dims=1,
                                            min_side=min_count,
                                            max_side=max_count)
    strategy_params = {
        "dtype":
        np.float,
        "shape":
        shape_for_timestamps,
        "elements":
        st.floats(min_value=-float_abs_max,
                  max_value=float_abs_max,
                  **float_generic_params),
        "unique":
        True,
    }
    # Draw "original" timestamps (or frequencies).
    t = draw(hnp.arrays(**strategy_params))
    # Sort timestamps (or frequencies) in ascending order.
    if sorted_timestamps:
        ind = np.argsort(t)
        t = t[ind]

    # Reuse "original" timestamps (or frequencies) shape for measurements values and
    # associated uncertainties and draw both.
    strategy_params["shape"] = np.shape(t)
    y = draw(hnp.arrays(**strategy_params))
    uy = draw(hnp.arrays(**strategy_params))

    # Draw the interpolation kind from the provided tuple.
    kind = draw(st.sampled_from(kind_tuple))

    if for_make_equidistant:
        dt = draw(
            st.floats(
                min_value=(np.max(t) - np.min(t)) * 1e-3,
                max_value=(np.max(t) - np.min(t)) / 2,
                exclude_min=True,
                allow_nan=False,
                allow_infinity=False,
            ))
        return {"t": t, "y": y, "uy": uy, "dt": dt, "kind": kind}
    else:
        # Reset shape for interpolation timestamps (or frequencies).
        strategy_params["shape"] = shape_for_timestamps
        # Look up minimum and maximum of original timestamps (or frequencies) just once.
        t_min = np.min(t)
        t_max = np.max(t)

        if not extrapolate:
            # In case we do not want to extrapolate, use range of "original"
            # timestamps (or frequencies) as boundaries.
            strategy_params["elements"] = st.floats(min_value=t_min,
                                                    max_value=t_max,
                                                    **float_generic_params)
            fill_value = fill_unc = np.nan
            # Switch between default value None and intentionally setting to True,
            # which should behave identically.
            bounds_error = draw(st.one_of(st.just(True), st.none()))
        else:
            # In case we want to extrapolate, draw some fill values for the
            # out-of-bounds range. Those will be either single floats or a 2-tuple of
            # floats or the special value "extrapolate".
            fill_value = draw_fill_values(restrict_fill_value)
            fill_unc = draw_fill_values(restrict_fill_unc)
            bounds_error = False

        # Draw interpolation timestamps (or frequencies).
        t_new = draw(hnp.arrays(**strategy_params))

        if extrapolate:
            # In case we want to extrapolate, make sure we actually do after having
            # drawn the timestamps (or frequencies) not to randomly have drawn values
            # inside original bounds and if even more constraints are given ensure
            # those.
            assume(np.min(t_new) < np.min(t) or np.max(t_new) > np.max(t))
            if extrapolate == "above":
                assume(np.max(t_new) > np.max(t))
            else:
                assume(np.min(t_new) < np.min(t))

        assume_sorted = sorted_timestamps
        return {
            "t_new": t_new,
            "t": t,
            "y": y,
            "uy": uy,
            "kind": kind,
            "fill_value": fill_value,
            "fill_unc": fill_unc,
            "bounds_error": bounds_error,
            "assume_sorted": assume_sorted,
            "returnC": returnC,
        }
Example #25
0
    return mg.multi_matmul(args, constant)


def multi_matmul_slow(*arrays, **kwargs):
    return functools.reduce(np.matmul, arrays)


@given(st.lists(st.just(mg.Tensor([0.0, 1.0])), min_size=0, max_size=1))
def test_input_validation_too_few_tensors(tensors: List[mg.Tensor]):
    """multi_matmul requires at least two input-tensors"""
    with pytest.raises(ValueError):
        mg.multi_matmul(tensors)


@given(
    st.lists(hnp.array_shapes(min_dims=1), min_size=2).filter(
        lambda shapes: any(not (1 <= len(x) <= 2) for x in shapes)))
def test_input_validation_large_dimensionality(shapes: List[Tuple[int, ...]]):
    """multi_matmul only operates on 1D and 2D tensors"""
    tensors = [mg.ones(shape=shape) for shape in shapes]
    with pytest.raises(ValueError):
        mg.multi_matmul(tensors)


@pytest.mark.parametrize(
    "signature",
    (
        "(a?,b),(b,e?)->(a?,e?)",
        "(a?,b),(b,c),(c,e?)->(a?,e?)",
        "(a?,b),(b,c),(c,d),(d,e?)->(a?,e?)",
    ),
Example #26
0
    grid_b = grid.copy()
    grid_c = grid.copy()
    grid_a.transform(trans)
    grid_b.transform(trans.GetMatrix())
    grid_c.transform(pyvista.trans_from_matrix(trans.GetMatrix()))
    assert np.allclose(grid_a.points, grid_b.points, equal_nan=True)
    assert np.allclose(grid_a.points, grid_c.points, equal_nan=True)


def test_translate_should_fail_given_none(grid):
    with pytest.raises(TypeError):
        grid.transform(None)


@given(array=arrays(dtype=np.float32,
                    shape=array_shapes(max_dims=5, max_side=5)))
def test_transform_should_fail_given_wrong_numpy_shape(array, grid):
    assume(array.shape != (4, 4))
    with pytest.raises(ValueError):
        grid.transform(array)


@pytest.mark.parametrize('axis_amounts', [[1, 1, 1], [0, 0, 0], [-1, -1, -1]])
def test_translate_should_translate_grid(grid, axis_amounts):
    grid_copy = grid.copy()
    grid_copy.translate(axis_amounts)

    grid_points = grid.points.copy() + np.array(axis_amounts)
    assert np.allclose(grid_copy.points, grid_points)

Example #27
0
    assert Wr.grad is None
    assert Wh.grad is None

    assert Uz.grad is None
    assert Ur.grad is None
    assert Uh.grad is None

    assert bz.grad is None
    assert br.grad is None
    assert bh.grad is None


@settings(deadline=None)
@given(
    X=hnp.arrays(
        shape=hnp.array_shapes(max_side=5, min_dims=3, max_dims=3),
        dtype=float,
        elements=st.floats(-10, 10),
    ),
    D=st.sampled_from(list(range(1, 5))),
    dropout=st.sampled_from([0, 0.45]),
    data=st.data(),
)
def test_gru_fwd(X, D, dropout, data: st.DataObject):
    T, N, C = X.shape

    Wz, Wr, Wh = data.draw(
        hnp.arrays(shape=(3, D, D),
                   dtype=float,
                   elements=st.floats(-10.0, 10.0)),
        label="Wz, Wr, Wh",
Example #28
0
def test_basic_indices_options(condition):
    indexers = nps.array_shapes(min_dims=0, max_dims=32).flatmap(
        lambda shape: nps.basic_indices(shape, allow_newaxis=True))
    find_any(indexers, condition)
Example #29
0
    o = x[index]  # raises if invalid index
    assert isinstance(o, np.ndarray) and o.ndim == 1, (
        "A slice index should produce " "a 1D array from a 1D array"
    )
    if o.size:
        assert np.shares_memory(o, x), "A slice should produce a view of `x`"

    if index.start is not None:
        assert -size <= index.start <= size

    if index.stop is not None:
        assert -size <= index.stop <= size


@given(
    shape=hnp.array_shapes(),
    allow_singleton=st.booleans(),
    min_dim=st.integers(0, 6),
    min_side=st.integers(1, 6),
    data=st.data(),
)
def test_broadcast_compat_shape(
    shape: Tuple[int, ...],
    allow_singleton: bool,
    min_dim: int,
    min_side: int,
    data: st.SearchStrategy,
):
    """ Ensures that the `broadcastable_shape` strategy:
        - produces broadcastable shapes
        - respects input parameters"""
Example #30
0
def timestamps_values_uncertainties_kind(
    draw,
    min_count: Optional[int] = 2,
    max_count: Optional[int] = None,
    kind_tuple: Optional[Tuple[str]] = ("linear", "previous", "next",
                                        "nearest"),
    sorted_timestamps: Optional[bool] = True,
) -> Dict[str, Union[np.ndarray, str]]:
    """Set custom strategy for _hypothesis_ to draw desired input from

    Parameters
    ----------
        draw: callable
            this is a hypothesis internal callable to actually draw from provided
            strategies
        min_count: int
            the minimum number of elements expected inside the arrays of timestamps,
            measurement values and associated uncertainties
        max_count: int
            the maximum number of elements expected inside the arrays of timestamps,
            measurement values and associated uncertainties
        kind_tuple: tuple(str), optional
            the tuple of strings out of "linear", "previous", "next", "nearest",
            "spline", "least-squares" from which the strategy for the
            kind randomly chooses. Defaults to the valid options "linear",
            "previous", "next", "nearest".
        sorted_timestamps: bool
            if the timestamps should be in ascending order or not

    Returns
    -------
        A dict containing the randomly generated expected input parameters t, y, uy,
        dt, kind for make_equidistant()
    """
    # Set the maximum absolute value for floats to be really unique in calculations.
    float_abs_max = 1e64
    # Set all common parameters for timestamps, measurements values and associated
    # uncertainties including allowed ranges and number of elements.
    shape_for_timestamps = hnp.array_shapes(max_dims=1,
                                            min_side=min_count,
                                            max_side=max_count)
    strategy_params = {
        "dtype":
        np.float,
        "shape":
        shape_for_timestamps,
        "elements":
        st.floats(
            min_value=-float_abs_max,
            max_value=float_abs_max,
            allow_nan=False,
            allow_infinity=False,
        ),
        "unique":
        True,
    }
    # Draw "original" timestamps.
    t = draw(hnp.arrays(**strategy_params))
    # Sort timestamps in ascending order.
    if sorted_timestamps:
        t.sort()
    # Reuse "original" timestamps shape for measurements values and associated
    # uncertainties and draw both.
    strategy_params["shape"] = np.shape(t)
    y = draw(hnp.arrays(**strategy_params))
    uy = draw(hnp.arrays(**strategy_params))
    dt = draw(
        st.floats(
            min_value=(np.max(t) - np.min(t)) * 1e-3,
            max_value=(np.max(t) - np.min(t)) / 2,
            exclude_min=True,
            allow_nan=False,
            allow_infinity=False,
        ))
    kind = draw(st.sampled_from(kind_tuple))
    return {"t": t, "y": y, "uy": uy, "dt": dt, "kind": kind}
Example #31
0
        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(),
Example #32
0
    pe = PersistenceEntropy()
    diagram_res = np.array([[0.69314718, 0.63651417]])

    assert_almost_equal(pe.fit_transform(diagram), diagram_res)


def test_pi_not_fitted():
    pi = PersistenceImage(sigma=1)
    with pytest.raises(NotFittedError):
        pi.transform(diagram)


@given(X=arrays(dtype=np.float, unique=True,
                elements=integers(min_value=-1e10,
                                  max_value=1e6),
                shape=array_shapes(min_dims=1, max_dims=1, min_side=11)))
def test_pi_null(X):
    """Test that, if one trivial diagram (all pts on the diagonal) is provided,
    (along with a non-trivial one), then its pi is null"""
    pi = PersistenceImage(sigma=1, n_bins=10)
    X = np.append(X, 1 + X[-1])
    diagrams = np.expand_dims(np.stack([X, X,
                                        np.zeros((X.shape[0],),
                                                 dtype=int)]).transpose(),
                              axis=0)
    diagrams = np.repeat(diagrams, 2, axis=0)
    diagrams[1, :, 1] += 1

    assert_almost_equal(pi.fit_transform(diagrams)[0], 0)

Example #33
0
from hypothesis import given, settings

import xarray as xr

# Run for a while - arrays are a bigger search space than usual
settings.register_profile("ci", deadline=None)
settings.load_profile("ci")


an_array = npst.arrays(
    dtype=st.one_of(
        npst.unsigned_integer_dtypes(),
        npst.integer_dtypes(),
        npst.floating_dtypes(),
    ),
    shape=npst.array_shapes(max_side=3),  # max_side specified for performance
)


@given(st.data(), an_array)
def test_CFMask_coder_roundtrip(data, arr):
    names = data.draw(st.lists(st.text(), min_size=arr.ndim,
                               max_size=arr.ndim, unique=True).map(tuple))
    original = xr.Variable(names, arr)
    coder = xr.coding.variables.CFMaskCoder()
    roundtripped = coder.decode(coder.encode(original))
    xr.testing.assert_identical(original, roundtripped)


@given(st.data(), an_array)
def test_CFScaleOffset_coder_roundtrip(data, arr):
Example #34
0
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
Example #35
0
# primitive data #
##################

frequencies = st.floats(min_value=0.0, max_value=1e15)
non_zero_frequencies = st.floats(min_value=1e-15, max_value=1e15)
sampling_rates = st.floats(min_value=1e-8, max_value=1e8)
resolutions = sampling_rates
short_lengths = st.integers(min_value=0, max_value=2 ** 10)
texts = st.text(alphabet=st.characters(blacklist_categories=("Cs",), blacklist_characters=("\x00",)))

##########
# Signal #
##########

_signal_parameters = {"channels": stn.arrays(dtype=numpy.float64,       # pylint: disable=no-value-for-parameter; there is a false alarm about a missing parameter for ``draw``
                                             shape=stn.array_shapes(min_dims=2, max_dims=2),
                                             elements=st.floats(min_value=-1e100, max_value=1e100)),
                      "sampling_rate": sampling_rates,
                      "offset": st.integers(min_value=-2 ** 24, max_value=2 ** 24),
                      "labels": st.lists(elements=texts)}
signal_parameters = st.fixed_dictionaries(_signal_parameters)
signals = st.builds(sumpf.Signal, **_signal_parameters)
_normalized_signal_parameters = {"channels": stn.arrays(dtype=numpy.float64,       # pylint: disable=no-value-for-parameter; there is a false alarm about a missing parameter for ``draw``
                                                        shape=stn.array_shapes(min_dims=2, max_dims=2),
                                                        elements=st.floats(min_value=-255.0 / 256.0, max_value=254.0 / 256.0)),     # pylint: disable=line-too-long
                                 "sampling_rate": sampling_rates,
                                 "offset": st.integers(min_value=-2 ** 24, max_value=2 ** 24),
                                 "labels": st.lists(elements=texts)}
normalized_signal_parameters = st.fixed_dictionaries(_normalized_signal_parameters)
normalized_signals = st.builds(sumpf.Signal, **_normalized_signal_parameters)
Example #36
0
def array_pair(draw, dtype1, elements1, dtype2, elements2):
    s = draw(array_shapes())
    a = draw(arrays(dtype=dtype1, shape=s, elements=elements1))
    b = draw(arrays(dtype=dtype2, shape=s, elements=elements2))
    return s, a, b
Example #37
0
# 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 https://mozilla.org/MPL/2.0/.
#
# END HEADER

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))
Example #38
0
    assert_almost_equal(x_fit_transf, x_fit_and_transf)


def test_fit_transform_limits_not_computed():
    """We do not compute intervals when `kind`= `'balanced'`,
    unless fit is explicitly called."""
    cover = OneDimensionalCover(n_intervals=10, kind='balanced',
                                overlap_frac=0.3)
    x = np.arange(0, 30)
    _ = cover.fit_transform(x)
    with pytest.raises(NotFittedError):
        _ = cover.get_fitted_intervals()


@given(pts=get_filter_values(
    shape=array_shapes(min_dims=2, max_dims=2, min_side=2)
    ))
def test_two_dimensional_tensor(pts):
    """Verify that the oneDimensionalCover fails for an input
    with more than one dimension, and that the CubicalCover
    does not."""
    one_d = OneDimensionalCover()
    with pytest.raises(ValueError):
        one_d.fit(pts)
    cubical = CubicalCover()
    _ = cubical.fit(pts)


@given(filter_values=get_filter_values(), kind=get_kind(),
       n_intervals=get_nb_intervals(), overlap_fraction=get_overlap_fraction())
@pytest.mark.parametrize("cover_cls", [OneDimensionalCover, CubicalCover])
Example #39
0
        handle.write(img, method="tile")
    with Tiff(filename) as handle:
        data = handle[:]
        assert np.all(img == data[:, :, :3])

    with Tiff(filename, "w") as handle:
        handle.write(img, method="scanline")
    with Tiff(filename) as handle:
        data = handle[:]
        assert np.all(img == data[:, :, :3])

# scanline integer tests

@settings(buffer_size=11000000)
@given(data=hnp.arrays(dtype=st.one_of(hnp.integer_dtypes(endianness="="), hnp.unsigned_integer_dtypes(endianness="=")),
    shape=hnp.array_shapes(min_dims=2, max_dims=2, min_side=10, max_side=50)))
def test_write_int_scanline(data, tmpdir_factory):
    filename = str(tmpdir_factory.mktemp("write").join("int_img.tif"))
    with Tiff(filename, "w") as handle:
        handle.write(data, method="scanline")

    with tifffile.TiffFile(filename) as handle:
        img = handle.asarray()
        np.testing.assert_array_equal(data, img)

@settings(buffer_size=11000000)
@given(data=hnp.arrays(dtype=st.one_of(hnp.integer_dtypes(endianness="="), hnp.unsigned_integer_dtypes(endianness="=")),
    shape=hnp.array_shapes(min_dims=2, max_dims=2, min_side=10, max_side=50)))
def test_write_int_scanline_set_rows_per_strip(data, tmpdir_factory):
    filename = str(tmpdir_factory.mktemp("write").join("int_img.tif"))
    rows_per_strip = 1