|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import bioimage_cpp as bic |
| 5 | + |
| 6 | +SUPPORTED_DTYPES = [ |
| 7 | + np.bool_, |
| 8 | + np.uint8, |
| 9 | + np.uint16, |
| 10 | + np.uint32, |
| 11 | + np.uint64, |
| 12 | + np.int32, |
| 13 | + np.int64, |
| 14 | +] |
| 15 | + |
| 16 | + |
| 17 | +def _rle(values, dtype=np.uint8): |
| 18 | + return bic.utils.compute_rle(np.asarray(values, dtype=dtype)) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.parametrize( |
| 22 | + "values, expected", |
| 23 | + [ |
| 24 | + ([0, 0, 1, 1, 1, 0, 0, 1, 1], [2, 3, 2, 2]), |
| 25 | + ([1, 1, 0, 1], [0, 2, 1, 1]), |
| 26 | + ([1, 1, 1, 1, 1], [0, 5]), |
| 27 | + ([0, 0, 0, 0], [4]), |
| 28 | + ([1], [0, 1]), |
| 29 | + ([0], [1]), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_known_cases(values, expected): |
| 33 | + result = _rle(values) |
| 34 | + np.testing.assert_array_equal(result, np.asarray(expected, dtype=np.int64)) |
| 35 | + |
| 36 | + |
| 37 | +def test_empty_input(): |
| 38 | + result = _rle([]) |
| 39 | + assert result.dtype == np.int64 |
| 40 | + assert result.shape == (0,) |
| 41 | + |
| 42 | + |
| 43 | +def test_output_is_int64_1d(): |
| 44 | + result = _rle([0, 1, 1]) |
| 45 | + assert result.dtype == np.int64 |
| 46 | + assert result.ndim == 1 |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("dtype", SUPPORTED_DTYPES) |
| 50 | +def test_dtype_variants(dtype): |
| 51 | + mask = np.array([0, 1, 1, 0, 0, 1], dtype=dtype) |
| 52 | + result = bic.utils.compute_rle(mask) |
| 53 | + np.testing.assert_array_equal(result, np.array([1, 2, 2, 1], dtype=np.int64)) |
| 54 | + |
| 55 | + |
| 56 | +def _reference_rle(flat): |
| 57 | + # COCO-style: counts of alternating runs starting with zeros. |
| 58 | + counts = [] |
| 59 | + current = 0 |
| 60 | + run = 0 |
| 61 | + for value in flat: |
| 62 | + binary = int(value != 0) |
| 63 | + if binary == current: |
| 64 | + run += 1 |
| 65 | + else: |
| 66 | + counts.append(run) |
| 67 | + current = binary |
| 68 | + run = 1 |
| 69 | + counts.append(run) |
| 70 | + return np.asarray(counts, dtype=np.int64) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("shape", [(6, 8), (3, 4, 5)]) |
| 74 | +def test_c_order_flatten(shape): |
| 75 | + rng = np.random.default_rng(0) |
| 76 | + mask = (rng.integers(0, 2, size=shape)).astype(np.uint8) |
| 77 | + result = bic.utils.compute_rle(mask) |
| 78 | + expected = _reference_rle(mask.ravel(order="C")) |
| 79 | + np.testing.assert_array_equal(result, expected) |
| 80 | + |
| 81 | + |
| 82 | +def test_non_contiguous_input(): |
| 83 | + rng = np.random.default_rng(1) |
| 84 | + base = (rng.integers(0, 2, size=(5, 6))).astype(np.uint8) |
| 85 | + view = base.T # non-contiguous (Fortran-ordered view) |
| 86 | + assert not view.flags["C_CONTIGUOUS"] |
| 87 | + result = bic.utils.compute_rle(view) |
| 88 | + expected = _reference_rle(np.ascontiguousarray(view).ravel(order="C")) |
| 89 | + np.testing.assert_array_equal(result, expected) |
| 90 | + |
| 91 | + |
| 92 | +def test_nonzero_values_are_binary(): |
| 93 | + # Values other than 0/1 are treated as "set". |
| 94 | + mask = np.array([0, 7, 3, 0], dtype=np.int32) |
| 95 | + result = bic.utils.compute_rle(mask) |
| 96 | + np.testing.assert_array_equal(result, np.array([1, 2, 1], dtype=np.int64)) |
| 97 | + |
| 98 | + |
| 99 | +def test_invalid_dtype_raises(): |
| 100 | + with pytest.raises(TypeError): |
| 101 | + bic.utils.compute_rle(np.array([0.0, 1.0], dtype=np.float32)) |
0 commit comments