1 | # global |
---|
2 | import sys |
---|
3 | from PyQt4 import QtCore |
---|
4 | from PyQt4 import QtGui |
---|
5 | from PyQt4 import QtWebKit |
---|
6 | |
---|
7 | from twisted.internet import threads |
---|
8 | from twisted.internet import reactor |
---|
9 | |
---|
10 | # sas-global |
---|
11 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
12 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
13 | from sas.sascalc.corfunc.corfunc_calculator import CorfuncCalculator |
---|
14 | |
---|
15 | # local |
---|
16 | from UI.CorfuncPanel import Ui_CorfuncDialog |
---|
17 | # from InvariantDetails import DetailsDialog |
---|
18 | from CorfuncUtils import WIDGETS as W |
---|
19 | |
---|
20 | from matplotlib.backends import qt_compat |
---|
21 | from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas |
---|
22 | from matplotlib.figure import Figure |
---|
23 | |
---|
24 | |
---|
25 | class MyMplCanvas(FigureCanvas): |
---|
26 | """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.).""" |
---|
27 | def __init__(self, model, parent=None, width=5, height=4, dpi=100): |
---|
28 | self.model = model |
---|
29 | self.fig = Figure(figsize=(width, height), dpi=dpi) |
---|
30 | self.axes = self.fig.add_subplot(111) |
---|
31 | |
---|
32 | FigureCanvas.__init__(self, self.fig) |
---|
33 | # self.reparent(parent, QPoint(0, 0)) |
---|
34 | |
---|
35 | # FigureCanvas.setSizePolicy(self, |
---|
36 | # QSizePolicy.Expanding, |
---|
37 | # QSizePolicy.Expanding) |
---|
38 | # FigureCanvas.updateGeometry(self) |
---|
39 | |
---|
40 | self.data = None |
---|
41 | self.extrap = None |
---|
42 | |
---|
43 | def drawQSpace(self): |
---|
44 | self.fig.clf() |
---|
45 | |
---|
46 | self.qmin = None |
---|
47 | self.qmax1 = None |
---|
48 | self.qmax2 = None |
---|
49 | |
---|
50 | self.axes = self.fig.add_subplot(111) |
---|
51 | self.axes.set_xscale("log") |
---|
52 | self.axes.set_yscale("log") |
---|
53 | |
---|
54 | qmin = float(self.model.item(W.W_QMIN).text()) |
---|
55 | qmax1 = float(self.model.item(W.W_QMAX).text()) |
---|
56 | qmax2 = float(self.model.item(W.W_QCUTOFF).text()) |
---|
57 | |
---|
58 | if self.data: |
---|
59 | self.axes.plot(self.data.x, self.data.y) |
---|
60 | self.axes.axvline(qmin) |
---|
61 | self.axes.axvline(qmax1) |
---|
62 | self.axes.axvline(qmax2) |
---|
63 | self.axes.set_xlim(min(self.data.x), max(self.data.x)*1.5-0.5*min(self.data.x)) |
---|
64 | if self.extrap: |
---|
65 | self.axes.plot(self.extrap.x, self.extrap.y) |
---|
66 | |
---|
67 | self.draw() |
---|
68 | |
---|
69 | def drawRealSpace(self): |
---|
70 | self.fig.clf() |
---|
71 | |
---|
72 | self.axes = self.fig.add_subplot(111) |
---|
73 | self.axes.set_xscale("linear") |
---|
74 | self.axes.set_yscale("linear") |
---|
75 | |
---|
76 | if self.data: |
---|
77 | self.axes.plot(self.data.x, self.data.y) |
---|
78 | self.axes.set_xlim(min(self.data.x), max(self.data.x)/4) |
---|
79 | |
---|
80 | self.draw() |
---|
81 | |
---|
82 | |
---|
83 | # def sizeHint(self): |
---|
84 | # w, h = self.get_width_height() |
---|
85 | # return QSize(w, h) |
---|
86 | |
---|
87 | # def minimumSizeHint(self): |
---|
88 | # return QSize(10, 10) |
---|
89 | |
---|
90 | |
---|
91 | class CorfuncWindow(QtGui.QDialog, Ui_CorfuncDialog): |
---|
92 | # The controller which is responsible for managing signal slots connections |
---|
93 | # for the gui and providing an interface to the data model. |
---|
94 | name = "Corfunc" # For displaying in the combo box |
---|
95 | #def __init__(self, manager=None, parent=None): |
---|
96 | def __init__(self, parent=None): |
---|
97 | super(CorfuncWindow, self).__init__() |
---|
98 | self.setupUi(self) |
---|
99 | |
---|
100 | self.setWindowTitle("Corfunc Perspective") |
---|
101 | |
---|
102 | self.model = QtGui.QStandardItemModel(self) |
---|
103 | self.communicate = GuiUtils.Communicate() |
---|
104 | self._calculator = CorfuncCalculator() |
---|
105 | |
---|
106 | self._canvas = MyMplCanvas(self.model, self) |
---|
107 | self._realplot = MyMplCanvas(self.model, self) |
---|
108 | self.verticalLayout_7.insertWidget(0, self._canvas) |
---|
109 | self.verticalLayout_7.insertWidget(1, self._realplot) |
---|
110 | |
---|
111 | # Connect buttons to slots. |
---|
112 | # Needs to be done early so default values propagate properly. |
---|
113 | self.setupSlots() |
---|
114 | |
---|
115 | # Set up the model. |
---|
116 | self.setupModel() |
---|
117 | |
---|
118 | # Set up the mapper |
---|
119 | self.setupMapper() |
---|
120 | |
---|
121 | def setupSlots(self): |
---|
122 | self.extrapolateBtn.clicked.connect(self.extrapolate) |
---|
123 | self.transformBtn.clicked.connect(self.transform) |
---|
124 | |
---|
125 | self.calculateBgBtn.clicked.connect(self.calculateBackground) |
---|
126 | |
---|
127 | self.model.itemChanged.connect(self.modelChanged) |
---|
128 | |
---|
129 | def setupModel(self): |
---|
130 | self.model.setItem(W.W_QMIN, |
---|
131 | QtGui.QStandardItem("0.01")) |
---|
132 | self.model.setItem(W.W_QMAX, |
---|
133 | QtGui.QStandardItem("0.20")) |
---|
134 | self.model.setItem(W.W_QCUTOFF, |
---|
135 | QtGui.QStandardItem("0.22")) |
---|
136 | self.model.setItem(W.W_BACKGROUND, |
---|
137 | QtGui.QStandardItem("0")) |
---|
138 | self.model.setItem(W.W_TRANSFORM, |
---|
139 | QtGui.QStandardItem("Fourier")) |
---|
140 | self.model.setItem(W.W_GUINIERA, |
---|
141 | QtGui.QStandardItem("0.0")) |
---|
142 | self.model.setItem(W.W_GUINIERB, |
---|
143 | QtGui.QStandardItem("0.0")) |
---|
144 | self.model.setItem(W.W_PORODK, |
---|
145 | QtGui.QStandardItem("0.0")) |
---|
146 | self.model.setItem(W.W_PORODSIGMA, |
---|
147 | QtGui.QStandardItem("0.0")) |
---|
148 | self.model.setItem(W.W_CORETHICK, QtGui.QStandardItem(str(0))) |
---|
149 | self.model.setItem(W.W_INTTHICK, QtGui.QStandardItem(str(0))) |
---|
150 | self.model.setItem(W.W_HARDBLOCK, QtGui.QStandardItem(str(0))) |
---|
151 | self.model.setItem(W.W_CRYSTAL, QtGui.QStandardItem(str(0))) |
---|
152 | self.model.setItem(W.W_POLY, QtGui.QStandardItem(str(0))) |
---|
153 | self.model.setItem(W.W_PERIOD, QtGui.QStandardItem(str(0))) |
---|
154 | |
---|
155 | def modelChanged(self, item): |
---|
156 | self.mapper.toFirst() |
---|
157 | self._canvas.drawQSpace() |
---|
158 | |
---|
159 | def _update_calculator(self): |
---|
160 | self._calculator.lowerq = float(self.model.item(W.W_QMIN).text()) |
---|
161 | qmax1 = float(self.model.item(W.W_QMAX).text()) |
---|
162 | qmax2 = float(self.model.item(W.W_QCUTOFF).text()) |
---|
163 | self._calculator.upperq = (qmax1, qmax2) |
---|
164 | self._calculator.background = float(self.model.item(W.W_BACKGROUND).text()) |
---|
165 | |
---|
166 | def extrapolate(self): |
---|
167 | self._update_calculator() |
---|
168 | params, extrapolation = self._calculator.compute_extrapolation() |
---|
169 | |
---|
170 | self.model.setItem(W.W_GUINIERA, QtGui.QStandardItem(str(params['A']))) |
---|
171 | self.model.setItem(W.W_GUINIERB, QtGui.QStandardItem(str(params['B']))) |
---|
172 | self.model.setItem(W.W_PORODK, QtGui.QStandardItem(str(params['K']))) |
---|
173 | self.model.setItem(W.W_PORODSIGMA, QtGui.QStandardItem(str(params['sigma']))) |
---|
174 | |
---|
175 | self._canvas.extrap = extrapolation |
---|
176 | self._canvas.drawQSpace() |
---|
177 | |
---|
178 | |
---|
179 | def transform(self): |
---|
180 | if self.fourierBtn.isChecked(): |
---|
181 | method = "fourier" |
---|
182 | elif self.hilbertBtn.isChecked(): |
---|
183 | method = "hilbert" |
---|
184 | |
---|
185 | extrap = self._canvas.extrap |
---|
186 | bg = float(self.model.item(W.W_BACKGROUND).text()) |
---|
187 | def updatefn(*args, **kwargs): |
---|
188 | pass |
---|
189 | |
---|
190 | def completefn(transform): |
---|
191 | self._realplot.data = transform |
---|
192 | self._realplot.drawRealSpace() |
---|
193 | params = self._calculator.extract_parameters(transform) |
---|
194 | self.model.setItem(W.W_CORETHICK, QtGui.QStandardItem(str(params['d0']))) |
---|
195 | self.model.setItem(W.W_INTTHICK, QtGui.QStandardItem(str(params['dtr']))) |
---|
196 | self.model.setItem(W.W_HARDBLOCK, QtGui.QStandardItem(str(params['Lc']))) |
---|
197 | self.model.setItem(W.W_CRYSTAL, QtGui.QStandardItem(str(params['fill']))) |
---|
198 | self.model.setItem(W.W_POLY, QtGui.QStandardItem(str(params['A']))) |
---|
199 | self.model.setItem(W.W_PERIOD, QtGui.QStandardItem(str(params['max']))) |
---|
200 | |
---|
201 | self._update_calculator() |
---|
202 | self._calculator.compute_transform(extrap, method, bg, completefn, updatefn) |
---|
203 | |
---|
204 | |
---|
205 | def setupMapper(self): |
---|
206 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
207 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
208 | self.mapper.setModel(self.model) |
---|
209 | |
---|
210 | self.mapper.addMapping(self.qMin, W.W_QMIN) |
---|
211 | self.mapper.addMapping(self.qMax1, W.W_QMAX) |
---|
212 | self.mapper.addMapping(self.qMax2, W.W_QCUTOFF) |
---|
213 | self.mapper.addMapping(self.bg, W.W_BACKGROUND) |
---|
214 | |
---|
215 | self.mapper.addMapping(self.guinierA, W.W_GUINIERA) |
---|
216 | self.mapper.addMapping(self.guinierB, W.W_GUINIERB) |
---|
217 | self.mapper.addMapping(self.porodK, W.W_PORODK) |
---|
218 | self.mapper.addMapping(self.porodSigma, W.W_PORODSIGMA) |
---|
219 | |
---|
220 | self.mapper.addMapping(self.avgCoreThick, W.W_CORETHICK) |
---|
221 | self.mapper.addMapping(self.avgIntThick, W.W_INTTHICK) |
---|
222 | self.mapper.addMapping(self.avgHardBlock, W.W_HARDBLOCK) |
---|
223 | self.mapper.addMapping(self.polydisp, W.W_POLY) |
---|
224 | self.mapper.addMapping(self.longPeriod, W.W_PERIOD) |
---|
225 | self.mapper.addMapping(self.localCrystal, W.W_CRYSTAL) |
---|
226 | |
---|
227 | self.mapper.toFirst() |
---|
228 | |
---|
229 | def calculateBackground(self): |
---|
230 | self._update_calculator() |
---|
231 | bg = self._calculator.compute_background() |
---|
232 | temp = QtGui.QStandardItem(str(bg)) |
---|
233 | self.model.setItem(W.W_BACKGROUND, temp) |
---|
234 | |
---|
235 | def allowBatch(self): |
---|
236 | """ |
---|
237 | We cannot perform corfunc analysis in batch at this time. |
---|
238 | """ |
---|
239 | return False |
---|
240 | |
---|
241 | def setData(self, data_item, is_batch=False): |
---|
242 | """ |
---|
243 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
244 | Pass it over to the calculator |
---|
245 | """ |
---|
246 | if not isinstance(data_item, list): |
---|
247 | msg = "Incorrect type passed to the Corfunc Perpsective" |
---|
248 | raise AttributeError(msg) |
---|
249 | |
---|
250 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
251 | msg = "Incorrect type passed to the Corfunc Perspective" |
---|
252 | raise AttributeError(msg) |
---|
253 | |
---|
254 | self._model_item = data_item[0] |
---|
255 | data = GuiUtils.dataFromItem(self._model_item) |
---|
256 | self._calculator.set_data(data) |
---|
257 | |
---|
258 | self._canvas.data = data |
---|
259 | self._canvas.drawQSpace() |
---|
260 | |
---|
261 | # self.model.item(WIDGETS.W_FILENAME).setData(QtCoreQVariant(self._model_item.text())) |
---|
262 | |
---|
263 | def setClosable(self, value=True): |
---|
264 | """ |
---|
265 | Allow outsiders close this widget |
---|
266 | """ |
---|
267 | assert isinstance(value, bool) |
---|
268 | |
---|
269 | self._allow_close = value |
---|
270 | |
---|
271 | |
---|
272 | if __name__ == "__main__": |
---|
273 | app = QtGui.QApplication([]) |
---|
274 | import qt4reactor |
---|
275 | # qt4reactor.install() |
---|
276 | # DO NOT move the following import to the top! |
---|
277 | # (unless you know what you're doing) |
---|
278 | from twisted.internet import reactor |
---|
279 | dlg = CorfuncWindow(reactor) |
---|
280 | print(dlg) |
---|
281 | dlg.show() |
---|
282 | # print(reactor) |
---|
283 | # reactor.run() |
---|
284 | sys.exit(app.exec_()) |
---|