-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_qt.py
173 lines (146 loc) · 6.62 KB
/
main_qt.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'''
Open WinPython Command Prompt.exe
cd Scripts\dicom2mhd\
python setup.py build
'''
from anonymizeImage import anonymizeImage
from anonymizeImage import readImage
from anonymizeImage import saveImage
from convertTagFiles import readTagFiles
from convertTagFiles import writeFcsv
from convertDicom2Mhd import readDicom
from convertDicom2Mhd import writeMhdRaw
import os
import sys
from PyQt4 import QtGui, uic
import logging
import traceback
def generateOutFolder(root, folderPathIn, folderPathOut):
relDir = os.path.relpath(root, folderPathIn)
tempFolderPathOut = os.path.join(folderPathOut,relDir)
if not os.path.exists(tempFolderPathOut):
os.makedirs(tempFolderPathOut)
return tempFolderPathOut
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
form_class = uic.loadUiType("convertData.ui")[0] # Load the UI
class ConvertData(QtGui.QMainWindow, form_class):
def __init__(self, parent=None):
logging.getLogger('Convert data')
#logging.basicConfig(filename='convertData.log',level=logging.DEBUG)
self.logger = logging.getLogger('Convert data')
self.logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('convertData.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
self.logger.addHandler(fh)
self.logger.info("Program started")
QtGui.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.setWindowTitle('Convert data')
self.setFixedSize(self.width(), self.height())
self.pushButton.clicked.connect(self.select_input)
self.pushButton_2.clicked.connect(self.select_output)
self.pushButton_3.clicked.connect(self.select_convert)
self.pushButton_4.clicked.connect(self.select_cancel)
self.label.setText("")
self.label_2.setText("")
self.label_5.setText("")
self.folderPathOut = ''
self.folderPathIn = ''
self.cancel = False
def select_input(self):
self.logger.info("input")
self.folderPathIn = QtGui.QFileDialog.getExistingDirectory(None, 'Select input folder:', self.folderPathIn)
if len(self.folderPathIn) < 33:
self.label_2.setText(self.folderPathIn)
else:
self.label_2.setText("..."+self.folderPathIn[-30:])
self.logger.info(self.folderPathIn)
def select_output(self):
self.logger.info("output")
self.folderPathOut = QtGui.QFileDialog.getExistingDirectory(None, 'Select a folder to store data:',self.folderPathIn, QtGui.QFileDialog.ShowDirsOnly)
if len(self.folderPathOut) < 33:
self.label_5.setText(self.folderPathOut)
else:
self.label_5.setText("..."+self.folderPathOut[-30:])
self.logger.info(self.folderPathOut)
def select_convert(self):
self.logger.info("convert")
if len(self.folderPathIn)>0 and len(self.folderPathOut)>0:
self.cancel = False
self.pushButton_3.setEnabled(False)
try:
self.convertFolder(str(self.folderPathIn), str(self.folderPathOut))
except Exception, e:
logging.exception(e)
self.label.setText("Failed: " + e.message)
self.logger.exception(e)
self.pushButton_3.setEnabled(True)
def select_cancel(self):
self.logger.info("cancel")
self.cancel=True
def convertFolder(self, folderPathIn, folderPathOut):
errors = False
for root, dirs, files in os.walk(folderPathIn):
tagData=[]
for file_i in files:
filePath = os.path.join(root, file_i)
if self.cancel:
self.cancel = False
self.label.setText("Canceled")
self.logger.info("Canceled")
return
self.label.setText("Processing file: " + os.path.basename(filePath))
app.processEvents()
tag = readTagFiles(filePath)
if tag:
self.logger.info("Tag file: " + os.path.basename(filePath))
tagData.append(tag)
continue
dicomData = readDicom(filePath)
if dicomData:
self.logger.info("Dicom file: " + os.path.basename(filePath))
out = rreplace(root,"Dicom","Data",1)
out = rreplace(out,"DICOM","Data",1)
tempFolderPathOut = generateOutFolder(out, folderPathIn, folderPathOut)
try:
writeMhdRaw(tempFolderPathOut, dicomData)
except Exception as e:
self.label.setText(str(e))
self.logger.error("File: "+ filePath)
self.logger.error(traceback.format_exc())
errors = True
continue
imData = readImage(filePath)
if imData:
self.logger.info("Image file: " + os.path.basename(filePath))
tempFolderPathOut = generateOutFolder(root, folderPathIn, folderPathOut)
filePathOut = os.path.join(tempFolderPathOut, os.path.basename(filePath))
try:
imDataOut = anonymizeImage(imData, pixelsX=range(230, 1530), pixelsY=range(7, 43), color=(190, 192, 194))
saveImage(filePathOut, imDataOut)
except Exception as e:
self.label.setText(str(e))
self.logger.error("File: "+ filePath)
self.logger.error(traceback.format_exc())
errors = True
continue
if tagData:
tempFolderPathOut = generateOutFolder(root, folderPathIn, folderPathOut)
filePathOut = os.path.join(tempFolderPathOut, 'Tags_'+str(1)+'.fcsv')
k=1
while os.path.exists(filePathOut):
k = k+1
filePathOut = os.path.join(tempFolderPathOut, 'Tags_'+str(k)+'.fcsv')
writeFcsv(filePathOut, tagData)
self.label.setText("Finished")
self.logger.info("Finished")
if errors:
self.label.setText("Finished with error. Check log!!")
app = QtGui.QApplication(sys.argv)
myWindow = ConvertData(None)
myWindow.show()
app.exec_()