File size: 5,148 Bytes
12d2e9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""

import numpy as np
import pytest

from pathml.core import HESlide, Tile, types


@pytest.fixture
def emptytiles():
    slidedata = HESlide("tests/testdata/small_HE.svs")
    return slidedata.tiles


@pytest.fixture
def tiles():
    """
    dict of adjacent tiles
    """
    tiles = []
    for i in range(2):
        for j in range(2):
            # create tile
            shape = (224, 224, 3)
            coords = (224 * i, 224 * j)
            name = f"{i}_{j}"
            masks = {str(k): np.random.randint(2, size=shape) for k in range(2)}
            labs = {
                "test_string_label": "testlabel",
                "test_array_label": np.array([2, 3, 4]),
                "test_int_label": 3,
                "test_float_label": 3.0,
            }
            image = np.random.random_sample(shape)
            tile = Tile(
                image=image,
                name=name,
                coords=coords,
                slide_type=types.HE,
                masks=masks,
                labels=labs,
            )
            # add to dict
            tiles.append(tile)
    return tiles


@pytest.fixture
def tilesnonconsecutive():
    """
    dict of nonconsecutive tiles
    """
    tiles = []
    for i in range(2):
        for j in range(2):
            # create tile
            shape = (224, 224, 3)
            coords = (224 * 2 * (i + 1), 224 * 2 * (j + 2))
            name = f"{i}_{j}"
            masks = {str(k): np.random.randint(2, size=shape) for k in range(2)}
            labs = {
                "test_string_label": "testlabel",
                "test_array_label": np.array([2, 3, 4]),
                "test_int_label": 3,
                "test_float_label": 3.0,
            }
            image = np.random.randint(low=0, high=255, size=shape, dtype=np.uint8)
            tile = Tile(
                image=image,
                name=name,
                coords=coords,
                slide_type=types.HE,
                masks=masks,
                labels=labs,
            )
            # add to dict
            tiles.append(tile)
    return tiles


@pytest.mark.parametrize(
    "incorrect_input", ["string", True, 5, [5, 4, 3], {"dict": "testing"}]
)
def test_init(tiles, tilesnonconsecutive, incorrect_input):
    # init from dict
    slidedata = HESlide("tests/testdata/small_HE.svs", tiles=tiles)
    assert (slidedata.tiles[0].image == tiles[0].image.astype(np.float16)).all()
    # init len
    assert len(slidedata.tiles) == 4
    # incorrect input
    with pytest.raises(AssertionError):
        # fix obj
        slidedata = HESlide("tests/testdata/small_HE.svs", tiles=incorrect_input)
    # nonconsecutive tiles
    slidedata = HESlide("tests/testdata/small_HE.svs", tiles=tilesnonconsecutive)
    np.testing.assert_array_equal(
        slidedata.tiles[(896, 1344)].image, tilesnonconsecutive[3].image
    )


def test_repr(tiles):
    slidedata = HESlide("tests/testdata/small_HE.svs", tiles=tiles)
    assert repr(slidedata.tiles)


@pytest.mark.parametrize(
    "incorrect_input", ["string", None, True, 5, [5, 4, 3], {"dict": "testing"}]
)
@pytest.mark.parametrize(
    "incorrect_input2", [None, True, [5, 4, 3], {"dict": "testing"}]
)
def test_add_get(emptytiles, tileHE, incorrect_input, incorrect_input2):
    # add single tile
    tiles = emptytiles
    tiles.add(tileHE)
    # get by coords and by index
    assert (tiles[(1, 3)].image == tileHE.image).all()
    assert (tiles[0].image == tileHE.image).all()
    assert tiles[(1, 3)].name == tileHE.name
    assert tiles[(1, 3)].coords == tileHE.coords
    for label in tiles[(1, 3)].labels:
        if isinstance(tiles[(1, 3)].labels[label], np.ndarray):
            np.testing.assert_array_equal(
                tiles[(1, 3)].labels[label], tileHE.labels[label]
            )
        else:
            assert tiles[(1, 3)].labels[label] == tileHE.labels[label]
    assert tiles[(1, 3)].slide_type == tileHE.slide_type
    # get masks
    for mask in tiles.h5manager.h5["masks"].keys():
        # masks by coords and by index
        assert (tiles[(1, 3)].masks[mask] == tileHE.masks[mask]).all()
        assert (tiles[0].masks[mask] == tileHE.masks[mask]).all()
    # incorrect input
    with pytest.raises(ValueError or KeyError):
        tiles.add(incorrect_input)
    with pytest.raises(KeyError or IndexError):
        tiles[incorrect_input2]
    # wrong shape
    im = np.arange(np.product((225, 224, 3))).reshape((225, 224, 3))
    wrongshapetile = Tile(image=im, coords=(4, 5), name="wrong")
    with pytest.raises(ValueError):
        tiles.add(wrongshapetile)


@pytest.mark.parametrize(
    "incorrect_input", ["string", None, True, 5, [5, 4, 3], {"dict": "testing"}]
)
def test_remove(emptytiles, tileHE, incorrect_input):
    tiles = emptytiles
    tiles.add(tileHE)
    tiles.remove((1, 3))
    with pytest.raises(Exception):
        tiles[(1, 3)]
    with pytest.raises(KeyError):
        tiles.remove((1, 3))
    # incorrect input
    with pytest.raises(KeyError):
        tiles.remove(incorrect_input)