-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
111 lines (103 loc) · 4.5 KB
/
main.py
File metadata and controls
111 lines (103 loc) · 4.5 KB
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
from csvloader import CSVFile
from dcmImage import DicomImage
from preprocessing import Preprocessing
import cnn
from pathlib import Path
import time
import numpy as np
import matplotlib.pyplot as plt
def preprocess_dicom(folder_path, csv_filename, types, save_location, count, display=False):
csv = CSVFile()
dcm = DicomImage
pp = Preprocessing()
data_frame = csv.load_CSV(folder_path, csv_filename + ".csv", types, count)
ids_w_label = csv.img_ids_w_labels(data_frame, types)
ids_list = list(ids_w_label.keys())
print(ids_list)
for i in range(len(ids_list)):
dicom_image = dcm.load_dicom(folder_path, ids_list[i]+".dcm")
img_array = dcm.scale_to_hu(dicom_image)
img_array, mask = pp.make_mask(img_array)
img_array = pp.crop(img_array, mask)
# may be able to get away with 360, 360
img_array = pp.resize(img_array, (480, 480))
img_ch3_array = pp.channel_split(img_array)
img_array = img_array.astype(np.int16)
cnn.save_numpy_as_png(img_array, ids_list[i]+"_c1", save_location)
cnn.save_numpy_as_png(img_ch3_array, ids_list[i]+"_c3", save_location)
if i % 500 == 0:
print(i*10, "Images have been processed")
if display:
fig, ax = plt.subplots(2, 2, figsize=[12, 6])
ax[0, 0].set_title("Full Image")
ax[0, 0].imshow(img_array, cmap='gray')
ax[0, 0].axis('off')
ax[0, 1].set_title("Channeled Image")
ax[0, 1].imshow(img_ch3_array, cmap='gray')
ax[0, 1].axis('off')
ax[1, 0].hist(img_array.flatten(), bins=200, range=(-170, 75))
ax[1, 0].set_yscale("log")
ax[1, 0].set_ylim(bottom=10, top=10**4)
ax[1, 1].hist(img_ch3_array[:, :, 0].flatten(), bins=200,
range=(img_ch3_array[:, :, 0].min(), img_ch3_array[:, :, 0].max()))
ax[1, 1].set_yscale('log')
ax[1, 1].set_ylim(bottom=10, top=10**4)
plt.tight_layout()
plt.show()
print("Run has ended")
def preprocess_dicom_from_masterlist(data_path, types, save_location, display=False):
csv = CSVFile()
dcm = DicomImage
pp = Preprocessing()
homePath = Path.cwd()
train_data = csv.load_CSV_for_mod(homePath, "stage_2_train.csv", types, pos_id=True, only_neg=False)
ids_w_label = csv.img_ids_w_labels(train_data, types)
ids_list = list(ids_w_label.keys())
print(ids_list)
for i in range(len(ids_list)):
dicom_image = dcm.load_dicom(data_path, ids_list[i]+".dcm")
img_array = dcm.scale_to_hu(dicom_image)
img_array, mask = pp.make_mask(img_array)
img_array = pp.crop(img_array, mask)
# may be able to get away with 360, 360
img_array = pp.resize(img_array, (480, 480))
img_ch3_array = pp.channel_split(img_array)
img_array = img_array.astype(np.int16)
cnn.save_numpy_as_png(img_array, ids_list[i]+"_c1", save_location)
cnn.save_numpy_as_png(img_ch3_array, ids_list[i]+"_c3", save_location)
if i % 500 == 0:
print(i, "Images have been processed")
if display:
fig, ax = plt.subplots(2, 2, figsize=[12, 6])
ax[0, 0].set_title("Full Image")
ax[0, 0].imshow(img_array, cmap='gray')
ax[0, 0].axis('off')
ax[0, 1].set_title("Channeled Image")
ax[0, 1].imshow(img_ch3_array, cmap='gray')
ax[0, 1].axis('off')
ax[1, 0].hist(img_array.flatten(), bins=200, range=(-170, 75))
ax[1, 0].set_yscale("log")
ax[1, 0].set_ylim(bottom=10, top=10**4)
ax[1, 1].hist(img_ch3_array[:, :, 0].flatten(), bins=200,
range=(img_ch3_array[:, :, 0].min(), img_ch3_array[:, :, 0].max()))
ax[1, 1].set_yscale('log')
ax[1, 1].set_ylim(bottom=10, top=10**4)
plt.tight_layout()
plt.show()
print("Run has ended")
if __name__ == '__main__':
start_time = time.time()
type_list = ['intraparenchymal']
data_location = Path('E:/', 'rsna-intracranial-hemorrhage-detection', 'rsna-intracranial-hemorrhage-detection', 'stage_2_train')
save_location = Path('C:/', 'Users', 'ryanb', 'Desktop', 'ECE 431 Project', 'intraparenchymal_png')
preprocess_dicom_from_masterlist(data_location, type_list, save_location)
print("Preprocessing Dicom Images Run Time:", time.time() - start_time, "Seconds")
# ***************************** For already divided data ******************************
# start_time = time.time()
# count = 1 # multiplied by a factor of 10
# data_location = Path('C:/', 'Users', 'ryanb', 'Desktop', 'ECE 431 Project', 'intraparenchymal')
# save_location = Path('C:/', 'Users', 'ryanb', 'Desktop', 'ECE 431 Project', 'intraparenchymal_png')
# data_csv_filename = 'intraparenchymal'
# types = ["intraparenchymal"]
# preprocess_dicom(data_location, data_csv_filename, types, save_location, count)
# print("Preprocessing Dicom Images Run Time:", time.time() - start_time, "Seconds")