-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot_main.pyw
255 lines (207 loc) · 7.73 KB
/
plot_main.pyw
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
import sys
import os
from plot_lib.plot_interface import *
import subprocess
class Interface(QtWidgets.QMainWindow):
def __init__(self, parent=None):
# Qt
QtWidgets.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setWindowIcon(QtGui.QIcon('assets/logo_rthy.png'))
self.expand=False
self.show()
# Init and variables
self.path_text = 'Select file...'
self.route = self.path_text
self.filename = ''
self.filenotes = ''
self.fileday = ''
self.day_route = None
# Buttons conectors
self.ui.pushButton_plot.clicked.connect(self.pyplot_call)
self.ui.pushButton_selectfile.clicked.connect(self.file_explorer)
self.ui.pushButton_selectlast.clicked.connect(self.get_last)
self.ui.pushButton_savenotes.clicked.connect(self.save_notes)
self.ui.pushButton_saveday.clicked.connect(self.save_day)
self.ui.pushButton_reload.clicked.connect(self.day_resume_reload)
self.ui.frecuency.valueChanged.connect(self.cal_time)
self.ui.edit_mode.clicked.connect(self.editable_on)
self.ui.push_resize.clicked.connect(self.size)
#########################
# Plot program call #
#########################
def pyplot_call(self):
self.route = self.ui.textBrowser_experiment.toPlainText()
error_plot='None'
program = 'python3 plot_lib/plot.py'
# File to plot
program += ' -f ' + self.route
if self.route == self.path_text:
error_plot='You have to select a file'
self.ui.textBrowser_experiment.setPlainText( self.path_text )
# Jump points
program += ' -j ' + str( self.ui.JumpPointsValue.value() )
# Frecuency
program += ' -fr ' + str ( self.ui.frecuency.value() )
# Start and end poinst
if self.ui.PlotAll.isChecked() == False:
s = self.ui.spinBox_from_m.value()*60 + self.ui.spinBox_from_s.value()
e = self.ui.spinBox_to_m.value()*60 + self.ui.spinBox_to_s.value()
if e==0:
program += ' -s ' + str(s)
elif s<e:
program += ' -s ' + str(s)
program += ' -e ' + str(e)
else:
error_plot='End time must be greater than start time'
# Latency
if self.ui.latency_on.isChecked() == True:
program += ' -lat 1'
# Drift
program += ' -d 0 '
# Errors
if error_plot != 'None':
QtWidgets.QMessageBox.warning(self, "Can't plot", error_plot)
# Plot
else:
print(program)
pid=os.fork()
if pid==0: # new process
os.system(program)
exit()
##############################
# Funcs for custom actions #
##############################
def file_explorer(self):
self.route = self.ui.textBrowser_experiment.toPlainText()
if self.route.find('s_data.txt') != -1:
# We want to stay in the same day
result = self.route.split('/')[-2]
self.route = QtWidgets.QFileDialog.getOpenFileName(self, 'Select file', 'data/'+result, 'Text files (*_data.txt);;All files (*)')[0]
else:
# No day choose yet
self.route = QtWidgets.QFileDialog.getOpenFileName(self, 'Select file', 'data', 'Text files (*_data.txt);;All files (*)')[0]
if self.route != '':
self.filename = self.route.split('/')[-2] +'/'+ self.route.split('/')[-1]
self.fileday = '/'.join ( self.route.split('/')[:-1] ) + '/log.txt'
else:
self.filename = ''
self.fileday = ''
self.init_experiment()
def get_last(self):
# Get last day
folders = [f for f in os.listdir('data') if os.path.dirname(os.path.join('data', f))]
folders.sort(key=lambda x: os.path.getmtime('data/'+x))
# No days
if len(folders)==0:
self.ui.pushButton_savenotes.setDisabled(True)
self.ui.pushButton_savenotes.setStyleSheet("")
self.route = self.path_text
self.fileday = ''
return
# Get las file of the day
onlyfiles = [f for f in os.listdir('data/'+folders[-1]) if os.path.isfile(os.path.join('data/'+folders[-1], f)) and f.find('_data.txt')!=-1]
onlyfiles.sort(key=lambda x: os.path.getmtime('data/'+folders[-1]+'/'+x))
self.filename = folders[-1]+'/'+onlyfiles[-1]
self.route = os.getcwd()+"/data/"+folders[-1]+'/'+onlyfiles[-1]
self.fileday = os.getcwd()+"/data/"+folders[-1]+'/log.txt'
self.init_experiment()
def init_experiment(self):
if self.route=='' or self.route==self.path_text:
# Bad
self.ui.textBrowser_experiment.setPlainText(self.path_text)
self.route = self.path_text
self.ui.pushButton_plot.setDisabled(True)
self.ui.pushButton_plot.setStyleSheet("")
# Notes
self.ui.pushButton_savenotes.setDisabled(True)
self.ui.pushButton_savenotes.setStyleSheet("")
self.ui.textEdit_notes.setDisabled(True)
self.ui.textEdit_notes.setStyleSheet("background-color: rgb(235, 235, 235);")
self.filenotes = ''
self.ui.textEdit_notes.setPlainText('')
else:
# Good
self.ui.textBrowser_experiment.setPlainText(self.route)
self.num_lines = sum(1 for line in open(self.route)) - 1
self.ui.pushButton_plot.setEnabled(True)
self.ui.pushButton_plot.setStyleSheet("background:rgb(32, 74, 135); color:white")
# Notes
self.ui.pushButton_savenotes.setEnabled(True)
self.ui.pushButton_savenotes.setStyleSheet("background:rgb(78, 154, 6); color:white;")
self.ui.textEdit_notes.setEnabled(True)
self.ui.textEdit_notes.setStyleSheet("background-color: rgb(245, 245, 245);")
self.exp_notes()
self.day_resume()
self.cal_time()
def day_resume(self):
tam = len (self.filenotes.split('/')[-1:][0]) + 1
self.day_route = self.route[:-tam]+'log.txt'
txt=''
with open(self.day_route, 'r') as f:
txt = f.read()
self.ui.textEdit_resume.setText(txt)
self.ui.textEdit_resume.setEnabled(True)
self.ui.edit_mode.setEnabled(True)
self.ui.pushButton_reload.setEnabled(True)
self.ui.pushButton_reload.setStyleSheet("background:rgb(78, 154, 6);")
self.ui.textEdit_resume.setStyleSheet("background-color: rgb(245, 245, 245);")
self.ui.textEdit_resume.verticalScrollBar().setValue(self.ui.textEdit_resume.verticalScrollBar().maximum())
def day_resume_reload(self):
txt=''
with open(self.day_route, 'r') as f:
txt = f.read()
self.ui.textEdit_resume.setText( txt )
self.ui.textEdit_resume.verticalScrollBar().setValue(self.ui.textEdit_resume.verticalScrollBar().maximum())
def exp_notes(self):
self.filenotes = self.route[:-8]+'log.txt'
txt=''
if os.path.isfile(self.filenotes) == False:
txt = self.filename+'\n------------------------------------------------------\n\n'
else:
with open(self.filenotes, 'r') as f:
txt = f.read()
self.ui.textEdit_notes.setText( txt )
def save_notes(self):
with open(self.filenotes, 'w') as f:
f.write( self.ui.textEdit_notes.toPlainText() )
def save_day(self):
with open(self.fileday, 'w') as f:
f.write( self.ui.textEdit_resume.toPlainText() )
def editable_on(self):
if self.ui.edit_mode.isChecked() == True:
self.ui.pushButton_saveday.setEnabled(True)
self.ui.pushButton_saveday.setStyleSheet("background:rgb(78, 154, 6); color:white;")
self.ui.textEdit_resume.setTextInteractionFlags(QtCore.Qt.TextEditable | QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard);
else:
self.ui.pushButton_saveday.setDisabled(True)
self.ui.pushButton_saveday.setStyleSheet("")
self.ui.textEdit_resume.setTextInteractionFlags(QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard);
def cal_time(self):
if self.route == self.path_text:
self.ui.label_time.setText('-')
return
segs = int (self.num_lines / self.ui.frecuency.value())
if segs > 59:
mins = int (segs/60)
segs = segs-(mins*60)
self.ui.label_time.setText(str(mins)+'m '+str(segs)+'s')
else:
self.ui.label_time.setText(str(segs)+'s')
def size(self):
if self.expand==True:
self.resize(510,0)
self.expand=False
else:
self.resize(1450,0)
self.expand=True
############
# MAIN #
############
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = Interface()
myapp.resize(510, 0)
myapp.show()
sys.exit(app.exec_())