示例#1
0
def from_dtype(dtype):
    # type: (np.dtype) -> st.SearchStrategy[Any]
    """Creates a strategy which can generate any value of the given dtype."""
    check_type(np.dtype, dtype, "dtype")
    # Compound datatypes, eg 'f4,f4,f4'
    if dtype.names is not None:
        # mapping np.void.type over a strategy is nonsense, so return now.
        return st.tuples(*[from_dtype(dtype.fields[name][0]) for name in dtype.names])

    # Subarray datatypes, eg '(2, 3)i4'
    if dtype.subdtype is not None:
        subtype, shape = dtype.subdtype
        return arrays(subtype, shape)

    # Scalar datatypes
    if dtype.kind == u"b":
        result = st.booleans()  # type: SearchStrategy[Any]
    elif dtype.kind == u"f":
        if dtype.itemsize == 2:
            result = st.floats(width=16)
        elif dtype.itemsize == 4:
            result = st.floats(width=32)
        else:
            result = st.floats()
    elif dtype.kind == u"c":
        if dtype.itemsize == 8:
            float32 = st.floats(width=32)
            result = st.builds(complex, float32, float32)
        else:
            result = st.complex_numbers()
    elif dtype.kind in (u"S", u"a"):
        # Numpy strings are null-terminated; only allow round-trippable values.
        # `itemsize == 0` means 'fixed length determined at array creation'
        result = st.binary(max_size=dtype.itemsize or None).filter(
            lambda b: b[-1:] != b"\0"
        )
    elif dtype.kind == u"u":
        result = st.integers(min_value=0, max_value=2 ** (8 * dtype.itemsize) - 1)
    elif dtype.kind == u"i":
        overflow = 2 ** (8 * dtype.itemsize - 1)
        result = st.integers(min_value=-overflow, max_value=overflow - 1)
    elif dtype.kind == u"U":
        # Encoded in UTF-32 (four bytes/codepoint) and null-terminated
        result = st.text(max_size=(dtype.itemsize or 0) // 4 or None).filter(
            lambda b: b[-1:] != u"\0"
        )
    elif dtype.kind in (u"m", u"M"):
        if "[" in dtype.str:
            res = st.just(dtype.str.split("[")[-1][:-1])
        else:
            res = st.sampled_from(TIME_RESOLUTIONS)
        result = st.builds(dtype.type, st.integers(-(2 ** 63), 2 ** 63 - 1), res)
    else:
        raise InvalidArgument(u"No strategy inference for {}".format(dtype))
    return result.map(dtype.type)
示例#2
0
文件: merger.py 项目: gasparka/wifi
# not a perfect reconstuction!
def undo(iq):
    short = iq[:160]
    long = iq[160:320]

    frame_size = guard_interval.SIZE + 64
    frames = list(chunked(iq[320:], frame_size))
    if len(frames[-1]) != frame_size:
        frames = frames[:-1]

    return short, long, frames


@given(
    lists(lists(complex_numbers(), min_size=80, max_size=80),
          min_size=1,
          max_size=32))
def test_hypothesis(frames):
    from wifi import preambler
    short = preambler.short_training_sequence()
    long = preambler.long_training_sequence()

    res_short, res_long, res_frames = undo(do(short, long, frames))

    # 'do' contaminates the first sample of each symbol, cannot be restored with 'undo'
    assert res_short[1:] == short[1:]
    assert res_long[1:] == long[1:]
    for frame, res_frame in zip(frames, res_frames):
        assert frame[1:] == res_frame[1:]
示例#3
0
from typing import List
import numpy as np
from hypothesis import given
from hypothesis._strategies import lists, complex_numbers
from wifi.to_time_domain import OFDMFrame

SIZE = 16


def do(frames: List[OFDMFrame]) -> List[OFDMFrame]:
    return [frame[-SIZE:] + frame for frame in frames]


def undo(frames: List[OFDMFrame]) -> List[OFDMFrame]:
    return [frame[SIZE:] for frame in frames]


@given(
    lists(lists(complex_numbers(), min_size=64, max_size=64),
          min_size=1,
          max_size=32))
def test_hypothesis(data):
    un = undo(do(data))
    np.testing.assert_equal(data, un)
示例#4
0

def undo_one(carriers: Carriers, index_in_package: int) -> Carriers:

    pilots = np.empty(4, dtype=complex)
    pilots[0] = carriers[-21]
    pilots[1] = carriers[-7]
    pilots[2] = carriers[7]
    pilots[3] = carriers[21]

    # remove latent frequency offset by using pilot symbols
    pilots *= PILOT_POLARITY[index_in_package % 127]
    mean_phase_offset = np.angle(np.mean(pilots))
    carriers = np.array(carriers) * np.exp(-1j * mean_phase_offset)

    return carriers.tolist()


def do(carriers: List[Carriers]) -> List[Carriers]:
    return [do_one(carrier, index) for index, carrier in enumerate(carriers)]


def undo(carriers: List[Carriers]) -> List[Carriers]:
    return [undo_one(carrier, index) for index, carrier in enumerate(carriers)]


@given(lists(lists(complex_numbers(allow_nan=False, allow_infinity=False), min_size=64, max_size=64), min_size=1, max_size=32))
def test_hypothesis(data):
    un = undo(do(data))
    np.testing.assert_allclose(data, un, rtol=1e-16, atol=1e-16)