rle = "29102 12 29346 24 29602 24 29858 24 30114 24 30370 24 30626 24 30882 24 31139 23 31395 23 31651 23 31907 23 32163 23 32419 23 32675 23 77918 27 78174 55 78429 60 78685 64 78941 68 79197 72 79452 77 79708 81 79964 85 80220 89 80475 94 80731 98 80987 102 81242 105 81498 105 81754 104 82010 104 82265 105 82521 31 82556 69 82779 27 82818 63 83038 22 83080 57 83297 17 83342 50 83555 13 83604 44 83814 8 83866 37 84073 3 84128 31 84390 25 84652 18 84918 8 85239 10 85476 29 85714 47 85960 57 86216 57 86471 58 86727 58 86983 58 87238 59 87494 59 87750 59 88005 60 88261 60 88517 60 88772 61 89028 53 89283 40 89539 32 89667 10 89795 30 89923 28 90050 29 90179 37 90306 27 90434 38 90562 14 90690 38 90817 9 90946 38 91073 3 91202 38 91458 38 91714 38 91969 39 92225 39 92481 39 92737 39 92993 39 93248 40 93504 40 93760 40 94026 30 94302 10 189792 7 190034 21 190283 28 190539 28 190795 28 191051 28 191307 28 191563 28 191819 28 192075 28 192331 28 192587 28 192843 23 193099 14 193355 5"  # noqa: E501

    width, height = 1600, 256

    mask = rle2mask(rle, (width, height))

    assert mask.shape == (height, width)

    inversed_rle = mask2rle(mask)

    assert rle == inversed_rle


@given(mask=h_arrays(
    dtype=np.uint8,
    shape=(np.random.randint(1, 10), np.random.randint(1, 11)),
    elements=h_int(min_value=0, max_value=255),
))
def test_mask2one_hot(mask):
    num_classes = mask.max() + 1

    one_hot_mask = one_hot(mask, num_classes)

    assert np.all(mask == reverse_one_hot(one_hot_mask))


@given(mask=h_arrays(
    dtype=np.uint8,
    shape=(np.random.randint(1, 11), np.random.randint(1, 10)),
    elements=h_int(min_value=0, max_value=255),
))
def test_rle():
    rle = "29102 12 29346 24 29602 24 29858 24 30114 24 30370 24 30626 24 30882 24 31139 23 31395 23 31651 23 31907 23 32163 23 32419 23 32675 23 77918 27 78174 55 78429 60 78685 64 78941 68 79197 72 79452 77 79708 81 79964 85 80220 89 80475 94 80731 98 80987 102 81242 105 81498 105 81754 104 82010 104 82265 105 82521 31 82556 69 82779 27 82818 63 83038 22 83080 57 83297 17 83342 50 83555 13 83604 44 83814 8 83866 37 84073 3 84128 31 84390 25 84652 18 84918 8 85239 10 85476 29 85714 47 85960 57 86216 57 86471 58 86727 58 86983 58 87238 59 87494 59 87750 59 88005 60 88261 60 88517 60 88772 61 89028 53 89283 40 89539 32 89667 10 89795 30 89923 28 90050 29 90179 37 90306 27 90434 38 90562 14 90690 38 90817 9 90946 38 91073 3 91202 38 91458 38 91714 38 91969 39 92225 39 92481 39 92737 39 92993 39 93248 40 93504 40 93760 40 94026 30 94302 10 189792 7 190034 21 190283 28 190539 28 190795 28 191051 28 191307 28 191563 28 191819 28 192075 28 192331 28 192587 28 192843 23 193099 14 193355 5"  # noqa: E501

    width, height = 1600, 256

    mask = rle2mask(rle, (width, height))

    assert mask.shape == (height, width)

    inversed_rle = mask2rle(mask)

    assert rle == inversed_rle


@given(mask=h_arrays(dtype=np.uint8,
                     shape=(np.random.randint(1, 1000),
                            np.random.randint(1, 1000)),
                     elements=h_int(0, 1)))
def test_kaggle_rle(mask):
    height, width = mask.shape

    kaggle_rle = kaggle_rle_encode(mask)
    coco_rle = coco_rle_encode(mask)
    assert coco_rle == kaggle2coco(kaggle_rle, height, width)
    assert np.all(mask == kaggle_rle_decode(kaggle_rle, height, width))
    assert np.all(mask == coco_rle_decode(coco_rle, height, width))


def test_coco2binary_mask():
    height, width = 130, 120
    mask = np.zeros((height, width))
    mask[50:70, 30:90] = 1
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.image_utils import pad, unpad


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)), factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)), factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.img_tools import pad, unpad


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)
Ejemplo n.º 5
0
from hypothesis.strategies import characters as h_char
from hypothesis.strategies import floats as h_float

from help_functions.utils.tabular_utils import (
    CyclicEncoder,
    LabelEncoderUnseen,
    GeneralEncoder,
)

MIN_VALUE = -11
MAX_VALUE = 17
ARRAY_SHAPE = 3


@given(
    x=h_arrays(dtype=float, shape=ARRAY_SHAPE, elements=h_float(-MIN_VALUE, MAX_VALUE))
)
def test_cyclic_day_hours(x):
    amplitude = MAX_VALUE - MIN_VALUE

    encoder = CyclicEncoder(amplitude)

    transformed = encoder.fit_transform(x)

    assert transformed.shape[1] == 2

    encoder2 = CyclicEncoder(amplitude)
    encoder2.fit(x)
    transformed2 = encoder2.transform(x)

    assert transformed2.shape[1] == 2
import numpy as np
from hypothesis import given
from hypothesis.extra.numpy import arrays as h_arrays
from hypothesis.strategies import integers as h_int

from iglovikov_helper_functions.utils.image_utils import (
    pad,
    pad_to_size,
    unpad,
    unpad_from_size,
)


@given(input_array=h_arrays(dtype=np.uint8, shape=(351, 619)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_grayscale(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)


@given(input_array=h_arrays(dtype=np.uint8, shape=(174, 413, 3)),
       factor=h_int(min_value=1, max_value=65))
def test_pad_rgb(input_array, factor):
    padded_array, pads = pad(input_array, factor)
    unpadded_array = unpad(padded_array, pads)

    assert np.array_equal(input_array, unpadded_array)