File size: 4,718 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 |
"""
Copyright 2021, Dana-Farber Cancer Institute and Weill Cornell Medicine
License: GNU GPL 2.0
"""
import numpy as np
import pytest
import torch
from skimage.draw import ellipse
from skimage.measure import label
from torch_geometric.loader import DataLoader
import pathml
from pathml.core import SlideData
from pathml.graph import Graph, HACTPairData, build_assignment_matrix
from pathml.graph.utils import get_full_instance_map
from pathml.preprocessing import Pipeline
from pathml.preprocessing.transforms import Transform
@pytest.mark.parametrize("batch_size", [1, 8, 32])
@pytest.mark.parametrize("include_target", [True, False])
def test_pathml_graph(batch_size, include_target):
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long)
node_centroids = torch.randn(3, 2)
node_features = torch.randn(3, 2)
if include_target:
target = torch.tensor([1])
graph_obj = Graph(
edge_index=edge_index,
node_centroids=node_centroids,
node_features=node_features,
target=target if include_target else None,
)
loader = DataLoader([graph_obj] * batch_size, batch_size=batch_size)
batch = next(iter(loader))
assert batch.node_centroids.shape == (batch_size * 3, 2)
assert batch.node_features.shape == (batch_size * 3, 2)
assert batch.edge_index.shape == (2, batch_size * 4)
assert batch.batch.shape == (batch_size * 3,)
@pytest.mark.parametrize("batch_size", [1, 8, 32])
def test_pathml_hactnet_graph(batch_size):
edge_index = torch.tensor([[0, 1, 1, 2], [1, 0, 2, 1]], dtype=torch.long)
node_features = torch.randn(3, 2)
x_cell = node_features
edge_index_cell = edge_index
x_tissue = node_features
edge_index_tissue = edge_index
assignment = edge_index
target = torch.tensor([2])
graph_obj = HACTPairData(
x_cell=x_cell,
edge_index_cell=edge_index_cell,
x_tissue=x_tissue,
edge_index_tissue=edge_index_tissue,
assignment=assignment,
target=target,
)
loader = DataLoader([graph_obj] * batch_size, batch_size=batch_size)
batch = next(iter(loader))
assert batch.x_cell.shape == (batch_size * 3, 2)
assert batch.x_tissue.shape == (batch_size * 3, 2)
assert batch.edge_index_cell.shape == (2, batch_size * 4)
assert batch.edge_index_tissue.shape == (2, batch_size * 4)
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
@pytest.mark.parametrize("matrix", [True, False])
def test_build_assignment_matrix(matrix):
image_size = (1024, 2048)
tissue_instance_map = make_fake_instance_maps(
num=20, image_size=image_size, ellipse_height=20, ellipse_width=8
)
cell_centroids = np.random.rand(200, 2)
assignment = build_assignment_matrix(
cell_centroids, tissue_instance_map, matrix=matrix
)
if matrix:
assert assignment.shape[0] == 200
else:
assert assignment.shape[1] == 200
class DummyTransform(Transform):
def __init__(
self,
mask_name,
):
self.mask_name = mask_name
def F(self, image):
return image[:, :, 0]
def apply(self, tile):
assert isinstance(
tile, pathml.core.tile.Tile
), f"tile is type {type(tile)} but must be pathml.core.tile.Tile"
nucleus_mask = self.F(tile.image)
tile.masks[self.mask_name] = nucleus_mask
@pytest.mark.parametrize("mask_name", ["test"])
def test_instance_map(mask_name):
image_path = "tests/testdata/small_HE.svs"
wsi = SlideData(image_path, name=image_path, backend="openslide", stain="HE")
pipeline = Pipeline([DummyTransform(mask_name)])
wsi.run(
pipeline,
overwrite_existing_tiles=True,
distributed=False,
tile_pad=True,
tile_size=1024,
)
image_norm, label_instance_map, instance_centroids = get_full_instance_map(
wsi, patch_size=1024, mask_name="test"
)
assert image_norm.shape == (wsi.shape[0], wsi.shape[1], 3)
assert label_instance_map.shape == (wsi.shape[0], wsi.shape[1])
assert instance_centroids.shape[1] == 2
|