forked from dguari1/Auto-eFace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save_window.py
470 lines (321 loc) · 18.4 KB
/
save_window.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 2 10:33:30 2017
@author: Diego L.Guarin -- diego_guarin at meei.harvard.edu
"""
import os
import numpy as np
import pandas as pd
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtWidgets import QLabel, QLineEdit, QPushButton, QGridLayout, QFileDialog, QDialog, QComboBox, QGroupBox
"""
This window will ask for some additional information and then save the facial measurements in a xls document.
It will also save a txt file with the position of landmarks and eyes
it has an additional functionality where you can add results to an existing xls document by adding and additional row with the information of the current photo
"""
class MyLineEdit(QLineEdit):
#I created a custom LineEdit object that will clear its content when selected
#is used for the Patient ID which is initialized by default to the current date
def __init__(self, parent=None):
super(MyLineEdit, self).__init__(parent)
def mousePressEvent(self, event):
self.clear()
class SaveWindow(QDialog):
def __init__(self, parent=None, file_name = None, expression='', MeasurementsLeft = None, MeasurementsRight = None, MeasurementsDeviation = None, MeasurementsPercentual = None):
super(SaveWindow, self).__init__(parent)
self._NewFile = True #This variable defines if the user is
#trying to save results in a new file or to a
#append results to an existing file
self._name_of_file = file_name #this variable stores the name of the
#file, it won't be modified during
#execution
self._file_name = file_name #this variable stores the name of the
#file to be displayed. It will be
#modified during execution
self.parent = parent
filename, file_extension = os.path.splitext(self._file_name)
delimiter = os.path.sep
temp=filename.split(delimiter)
photo_location = temp[0:-1]
photo_location = delimiter.join(photo_location)
photo_name=temp[-1]
#measurements
self._MeasurementsLeft = MeasurementsLeft
self._MeasurementsRight = MeasurementsRight
self._MeasurementsDeviation = MeasurementsDeviation
self._MeasurementsPercentual = MeasurementsPercentual
self._file_name = photo_name #path + file name
self._photo_location = photo_location
self._ID = photo_name #unique identifier
self._prevspost = '' #pre-treatment vs post-treatment
self._surgery = '' #type of surgery
self._expression = expression #type of expression
self._other = '' #additional comments
self._file_to_save = '' #file to add data
self._acceptSave = True #variable that indicates if the user will save results or not
self.initUI()
def initUI(self):
self.setWindowTitle('Save')
scriptDir = os.getcwd()#os.path.dirname(os.path.realpath(__file__))
self.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'include' +os.path.sep +'icon_color'+ os.path.sep + 'save_icon.ico'))
self.main_Widget = QtWidgets.QWidget(self)
spacerh = QtWidgets.QWidget(self)
spacerh.setFixedSize(10,0)
spacerv = QtWidgets.QWidget(self)
spacerv.setFixedSize(0,10)
file = QLabel('File Name:')
self._file = QLineEdit(self)
self._file.setText(self._file_name)
self.SelectFolderButton = QPushButton('Select &Folder', self)
self.SelectFolderButton.setFixedWidth(150)
self.SelectFolderButton.clicked.connect(self.SelectFolder)
self._SelectFolder = QLineEdit(self)
self._SelectFolder.setText(self._photo_location)
self._SelectFolder.setFixedWidth(350)
NewFileBox = QGroupBox('Create new File')
NewFileBoxLayout = QGridLayout()
NewFileBoxLayout.addWidget(file,0,0)
NewFileBoxLayout.addWidget(spacerh,0,1)
NewFileBoxLayout.addWidget(self._file,0,2)
NewFileBoxLayout.addWidget(spacerv,1,0)
NewFileBoxLayout.addWidget(self.SelectFolderButton,2,0)
NewFileBoxLayout.addWidget(self._SelectFolder,2,2)
NewFileBox.setLayout(NewFileBoxLayout)
SelectFileButton = QPushButton('&Select File', self)
SelectFileButton.setFixedWidth(150)
SelectFileButton.clicked.connect(self.SelectFile)
self._SelectFile = QLineEdit(self)
self._SelectFile.setText(self._file_to_save)
AppendFileBox = QGroupBox('Append to Existing File')
AppendFileBoxLayout = QGridLayout()
AppendFileBoxLayout.addWidget(SelectFileButton,0,0)
AppendFileBoxLayout.addWidget(spacerh,0,1)
AppendFileBoxLayout.addWidget(self._SelectFile,0,2)
AppendFileBox.setLayout(AppendFileBoxLayout)
Identifier = QLabel('Photo Identifier:')
Identifier.setFixedWidth(120)
self._Identifier = QLineEdit(self)
self._Identifier.setText(self._ID)
PrevsPost = QLabel('Pre or Post Procedure:')
self._PrevsPost = QComboBox()
self._PrevsPost.setFixedWidth(200)
self._PrevsPost.addItem('')
self._PrevsPost.addItem('Pre - Procedure')
self._PrevsPost.addItem('Post - Procedure')
SurgeryType = QLabel('Procedure:')
self._SurgeryType = QLineEdit(self)
self._SurgeryType.setText(self._surgery)
ExpressionType = QLabel('Expression:')
self._ExpressionType = QLineEdit(self)
self._ExpressionType.setText(self._expression)
AddtitionalComments = QLabel('Addtitional Comments:')
self._AddtitionalComments = QLineEdit(self)
self._AddtitionalComments.setText(self._other)
AdditionalInformationBox = QGroupBox('Optional Information')
AdditionalInformationBoxLayout = QGridLayout()
AdditionalInformationBoxLayout.addWidget(Identifier,0,0)
AdditionalInformationBoxLayout.addWidget(spacerh,0,1)
AdditionalInformationBoxLayout.addWidget(self._Identifier,0,2)
AdditionalInformationBoxLayout.addWidget(spacerv,1,0)
AdditionalInformationBoxLayout.addWidget(PrevsPost,2,0)
AdditionalInformationBoxLayout.addWidget(self._PrevsPost,2,2)
#AdditionalInformationBoxLayout.addWidget(spacerv,3,0)
AdditionalInformationBoxLayout.addWidget(SurgeryType,4,0)
AdditionalInformationBoxLayout.addWidget(self._SurgeryType,4,2)
#AdditionalInformationBoxLayout.addWidget(spacerv,5,0)
AdditionalInformationBoxLayout.addWidget(ExpressionType,6,0)
AdditionalInformationBoxLayout.addWidget(self._ExpressionType,6,2)
#AdditionalInformationBoxLayout.addWidget(spacerv,7,0)
AdditionalInformationBoxLayout.addWidget(AddtitionalComments,8,0)
AdditionalInformationBoxLayout.addWidget(self._AddtitionalComments,8,2)
AdditionalInformationBox.setLayout(AdditionalInformationBoxLayout)
SaveButton = QPushButton('&Save', self)
SaveButton.setFixedWidth(150)
SaveButton.clicked.connect(self.Save)
CancelButton = QPushButton('&Cancel', self)
CancelButton.setFixedWidth(150)
CancelButton.clicked.connect(self.Cancel)
ButtonBox = QGroupBox('')
ButtonBoxLayout = QGridLayout()
ButtonBoxLayout.addWidget(SaveButton,0,0,QtCore.Qt.AlignCenter)
ButtonBoxLayout.addWidget(spacerh,0,1)
ButtonBoxLayout.addWidget(CancelButton,0,2,QtCore.Qt.AlignCenter)
ButtonBox.setLayout(ButtonBoxLayout)
ButtonBox.setStyleSheet("QGroupBox { border: 0px solid gray;}");
layout = QGridLayout()
layout.addWidget(NewFileBox,0,0,2,2)
layout.addWidget(spacerv,1,0)
layout.addWidget(AppendFileBox,2,0,1,2)
layout.addWidget(AdditionalInformationBox,4,0,8,2)
layout.addWidget(ButtonBox,17,0,1,2)
self.setLayout(layout)
#self.show()
def Cancel(self):
self._acceptSave = False
self.close()
def SelectFolder(self):
name = QtWidgets.QFileDialog.getExistingDirectory(self, 'Select directory')
if not name:
pass
else:
if not self._SelectFolder.isEnabled():
self._file.setEnabled(True)
self._SelectFolder.setEnabled(True)
self._SelectFile.setText('')
filename, file_extension = os.path.splitext(self._name_of_file)
delimiter = os.path.sep
temp=filename.split(delimiter)
photo_name=temp[-1]
self._file.setText(photo_name)
name = os.path.normpath(name)
self._photo_location = name
self._SelectFolder.setText(self._photo_location)
self._NewFile = True #a new file will be created
self.update()
def SelectFile(self):
name,_ = QtWidgets.QFileDialog.getOpenFileName(
self,'Load File',
'',"Excel Spreadsheet (*.xls *.xlsx)")
if not name:
pass
else:
name = os.path.normpath(name)
delimiter = os.path.sep
filename, file_extension = os.path.splitext(name)
temp=filename.split(delimiter)
photo_location = temp[0:-1]
photo_location = delimiter.join(photo_location)
photo_name=temp[-1]
self._file_name = photo_name
self._file.setText(self._file_name)
self._file.setEnabled(False)
self._photo_location = photo_location
self._SelectFolder.setText(self._photo_location)
self._SelectFolder.setEnabled(False)
#self.SelectFolderButton.setEnabled(False)
self._file_to_save = name
self._SelectFile.setText(self._file_to_save)
self._NewFile = False #data will be appended to an exisiting file
self.update()
def Save(self):
number_of_measurements = 9
Columns = ['Right','Left','Deviation (absolute)','Deviation (percent)']
Columns = Columns * number_of_measurements
Columns.insert(0,'')
Columns.insert(0,'')
Columns.insert(0,'')
Columns.insert(0,'')
Columns.append('')
temp = ['Brow Height', 'Marginal Reflex Distance 1', 'Marginal Reflex Distance 2',
'Commisure Excursion', 'Commisure Height Deviation', 'Smile Angle',
'Upper Lip Height Deviation', 'Dental Show', 'Lower Lip Height Deviation']
number_of_repetitions=4
Header = [item for item in temp for i in range(number_of_repetitions)]
Header.insert(0,'Expression')
Header.insert(0,'Procedure')
Header.insert(0,'Pre vs Post Procedure')
Header.insert(0,'Unique Identifier')
Header.append('Additional Comments')
#measurements
elements = ['BH', 'MRD1', 'MRD2', 'CE', 'CH', 'SA', 'UVH', 'DS', 'LVH']
MeasurementsLeft = self._MeasurementsLeft
MeasurementsRight = self._MeasurementsRight
MeasurementsDeviation = self._MeasurementsDeviation
MeasurementsPercentual = self._MeasurementsPercentual
BH = np.array([[MeasurementsRight.BrowHeight,MeasurementsLeft.BrowHeight,MeasurementsDeviation.BrowHeight,MeasurementsPercentual.BrowHeight]],dtype=object)
#BH=np.array([[1,1,1,1]],dtype = object)
MRD1 = np.array([[MeasurementsRight.MarginalReflexDistance1, MeasurementsLeft.MarginalReflexDistance1,MeasurementsDeviation.MarginalReflexDistance1,MeasurementsPercentual.MarginalReflexDistance1]], dtype=object)
#MRD1=np.array([[1,1,1,1]],dtype = object)
MRD2 = np.array([[MeasurementsRight.MarginalReflexDistance2, MeasurementsLeft.MarginalReflexDistance2,MeasurementsDeviation.MarginalReflexDistance2,MeasurementsPercentual.MarginalReflexDistance2]],dtype=object)
#MRD2=np.array([[1,1,1,1]],dtype = object)
CE = np.array([[MeasurementsRight.CommissureExcursion, MeasurementsLeft.CommissureExcursion,MeasurementsDeviation.CommissureExcursion,MeasurementsPercentual.CommissureExcursion]],dtype=object)
#CE=np.array([[1,1,1,1]],dtype = object)
CH = np.array([['', '',MeasurementsDeviation.CommisureHeightDeviation,'']],dtype=object)
#CH=np.array([[1,1,1,1]],dtype = object)
SA = np.array([[MeasurementsRight.SmileAngle, MeasurementsLeft.SmileAngle,MeasurementsDeviation.SmileAngle,MeasurementsPercentual.SmileAngle]],dtype=object)
#SA=np.array([[1,1,1,1]],dtype = object)
UVH = np.array([['', '',MeasurementsDeviation.UpperLipHeightDeviation,'']],dtype=object)
#UVH=np.array([[1,1,1,1]],dtype = object)
DS = np.array([[MeasurementsRight.DentalShow, MeasurementsLeft.DentalShow,MeasurementsDeviation.DentalShow,MeasurementsPercentual.DentalShow]],dtype=object)
#DS=np.array([[1,1,1,1]],dtype = object)
LVH = np.array([['', '',MeasurementsDeviation.LowerLipHeightDeviation,'']],dtype=object)
#LVH=np.array([[1,1,1,1]],dtype = object)
UI = np.array([[self._Identifier.text()]],dtype = object)
#UI = np.array([['uno']],dtype = object)
PvsP = np.array([[str(self._PrevsPost.currentText())]],dtype = object)
#PvsP = np.array([['dos']],dtype = object)
PC = np.array([[self._SurgeryType.text()]],dtype = object)
#PC = np.array([['tres']],dtype = object)
EX = np.array([[self._ExpressionType.text()]],dtype = object)
#EX = np.array([['cuatro']],dtype = object)
AD = np.array([[self._AddtitionalComments.text()]],dtype = object)
#AD = np.array([['cinco']],dtype = object)
fill= UI
fill= np.append(fill, PvsP, axis = 1)
fill= np.append(fill, PC, axis = 1)
fill= np.append(fill, EX, axis = 1)
for i in elements:
fill = np.append(fill, eval(i), axis = 1)
fill= np.append(fill, AD, axis = 1)
if self._NewFile: #the user wants to create a new file
filename, file_extension = os.path.splitext(self._name_of_file )
delimiter = os.path.sep
temp=filename.split(delimiter)
photo_name=temp[-1] + file_extension
file_no_ext = os.path.join(str(self._SelectFolder.text()),str(self._file.text()))
Index = [photo_name]
#create data frame using data
df = pd.DataFrame(fill, index = Index, columns = Columns)
df.columns = pd.MultiIndex.from_tuples(list(zip(Header,df.columns)))
#write data frame to an excel file
writer = pd.ExcelWriter(file_no_ext+'.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', index = True)
#adjust the size of each column to fit the text
size_list = [15,20,20,20,20,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,20]
worksheet = writer.sheets['Sheet1']
for k in range (0,42):
worksheet.set_column(k,k,size_list[k])
writer.save()
self.close()
else: #the user wants to appedn to an existing file
filename, file_extension = os.path.splitext(self._name_of_file )
delimiter = os.path.sep
temp=filename.split(delimiter)
photo_name=temp[-1] + file_extension
#create data frame with new ata
Index = [photo_name]
df = pd.DataFrame(fill, index = Index, columns = Columns)
df.columns = pd.MultiIndex.from_tuples(list(zip(Header,df.columns)))
#load data from file and arrange its columns to fit the template
old_df = pd.read_excel(str(self._SelectFile.text()), sheetname=0,header=[0, 1], index_col=0)
#verify if the loaded file is valid
if len(old_df.columns) is 41:
old_df.columns = pd.MultiIndex.from_tuples(df.columns)
#concatenate old and new data frame
Frames = [old_df, df]
resuls = pd.concat(Frames, axis=0)
#write results in selected file
writer = pd.ExcelWriter(str(self._SelectFile.text()), engine='xlsxwriter')
resuls.to_excel(writer, sheet_name='Sheet1', index = True)
#adjust the size of each column to fit the text
size_list = [15,20,20,20,20,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,10,10,18,18,20]
worksheet = writer.sheets['Sheet1']
for k in range (0,42):
worksheet.set_column(k,k,size_list[k])
writer.save()
self.close()
else:
QtWidgets.QMessageBox.warning(self,"Warning",
"Invalid File",
QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.NoButton)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
#if not QtWidgets.QApplication.instance():
# app = QtWidgets.QApplication(sys.argv)
#else:
# app = QtWidgets.QApplication.instance()
GUI = SaveWindow()
#GUI.show()
app.exec_()