Exemplo n.º 1
0
def get_scalar_dtype_strategy(exclude=None):
    """
    A `hypothesis` strategy yielding
    """
    possible_strategies = {
        "datetime": hyp_np.datetime64_dtypes(max_period="ms", min_period="ns"),
        "uint": hyp_np.unsigned_integer_dtypes(),
        "int": hyp_np.integer_dtypes(),
        "float": hyp_np.floating_dtypes(),
        "byte": hyp_np.byte_string_dtypes(),
        "unicode": hyp_np.unicode_string_dtypes(),
    }
    if exclude is None:
        exclude = {}
    elif not isinstance(exclude, list):
        exclude = [exclude]
    for ex in exclude:
        if ex in possible_strategies:
            del possible_strategies[ex]
        else:
            raise ValueError(
                "Strategy {} unknown. Possible values are {}".format(
                    ex, possible_strategies.keys()
                )
            )
    return hyp_st.one_of(*list(possible_strategies.values()))
Exemplo n.º 2
0
def ints_floats_datetimes_and_timedeltas(draw):
    dtypes = (
        unsigned_integer_dtypes(endianness="="),
        integer_dtypes(endianness="="),
        floating_dtypes(endianness="="),
        datetime64_dtypes(endianness="="),
        timedelta64_dtypes(endianness="="),
    )
    return draw(one_of(dtypes))
Exemplo n.º 3
0
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(
        st.one_of(
            npst.boolean_dtypes(),
            npst.integer_dtypes(endianness="="),
            npst.floating_dtypes(endianness="="),
            npst.complex_number_dtypes(endianness="="),
            npst.datetime64_dtypes(endianness="="),
            npst.timedelta64_dtypes(endianness="="),
        ).filter(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)
Exemplo n.º 4
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"]
Exemplo n.º 5
0
    booleans,
    composite,
    floats,
    integers,
    lists,
    one_of,
    text,
    tuples,
)

from superintendent.distributed.dbqueue 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(),
                np_strategies.unicode_string_dtypes(),
            ),
 "datatype, elements",
 [
     pytest.param(
         integer_dtypes(endianness="=", sizes=(64, )),
         integers(min_value=1, max_value=np.iinfo(np.int64).max),
         id="integer_dtype",
     ),
     pytest.param(
         unsigned_integer_dtypes(endianness="=", sizes=(64, )),
         integers(min_value=1, max_value=np.iinfo(np.int64).max),
         id="unsigned_integer_dtype",
     ),
     pytest.param(
         byte_string_dtypes(endianness="="), None, id="byte_string_dtype"),
     pytest.param(
         datetime64_dtypes(endianness="="),
         None,
         id="datetime64_dtype",
         marks=[
             pytest.mark.xfail(
                 reason="RIP-375 - Categorical unsupported dtypes"),
             pytest.mark.skip,
         ],
     ),
     pytest.param(
         timedelta64_dtypes(endianness="="),
         None,
         id="timedelta64_dtype",
         marks=[
             pytest.mark.xfail(
                 reason="RIP-375 - Categorical unsupported dtypes"),
    arrays,
    scalar_dtypes,
    unsigned_integer_dtypes,
    datetime64_dtypes,
    floating_dtypes,
    integer_dtypes,
)

from hypothesis import HealthCheck

from superintendent.queueing.utils 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))
    colnames = draw(
        lists(text() | integers(),
              min_size=n_cols,
Exemplo n.º 8
0
    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,))
@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(