-
Notifications
You must be signed in to change notification settings - Fork 39
/
prepare_spineweb.py
130 lines (105 loc) · 5.11 KB
/
prepare_spineweb.py
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
import os
import os.path as path
import yaml
import torch
import numpy as np
import SimpleITK as sitk
import random
import shutil
from tqdm import tqdm
from PIL import Image
from adn.utils import read_dir, get_connected_components
from collections import defaultdict
from torchvision.utils import make_grid
def make_thumbnails(images):
images = torch.tensor(np.array(images).astype(float))[:, np.newaxis, ...]
images = (images - images.min()) / (images.max() - images.min())
num_rows = int(len(images) ** 0.5)
image = make_grid(
images, nrow=images.shape[0] // num_rows, normalize=False)
image = image.numpy().transpose(1, 2, 0)
image = (image * 255).astype(np.uint8)
return image
if __name__ == "__main__":
config_file = "config/dataset.yaml"
with open(config_file) as f:
config = yaml.load(f, Loader=yaml.FullLoader)['spineweb']
patient_dirs = read_dir(
config['raw_dir'], predicate=lambda x: "patient" in x, recursive=True)
image_size = config['image_size']
if type(image_size) is not list: image_size = [image_size] * 2
thumbnail_size = config['thumbnail_size']
if type(thumbnail_size) is not list: thumbnail_size = [thumbnail_size] * 2
for patient_dir in tqdm(patient_dirs):
patient_name = path.basename(patient_dir)
volume_files = read_dir(patient_dir,
predicate=lambda x: x.endswith("mhd") or x.endswith("nii.gz"), recursive=True)
for volume_file in volume_files:
volume_obj = sitk.ReadImage(volume_file)
volume = sitk.GetArrayFromImage(volume_obj)
volume_name = path.basename(volume_file).split(".")[0]
thumbnails = defaultdict(list)
index = 0
for image in tqdm(volume,
desc="Preparing {}_{}".format(patient_name, volume_name)):
image_type = "no_artifact"
# Check if the image has metal artifacts
if image.max() > config["max_hu"][1]:
points = np.array(np.where(image > config["max_hu"][1])).T
points = set(tuple(p) for p in points)
components = get_connected_components(points)
max_area = max(len(c) for c in components)
if max_area > config["connected_area"]: image_type = "artifact"
else: continue
elif image.max() > config["max_hu"][0]: continue
output_dir = path.join(config["dataset_dir"], image_type,
"{}_{}".format(patient_name, volume_name))
if not path.isdir(output_dir): os.makedirs(output_dir)
image = Image.fromarray(image).resize(image_size)
image = np.array(image)
thumbnail = (image - image.min()) / (image.max() - image.min())
thumbnail = (thumbnail * 255).astype(np.uint8)
thumbnails[image_type].append(
np.array(Image.fromarray(image).resize(thumbnail_size)))
image_name = "{}_{}_{:03d}".format(patient_name, volume_name, index)
image_file = path.join(output_dir, image_name + ".npy")
thumbnail_file = path.join(output_dir, image_name + ".png")
np.save(image_file, image)
Image.fromarray(thumbnail).save(thumbnail_file)
index += 1
# Create an overview of images from this patient
for k, ts in thumbnails.items():
output_dir = path.join(
config["dataset_dir"], k, "{}_{}".format(patient_name, volume_name))
if len(ts) > 0:
thumbnails_file = path.join(config["dataset_dir"], k, "{}_{}.png".format(patient_name, volume_name))
Image.fromarray(make_thumbnails(ts)).save(thumbnails_file)
else: os.removedirs(output_dir)
# Create train and test split
artifact_dir = path.join(config["dataset_dir"], "artifact")
patient_dirs = read_dir(artifact_dir, "dir")
random.shuffle(patient_dirs)
test_patients = []
test_cnt = 0
index = 0
while index < len(patient_dirs) and test_cnt < config["num_tests"]:
num_images = len(read_dir(patient_dirs[index], "file"))
if num_images < 100:
test_patients.append(path.basename(patient_dirs[index]))
test_cnt += num_images
index += 1
no_artifact_dir = path.join(config["dataset_dir"], "no_artifact")
items = read_dir(artifact_dir) + read_dir(no_artifact_dir)
test_dir = path.join(config["dataset_dir"], "test")
train_dir = path.join(config["dataset_dir"], "train")
if not path.isdir(test_dir): os.makedirs(test_dir)
if not path.isdir(train_dir): os.makedirs(train_dir)
for item in items:
item_type, item_name = item.split(path.sep)[-2:]
patient_name = path.splitext(item_name)[0]
if patient_name in test_patients:
shutil.move(item, path.join(test_dir, item_type, item_name))
else:
shutil.move(item, path.join(train_dir, item_type, item_name))
shutil.rmtree(artifact_dir)
shutil.rmtree(no_artifact_dir)