-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_series.py
151 lines (114 loc) · 4.14 KB
/
sort_series.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import shutil
import numpy as np
import json
import pandas as pd
import time
from PIL import Image
from torch._C import dtype
from tqdm import tqdm
from pydicom import dcmread
from multiprocessing import Pool
data_dir = "data"
def z_coord(op_dir, image):
image_path = os.path.join(op_dir, f"ID_{image}.dcm")
ds = dcmread(image_path)
image_position = ds[0x20, 0x32].value
return image_position[-1]
def get_series_id(op_dir, u, pbar):
img_path = os.path.join(op_dir, f"ID_{u}.dcm")
ds = dcmread(img_path)
series_id = ds["SeriesInstanceUID"].value
# series_id = ds["StudyInstanceUID"].value
# series_id = ds["PatientID"].value
pbar.update(1)
return series_id
def sort_series(op_dir, series, pbar):
series_images = series[:, 0]
series_labels = series[:, 1]
series_target = series_labels.astype(int).any()
series_z = np.fromiter((z_coord(op_dir, im) for im in series_images), dtype=float)
sorted_series_idx = np.argsort(series_z)
sorted_series = series[sorted_series_idx]
pbar.update(1)
return {"series": sorted_series.tolist(), "target": str(series_target.astype(int))}
op = "train"
op_dir = os.path.join(data_dir, f"stage_2_{op}")
op_csv = os.path.join(data_dir, f"stage_2_{op}.csv")
op_df = pd.read_csv(op_csv)
img_ids = op_df["ID"].values.astype(str)
img_labels = op_df["Label"].values
any_rows = np.arange(5, len(op_df), 6)
any_ids = img_ids[any_rows]
any_labels = img_labels[any_rows]
print(f"There are {sum(any_labels)}/{len(any_labels)} images with hemorrhage.")
ids = np.fromiter((u.split("_")[1] for u in any_ids), dtype=any_ids.dtype)
# L = 20000
# ids = ids[:L]
# any_labels = any_labels[:L]
n = len(ids) // 10
l = list(range(0, len(ids), n))
def f(l):
_ids = ids[l : l + n] if l + n < len(ids) else ids[l:]
with tqdm(total=len(_ids)) as pbar:
return np.fromiter(
(get_series_id(op_dir, u, pbar) for u in _ids),
dtype=ids.dtype,
)
t0 = time.time()
p = Pool(len(l))
series_ids = p.map(f, l)
p.close()
p.join()
series_ids = np.concatenate(series_ids)
t = time.time()
print(f"Read all series ids in {np.around(t - t0, 4)} s")
zipped_labels = np.stack((ids, any_labels), axis=1)
u_series_ids, u_series_inverse, u_series_counts = np.unique(
series_ids, return_inverse=True, return_counts=True
)
n = len(u_series_ids) // 10
l = list(range(0, len(u_series_ids), n))
def g(l):
_ids = u_series_ids[l : l + n] if l + n < len(u_series_ids) else u_series_ids[l:]
with tqdm(total=len(_ids)) as pbar:
return {
s_id.split("_")[1]: sort_series(
op_dir, zipped_labels[u_series_inverse == l + i], pbar
)
for i, s_id in enumerate(_ids)
}
t0 = time.time()
p = Pool(len(l))
series_dictionaries = p.map(g, l)
p.close()
p.join()
series_dictionary = {}
for u in series_dictionaries:
series_dictionary = {**series_dictionary, **u}
t = time.time()
print(f"Sorted {len(series_dictionary)} unique series in {np.around(t - t0, 4)} s")
with open(os.path.join(data_dir, "raw_series.json"), "w", encoding="utf-8") as f:
json.dump(series_dictionary, f, ensure_ascii=False, indent=4)
# positive_series = []
# negative_series = []
# for series in series_dictionary:
# if series_dictionary[series]["target"]:
# positive_series.append(series)
# else:
# negative_series.append(series)
# print(
# f"There are {len(positive_series)} positive and {len(negative_series)} negative series"
# )
# keys = np.array(list(series_dictionary.keys()))
# split = 0.8
# total_len = len(keys)
# train_len = int(split * total_len)
# train_idx = np.random.choice(range(total_len), train_len, replace=False)
# val_idx = np.setxor1d(np.arange(total_len), train_idx)
# train_series = {key: series_dictionary[key] for key in keys[train_idx]}
# val_series = {key: series_dictionary[key] for key in keys[val_idx]}
# with open(os.path.join(data_dir, "train_series.json"), "w", encoding="utf-8") as f:
# json.dump(train_series, f, ensure_ascii=False, indent=4)
# with open(os.path.join(data_dir, "val_series.json"), "w", encoding="utf-8") as f:
# json.dump(val_series, f, ensure_ascii=False, indent=4)