/
PeakFinderGUI.py
executable file
·321 lines (248 loc) · 9.87 KB
/
PeakFinderGUI.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
#!/usr/bin/env python
"""
This demo demonstrates how to embed a matplotlib (mpl) plot
into a PyQt4 GUI application, including:
* Using the navigation toolbar
* Adding data to the plot
* Dynamically modifying the plot's properties
* Processing mpl events
* Saving the plot to a file from a menu
The main goal is to serve as a basis for developing rich PyQt GUI
applications featuring mpl plots (using the mpl OO API).
Eli Bendersky (eliben@gmail.com)
License: this code is in the public domain
Last modified: 19.01.2009
"""
import sys, os, random
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import getopt
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.figure import Figure
import PeakFinder
class PeakFinderGUI(QMainWindow):
#def __init__(self, parent=None):
def __init__( self, csv, delta, numnei, a, b, c, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle('PeakFinder')
self.create_menu()
self.create_main_frame()
self.create_status_bar()
self.spinboxes['a'].setValue(int(a))
self.spinboxes['b'].setValue(int(b))
self.spinboxes['c'].setValue(int(c))
self.spinboxes['numnei'].setValue(int(numnei))
self.spinboxes['delta'].setValue(float(delta))
self.csv = csv
self.myPeakFinder = PeakFinder.PeakFinder( csv, delta, numnei, a, b, c )
self.myPeakFinder.Run()
self.myPeakFinder.Print()
self.on_draw()
def save_plot(self):
file_choices = "PNG (*.png)|*.png"
path = unicode(QFileDialog.getSaveFileName(self,
'Save file', '',
file_choices))
if path:
self.canvas.print_figure(path, dpi=self.dpi)
self.statusBar().showMessage('Saved to %s' % path, 2000)
def on_pick(self, event):
# The event received here is of the type
# matplotlib.backend_bases.PickEvent
#
# It carries lots of information, of which we're using
# only a small amount here.
#
box_points = event.artist.get_bbox().get_points()
msg = "You've clicked on a bar with coords:\n %s" % box_points
QMessageBox.information(self, "Click!", msg)
def on_draw(self):
""" Redraws the figure
"""
self.fig.clear()
self.myPeakFinder.Plot(self.fig)
self.canvas.draw()
def on_recalculate(self):
A = self.spinboxes['a'].value()
B = self.spinboxes['b'].value()
C = self.spinboxes['c'].value()
delta = self.spinboxes['delta'].value()
numnei = self.spinboxes['numnei'].value()
self.myPeakFinder.Reset( delta, numnei, A, B, C )
print
print
print '################ recalculating... ######################'
print
self.myPeakFinder.Print()
self.on_draw()
def new_spin_box( self, label, minval, maxval, double=False ):
if double:
spinbox = QDoubleSpinBox()
else:
spinbox = QSpinBox()
spinbox.setMinimumWidth(100)
spinbox.setMinimum(minval)
spinbox.setMaximum(maxval)
#self.connect(spinbox, SIGNAL('editingFinished ()'), self.on_recalculate)
#hbox.addWidget(spinbox)
#hbox.setAlignment(spinbox, Qt.AlignVCenter)
return spinbox
def create_main_frame(self):
self.main_frame = QWidget()
#self.main_frame.resize(500,500)
#self.main_frame.setGeometry(500,500,500,500)
# Create the mpl Figure and FigCanvas objects.
# 5x4 inches, 100 dots-per-inch
#
#self.dpi = 100
#self.fig = Figure((5.0, 4.0), dpi=self.dpi,linewidth=0.1)
self.fig = Figure()
#self.fig = Figure(figsize=(5,5),dpi=100)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
# Since we have only one plot, we can use add_axes
# instead of add_subplot, but then the subplot
# configuration tool in the navigation toolbar wouldn't
# work.
#
#self.axes = self.fig.add_subplot(111)
# Bind the 'pick' event for clicking on one of the bars
#
#self.canvas.mpl_connect('pick_event', self.on_pick)
# Create the navigation toolbar, tied to the canvas
#
#self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
# Other GUI controls
self.draw_button = QPushButton("&Recalculate")
self.connect(self.draw_button, SIGNAL('clicked()'), self.on_recalculate)
#self.grid_cb = QCheckBox("Show &Grid")
#self.grid_cb.setChecked(False)
#self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw)
#
# Layout with box sizers
#
self.spinboxes = {
'a':self.new_spin_box( 'a', 0, 5000 ) ,
'b':self.new_spin_box( 'b', 0, 5000 ) ,
'c':self.new_spin_box( 'c', 0, 5000 ) ,
'numnei':self.new_spin_box( 'numnei', 0, 100 ) ,
'delta':self.new_spin_box( 'delta', 0.0, 1.0,double=True ) }
gridlayout = QGridLayout()
layout_order = ['a','b','c','numnei','delta']
row = 1
for lbl in layout_order:
v = self.spinboxes[lbl]
label = QLabel(lbl)
label.setMinimumWidth(50)
gridlayout.addWidget(label,row,0)
gridlayout.setAlignment(label, Qt.AlignVCenter)
gridlayout.addWidget(v,row,1)
gridlayout.setAlignment(v, Qt.AlignVCenter)
row += 1
# and the draw button...
gridlayout.addWidget(self.draw_button,row,0)
#gridlayout.setAlignment(self.draw_button, Qt.AlignVCenter)
self.open_button = QPushButton("&Open CSV")
self.connect( self.open_button, SIGNAL('clicked()'), self.on_new_file)
gridlayout.addWidget( self.open_button, row,1 )
gridlayout.setAlignment(self.open_button, Qt.AlignVCenter)
self.quit_button = QPushButton("&Quit")
self.connect(self.quit_button, SIGNAL('clicked()'), qApp, SLOT('quit()'))
gridlayout.addWidget( self.quit_button, row,2 )
gridlayout.setAlignment(self.quit_button, Qt.AlignVCenter)
hbox = QGridLayout()
hbox.addWidget(self.canvas,0,0)
#hbox.addWidget(self.mpl_toolbar)
hbox.addLayout(gridlayout,0,1)
self.main_frame.setLayout(hbox)
self.setCentralWidget(self.main_frame)
def on_new_file(self):
filename = QFileDialog.getOpenFileName( self, 'Open CSV file' )
self.csv = filename
print
print
print '################ loaded %s' % self.csv
print
self.myPeakFinder.OnNewFile( self.csv )
self.myPeakFinder.Run()
self.myPeakFinder.Print()
self.on_draw()
def create_status_bar(self):
self.status_text = QLabel("bugs: robbie.shade@gmail.com")
self.statusBar().addWidget(self.status_text, 1)
def create_menu(self):
self.file_menu = self.menuBar().addMenu("&File")
load_file_action = self.create_action("&Save plot",
shortcut="Ctrl+S", slot=self.save_plot,
tip="Save the plot")
quit_action = self.create_action("&Quit", slot=self.close,
shortcut="Ctrl+Q", tip="Close the application")
self.add_actions(self.file_menu,
(load_file_action, None, quit_action))
def add_actions(self, target, actions):
for action in actions:
if action is None:
target.addSeparator()
else:
target.addAction(action)
def create_action( self, text, slot=None, shortcut=None,
icon=None, tip=None, checkable=False,
signal="triggered()"):
action = QAction(text, self)
if icon is not None:
action.setIcon(QIcon(":/%s.png" % icon))
if shortcut is not None:
action.setShortcut(shortcut)
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
self.connect(action, SIGNAL(signal), slot)
if checkable:
action.setCheckable(True)
return action
def ParseArgs( argv ):
filename = "data.csv"
delta = 0.2 # jump in value to determine maxima/minima
numnei = 10 # +- numnei indices are searched to find bases
a = 50
b = 500
c = 1500
try:
opts, args = getopt.getopt(argv[1:], "f:d:n:pa:b:c:", ["file=", "delta=", "numnei=", "plot"])
except getopt.GetoptError, err:
print str(err)
#usage()
print 'usage: ', argv[0], ' --file data.csv --delta 0.2 --numnei 10 -a 50 -b 500 -c 1500'
sys.exit(2)
output = None
verbose = False
plotfig = False
for o, arg in opts:
if o in ("-f", "--file"):
filename = arg
elif o in ("-d", "--delta"):
delta = arg
elif o in ("-n", "--numnei"):
numnei = arg
elif o in ("-p", "--plot"):
plotfig = True
elif o in ("-a"):
a = arg
elif o in ("-b"):
b = arg
elif o in ("-c"):
c = arg
else:
assert False, "unhandled option"
return filename, float(delta), int(numnei), plotfig, a, b, c
def main():
app = QApplication(sys.argv)
filename, delta, numnei, plotfig, a, b, c = ParseArgs( sys.argv )
form = PeakFinderGUI( filename, delta, numnei, a, b, c )
form.show()
app.exec_()
if __name__ == "__main__":
main()