-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctfevalgui.py
executable file
·378 lines (335 loc) · 14 KB
/
ctfevalgui.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.6.8 on Thu May 8 09:51:21 2014
#
import os
import wx
import numpy
from PIL import Image
from pyami import mrc
from pyami import imagefun
from pyami.Entry import FloatEntry
from appionlib.apCtf import ctfdisplay
# begin wxGlade: dependencies
# end wxGlade
# begin wxGlade: extracode
# end wxGlade
imagetypefilter = (
"MRC Files (*.mrc)|*.mrc"
+"|"+
"PNG Files (*.png)|*.png"
+"|"+
"Spider Files (*.spi)|*.spi"
+"|"+
"TIFF Files (*.tif)|*.tif"
+"|"+
"JPEG Files (*.jpg)|*.jpg"
)
class PopUpDialog(wx.Frame):
def __init__(self, imagefile, *args, **kwds):
wx.Frame.__init__(self, None, title="Popup Frame", size=(600,600))
# begin wxGlade: PopupDialog.__init__
kwds["style"] = wx.TAB_TRAVERSAL
panel = wx.Panel(self, *args, **kwds)
#bitmap = wx.Bitmap(imagefile, wx.BITMAP_TYPE_ANY)
image = wx.Image(imagefile, wx.BITMAP_TYPE_ANY)
xscale = 800./image.GetWidth()
yscale = 800./image.GetHeight()
scale = max(xscale,yscale)
width = int(round(image.GetWidth()*scale))
height = int(round(image.GetHeight()*scale))
bitmap = wx.BitmapFromImage(image.Scale(width, height))
self.Image = wx.StaticBitmap(self, wx.ID_ANY, bitmap)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: PopupDialog.__set_properties
pass
# end wxGlade
def __do_layout(self):
# begin wxGlade: PopupDialog.__do_layout
PopUpSize = wx.FlexGridSizer(wx.HORIZONTAL)
PopUpSize.Add(self.Image, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
PopUpSize.AddGrowableRow(0)
PopUpSize.AddGrowableCol(0)
self.SetSizer(PopUpSize)
PopUpSize.Fit(self)
# end wxGlade
# end of class PopupDialog
class MyFrame(wx.Frame):
#--------------------
def __init__(self, *args, **kwds):
# begin wxGlade: MyFrame.__init__
self.fullimagepath = None
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel = wx.Panel(self, wx.ID_ANY)
logofile = "images/ctfeval_logo.png"
if not os.path.isfile(logofile):
logofile = "ctfeval_logo.png"
self.Logo = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.Bitmap(logofile, wx.BITMAP_TYPE_ANY))
self.static_line_1a = wx.StaticLine(self.panel, wx.ID_ANY)
self.static_line_1b = wx.StaticLine(self.panel, wx.ID_ANY)
self.openFileButton = wx.Button(self.panel, wx.ID_ANY, "Select Micrograph...")
self.imageLabel = wx.StaticText(self.panel, wx.ID_ANY, "<- click button to select micrograph")
self.static_line_2a = wx.StaticLine(self.panel, wx.ID_ANY)
self.static_line_2b = wx.StaticLine(self.panel, wx.ID_ANY)
self.voltLabel = wx.StaticText(self.panel, wx.ID_ANY, "Voltage Potential (kV)")
self.voltValue = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.csLabel = wx.StaticText(self.panel, wx.ID_ANY, "Spherical Abberation, Cs (mm)")
self.csValue = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.pixelSizeLabel = wx.StaticText(self.panel, wx.ID_ANY, "Pixel size (Angstroms, A)")
self.pixelSizeValue = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.static_line_3a = wx.StaticLine(self.panel, wx.ID_ANY)
self.static_line_3b = wx.StaticLine(self.panel, wx.ID_ANY)
self.defoc1Label = wx.StaticText(self.panel, wx.ID_ANY, "Defocus 1 (microns, um)")
self.defoc1Value = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.defoc2Label = wx.StaticText(self.panel, wx.ID_ANY, "Defocus 2 (microns, um)")
self.defoc2Value = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.angleLabel = wx.StaticText(self.panel, wx.ID_ANY, "Angle Astimatism (degrees)")
self.angleValue = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.ampConLabel = wx.StaticText(self.panel, wx.ID_ANY, "Amplitude Contrast (0..1)")
self.ampConValue = FloatEntry(self.panel, wx.ID_ANY, allownone=True, chars=8, value="", style=wx.TE_RIGHT)
self.processButton = wx.Button(self.panel, wx.ID_ANY, "Process CTF...")
self.quitButton = wx.Button(self.panel, wx.ID_EXIT, "Quit")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.selectMicrograph, self.openFileButton)
self.Bind(wx.EVT_BUTTON, self.processCTF, self.processButton)
self.Bind(wx.EVT_BUTTON, self.quit, self.quitButton)
# end wxGlade
#--------------------
def __set_properties(self):
# begin wxGlade: MyFrame.__set_properties
self.SetTitle("MyFrame")
self.processButton.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0))
# end wxGlade
#--------------------
def __do_layout(self):
# begin wxGlade: MyFrame.__do_layout
FrameSizer = wx.BoxSizer(wx.VERTICAL) # = sizer_1
MainSizer = wx.FlexGridSizer(2, 3, 10, 10) # = grid_sizer_1
self.Layout()
Input = wx.FlexGridSizer(11, 2, 5, 15)
MainSizer.Add(self.Logo, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.static_line_1a, 0, wx.EXPAND, 0)
Input.Add(self.static_line_1b, 0, wx.EXPAND, 0)
Input.Add(self.openFileButton, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.imageLabel, 0, wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.static_line_2a, 0, wx.EXPAND, 0)
Input.Add(self.static_line_2b, 0, wx.EXPAND, 0)
Input.Add(self.voltLabel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.voltValue, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.csLabel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.csValue, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.pixelSizeLabel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.pixelSizeValue, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.static_line_3a, 0, wx.EXPAND, 0)
Input.Add(self.static_line_3b, 0, wx.EXPAND, 0)
Input.Add(self.defoc1Label, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.defoc1Value, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.defoc2Label, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.defoc2Value, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.angleLabel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.angleValue, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.ampConLabel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.Add(self.ampConValue, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL | wx.ADJUST_MINSIZE, 0)
Input.AddGrowableRow(0)
Input.AddGrowableRow(2)
Input.AddGrowableRow(6)
Input.AddGrowableCol(1)
MainSizer.Add(Input, 1, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
MainSizer.Add((10, 10), 0, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
MainSizer.Add(self.quitButton, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 2)
MainSizer.Add(self.processButton, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 2)
MainSizer.Add((10, 10), 0, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
self.statbar = self.CreateStatusBar()
self.statbar.PushStatusText("Ready: "+os.getcwd(), 0)
self.statbar.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0))
self.panel.SetSizer(MainSizer)
FrameSizer.Add(self.panel, 1, wx.EXPAND, 0)
MainSizer.Fit(self)
MainSizer.AddGrowableRow(0)
MainSizer.AddGrowableRow(1)
MainSizer.AddGrowableCol(1)
self.SetSizer(FrameSizer)
FrameSizer.Fit(self)
self.Layout()
# end wxGlade
#--------------------
def selectMicrograph(self, event): # wxGlade: MyFrame.<event_handler>
dlg = wx.FileDialog(self, "Choose a micrograph to open", os.getcwd(), "",
imagetypefilter, wx.OPEN)
self.data = {}
if dlg.ShowModal() == wx.ID_OK:
self.data['outfile'] = dlg.GetFilename()
self.data['dirname'] = os.path.abspath(dlg.GetDirectory())
os.chdir(self.data['dirname'])
self.fullimagepath = os.path.join(self.data['dirname'], self.data['outfile'])
if (dlg.GetFilterIndex() == 0):
self.processMrc(self.fullimagepath)
self.imageLabel.SetLabel(dlg.GetFilename())
self.statbar.PushStatusText(self.fullimagepath, 0)
self.processButton.SetBackgroundColour(wx.Colour(191, 255, 191))
dlg.Destroy()
event.Skip()
#--------------------
def processMrc(self, mrcfile): # wxGlade: MyFrame.<event_handler>
if not os.path.isfile(mrcfile):
print "ERROR: could not read file %s"%(mrcfile)
self.statbar.PushStatusText("ERROR: could not read file %s"%(mrcfile), 0)
header = mrc.read_file_header(mrcfile)
pixelsize = header['xlen']/float(header['nx'])
#print "'%s', %.3f"%(self.pixelSizeValue.GetValue(), pixelsize)
if self.pixelSizeValue.GetValue() is None:
self.pixelSizeValue.SetValue(pixelsize)
#mrc.printHeader(header)
array = mrc.read(mrcfile)
self.setBitmap(array)
#--------------------
def setBitmap(self, array):
'''
Set the internal wx.Bitmap to current Numeric image
'''
if isinstance(array, numpy.ndarray):
wximage = self.numpyToWxImage(array)
else:
self.bitmap = None
return False
if wximage is None:
return False
xscale = 384./wximage.GetWidth()
yscale = 384./wximage.GetHeight()
scale = max(xscale,yscale)
width = int(round(wximage.GetWidth()*scale))
height = int(round(wximage.GetHeight()*scale))
bitmap = wx.BitmapFromImage(wximage.Scale(width, height))
self.Logo.SetBitmap(bitmap)
return True
#--------------------
def numpyToWxImage(self, array):
numStdev = 3.0
clip = (array.mean()-numStdev*array.std(), array.mean()+numStdev*array.std())
wximage = wx.EmptyImage(array.shape[1], array.shape[0])
normarray = array.astype(numpy.float32)
normarray = normarray.clip(min=clip[0], max=clip[1])
if clip[1] - clip[0] != 0:
normarray = (normarray - clip[0]) / (clip[1] - clip[0]) * 255.0
normarray = normarray.astype(numpy.uint8)
h, w = normarray.shape[:2]
imagedata = Image.fromstring("L", (w, h), normarray.tostring())
wximage.SetData(imagedata.convert('RGB').tostring())
return wximage
#--------------------
def processCTF(self, event): # wxGlade: MyFrame.<event_handler>
if self.checkCTFvalues() is False:
event.Skip()
return
self.statbar.PushStatusText("Processing please wait...", 0)
self.processButton.SetBackgroundColour(wx.Colour(255, 191, 191))
self.Update()
#self.statbar.SetBackgroundColour(wx.Colour(255, 191, 191))
imgdata = {
'filename': self.fullimagepath,
'image': mrc.read(self.fullimagepath),
}
ctfdata = {
'volts': self.voltValue.GetValue()*1e3,
'cs': self.csValue.GetValue(),
'apix': self.pixelSizeValue.GetValue(),
'defocus1': self.defoc1Value.GetValue()*1e-6,
'defocus2': self.defoc2Value.GetValue()*1e-6,
'angle_astigmatism': self.angleValue.GetValue(),
'amplitude_contrast': self.ampConValue.GetValue(),
}
a = ctfdisplay.CtfDisplay()
ctfdisplaydict = a.CTFpowerspec(imgdata, ctfdata, None, None, True)
self.statbar.PushStatusText("Finished", 0)
self.processButton.SetBackgroundColour(wx.Colour(191, 255, 191))
self.Update()
self.showImages(ctfdisplaydict)
event.Skip()
#return ctfdisplaydict
#--------------------
def showImages(self, ctfdisplaydict):
print ctfdisplaydict
a = PopUpDialog(ctfdisplaydict['powerspecfile'])
a.Show()
b = PopUpDialog(ctfdisplaydict['plotsfile'])
b.Show()
#--------------------
def popupError(self, msg):
self.statbar.PushStatusText("ERROR: %s"%(msg), 0)
dialog = wx.MessageDialog(self, "%s"%(msg),
'Error', wx.OK|wx.ICON_ERROR)
dialog.ShowModal()
dialog.Destroy()
#--------------------
def checkCTFvalues(self):
if self.fullimagepath is None:
self.popupError("Please select a micrograph for processing")
return False
volts = self.voltValue.GetValue()
if volts is None:
self.popupError("Please enter a voltage value")
return False
if volts > 400.0 or volts < 60:
self.popupError("atypical high tension value %.1f kiloVolts"%(volts))
return False
cs = self.csValue.GetValue()
if cs is None:
self.popupError("Please enter a spherical abberation value")
return False
if cs > 7.0 or cs < 0.4:
self.popupError("atypical C_s value %.1f mm"%(cs))
return False
pixelsize = self.pixelSizeValue.GetValue()
if pixelsize is None:
self.popupError("Please enter a pixel size value")
return False
if pixelsize > 20.0 or pixelsize < 0.1:
self.popupError("atypical pixel size value %.1f Angstroms"%(pixelsize))
return False
focus1 = self.defoc1Value.GetValue()
if focus1 is None:
self.popupError("Please enter a defocus 1 value")
return False
if focus1 > 15.0 or focus1 < 0.1:
self.popupError("atypical defocus #1 value %.1f microns (underfocus is positve)"%(focus1))
return False
focus2 = self.defoc2Value.GetValue()
if focus2 is None:
self.popupError("Please enter a defocus 2 value")
return False
if focus2 > 15.0 or focus2 < 0.1:
self.popupError("atypical defocus #2 value %.1f microns (underfocus is positve)"%(focus1))
return False
angle = self.angleValue.GetValue()
if angle is None:
self.popupError("Please enter a angle astigmatism value, zero for no astig")
return False
if abs(angle) > 0.01 and abs(angle) < 3.0:
self.popupError("atypical angle astigmatism value %.3f"%(angle))
return False
ampcon = self.ampConValue.GetValue()
if ampcon is None:
self.popupError("Please enter a amplitude contrast value")
return False
if ampcon < 0.0 or ampcon > 0.5:
self.popupError("atypical amplitude contrast value %.3f"%(ampcon))
return False
return True
#--------------------
def quit(self, event): # wxGlade: MyFrame.<event_handler>
wx.Exit()
event.Skip()
# end of class MyFrame
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
MyFrame = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(MyFrame)
MyFrame.Show()
app.MainLoop()