-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSVLoader.py
More file actions
150 lines (122 loc) · 4.36 KB
/
CSVLoader.py
File metadata and controls
150 lines (122 loc) · 4.36 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
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
import csv
import enum
import os
from os import listdir
from os.path import isfile, join
import re
class Labels(enum.Enum):
MASS_MALIGN = 0
MASS_BELIGN = 1
CALC_MALIGN = 2
CALC_BELIGN = 3
class CSVLoader:
def __init__(self, absolute_path):
self.REBUILD_CSV = False
self.absolute_path = absolute_path
def loadCSV(self, path):
if self.REBUILD_CSV:
with open(path) as file:
csvreader = csv.reader(file)
# create modified csv file
name = self.create_modified_file_name(path)
f = open(name, 'w', newline='')
writer = csv.writer(f)
header = ['CATEGORY', 'IMG_PATH']
writer.writerow(header)
header = next(csvreader)
line_count = 1
for row in csvreader:
# print(f'{", ".join(row)}')
cropped_image = row[12]
roi_mask = row[13]
line_count += 1
img_path = self.get_img_path(cropped_image, roi_mask)
category = row[9]
category = self.get_category(category, path)
new_row = [category, img_path]
writer.writerow(new_row)
print(f'Processed {line_count} lines.')
else:
print("Creation of new CSV file skipped")
@staticmethod
def create_modified_file_name(original_file):
list = original_file.split('.')
name = list[0] + "_for_training"
new_name = name + ".csv"
return new_name
@staticmethod
def get_category(category, path):
MASS = -1
CALC = -2
if bool(re.search('mass', path)):
pre_category = MASS
else:
pre_category = CALC
if pre_category == MASS:
if category.startswith('M'):
category = Labels.MASS_MALIGN.value
else:
category = Labels.MASS_BELIGN.value
else:
if category.startswith('M'):
category = Labels.CALC_MALIGN.value
else:
category = Labels.CALC_BELIGN.value
return category
def get_img_path(self, cropped_img_path, roi_mask_path):
img_path = self.fix_path(cropped_img_path)
mask_path = self.fix_path(roi_mask_path)
if img_path != mask_path:
# file 1
files = [f for f in listdir(img_path) if isfile(join(img_path, f))]
if len(files) != 1:
print("PANIKA!!!!")
os._exit(-1)
files = self.dcom_absolute_paths(img_path, files)
file_1 = files[0]
# file 2
files = [f for f in listdir(mask_path) if isfile(join(mask_path, f))]
if len(files) != 1:
print("PANIKA!!!!")
os._exit(-1)
files = self.dcom_absolute_paths(mask_path, files)
file_2 = files[0]
mask_path = self.get_bigger_file((file_1, file_2))
img_path = self.get_smaller_file((file_1, file_2))
else:
files = [f for f in listdir(img_path) if isfile(join(img_path, f))]
if len(files) != 2:
print("PANIKA!!!!")
os._exit(-1)
files = self.dcom_absolute_paths(img_path, files)
mask_path = self.get_bigger_file(files)
img_path = self.get_smaller_file(files)
return img_path
@staticmethod
def dcom_absolute_paths(directory, files):
file_paths = []
for file in files:
file = directory + "\\" + file
file_paths.append(file)
return file_paths
@staticmethod
def get_bigger_file(files):
file_1_size = os.path.getsize(files[0])
file_2_size = os.path.getsize(files[1])
if file_1_size > file_2_size:
return files[0]
else:
return files[1]
@staticmethod
def get_smaller_file(files):
file_1_size = os.path.getsize(files[0])
file_2_size = os.path.getsize(files[1])
if file_1_size > file_2_size:
return files[1]
else:
return files[0]
def fix_path(self, path):
path_list = path.split('/')
path_list.pop()
fixed_path = self.absolute_path + '\\'.join(path_list)
return fixed_path