1 | # global |
---|
2 | import sys |
---|
3 | import os |
---|
4 | from PyQt4 import QtCore |
---|
5 | from PyQt4 import QtGui |
---|
6 | from PyQt4 import QtWebKit |
---|
7 | |
---|
8 | from twisted.internet import threads |
---|
9 | from twisted.internet import reactor |
---|
10 | |
---|
11 | # sas-global |
---|
12 | from sas.sascalc.invariant import invariant |
---|
13 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
14 | #import GuiUtils |
---|
15 | import sas.qtgui.GuiUtils as GuiUtils |
---|
16 | |
---|
17 | # local |
---|
18 | from UI.TabbedInvariantUI import Ui_tabbedInvariantUI |
---|
19 | from InvariantDetails import DetailsDialog |
---|
20 | from InvariantUtils import WIDGETS |
---|
21 | |
---|
22 | # The minimum q-value to be used when extrapolating |
---|
23 | Q_MINIMUM = 1e-5 |
---|
24 | # The maximum q-value to be used when extrapolating |
---|
25 | Q_MAXIMUM = 10 |
---|
26 | # the ratio of maximum q value/(qmax of data) to plot the theory data |
---|
27 | Q_MAXIMUM_PLOT = 3 |
---|
28 | |
---|
29 | |
---|
30 | class MyModel(object): |
---|
31 | def __init__(self): |
---|
32 | self._model = QtGui.QStandardItemModel(self) |
---|
33 | |
---|
34 | def addItem(self, item): |
---|
35 | item = QtGui.QStandardItem(str(item)) |
---|
36 | self._model.appendRow(item) |
---|
37 | |
---|
38 | class InvariantWindow(QtGui.QDialog, Ui_tabbedInvariantUI): |
---|
39 | # The controller which is responsible for managing signal slots connections |
---|
40 | # for the gui and providing an interface to the data model. |
---|
41 | def __init__(self, manager=None, parent=None): |
---|
42 | super(InvariantWindow, self).__init__(parent) |
---|
43 | self.setupUi(self) |
---|
44 | |
---|
45 | self.setWindowTitle("Invariant Perspective") |
---|
46 | |
---|
47 | # initial input params |
---|
48 | self._background = 0.0 |
---|
49 | self._scale = 1.0 |
---|
50 | self._contrast = 1.0 |
---|
51 | self._porod = 1.0 |
---|
52 | self._npoints_low = 10 |
---|
53 | self._npoints_high = 10 |
---|
54 | self._power_low = 4 |
---|
55 | |
---|
56 | self._manager = manager |
---|
57 | self._reactor = self._manager.reactor() |
---|
58 | self._model_item = QtGui.QStandardItem() |
---|
59 | |
---|
60 | self._helpView = QtWebKit.QWebView() |
---|
61 | self.detailsDialog = DetailsDialog(self) |
---|
62 | |
---|
63 | self._low_extrapolate = False |
---|
64 | self._low_guinier = True |
---|
65 | self._low_fit = True |
---|
66 | self._high_extrapolate = False |
---|
67 | self._high_power_value = False |
---|
68 | |
---|
69 | # no reason to have this widget resizable |
---|
70 | self.setFixedSize(self.minimumSizeHint()) |
---|
71 | |
---|
72 | self.communicate = GuiUtils.Communicate() |
---|
73 | |
---|
74 | self._data = None |
---|
75 | self._path = "" |
---|
76 | |
---|
77 | # Mask file selector |
---|
78 | ################################################### |
---|
79 | #self._path = "cyl_400_20.txt" |
---|
80 | #from sas.sascalc.dataloader.loader import Loader |
---|
81 | #loader = Loader() |
---|
82 | #try: |
---|
83 | # self._data = loader.load(self._path) |
---|
84 | #except: |
---|
85 | # raise |
---|
86 | ################################################### |
---|
87 | |
---|
88 | self.lineEdit_8.setText(str(Q_MINIMUM)) |
---|
89 | self.lineEdit_9.setText(str(Q_MAXIMUM)) |
---|
90 | |
---|
91 | # Let's choose the Standard Item Model. |
---|
92 | self.model = QtGui.QStandardItemModel(self) |
---|
93 | |
---|
94 | # Connect buttons to slots. |
---|
95 | # Needs to be done early so default values propagate properly. |
---|
96 | self.setupSlots() |
---|
97 | |
---|
98 | # Set up the model. |
---|
99 | self.setupModel() |
---|
100 | |
---|
101 | # Set up the mapper |
---|
102 | self.setupMapper() |
---|
103 | |
---|
104 | def closeEvent(self, event): |
---|
105 | """ |
---|
106 | Overwrite the default close method of QWidget |
---|
107 | """ |
---|
108 | # No close on perspectives - one must always be active. |
---|
109 | event.ignore() |
---|
110 | |
---|
111 | def communicator(self): |
---|
112 | """ Getter for the communicator """ |
---|
113 | return self.communicate |
---|
114 | |
---|
115 | def updateFromModel(self): |
---|
116 | """ |
---|
117 | update the globals based on the data in the model |
---|
118 | """ |
---|
119 | self._background = float(self.model.item(WIDGETS.W_BACKGROUND).text()) |
---|
120 | self._contrast = float(self.model.item(WIDGETS.W_CONTRAST).text()) |
---|
121 | self._scale = float(self.model.item(WIDGETS.W_SCALE).text()) |
---|
122 | |
---|
123 | # High extrapolate |
---|
124 | self._low_extrapolate = ( str(self.model.item(WIDGETS.W_ENABLE_LOWQ).text()) == 'true') |
---|
125 | self._low_points = float(self.model.item(WIDGETS.W_NPTS_LOWQ).text()) |
---|
126 | self._low_guinier = ( str(self.model.item(WIDGETS.W_LOWQ_GUINIER).text()) == 'true') |
---|
127 | self._low_fit = ( str(self.model.item(WIDGETS.W_LOWQ_FIT).text()) == 'true') |
---|
128 | self._low_power_value = float(self.model.item(WIDGETS.W_LOWQ_POWER_VALUE).text()) |
---|
129 | |
---|
130 | # High extrapolating |
---|
131 | self._high_extrapolate = ( str(self.model.item(WIDGETS.W_ENABLE_HIGHQ).text()) == 'true') |
---|
132 | self._high_points = float(self.model.item(WIDGETS.W_NPTS_HIGHQ).text()) |
---|
133 | self._high_fit = ( str(self.model.item(WIDGETS.W_HIGHQ_FIT).text()) == 'true') |
---|
134 | self._high_power_value = float(self.model.item(WIDGETS.W_HIGHQ_POWER_VALUE).text()) |
---|
135 | |
---|
136 | def calculateInvariant(self): |
---|
137 | """ |
---|
138 | Use twisted to thread the calculations away. |
---|
139 | """ |
---|
140 | # Find out if extrapolation needs to be used. |
---|
141 | extrapolation = None |
---|
142 | if self._low_extrapolate and not self._high_extrapolate: |
---|
143 | extrapolation = "low" |
---|
144 | elif not self._low_extrapolate and self._high_extrapolate: |
---|
145 | extrapolation = "high" |
---|
146 | elif self._low_extrapolate and self._high_extrapolate: |
---|
147 | extrapolation = "both" |
---|
148 | try: |
---|
149 | # modify the Calculate button to indicate background process |
---|
150 | self.pushButton.setText("Calculating...") |
---|
151 | self.pushButton.setEnabled(False) |
---|
152 | self.style = self.pushButton.styleSheet() |
---|
153 | self.pushButton.setStyleSheet("background-color: rgb(255, 255, 0); color: rgb(0, 0, 0)") |
---|
154 | # Send the calculations to separate thread. |
---|
155 | d = threads.deferToThread(self.calculateThread, extrapolation) |
---|
156 | # Add deferred callback for call return |
---|
157 | d.addCallback(self.plotResult) |
---|
158 | except Exception as ex: |
---|
159 | # Set the button back to available |
---|
160 | self.pushButton.setEnabled(True) |
---|
161 | self.pushButton.setText("Calculate") |
---|
162 | self.pushButton.setStyleSheet(self.style) |
---|
163 | |
---|
164 | |
---|
165 | def plotResult(self, model): |
---|
166 | """ |
---|
167 | """ |
---|
168 | self.model = model |
---|
169 | self.mapper.toFirst() |
---|
170 | |
---|
171 | # Set the button back to available |
---|
172 | self.pushButton.setEnabled(True) |
---|
173 | self.pushButton.setText("Calculate") |
---|
174 | self.pushButton.setStyleSheet(self.style) |
---|
175 | |
---|
176 | # Send the modified model item to DE for keeping in the model |
---|
177 | self.communicate.updateModelFromPerspectiveSignal.emit(self._model_item) |
---|
178 | |
---|
179 | |
---|
180 | def calculateThread(self, extrapolation): |
---|
181 | """ |
---|
182 | Perform Invariant calculations. |
---|
183 | |
---|
184 | TODO: Create a dictionary of results to be sent to DE on completion. |
---|
185 | """ |
---|
186 | self.updateFromModel() |
---|
187 | |
---|
188 | qstar_low = 0.0 |
---|
189 | qstar_low_err = 0.0 |
---|
190 | qstar_high = 0.0 |
---|
191 | qstar_high_err = 0.0 |
---|
192 | qstar_total = 0.0 |
---|
193 | qstar_total_low_err = 0.0 |
---|
194 | |
---|
195 | # Prepare the invariant object |
---|
196 | inv = invariant.InvariantCalculator(data=self._data, |
---|
197 | background = self._background, |
---|
198 | scale = self._scale) |
---|
199 | |
---|
200 | if self._low_extrapolate: |
---|
201 | function_low = "power_law" |
---|
202 | if self._low_guinier: |
---|
203 | function_low = "guinier" |
---|
204 | if self._low_fit: |
---|
205 | self._low_power_value = None |
---|
206 | inv.set_extrapolation(range="low", |
---|
207 | npts=self._low_points, |
---|
208 | function=function_low, |
---|
209 | power=self._low_power_value) |
---|
210 | |
---|
211 | if self._high_extrapolate: |
---|
212 | function_low = "power_law" |
---|
213 | inv.set_extrapolation(range="high", |
---|
214 | npts=self._high_points, |
---|
215 | function=function_low, |
---|
216 | power=self._low_power_value) |
---|
217 | |
---|
218 | #Compute invariant |
---|
219 | # TODO: proper exception handling and logic - |
---|
220 | # display info, update lineedits, don't run extrapolations etc. |
---|
221 | calculation_failed = False |
---|
222 | try: |
---|
223 | qstar_total, qstar_total_error = inv.get_qstar_with_error() |
---|
224 | except Exception as ex: |
---|
225 | calculation_failed = True |
---|
226 | # Display relevant information |
---|
227 | item = QtGui.QStandardItem("ERROR") |
---|
228 | self.model.setItem(WIDGETS.W_INVARIANT, item) |
---|
229 | item = QtGui.QStandardItem("ERROR") |
---|
230 | self.model.setItem(WIDGETS.W_INVARIANT_ERR, item) |
---|
231 | try: |
---|
232 | volume_fraction, volume_fraction_error = \ |
---|
233 | inv.get_volume_fraction_with_error(self._contrast) |
---|
234 | except Exception as ex: |
---|
235 | calculation_failed = True |
---|
236 | # Display relevant information |
---|
237 | item = QtGui.QStandardItem("ERROR") |
---|
238 | self.model.setItem(WIDGETS.W_VOLUME_FRACTION, item) |
---|
239 | item = QtGui.QStandardItem("ERROR") |
---|
240 | self.model.setItem(WIDGETS.W_VOLUME_FRACTION_ERR, item) |
---|
241 | try: |
---|
242 | surface, surface_error = \ |
---|
243 | inv.get_surface_with_error(self._contrast, self._porod) |
---|
244 | except Exception as ex: |
---|
245 | calculation_failed = True |
---|
246 | # Display relevant information |
---|
247 | item = QtGui.QStandardItem("ERROR") |
---|
248 | self.model.setItem(WIDGETS.W_SPECIFIC_SURFACE, item) |
---|
249 | item = QtGui.QStandardItem("ERROR") |
---|
250 | self.model.setItem(WIDGETS.W_SPECIFIC_SURFACE_ERR, item) |
---|
251 | |
---|
252 | if(calculation_failed): |
---|
253 | # TODO: NOTIFY THE GUI MANAGER!! |
---|
254 | self.mapper.toFirst() |
---|
255 | return self.model |
---|
256 | |
---|
257 | if self._low_extrapolate: |
---|
258 | # for presentation in InvariantDetails |
---|
259 | qstar_low, qstar_low_err = inv.get_qstar_low() |
---|
260 | extrapolated_data = inv.get_extra_data_low(self._low_points) |
---|
261 | power_low = inv.get_extrapolation_power(range='low') |
---|
262 | |
---|
263 | #inv.data = extrapolated_data |
---|
264 | #qstar_total, qstar_total_error = inv.get_qstar_with_error() |
---|
265 | |
---|
266 | # Plot the chart |
---|
267 | title = "Low-Q extrapolation" |
---|
268 | |
---|
269 | # Convert the data into plottable |
---|
270 | extrapolated_data = self._manager.createGuiData(extrapolated_data) |
---|
271 | extrapolated_data.name = title |
---|
272 | extrapolated_data.title = title |
---|
273 | |
---|
274 | # Add the plot to the model item |
---|
275 | # variant_item = QtCore.QVariant(self._plotter) |
---|
276 | variant_item = QtCore.QVariant(extrapolated_data) |
---|
277 | |
---|
278 | # This needs to run in the main thread |
---|
279 | reactor.callFromThread(GuiUtils.updateModelItemWithPlot, |
---|
280 | self._model_item, variant_item, title) |
---|
281 | |
---|
282 | if self._high_extrapolate: |
---|
283 | # for presentation in InvariantDetails |
---|
284 | qmax_plot = Q_MAXIMUM_PLOT * max(self._data.x) |
---|
285 | if qmax_plot > Q_MAXIMUM: |
---|
286 | qmax_plot = Q_MAXIMUM |
---|
287 | qstar_high, qstar_high_err = inv.get_qstar_high() |
---|
288 | power_high = inv.get_extrapolation_power(range='high') |
---|
289 | high_out_data = inv.get_extra_data_high(q_end=qmax_plot, npts=500) |
---|
290 | |
---|
291 | # Plot the chart |
---|
292 | title = "High-Q extrapolation" |
---|
293 | |
---|
294 | # Convert the data into plottable |
---|
295 | high_out_data = self._manager.createGuiData(high_out_data) |
---|
296 | high_out_data.name = title |
---|
297 | high_out_data.title = title |
---|
298 | |
---|
299 | # Add the plot to the model item |
---|
300 | # variant_item = QtCore.QVariant(self._plotter) |
---|
301 | variant_item = QtCore.QVariant(high_out_data) |
---|
302 | # This needs to run in the main thread |
---|
303 | reactor.callFromThread(GuiUtils.updateModelItemWithPlot, |
---|
304 | self._model_item, variant_item, title) |
---|
305 | |
---|
306 | item = QtGui.QStandardItem(str(float('%.5g'% volume_fraction))) |
---|
307 | self.model.setItem(WIDGETS.W_VOLUME_FRACTION, item) |
---|
308 | item = QtGui.QStandardItem(str(float('%.5g'% volume_fraction_error))) |
---|
309 | self.model.setItem(WIDGETS.W_VOLUME_FRACTION_ERR, item) |
---|
310 | item = QtGui.QStandardItem(str(float('%.5g'% surface))) |
---|
311 | self.model.setItem(WIDGETS.W_SPECIFIC_SURFACE, item) |
---|
312 | item = QtGui.QStandardItem(str(float('%.5g'% surface_error))) |
---|
313 | self.model.setItem(WIDGETS.W_SPECIFIC_SURFACE_ERR, item) |
---|
314 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_total))) |
---|
315 | self.model.setItem(WIDGETS.W_INVARIANT, item) |
---|
316 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_total_error))) |
---|
317 | self.model.setItem(WIDGETS.W_INVARIANT_ERR, item) |
---|
318 | |
---|
319 | #item = QtGui.QStandardItem(str(float('%.5g'% qstar_total))) |
---|
320 | #self.model.setItem(WIDGETS.D_TOTAL_QSTAR, item) |
---|
321 | #item = QtGui.QStandardItem(str(float('%.5g'% qstar_total_err))) |
---|
322 | #self.model.setItem(WIDGETS.D_TOTAL_QSTAR_ERR, item) |
---|
323 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_low))) |
---|
324 | self.model.setItem(WIDGETS.D_LOW_QSTAR, item) |
---|
325 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_low_err))) |
---|
326 | self.model.setItem(WIDGETS.D_LOW_QSTAR_ERR, item) |
---|
327 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_high))) |
---|
328 | self.model.setItem(WIDGETS.D_HIGH_QSTAR, item) |
---|
329 | item = QtGui.QStandardItem(str(float('%.5g'% qstar_high_err))) |
---|
330 | self.model.setItem(WIDGETS.D_HIGH_QSTAR_ERR, item) |
---|
331 | |
---|
332 | self.mapper.toFirst() |
---|
333 | |
---|
334 | return self.model |
---|
335 | |
---|
336 | def title(self): |
---|
337 | """ |
---|
338 | Perspective name |
---|
339 | """ |
---|
340 | return "Invariant panel" |
---|
341 | |
---|
342 | def status(self): |
---|
343 | """ |
---|
344 | """ |
---|
345 | self.detailsDialog.setModel(self.model) |
---|
346 | self.detailsDialog.showDialog() |
---|
347 | |
---|
348 | def help(self): |
---|
349 | """ |
---|
350 | """ |
---|
351 | _TreeLocation = self._manager.HELP_DIRECTORY_LOCATION + \ |
---|
352 | "/user/sasgui/perspectives/invariant/invariant_help.html" |
---|
353 | self._helpView.load(QtCore.QUrl(_TreeLocation)) |
---|
354 | self._helpView.show() |
---|
355 | |
---|
356 | def setupSlots(self): |
---|
357 | self.pushButton.clicked.connect(self.calculateInvariant) |
---|
358 | self.pushButton_2.clicked.connect(self.status) |
---|
359 | self.pushButton_3.clicked.connect(self.help) |
---|
360 | |
---|
361 | self.radioButton.toggled.connect(self.lowGuinierAndPowerToggle) |
---|
362 | self.radioButton_8.toggled.connect(self.hiFitAndFixToggle) |
---|
363 | |
---|
364 | self.radioButton_3.toggled.connect(self.lowFitAndFixToggle) |
---|
365 | |
---|
366 | # Bug workaround for QDataWidgetMapper() not reacting to checkbox clicks. |
---|
367 | # https://bugreports.qt.io/browse/QTBUG-1818 |
---|
368 | self.checkBox.clicked.connect(self.setFocus) |
---|
369 | self.checkBox_2.clicked.connect(self.setFocus) |
---|
370 | |
---|
371 | self.model.itemChanged.connect(self.modelChanged) |
---|
372 | |
---|
373 | def modelChanged(self, item): |
---|
374 | """ |
---|
375 | """ |
---|
376 | if item.row() == WIDGETS.W_ENABLE_LOWQ: |
---|
377 | toggle = (str(item.text()) == 'true') |
---|
378 | self._low_extrapolate = toggle |
---|
379 | self.lowQToggle(toggle) |
---|
380 | elif item.row() == WIDGETS.W_ENABLE_HIGHQ: |
---|
381 | toggle = (str(item.text()) == 'true') |
---|
382 | self._high_extrapolate = toggle |
---|
383 | self.highQToggle(toggle) |
---|
384 | |
---|
385 | def lowGuinierAndPowerToggle(self, toggle): |
---|
386 | """ |
---|
387 | """ |
---|
388 | self._low_guinier = toggle |
---|
389 | toggle = not toggle |
---|
390 | self.lineEdit_11.setEnabled(toggle) |
---|
391 | self.radioButton_3.setEnabled(toggle) |
---|
392 | self.radioButton_4.setEnabled(toggle) |
---|
393 | self.lineEdit_11.setEnabled(toggle and (not self._low_fit)) |
---|
394 | |
---|
395 | def lowFitAndFixToggle(self, toggle): |
---|
396 | """ |
---|
397 | """ |
---|
398 | self._low_fit = toggle |
---|
399 | toggle = not toggle |
---|
400 | self.lineEdit_11.setEnabled(toggle) |
---|
401 | |
---|
402 | def hiFitAndFixToggle(self, toggle): |
---|
403 | """ |
---|
404 | """ |
---|
405 | self.lineEdit_13.setEnabled(toggle) |
---|
406 | |
---|
407 | def highQToggle(self, clicked): |
---|
408 | """ |
---|
409 | Disable/enable High Q extrapolation |
---|
410 | """ |
---|
411 | self.radioButton_7.setEnabled(clicked) |
---|
412 | self.radioButton_8.setEnabled(clicked) |
---|
413 | self.lineEdit_12.setEnabled(clicked) |
---|
414 | self.lineEdit_13.setEnabled(clicked) |
---|
415 | |
---|
416 | def lowQToggle(self, clicked): |
---|
417 | """ |
---|
418 | Disable/enable Low Q extrapolation |
---|
419 | """ |
---|
420 | self.radioButton.setEnabled(clicked) |
---|
421 | self.radioButton_2.setEnabled(clicked) |
---|
422 | self.lineEdit_11.setEnabled(clicked and not self._low_fit) |
---|
423 | |
---|
424 | # Enable subelements |
---|
425 | self.radioButton_3.setEnabled(clicked and not self._low_guinier) |
---|
426 | self.radioButton_4.setEnabled(clicked and not self._low_guinier) |
---|
427 | self.lineEdit_10.setEnabled(clicked and not self._low_guinier) |
---|
428 | |
---|
429 | def setupModel(self): |
---|
430 | |
---|
431 | # filename |
---|
432 | item = QtGui.QStandardItem(self._path) |
---|
433 | self.model.setItem(WIDGETS.W_FILENAME, item) |
---|
434 | |
---|
435 | # add Q parameters to the model |
---|
436 | #qmin = min(self._data.x) |
---|
437 | qmin = 0.0 |
---|
438 | item = QtGui.QStandardItem(str(qmin)) |
---|
439 | self.model.setItem(WIDGETS.W_QMIN, item) |
---|
440 | qmax = 0.0 |
---|
441 | item = QtGui.QStandardItem(str(qmax)) |
---|
442 | self.model.setItem(WIDGETS.W_QMAX, item) |
---|
443 | |
---|
444 | # add custom input params |
---|
445 | item = QtGui.QStandardItem(str(self._background)) |
---|
446 | self.model.setItem(WIDGETS.W_BACKGROUND, item) |
---|
447 | item = QtGui.QStandardItem(str(self._contrast)) |
---|
448 | self.model.setItem(WIDGETS.W_CONTRAST, item) |
---|
449 | item = QtGui.QStandardItem(str(self._scale)) |
---|
450 | self.model.setItem(WIDGETS.W_SCALE, item) |
---|
451 | |
---|
452 | # Dialog elements |
---|
453 | itemf = QtGui.QStandardItem("false") |
---|
454 | self.model.setItem(WIDGETS.W_ENABLE_HIGHQ, itemf) |
---|
455 | itemf = QtGui.QStandardItem("false") |
---|
456 | self.model.setItem(WIDGETS.W_ENABLE_LOWQ, itemf) |
---|
457 | |
---|
458 | item = QtGui.QStandardItem(str(self._npoints_low)) |
---|
459 | self.model.setItem(WIDGETS.W_NPTS_LOWQ, item) |
---|
460 | item = QtGui.QStandardItem(str(self._npoints_high)) |
---|
461 | self.model.setItem(WIDGETS.W_NPTS_HIGHQ, item) |
---|
462 | |
---|
463 | itemt = QtGui.QStandardItem("true") |
---|
464 | self.model.setItem(WIDGETS.W_LOWQ_GUINIER, itemt) |
---|
465 | |
---|
466 | itemt = QtGui.QStandardItem("true") |
---|
467 | self.model.setItem(WIDGETS.W_LOWQ_FIT, itemt) |
---|
468 | item = QtGui.QStandardItem(str(self._power_low)) |
---|
469 | self.model.setItem(WIDGETS.W_LOWQ_POWER_VALUE, item) |
---|
470 | |
---|
471 | itemt = QtGui.QStandardItem("true") |
---|
472 | self.model.setItem(WIDGETS.W_HIGHQ_FIT, itemt) |
---|
473 | item = QtGui.QStandardItem(str(self._power_low)) |
---|
474 | self.model.setItem(WIDGETS.W_HIGHQ_POWER_VALUE, item) |
---|
475 | |
---|
476 | |
---|
477 | def setupMapper(self): |
---|
478 | # Set up the mapper. |
---|
479 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
480 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
481 | self.mapper.setModel(self.model) |
---|
482 | |
---|
483 | # Set up the view on the model for testing |
---|
484 | # self.tableView.setModel(self.model) |
---|
485 | |
---|
486 | # Filename |
---|
487 | self.mapper.addMapping(self.lineEdit, WIDGETS.W_FILENAME) |
---|
488 | # Qmin/Qmax |
---|
489 | self.mapper.addMapping(self.lineEdit_2, WIDGETS.W_QMIN) |
---|
490 | self.mapper.addMapping(self.lineEdit_3, WIDGETS.W_QMAX) |
---|
491 | |
---|
492 | # Background |
---|
493 | self.mapper.addMapping(self.lineEdit_4, WIDGETS.W_BACKGROUND) |
---|
494 | # Scale |
---|
495 | self.mapper.addMapping(self.lineEdit_5, WIDGETS.W_SCALE) |
---|
496 | # Contrast |
---|
497 | self.mapper.addMapping(self.lineEdit_6, WIDGETS.W_CONTRAST) |
---|
498 | |
---|
499 | # Lowq/Highq items |
---|
500 | self.mapper.addMapping(self.checkBox, WIDGETS.W_ENABLE_LOWQ) |
---|
501 | self.mapper.addMapping(self.checkBox_2, WIDGETS.W_ENABLE_HIGHQ) |
---|
502 | |
---|
503 | self.mapper.addMapping(self.lineEdit_10, WIDGETS.W_NPTS_LOWQ) |
---|
504 | self.mapper.addMapping(self.radioButton, WIDGETS.W_LOWQ_GUINIER) |
---|
505 | |
---|
506 | self.mapper.addMapping(self.radioButton_3, WIDGETS.W_LOWQ_FIT) |
---|
507 | self.mapper.addMapping(self.lineEdit_11, WIDGETS.W_LOWQ_POWER_VALUE) |
---|
508 | |
---|
509 | self.mapper.addMapping(self.radioButton_7, WIDGETS.W_HIGHQ_FIT) |
---|
510 | self.mapper.addMapping(self.lineEdit_13, WIDGETS.W_HIGHQ_POWER_VALUE) |
---|
511 | |
---|
512 | # Output |
---|
513 | self.mapper.addMapping(self.lineEdit_14, WIDGETS.W_VOLUME_FRACTION) |
---|
514 | self.mapper.addMapping(self.lineEdit_15, WIDGETS.W_VOLUME_FRACTION_ERR) |
---|
515 | self.mapper.addMapping(self.lineEdit_16, WIDGETS.W_SPECIFIC_SURFACE) |
---|
516 | self.mapper.addMapping(self.lineEdit_17, WIDGETS.W_SPECIFIC_SURFACE_ERR) |
---|
517 | self.mapper.addMapping(self.lineEdit_19, WIDGETS.W_INVARIANT) |
---|
518 | self.mapper.addMapping(self.lineEdit_18, WIDGETS.W_INVARIANT_ERR) |
---|
519 | |
---|
520 | self.mapper.toFirst() |
---|
521 | |
---|
522 | def setData(self, data_item): |
---|
523 | """ |
---|
524 | Obtain a QStandardItem object and dissect it to get Data1D/2D |
---|
525 | Pass it over to the calculator |
---|
526 | """ |
---|
527 | if not isinstance(data_item, list): |
---|
528 | msg = "Incorrect type passed to the Invariant Perspective" |
---|
529 | raise AttributeError, msg |
---|
530 | |
---|
531 | if not isinstance(data_item[0], QtGui.QStandardItem): |
---|
532 | msg = "Incorrect type passed to the Invariant Perspective" |
---|
533 | raise AttributeError, msg |
---|
534 | |
---|
535 | self._model_item = data_item[0] |
---|
536 | |
---|
537 | # Extract data on 1st child - this is the Data1D/2D component |
---|
538 | data = GuiUtils.dataFromItem(self._model_item) |
---|
539 | self.model.item(WIDGETS.W_FILENAME).setData(QtCore.QVariant(self._model_item.text())) |
---|
540 | |
---|
541 | ##### DEBUG #### |
---|
542 | # set data in the debug tree view window |
---|
543 | self.treeView.setModel(self.model) |
---|
544 | |
---|
545 | self.calculate(data_list=[data]) |
---|
546 | |
---|
547 | def calculate(self, data_list=None): |
---|
548 | """ |
---|
549 | receive a list of data and compute invariant |
---|
550 | |
---|
551 | TODO: pass warnings/messages to log |
---|
552 | """ |
---|
553 | msg = "" |
---|
554 | data = None |
---|
555 | if data_list is None: |
---|
556 | data_list = [] |
---|
557 | if len(data_list) >= 1: |
---|
558 | if len(data_list) == 1: |
---|
559 | data = data_list[0] |
---|
560 | else: |
---|
561 | data_1d_list = [] |
---|
562 | data_2d_list = [] |
---|
563 | error_msg = "" |
---|
564 | # separate data into data1d and data2d list |
---|
565 | for data in data_list: |
---|
566 | if data is not None: |
---|
567 | if issubclass(data.__class__, Data1D): |
---|
568 | data_1d_list.append(data) |
---|
569 | else: |
---|
570 | error_msg += " %s type %s \n" % (str(data.name), |
---|
571 | str(data.__class__.__name__)) |
---|
572 | data_2d_list.append(data) |
---|
573 | if len(data_2d_list) > 0: |
---|
574 | msg = "Invariant does not support the following data types:\n" |
---|
575 | msg += error_msg |
---|
576 | if len(data_1d_list) == 0: |
---|
577 | # remake this as a qt event |
---|
578 | #wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) |
---|
579 | return |
---|
580 | |
---|
581 | # TODO: add msgbox for data choice |
---|
582 | #msg += "Invariant panel does not allow multiple data!\n" |
---|
583 | #msg += "Please select one.\n" |
---|
584 | #if len(data_list) > 1: |
---|
585 | #from invariant_widgets import DataDialog |
---|
586 | #dlg = DataDialog(data_list=data_1d_list, text=msg) |
---|
587 | #if dlg.ShowModal() == wx.ID_OK: |
---|
588 | # data = dlg.get_data() |
---|
589 | #else: |
---|
590 | # data = None |
---|
591 | #dlg.Destroy() |
---|
592 | |
---|
593 | if data is None: |
---|
594 | msg += "invariant receives no data. \n" |
---|
595 | #wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) |
---|
596 | return |
---|
597 | if not issubclass(data.__class__, Data1D): |
---|
598 | msg += "invariant cannot be computed for data of " |
---|
599 | msg += "type %s\n" % (data.__class__.__name__) |
---|
600 | #wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) |
---|
601 | return |
---|
602 | else: |
---|
603 | #wx.PostEvent(self.parent, NewPlotEvent(plot=data, title=data.title)) |
---|
604 | try: |
---|
605 | self._data = data |
---|
606 | self._path = "unique path" |
---|
607 | self.calculateInvariant() |
---|
608 | except: |
---|
609 | msg = "Invariant Set_data: " + str(sys.exc_value) |
---|
610 | #wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) |
---|
611 | else: |
---|
612 | msg = "invariant cannot be computed for data of " |
---|
613 | msg += "type %s" % (data.__class__.__name__) |
---|
614 | #wx.PostEvent(self.parent, StatusEvent(status=msg, info='error')) |
---|
615 | |
---|
616 | def allowBatch(self): |
---|
617 | """ |
---|
618 | Tell the caller that we don't accept multiple data instances |
---|
619 | """ |
---|
620 | return False |
---|
621 | |
---|
622 | if __name__ == "__main__": |
---|
623 | app = QtGui.QApplication([]) |
---|
624 | import qt4reactor |
---|
625 | qt4reactor.install() |
---|
626 | # DO NOT move the following import to the top! |
---|
627 | # (unless you know what you're doing) |
---|
628 | from twisted.internet import reactor |
---|
629 | dlg = InvariantWindow(reactor) |
---|
630 | dlg.show() |
---|
631 | reactor.run() |
---|