-
Notifications
You must be signed in to change notification settings - Fork 3
/
viewdm3.py
executable file
·397 lines (320 loc) · 18.2 KB
/
viewdm3.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# viewdm3.py --- Main file
# This file is part of DM3Viewer, a simple PyQt application to
# view and export DM3 files.
#
# Copyright (C) 2018-2023 Ovidio Peña Rodríguez <[email protected]>
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is based on the example "embedding_in_qt4.py" available
# at http://matplotlib.org/examples/user_interfaces/embedding_in_qt4.html
'''
Created on Feb 05, 2018
@author: ovidio
'''
import os
import platform
from PyQt5 import uic, QtCore, QtGui, QtWidgets
from utils import version, Plot3D, Options, OptionsDlg
class DM3Viewer(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self)
self.programDirPath = os.path.dirname(os.path.realpath(__file__))
self.ui = uic.loadUi(os.path.join(self.programDirPath, 'DM3Viewer.ui'), self)
self.setWindowTitle(version.__name__)
self.options = None
self.optionsDlg = None
self.loadFileDlg = None
# This gets the settings from wherever they are stored (the storage place is platform dependent)
self.settings = QtCore.QSettings()
self.loadOptions()
self.plot = Plot3D(self.plotQF, toolbar=False, cmap=self.options.colorMap)
self.plot.scale_pos = self.options.scalePos
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.plot.nextFile)
self.action_Open_DM3.triggered.connect(self.fileOpen)
self.action_Reload_DM3.triggered.connect(self.plot.readFile)
self.action_Close_DM3.triggered.connect(self.fileClose)
self.action_Export_Data.triggered.connect(self.fileExportData)
self.action_Export_PNG.triggered.connect(lambda: self.fileSaveAs(name='PNG', ext='png'))
self.action_Export_JPG.triggered.connect(lambda: self.fileSaveAs(name='JPG', ext='jpg'))
self.action_Export_TIF.triggered.connect(lambda: self.fileSaveAs(name='TIFF', ext='tif'))
self.action_Export_PDF.triggered.connect(lambda: self.fileSaveAs(name='PDF', ext='pdf'))
self.action_Quit_Viewer.triggered.connect(self.fileQuit)
self.action_First_Image.triggered.connect(self.plot.firstFile)
self.action_Previous_Image.triggered.connect(self.plot.previousFile)
self.action_Next_Image.triggered.connect(self.plot.nextFile)
self.action_Last_Image.triggered.connect(self.plot.lastFile)
self.action_Info.triggered.connect(self.imageInfo)
self.action_FFT.triggered.connect(self.switchFFT)
self.action_Options.triggered.connect(self.onOptions)
self.action_Timer.triggered.connect(self.timerInterval)
self.action_About.triggered.connect(self.helpAbout)
self.plot.coordsChanged.connect(self.setPlotCoords)
self.plot.dataChanged.connect(self.onDataChanged)
# Restore last session state for the main window
self.resize(self.settings.value("MainWindow/Size", QtCore.QVariant(QtCore.QSize(800, 600))))
self.move(self.settings.value("MainWindow/Position", QtCore.QVariant(QtCore.QPoint(0, 0))))
# self.restoreState(self.settings.value("MainWindow/State"))
# Launch low-priority initializations (to speed up load time)
QtCore.QTimer.singleShot(0, self.createLoadFileDlg) # Create the Load DM3 File dialog
self.updateControls()
def onDataChanged(self):
self.updateControls()
self.setTitle()
def setTitle(self):
if os.path.exists(self.plot.fname):
self.setWindowTitle('DM3 Viewer - ' + os.path.basename(self.plot.fname))
else:
self.setWindowTitle('DM3 Viewer')
def updateControls(self):
'''Update the state of the controls (Enabled|Disabled)'''
self.action_Close_DM3.setEnabled(self.plot.valid_source)
self.action_Reload_DM3.setEnabled(self.plot.valid_source)
self.action_Export_Data.setEnabled(self.plot.valid_source and not self.plot.data_is_spectra)
self.action_Export_PNG.setEnabled(self.plot.valid_source and not self.plot.data_is_spectra)
self.action_Export_JPG.setEnabled(self.plot.valid_source and not self.plot.data_is_spectra)
self.action_Export_TIF.setEnabled(self.plot.valid_source and not self.plot.data_is_spectra)
self.action_Export_PDF.setEnabled(self.plot.valid_source and not self.plot.data_is_spectra)
self.action_First_Image.setEnabled(self.plot.valid_source)
self.action_Previous_Image.setEnabled(self.plot.valid_source)
self.action_Next_Image.setEnabled(self.plot.valid_source)
self.action_Last_Image.setEnabled(self.plot.valid_source)
self.action_Info.setEnabled(self.plot.valid_data)
self.action_FFT.setEnabled(self.plot.valid_data and not self.plot.data_is_spectra)
if self.plot.data_is_fft:
self.action_FFT.setIcon(QtGui.QIcon(':/Icons/ifft.svg'))
self.action_FFT.setIconText('IFFT')
self.action_FFT.setToolTip('Inverse Fast Fourier Transform')
self.action_FFT.setStatusTip('Calculate the Inverse Fast Fourier Transform of the current DM3 file')
else:
self.action_FFT.setIcon(QtGui.QIcon(':/Icons/fft.svg'))
self.action_FFT.setIconText('FFT')
self.action_FFT.setToolTip('Fast Fourier Transform')
self.action_FFT.setStatusTip('Calculate the Fast Fourier Transform of the current DM3 file')
self.action_FFT.setChecked(self.plot.switch_fft)
self.action_Timer.setEnabled(self.plot.valid_data)
def setPlotCoords(self, coords=None):
if coords is None:
self.statusbar.showMessage("")
elif len(coords) == 1:
self.statusbar.showMessage("x = %.5g" % coords)
elif len(coords) == 2:
self.statusbar.showMessage("x = %.5g, y = %.5g" % coords)
elif len(coords) == 3:
self.statusbar.showMessage("x = %.5g, y = %.5g, z = %.5g" % coords)
else:
self.statusbar.showMessage("")
def loadOptions(self):
'''create the self.options object from values stored in the settings'''
self.options = Options()
for opt, dflt in zip(self.options.optlist, self.options.dfltlist):
if isinstance(dflt, str):
setattr(self.options, opt, self.settings.value('Options/' + opt, dflt))
elif isinstance(dflt, bytes):
setattr(self.options, opt, self.settings.value('Options/' + opt, dflt.decode('utf-8')))
elif isinstance(dflt, float):
setattr(self.options, opt, self.settings.value('Options/' + opt, QtCore.QVariant(dflt)))
elif isinstance(dflt, bool):
setattr(self.options, opt, self.settings.value('Options/' + opt, QtCore.QVariant(dflt)).toBool())
elif isinstance(dflt, int):
setattr(self.options, opt, self.settings.value('Options/' + opt, QtCore.QVariant(dflt)))
else:
print(type(dflt))
raise ValueError('Unsupported type in option "%s"' % dflt)
def createLoadDlg(self, title, filters, selectedFilter, activeDir):
# General Load Dialog
dlg = QtWidgets.QFileDialog(self, title, "./", "")
dlg.setFileMode(QtWidgets.QFileDialog.ExistingFiles)
dlg.setViewMode(QtWidgets.QFileDialog.Detail)
dlg.setNameFilters(filters)
dlg.setDirectory(activeDir)
dlg.selectNameFilter(selectedFilter)
return dlg
def createLoadFileDlg(self):
# Load DM3 File Dialog (it is never closed, just hidden)
title = "%s - Load DM3 File" % QtWidgets.QApplication.applicationName()
filters = ["DM3 Files (*.dm3)", "All Files (*.*)"]
selectedFilter = self.settings.value("LoadFileFilter", QtCore.QVariant(filters[0]))
activeDir = self.options.workDirectory
self.loadFileDlg = self.createLoadDlg(title, filters, selectedFilter, activeDir)
def fileOpen(self):
self.loadFileDlg.setDirectory(self.options.workDirectory)
if self.loadFileDlg.exec_():
self.options.workDirectory = self.loadFileDlg.directory().path() # update the working directory
self.settings.setValue("Options/workDirectory",
QtCore.QVariant(self.options.workDirectory)) # save the new working directory
fileNames = [item for item in self.loadFileDlg.selectedFiles()]
self.plot.fname = fileNames[-1]
self.setTitle()
def fileClose(self):
result = QtWidgets.QMessageBox.question(self, "Close '%s'" % (os.path.basename(self.plot.fname)),
"Are you sure that you want to close the image '%s'?" %
(os.path.basename(self.plot.fname)),
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if result == QtWidgets.QMessageBox.Yes:
self.plot.clearPlot()
self.setTitle()
def fileSaveAs(self, name='PNG', ext='png'):
if os.path.exists(self.plot.fname):
dirname = "%s.%s" % (os.path.splitext(self.plot.fname)[0], ext)
else:
dirname = self.options.workDirectory
fname, ftype = QtWidgets.QFileDialog.getSaveFileName(self, "Select File Name", dirname,
"%s files (*.%s)" % (name, ext))
if fname:
print(fname)
self.options.workDirectory = os.path.dirname(fname) # update the working directory
self.settings.setValue("Options/workDirectory",
QtCore.QVariant(self.options.workDirectory)) # save the new working directory
self.plot.writeFile(fname, zoom=self.options.zoom)
def fileExportData(self):
if os.path.exists(self.plot.fname):
dirname = "%s.%s" % (os.path.splitext(self.plot.fname)[0], 'dat')
else:
dirname = self.options.workDirectory
fname, ftype = QtWidgets.QFileDialog.getSaveFileName(self, "Select File Name", dirname,
"Data Files (*.dat)")
if fname:
self.options.workDirectory = os.path.dirname(fname) # update the working directory
self.settings.setValue("Options/workDirectory",
QtCore.QVariant(self.options.workDirectory)) # save the new working directory
import numpy as np
data = self.plot.data.copy()
if self.options.zoom != 1.0: # Resize only if necessary
from scipy.ndimage import zoom
data = zoom(data, zoom=self.options.zoom, order=3)
vmin = min(self.plot.dm3.cuts)
vmax = max(self.plot.dm3.cuts)
# Restrict values
data[np.where(data <= vmin)] = float(vmin)
data[np.where(data >= vmax)] = float(vmax)
# Normalize data, scaling to 0-65535, to convert to 16-bit integer
# data = np.uint16(np.round(65535*(data - vmin)/(vmax - vmin)))
# Normalize data, scaling to 0-255, to convert to 8-bit integer
data = np.uint8(np.round(255*(data - vmin)/(vmax - vmin)))
np.savetxt(fname, data.astype(int), fmt='%i')
# Save scale info
np.savetxt("%s.info" % (os.path.splitext(fname)[0]),
np.array((self.plot.data_width, self.plot.data_height)), fmt='%.3f')
def fileQuit(self):
# result = QtWidgets.QMessageBox.question(self, "Quit %s" % (version.__name__),
# "Are you sure that you want to quit %s?" % (version.__name__),
# QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
# if (result == QtWidgets.QMessageBox.Yes):
self.close()
def onOptions(self):
'''Shows the options dialog and saves any changes if accepted'''
if self.optionsDlg is None:
self.optionsDlg = OptionsDlg(self) # create the dialog if not already done
# Make sure that the Dlg is in sync with the options
self.optionsDlg.setOptions(self.options)
# Launch the options dialog
if self.optionsDlg.exec_():
self.options = self.optionsDlg.getOptions() # get the options from the dialog
if self.options.scalePos != self.plot.scale_pos:
self.plot.scale_pos = self.options.scalePos
if self.options.colorMap != self.plot.cmap:
self.plot.cmap = self.options.colorMap
else:
self.optionsDlg.setOptions(self.options) # reset previous options
def setUpdateInterval(self):
if self.options.timeInterval > 0:
self.timer.start(int(self.options.timeInterval))
else:
self.timer.stop()
def timerInterval(self):
interval, ok = QtWidgets.QInputDialog.getDouble(self, 'Update interval', 'Update interval (s):',
self.options.timeInterval/1000.0, 0)
if ok:
self.options.timeInterval = 1000.0*interval
self.setUpdateInterval()
def imageInfo(self):
info = self.plot.dm3.info
infoKeys = ('descrip', 'acq_date', 'acq_time', 'name', 'micro', 'hv', 'mag', 'mode', 'operator', 'specimen')
infoDesc = ('Description', 'Acquisition Date', 'Acquisition Time', 'Microscope Name', 'Microscope', 'Voltage',
'Magnification', 'Operation Mode', 'Operator', 'Specimen')
infoSuff = ('', '', '', '', '', ' V', ' X', '', '', '')
infoStr = ""
for i, key in enumerate(infoKeys):
if key in info:
infoStr += "<p><b>%s</b>: %s%s</p>" % (infoDesc[i], info[key], infoSuff[i])
shape = self.plot.data.shape
shape_str = '%i' % shape[0]
(origin, scale, units) = self.plot.dm3.axisunits(0)
size_str = '%.4g %s' % (shape[0]*scale, units)
for i in range(1, len(shape)):
shape_str = '%sx%i' % (shape_str, shape[i])
(origin, scale, units) = self.plot.dm3.axisunits(i)
size_str = '%s x %.4g %s' % (size_str, shape[i]*scale, units)
(zo, zf) = self.plot.dm3.cuts
QtWidgets.QMessageBox.information(self, "Image Info...",
"""<p><b>File Name</b>: %s</p>
<p><b>Size</b>: %s px</p>
<p><b>Dimensions</b>: %s</p>
<p><b>Scale</b>: %.3g %s/px</p>
<p><b>Contrast Limits</b>: %i - %i</p>
%s""" % (os.path.basename(self.plot.fname), shape_str,
size_str, scale, units, zo, zf, infoStr))
def switchFFT(self):
self.plot.switch_fft = self.action_FFT.isChecked()
def helpAbout(self):
from scipy import __version__ as scipy_version
from numpy import __version__ as numpy_version
from matplotlib import __version__ as mpl_version
QtWidgets.QMessageBox.about(self, "About %s" % version.__name__,
"""<b>%s</b> v%s
<p>Copyright © 2018 Ovidio Y. Peña Rodríguez <ovidio.pena [AT] upm.es></p>
<p></p>
<p>Home page: <a href='%s'>%s</a></p>
<p></p>
<p>Python %s - Qt %s - PyQt %s on %s</p>
<p>Numpy %s - Scipy %s - Matplotlib %s</p>""" % (version.__name__,
version.__version__, version.__url__,
version.__url__,
platform.python_version(),
QtCore.QT_VERSION_STR,
QtCore.PYQT_VERSION_STR,
platform.system(),
numpy_version, scipy_version,
mpl_version))
def closeEvent(self, event):
'''This event handler receives widget close events'''
# if not self.structureIsSaved():
# event.ignore()
# return
# Store the options as settings
for opt in self.options.optlist:
val = getattr(self.options, opt)
if isinstance(val, bytes):
val = val.decode('utf-8') # convert python strings to QStrings
self.settings.setValue("Options/" + opt, QtCore.QVariant(val))
# Save main window state before closing
self.settings.setValue("MainWindow/Size", QtCore.QVariant(self.size()))
self.settings.setValue("MainWindow/Position", QtCore.QVariant(self.pos()))
self.settings.setValue("MainWindow/State", QtCore.QVariant(self.saveState()))
self.settings.setValue("LoadFileFilter", QtCore.QVariant(self.loadFileDlg.selectedNameFilter()))
if __name__ == '__main__':
import sys
import DM3Viewer_rc
app = QtWidgets.QApplication(sys.argv)
app.setApplicationName(version.__name__)
app.setApplicationVersion(version.__version__)
app.setOrganizationName(version.__company__)
form = DM3Viewer()
form.show()
sys.exit(app.exec_())