File size: 5,855 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""

import importlib.util

import numpy as np
import pytest
import torch.nn as nn
import torch.nn.functional as F
from skimage.draw import ellipse
from skimage.measure import label, regionprops

from pathml.datasets.utils import DeepPatchFeatureExtractor


def requires_torchvision(func):
    """Decorator to skip tests that require torchvision."""
    torchvision_installed = importlib.util.find_spec("torchvision") is not None
    reason = "torchvision is required"
    return pytest.mark.skipif(not torchvision_installed, reason=reason)(func)


class SimpleCNN(nn.Module):
    def __init__(self, input_shape):
        super(SimpleCNN, self).__init__()

        self.conv1 = nn.Conv2d(
            in_channels=input_shape[0],
            out_channels=32,
            kernel_size=3,
            stride=1,
            padding=1,
        )
        self.conv2 = nn.Conv2d(
            in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1
        )
        self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)

        fc_input_size = (input_shape[1] // 4) * (input_shape[2] // 4) * 64

        self.fc1 = nn.Linear(fc_input_size, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))

        # Flatten the output for the fully connected layer
        x = x.view(x.size(0), -1)

        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x


def make_fake_instance_maps(num, image_size, ellipse_height, ellipse_width):
    img = np.zeros(image_size)

    # Draw n ellipses
    for i in range(num):
        # Random center for each ellipse
        center_x = np.random.randint(ellipse_width, image_size[1] - ellipse_width)
        center_y = np.random.randint(ellipse_height, image_size[0] - ellipse_height)

        # Coordinates for the ellipse
        rr, cc = ellipse(
            center_y, center_x, ellipse_height, ellipse_width, shape=image_size
        )

        # Draw the ellipse
        img[rr, cc] = 1

    label_img = label(img.astype(int))

    return label_img


def make_fake_image(instance_map):
    image = instance_map[:, :, None]
    image[image > 0] = 1
    noised_image = (
        np.random.rand(instance_map.shape[0], instance_map.shape[1], 3) * 0.15 + image
    ) * 255

    return noised_image.astype("uint8")


@pytest.mark.parametrize("patch_size", [1, 64, 128])
@pytest.mark.parametrize("entity", ["cell", "tissue"])
@pytest.mark.parametrize("threshold", [0, 0.1, 0.8])
@pytest.mark.parametrize("with_instance_masking", [True, False])
@pytest.mark.parametrize("extraction_layer", [None, "fc1"])
def test_feature_extractor(
    entity, patch_size, threshold, with_instance_masking, extraction_layer
):

    image_size = (256, 256)

    instance_map = make_fake_instance_maps(
        num=20, image_size=image_size, ellipse_height=20, ellipse_width=8
    )
    image = make_fake_image(instance_map.copy())
    regions = regionprops(instance_map)

    model = SimpleCNN(input_shape=(3, 224, 224))

    extractor = DeepPatchFeatureExtractor(
        patch_size=patch_size,
        batch_size=1,
        entity=entity,
        architecture=model,
        fill_value=255,
        resize_size=224,
        threshold=threshold,
        with_instance_masking=with_instance_masking,
        extraction_layer=extraction_layer,
    )
    features = extractor.process(image, instance_map)

    if threshold == 0:
        assert features.shape[0] == len(regions)
    else:
        assert features.shape[0] <= len(regions)


@requires_torchvision
@pytest.mark.parametrize("patch_size", [1, 64, 128])
@pytest.mark.parametrize("entity", ["cell", "tissue"])
@pytest.mark.parametrize("threshold", [0, 0.1, 0.8])
@pytest.mark.parametrize("extraction_layer", [None, "fc"])
def test_feature_extractor_torchvision(entity, patch_size, threshold, extraction_layer):
    # pytest.importorskip("torchvision")

    image_size = (256, 256)

    instance_map = make_fake_instance_maps(
        num=20, image_size=image_size, ellipse_height=20, ellipse_width=8
    )
    image = make_fake_image(instance_map.copy())
    regions = regionprops(instance_map)

    extractor = DeepPatchFeatureExtractor(
        patch_size=patch_size,
        batch_size=1,
        entity=entity,
        architecture="resnet34",
        fill_value=255,
        resize_size=224,
        threshold=threshold,
        extraction_layer=extraction_layer,
    )
    features = extractor.process(image, instance_map)

    if threshold == 0:
        assert features.shape[0] == len(regions)
    else:
        assert features.shape[0] <= len(regions)


@requires_torchvision
@pytest.mark.parametrize("patch_size", [64, 128])
@pytest.mark.parametrize("entity", ["cell", "tissue"])
@pytest.mark.parametrize("threshold", [0.8])
@pytest.mark.parametrize("extraction_layer", [None, "12"])
def test_feature_extractor_torchvision_no_resnet(
    entity, patch_size, threshold, extraction_layer
):
    # pytest.importorskip("torchvision")

    image_size = (256, 256)

    instance_map = make_fake_instance_maps(
        num=20, image_size=image_size, ellipse_height=20, ellipse_width=8
    )
    image = make_fake_image(instance_map.copy())
    regions = regionprops(instance_map)

    extractor = DeepPatchFeatureExtractor(
        patch_size=patch_size,
        batch_size=1,
        entity=entity,
        architecture="mobilenet_v3_small",
        fill_value=255,
        resize_size=224,
        threshold=threshold,
        extraction_layer=extraction_layer,
    )
    features = extractor.process(image, instance_map)

    if threshold == 0:
        assert features.shape[0] == len(regions)
    else:
        assert features.shape[0] <= len(regions)