-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCControl.py
312 lines (241 loc) · 12.8 KB
/
GCControl.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
# -*- coding: utf-8 -*
# GCControl, a program to record and evaluate chromatograms
#
# Copyright (C) 2020 Johannes Stoeckelmaier <[email protected]>
#
# This file is part of GCControl.
#
# GCControl is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GCControl is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCControl. If not, see <http://www.gnu.org/licenses/>.
#
# The main structure is based on the tutorial provided by: https://wiki.wxpython.org/AnotherTutorial#First_Steps
import wx
import time
from src.GCControl_Forms import MainFrame_preset
from src.GCControl_Forms import AboutFrame_preset
from src.GCControl_Forms import SettingsFrame_preset
import src.SettingsClass as SettingsClass
import src.SimulationClass as SimulationClass
import src.PlottingClass as plot
import src.ChromatogramClass as ChromatogramClass
import src.ArduinoClass as ArduinoClass
#----------------------------------------------------------------------
# Programming of the GUI
#----------------------------------------------------------------------
class AboutFrame(AboutFrame_preset):
#
# GUI programming of the About Window
#
def b_close_click( self, event ):
self.Close()
def AboutFrame_OnClose(self, event):
self.Destroy()
class SettingsFrame(SettingsFrame_preset):
#
# GUI programming of the Settings Window
#
def __init__(self, parent):
SettingsFrame_preset.__init__(self, parent)
self.globalSettings = SettingsClass.Settings()
self.globalSettings.loadSettings()
self.m_columLength.SetValue( self.globalSettings.colLength )
self.m_columnID.SetValue( self.globalSettings.colDiameter )
self.text_ArduinoLoc.SetValue( self.globalSettings.arduinoPath )
if (str(self.globalSettings.enableSimulation) == "True"):
self.checkBox_Simulate.SetValue(True)
else:
self.checkBox_Simulate.SetValue(False)
def b_settings_ok_click( self, event ):
self.globalSettings.colLength = self.m_columLength.GetValue()
self.globalSettings.colDiameter = self.m_columnID.GetValue()
self.globalSettings.arduinoPath = self.text_ArduinoLoc.GetValue()
if (self.checkBox_Simulate.GetValue() == True):
self.globalSettings.enableSimulation = "True"
else:
self.globalSettings.enableSimulation = "False"
self.globalSettings.saveSettings()
self.Close();
def SettingsFrame_OnClose(self, event):
self.Destroy()
class MainFrame(MainFrame_preset):
#
# GUI programming of the Main GCControl Window
#
def MainWindow_OnStartup( self, event ):
#Create an data-object
self.data = ChromatogramClass.Chromatogram()
#Safe start-up time
self.startUpTime = time.time()
#Create Panel
self.mainPanel = plot.CanvasPanel(self.panel_graphic)
self.mainPanel.clearPlot()
self.m_statusBar.SetStatusText('All practical experiments conducted in combination with this software can be dangerous and are carried out at your own risk! Take care!')
def MainWindow_OnClose(self, event):
self.Destroy()
def timer_signal( self, event ):
signal = self.arduino.readArduino()
timemark = round(time.time() - self.startUpTime, 3)
if (signal != None): #First Dataset after init alway none
self.data.addDatasetToChromatogram(timemark, signal)
self.mainPanel.clearPlot()
self.mainPanel.plot(self.data.original,"chromatogram", timeLimit=120, autoScale=True)
def timer_simulation_signal( self, event ):
timemark = round(time.time() - self.startUpTime,3)
runTime = round(timemark - self.data.startTime, 3)
timeColumn = self.data.original[:,0]
dt = (timeColumn[-1]-timeColumn[0])/len(timeColumn)
actualIndex = int(runTime/dt)
if (actualIndex >= len(timeColumn)): actualIndex=len(timeColumn)-1 #Stop chromatogram after reaching end of simulated area
self.mainPanel.clearPlot()
self.mainPanel.plot(self.data.original[:actualIndex,:actualIndex],"simulated chromatogram", timeLimit=120, autoScale=True)
def m_load_click( self, event ):
filePath = self.openDialog()
if (filePath != None):
self.data.loadChromatogramFromFile(filePath)
#panel = plot.CanvasPanel(self.panel_graphic)
self.mainPanel.clearPlot()
self.mainPanel.plot(self.data.original,"chromatogram", timeLimit=None, autoScale=True)
def m_save_click(self, event):
filePath = self.saveDialog()
if (filePath != None):
self.data.saveChromatogramToFile(filePath)
def m_close_click( self, event ):
self.Close()
def m_about_click( self, event ):
aboutFrame = AboutFrame(None)
aboutFrame.ShowModal()
def b_readArduino_click( self, event ):
#Load Arduino Adress
Settings = SettingsClass.Settings()
Settings.loadSettings()
if (Settings.enableSimulation == "False"):
self.arduino = ArduinoClass.ArduinoReader(serialPort=Settings.arduinoPath)
self.timer_arduinoRead.Start( 250 ) #Enable timer event
self.data.startNewChromatogram(time.time() - self.startUpTime) #Start a new Chromatogram
else:
simParam = SimulationClass.LoadSimParameters("sample.dat")
parameters = [simParam.skewFactor, simParam.noiseLevel, simParam.driftFactor, simParam.speed]
simChromatogram = SimulationClass.SimulatedChromatogram(simParam.peakTimes,simParam.peakArea,simParam.N_calculated,parameters)
self.data.startNewChromatogram(time.time() - self.startUpTime) #Start a new Chromatogram
self.data.original = simChromatogram.data
self.timer_simulation.Start( 250 ) #Disable timer event #Disable timer event.Start( 250 ) #Enable timer event
def b_simulate_click(self, event):
simParam = SimulationClass.LoadSimParameters("sample.dat")
self.m_DeadTime.SetValue(float(simParam.deadTime*60))
parameters = [simParam.skewFactor, simParam.noiseLevel, simParam.driftFactor, simParam.speed]
simChromatogram = SimulationClass.SimulatedChromatogram(simParam.peakTimes,simParam.peakArea,simParam.N_calculated,parameters)
self.mainPanel.clearPlot()
self.mainPanel.plot(simChromatogram.data, "simulated chromatogram", timeLimit=None, autoScale=False)
def b_stopArduino_click( self, event ):
self.t_perfOutput.AppendText("Arduino stoped!\n")
self.timer_arduinoRead.Stop() #Disable timer event
self.timer_simulation.Stop() #Disable timer event
self.arduino.closeArduino()
def b_saveData_click( self, event ):
#Save the data
filePath = self.saveDialog()
if (filePath != None):
self.data.saveChromatogramToFile(filePath)
def b_GCSettings_click( self, event ):
settingsFrame = SettingsFrame(None)
settingsFrame.ShowModal()
def b_evaluate_click( self, event ):
#Read in deadTime from GUI
deadTime = float(self.m_DeadTime.GetValue())
#plot chromatogram
self.mainPanel.clearPlot()
self.mainPanel.plot(self.data.original,"chromatogram", timeLimit=120, autoScale=False)
#Do the evaluations of the chromatogram
time_original = self.data.original[:,0]
signal_original = self.data.original[:,1]
self.denoisedChrom = ChromatogramClass.OptimizedChromatogram(time_original, signal_original)
import numpy as np
for index, peak in enumerate(self.denoisedChrom.denoisedPeaks):
x = self.denoisedChrom.time
y = self.denoisedChrom.denoisedPeaks[index]
#self.mainPanel.plot(np.transpose(np.array([x,y])),labelText="optimized peak",timeLimit=None, autoScale=False)
self.idealChrom = ChromatogramClass.IdealChromatogram(deadTime, self.denoisedChrom)
self.data.peakPos = np.array(self.denoisedChrom.peakTime)
self.data.peakArea = np.array(self.denoisedChrom.area)
self.data.N_calculated_global = self.idealChrom.N_calculated
self.data.N_calculated_avg = np.average(self.idealChrom.N_calculated)
self.data.N_measured_global = self.idealChrom.N_measured
self.data.N_measured_avg = np.average(self.idealChrom.N_measured)
self.data.totalRelPerf = self.data.N_measured_avg / self.data.N_calculated_avg
#Plot the ideal Chromatogram
"""Load Settings"""
globalSettings = SettingsClass.Settings()
globalSettings.loadSettings()
columnLength = globalSettings.colLength #Säulenlänge in Meter
print("#######################################################")
print("#######################################################")
print("")
print("PeakPos: ", self.data.peakPos)
print("PeakArea: ", self.data.peakArea)
print("Calculated Number of Plates: ", self.data.N_calculated_global)
print("Measured Number of Plates: ", self.data.N_measured_global)
print("")
print("#######################################################")
print("#######################################################")
#Plot the denoised chromatogram
#panel.plot(self.data.denoised,"Data denoised")
#Print to output-box
self.t_perfOutput.Clear()
self.t_perfOutput.AppendText("Deadtime: " + '{:.1f}'.format(deadTime) + " sec\n")
self.t_perfOutput.AppendText("Column-length: " + '{:.1f}'.format(columnLength) + " m\n")
self.t_perfOutput.AppendText("Measured number of plates: " + '{:.1f}'.format(self.data.N_measured_avg) + "\n")
self.t_perfOutput.AppendText("Expected number of plates: " + '{:.1f}'.format(self.data.N_calculated_avg) + "\n")
self.t_perfOutput.AppendText("relative performance: " + '{:.1f}'.format(self.data.totalRelPerf*100) + " %\n")
simChromatogram = SimulationClass.SimulatedChromatogram(self.data.peakPos, self.data.peakArea, self.data.N_calculated_global)
self.mainPanel.plot(simChromatogram.data, "ideal chromatogram", timeLimit=None, autoScale=False)
def openDialog(self):
with wx.FileDialog(self, "Open chromatography data", wildcard="chromatography files (*.txt)|*.txt", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return None # the user changed their mind
# save the current contents in the file
pathName = fileDialog.GetPath()
try:
print(pathName + " loaded...")
except IOError:
wx.LogError("Error while loading data '%s'." % pathName)
print("Error while loading data '%s'." % pathName)
return None
return pathName
def saveDialog(self):
with wx.FileDialog(self, "Save chromatography data", wildcard="chromatography files (*.txt)|*.txt", style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return None # the user changed their mind
# save the current contents in the file
pathName = fileDialog.GetPath()
try:
print("Path to save: " + pathName)
except IOError:
wx.LogError("Cannot save current data in file '%s'." % pathName)
return pathName
#----------------------------------------------------------------------
# Application Startup
#----------------------------------------------------------------------
class Application(wx.App):
def OnInit(self):
frame = MainFrame(None)
self.SetTopWindow(frame)
#Set minimum size of the window
sizeOfFrame = frame.GetSize()
frame.SetMinSize((sizeOfFrame[0], sizeOfFrame[1]))
#Set Icon
frame.SetIcon(wx.Icon("./img/icon.png"))
frame.Show(True)
return True
if __name__ == '__main__':
main = Application(redirect=False)
main.MainLoop()