-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawimageviewer.py
193 lines (153 loc) · 6.95 KB
/
rawimageviewer.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
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg
import sys
from astropy.io import fits
from viewergui import Ui_Form
from scipy import ndimage
from scipy.interpolate import splrep, splev, sproot
import glob
from PyQt4.QtCore import QFileSystemWatcher
import os
def xor(lst1, lst2):
""" returns a tuple of items of item not in either of lists
"""
x = lst2 if len(lst2) > len(lst1) else lst1
y = lst1 if len(lst1) < len(lst2) else lst2
return tuple(item for item in x if item not in y)
def FWHM_spline(x, y):
"""
Determine full-with-half-maximum of a peaked set of points, x and y.
Assumes that there is only one peak present in the datasset. The function
uses a spline interpolation of order k=3.
"""
ymin = np.min(y)
half_max = np.amax(y-ymin)/2.0
s = splrep(x, y - ymin - half_max)
roots = sproot(s)
x2 = np.linspace(min(x), max(x), 1000)
tck = splrep(x, y)
y2 = splev(x2, tck)
if len(roots) > 2:
print("The dataset appears to have multiple peaks, and thus the FWHM can't be determined.")
return 0, 0, 0
elif len(roots) < 2:
print("No proper peaks were found in the data set; likely the dataset is flat (e.g. all zeros).")
return 0, 0, 0
else:
return abs(roots[1] - roots[0]), roots[0], splev(roots[0], tck), roots[1], splev(roots[1], tck)
class RawViewer(QtGui.QDialog):
def __init__(self):
super(RawViewer, self).__init__()
self.ui = Ui_Form()
self.ui.setupUi(self)
self.rawdata = None
self.proxy = None
self.zoombox = pg.QtGui.QGraphicsRectItem()
self.zoombox.setPen(pg.mkPen((200, 0, 0, 150)))
self.ui.iv_2dspec.addItem(self.zoombox)
self.update_zoom = True
self.zoom_region = None
# setup plot style
self.ui.iv_2dspec_zoom.ui.roiBtn.hide()
self.ui.iv_2dspec_zoom.ui.menuBtn.hide()
self.ui.iv_2dspec_zoom.ui.histogram.hide()
self.ui.iv_2dspec.ui.roiBtn.hide()
self.ui.iv_2dspec.ui.menuBtn.hide()
self.ui.listWidget.itemClicked.connect(self.listwidget_click)
self.roi = pg.ROI([25, 50], [25, 50])
self.roi.addScaleHandle([0.5, 1], [0.5, 0.5])
self.roi.addScaleHandle([0, 0.5], [0.5, 0.5])
self.ui.iv_2dspec_zoom.addItem(self.roi)
self.roi.sigRegionChanged.connect(self.update_1d_zoom)
self.data1d_y = None
self.data1d_x = None
self.zoomx_1d_menu = self.ui.plotwidget_2.getPlotItem().getMenu()
self.zoomx_1d_menu.addAction("test")
self.ui.plotwidget_2.scene().sigMouseClicked.connect(self.MouseClick1d)
self.ui.plotwidget_3.scene().sigMouseClicked.connect(self.MouseClick1d)
self.proxy = pg.SignalProxy(self.ui.iv_2dspec.scene.sigMouseMoved, rateLimit=60, slot=self.MouseOver2d)
self.ui.iv_2dspec.scene.sigMouseClicked.connect(self.MouseClick2d)
self.directory = str(QtGui.QFileDialog.getExistingDirectory(self, "Select Directory"))
self.populate_image_list()
self._initialContent = os.listdir(self.directory)
self._fileSysWatcher = QtCore.QFileSystemWatcher()
self._fileSysWatcher.addPath(self.directory)
QtCore.QObject.connect(self._fileSysWatcher, QtCore.SIGNAL("directoryChanged(QString)"),
self, QtCore.SLOT("slotDirChanged(QString)"))
@QtCore.pyqtSlot("QString")
def slotDirChanged(self, path):
newContent = ''.join(xor(os.listdir(path), self._initialContent))
self._initialContent = os.listdir(path)
msg = ""
if newContent not in self._initialContent:
msg = "removed: %s" % newContent
else:
msg = "added: %s" % newContent
print("Detected Directory Change!! \n %s" % msg)
self.populate_image_list()
def populate_image_list(self):
self.ui.listWidget.clear()
for filename in glob.iglob(self.directory + '/*.fit'):
self.ui.listWidget.addItem(filename)
for filename in glob.iglob(self.directory + '/*.fits'):
self.ui.listWidget.addItem(filename)
def listwidget_click(self, item):
self.load_image(item.text(), self.ui.checkBox.isChecked())
def load_image(self, filepath, orientation):
self.rawdata = fits.getdata(str(filepath))
if orientation == 0:
self.rawdata = ndimage.rotate(self.rawdata, -90)
self.ui.iv_2dspec.setImage(self.rawdata.T)
def update_1d_zoom(self):
xmin, xmax, ymin, ymax = self.zoom_region
selected = self.roi.getArrayRegion(self.rawdata[ymin:ymax, xmin:xmax].T, self.ui.iv_2dspec_zoom.getImageItem())
self.data1d_x = selected.mean(axis=1)
self.ui.plotwidget_2.plot(self.data1d_x, clear=True)
self.data1d_y = selected.mean(axis=0)
curve = self.ui.plotwidget_3.plot(self.data1d_y, clear=True)
curve.rotate(-90)
def MouseOver2d(self, event):
position = event[0]
test = self.ui.iv_2dspec.getImageItem().mapFromScene(position)
x = test.x()
y = test.y()
if self.update_zoom:
if (0 < x < self.rawdata.shape[1]) and (0 < y < self.rawdata.shape[0]):
xmin = int(max(x - self.ui.dial.value(), 0))
xmax = int(min(self.rawdata.shape[1], x + self.ui.dial.value()))
ymin = int(max(0, y - self.ui.dial.value()))
ymax = int(min(self.rawdata.shape[0], y + self.ui.dial.value()))
self.zoom_region = [xmin, xmax, ymin, ymax]
self.ui.iv_2dspec_zoom.setImage(self.rawdata[ymin:ymax, xmin:xmax].T)
self.ui.label.setText("X: %d" % x)
self.ui.label_3.setText("Y: %d" % y)
self.ui.label_2.setText("Value: %d" % self.rawdata[int(y), int(x)])
self.zoombox.setRect(x - self.ui.dial.value(), y - self.ui.dial.value(), 2. * self.ui.dial.value(),
2. * self.ui.dial.value())
def MouseClick1d(self):
fwhm = FWHM_spline(np.arange(len(self.data1d_x)), self.data1d_x)
if not fwhm[0] == 0:
self.ui.plotwidget_2.plot([fwhm[1], fwhm[3]], [fwhm[2], fwhm[4]])
text = pg.TextItem("%.4f" % fwhm[0])
self.ui.plotwidget_2.addItem(text)
text.setPos(fwhm[1], fwhm[2])
fwhm = FWHM_spline(np.arange(len(self.data1d_y)), self.data1d_y)
if not fwhm[0] == 0:
curve = self.ui.plotwidget_3.plot([fwhm[1], fwhm[3]], [fwhm[2], fwhm[4]])
curve.rotate(-90)
text = pg.TextItem("%.4f" % fwhm[0])
self.ui.plotwidget_3.addItem(text)
text.setPos(fwhm[2], -fwhm[1])
text.rotate(90)
def MouseClick2d(self, event):
if event.button() == 1:
self.update_zoom = not (self.update_zoom)
self.update_1d_zoom()
def main():
app = QtGui.QApplication(sys.argv)
w = RawViewer()
w.showMaximized()
sys.exit(app.exec_())
if __name__ == "__main__":
main()