[dab3351] | 1 | import sys |
---|
| 2 | import logging |
---|
[d1a4793] | 3 | import pylab |
---|
[8045572] | 4 | import numpy as np |
---|
[e32b120] | 5 | |
---|
[d2d0441e] | 6 | from PyQt4 import QtGui, QtCore, QtWebKit |
---|
[e7651ff] | 7 | from twisted.internet import reactor |
---|
[6cbf8a5] | 8 | |
---|
| 9 | # sas-global |
---|
[e32b120] | 10 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[6cbf8a5] | 11 | |
---|
[e7651ff] | 12 | # pr inversion GUI elements |
---|
| 13 | from InversionUtils import WIDGETS |
---|
[57ad773] | 14 | import UI.TabbedInversionUI |
---|
| 15 | from UI.TabbedInversionUI import Ui_PrInversion |
---|
[e7651ff] | 16 | from InversionLogic import InversionLogic |
---|
[6cbf8a5] | 17 | |
---|
[bde1a6b] | 18 | # pr inversion calculation elements |
---|
[d1a4793] | 19 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[bde1a6b] | 20 | from sas.sascalc.pr.invertor import Invertor |
---|
| 21 | |
---|
[d1a4793] | 22 | def is_float(value): |
---|
[570a2f73] | 23 | """Converts text input values to floats. Empty strings throw ValueError""" |
---|
[d1a4793] | 24 | try: |
---|
| 25 | return float(value) |
---|
| 26 | except ValueError: |
---|
| 27 | return 0.0 |
---|
[bde1a6b] | 28 | |
---|
[570a2f73] | 29 | |
---|
| 30 | # TODO: Remove data |
---|
| 31 | # TODO: Modify plot references, don't just send new |
---|
| 32 | # TODO: Explorer button - link to PR from AW |
---|
| 33 | # TODO: Update help with batch capabilities |
---|
| 34 | # TODO: Window should not be fixed size |
---|
| 35 | # TODO: Easy way to scroll through results - no tabs in window(?) - 'spreadsheet' |
---|
| 36 | # TODO: Method to export results in some meaningful way |
---|
[e7651ff] | 37 | class InversionWindow(QtGui.QTabWidget, Ui_PrInversion): |
---|
[6cbf8a5] | 38 | """ |
---|
[406f420] | 39 | The main window for the P(r) Inversion perspective. |
---|
[6cbf8a5] | 40 | """ |
---|
| 41 | |
---|
[e7651ff] | 42 | name = "Inversion" |
---|
[6cbf8a5] | 43 | |
---|
| 44 | def __init__(self, parent=None, data=None): |
---|
[e7651ff] | 45 | super(InversionWindow, self).__init__() |
---|
[6cbf8a5] | 46 | self.setupUi(self) |
---|
| 47 | |
---|
| 48 | self.setWindowTitle("P(r) Inversion Perspective") |
---|
| 49 | |
---|
[d2d0441e] | 50 | self._manager = parent |
---|
| 51 | self._model_item = QtGui.QStandardItem() |
---|
| 52 | self._helpView = QtWebKit.QWebView() |
---|
[406f420] | 53 | |
---|
[bde1a6b] | 54 | self.communicate = GuiUtils.Communicate() |
---|
[6cbf8a5] | 55 | |
---|
[e7651ff] | 56 | self.logic = InversionLogic() |
---|
| 57 | |
---|
[bde1a6b] | 58 | # The window should not close |
---|
[6cbf8a5] | 59 | self._allow_close = False |
---|
| 60 | |
---|
[e7651ff] | 61 | # current QStandardItem showing on the panel |
---|
| 62 | self._data = None |
---|
| 63 | # current Data1D as referenced by self._data |
---|
| 64 | self._data_set = None |
---|
[d1a4793] | 65 | |
---|
| 66 | # p(r) calculator |
---|
| 67 | self._calculator = Invertor() |
---|
| 68 | self._last_calculator = None |
---|
| 69 | self.calc_thread = None |
---|
| 70 | self.estimation_thread = None |
---|
[bde1a6b] | 71 | |
---|
| 72 | # Current data object in view |
---|
[8045572] | 73 | self._data_index = 0 |
---|
[bde1a6b] | 74 | # list mapping data to p(r) calculation |
---|
[d132f33] | 75 | self._data_list = {} |
---|
[bde1a6b] | 76 | if not isinstance(data, list): |
---|
[d132f33] | 77 | data_list = [data] |
---|
| 78 | if data is not None: |
---|
| 79 | for datum in data_list: |
---|
| 80 | self._data_list[datum] = self._calculator.clone() |
---|
[bde1a6b] | 81 | |
---|
[570a2f73] | 82 | # plots for current data |
---|
[e7651ff] | 83 | self.pr_plot = None |
---|
| 84 | self.data_plot = None |
---|
[570a2f73] | 85 | # plot references for all data in perspective |
---|
| 86 | self.pr_plot_list = {} |
---|
| 87 | self.data_plot_list = {} |
---|
[e7651ff] | 88 | |
---|
[d2d0441e] | 89 | self.model = QtGui.QStandardItemModel(self) |
---|
[bde1a6b] | 90 | self.mapper = QtGui.QDataWidgetMapper(self) |
---|
[e32b120] | 91 | # Link user interactions with methods |
---|
| 92 | self.setupLinks() |
---|
[bde1a6b] | 93 | # Set values |
---|
| 94 | self.setupModel() |
---|
| 95 | # Set up the Widget Map |
---|
| 96 | self.setupMapper() |
---|
[c00a28ff] | 97 | # Set base window state |
---|
| 98 | self.setupWindow() |
---|
[d2d0441e] | 99 | |
---|
[8045572] | 100 | ###################################################################### |
---|
| 101 | # Base Perspective Class Definitions |
---|
| 102 | |
---|
[e7651ff] | 103 | def communicator(self): |
---|
| 104 | return self.communicate |
---|
| 105 | |
---|
[d2d0441e] | 106 | def allowBatch(self): |
---|
[d132f33] | 107 | return True |
---|
[d2d0441e] | 108 | |
---|
| 109 | def setClosable(self, value=True): |
---|
| 110 | """ |
---|
| 111 | Allow outsiders close this widget |
---|
| 112 | """ |
---|
| 113 | assert isinstance(value, bool) |
---|
| 114 | self._allow_close = value |
---|
| 115 | |
---|
| 116 | def closeEvent(self, event): |
---|
| 117 | """ |
---|
| 118 | Overwrite QDialog close method to allow for custom widget close |
---|
| 119 | """ |
---|
| 120 | if self._allow_close: |
---|
| 121 | # reset the closability flag |
---|
| 122 | self.setClosable(value=False) |
---|
| 123 | event.accept() |
---|
| 124 | else: |
---|
| 125 | event.ignore() |
---|
| 126 | # Maybe we should just minimize |
---|
| 127 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
[e32b120] | 128 | |
---|
[8045572] | 129 | ###################################################################### |
---|
| 130 | # Initialization routines |
---|
| 131 | |
---|
[e32b120] | 132 | def setupLinks(self): |
---|
[bde1a6b] | 133 | """Connect the use controls to their appropriate methods""" |
---|
[d132f33] | 134 | self.dataList.currentIndexChanged.connect(self.displayChange) |
---|
[c00a28ff] | 135 | self.calculateAllButton.clicked.connect(self.startThreadAll) |
---|
| 136 | self.calculateThisButton.clicked.connect(self.startThread) |
---|
[e32b120] | 137 | self.helpButton.clicked.connect(self.help) |
---|
| 138 | self.estimateBgd.toggled.connect(self.toggleBgd) |
---|
| 139 | self.manualBgd.toggled.connect(self.toggleBgd) |
---|
[dab3351] | 140 | self.regConstantSuggestionButton.clicked.connect(self.acceptAlpha) |
---|
| 141 | self.noOfTermsSuggestionButton.clicked.connect(self.acceptNoTerms) |
---|
[e32b120] | 142 | self.explorerButton.clicked.connect(self.openExplorerWindow) |
---|
[57ad773] | 143 | self.backgroundInput.textChanged.connect( |
---|
| 144 | lambda: self._calculator.set_est_bck(int(is_float( |
---|
| 145 | str(self.backgroundInput.text()))))) |
---|
| 146 | self.minQInput.textChanged.connect( |
---|
| 147 | lambda: self._calculator.set_qmin(is_float( |
---|
| 148 | str(self.minQInput.text())))) |
---|
| 149 | self.regularizationConstantInput.textChanged.connect( |
---|
| 150 | lambda: self._calculator.set_alpha(is_float( |
---|
| 151 | str(self.regularizationConstantInput.text())))) |
---|
| 152 | self.maxDistanceInput.textChanged.connect( |
---|
| 153 | lambda: self._calculator.set_dmax(is_float( |
---|
| 154 | str(self.maxDistanceInput.text())))) |
---|
| 155 | self.maxQInput.textChanged.connect( |
---|
| 156 | lambda: self._calculator.set_qmax(is_float( |
---|
| 157 | str(self.maxQInput.text())))) |
---|
| 158 | self.slitHeightInput.textChanged.connect( |
---|
| 159 | lambda: self._calculator.set_slit_height(is_float( |
---|
| 160 | str(self.slitHeightInput.text())))) |
---|
| 161 | self.slitWidthInput.textChanged.connect( |
---|
| 162 | lambda: self._calculator.set_slit_width(is_float( |
---|
| 163 | str(self.slitHeightInput.text())))) |
---|
[bde1a6b] | 164 | self.model.itemChanged.connect(self.model_changed) |
---|
[e32b120] | 165 | |
---|
| 166 | def setupMapper(self): |
---|
| 167 | # Set up the mapper. |
---|
| 168 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
| 169 | self.mapper.setModel(self.model) |
---|
| 170 | |
---|
| 171 | # Filename |
---|
[8045572] | 172 | self.mapper.addMapping(self.dataList, WIDGETS.W_FILENAME) |
---|
[e32b120] | 173 | # Background |
---|
[bde1a6b] | 174 | self.mapper.addMapping(self.backgroundInput, WIDGETS.W_BACKGROUND_INPUT) |
---|
[e32b120] | 175 | self.mapper.addMapping(self.estimateBgd, WIDGETS.W_ESTIMATE) |
---|
| 176 | self.mapper.addMapping(self.manualBgd, WIDGETS.W_MANUAL_INPUT) |
---|
| 177 | |
---|
| 178 | # Qmin/Qmax |
---|
| 179 | self.mapper.addMapping(self.minQInput, WIDGETS.W_QMIN) |
---|
| 180 | self.mapper.addMapping(self.maxQInput, WIDGETS.W_QMAX) |
---|
| 181 | |
---|
| 182 | # Slit Parameter items |
---|
| 183 | self.mapper.addMapping(self.slitWidthInput, WIDGETS.W_SLIT_WIDTH) |
---|
| 184 | self.mapper.addMapping(self.slitHeightInput, WIDGETS.W_SLIT_HEIGHT) |
---|
| 185 | |
---|
| 186 | # Parameter Items |
---|
[8045572] | 187 | self.mapper.addMapping(self.regularizationConstantInput, |
---|
| 188 | WIDGETS.W_REGULARIZATION) |
---|
| 189 | self.mapper.addMapping(self.regConstantSuggestionButton, |
---|
| 190 | WIDGETS.W_REGULARIZATION_SUGGEST) |
---|
[e32b120] | 191 | self.mapper.addMapping(self.explorerButton, WIDGETS.W_EXPLORE) |
---|
| 192 | self.mapper.addMapping(self.maxDistanceInput, WIDGETS.W_MAX_DIST) |
---|
| 193 | self.mapper.addMapping(self.noOfTermsInput, WIDGETS.W_NO_TERMS) |
---|
[8045572] | 194 | self.mapper.addMapping(self.noOfTermsSuggestionButton, |
---|
| 195 | WIDGETS.W_NO_TERMS_SUGGEST) |
---|
[e32b120] | 196 | |
---|
| 197 | # Output |
---|
| 198 | self.mapper.addMapping(self.rgValue, WIDGETS.W_RG) |
---|
| 199 | self.mapper.addMapping(self.iQ0Value, WIDGETS.W_I_ZERO) |
---|
[bde1a6b] | 200 | self.mapper.addMapping(self.backgroundValue, WIDGETS.W_BACKGROUND_OUTPUT) |
---|
[e32b120] | 201 | self.mapper.addMapping(self.computationTimeValue, WIDGETS.W_COMP_TIME) |
---|
| 202 | self.mapper.addMapping(self.chiDofValue, WIDGETS.W_CHI_SQUARED) |
---|
| 203 | self.mapper.addMapping(self.oscillationValue, WIDGETS.W_OSCILLATION) |
---|
| 204 | self.mapper.addMapping(self.posFractionValue, WIDGETS.W_POS_FRACTION) |
---|
[8045572] | 205 | self.mapper.addMapping(self.sigmaPosFractionValue, |
---|
| 206 | WIDGETS.W_SIGMA_POS_FRACTION) |
---|
[e32b120] | 207 | |
---|
| 208 | # Main Buttons |
---|
[c00a28ff] | 209 | self.mapper.addMapping(self.calculateAllButton, WIDGETS.W_CALCULATE_ALL) |
---|
| 210 | self.mapper.addMapping(self.calculateThisButton, |
---|
| 211 | WIDGETS.W_CALCULATE_VISIBLE) |
---|
[e32b120] | 212 | self.mapper.addMapping(self.helpButton, WIDGETS.W_HELP) |
---|
| 213 | |
---|
| 214 | self.mapper.toFirst() |
---|
| 215 | |
---|
| 216 | def setupModel(self): |
---|
| 217 | """ |
---|
[bde1a6b] | 218 | Update boxes with initial values |
---|
[e32b120] | 219 | """ |
---|
[bde1a6b] | 220 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 221 | self.model.setItem(WIDGETS.W_FILENAME, item) |
---|
[bde1a6b] | 222 | item = QtGui.QStandardItem('0.0') |
---|
| 223 | self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, item) |
---|
| 224 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 225 | self.model.setItem(WIDGETS.W_QMIN, item) |
---|
[bde1a6b] | 226 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 227 | self.model.setItem(WIDGETS.W_QMAX, item) |
---|
[bde1a6b] | 228 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 229 | self.model.setItem(WIDGETS.W_SLIT_WIDTH, item) |
---|
[bde1a6b] | 230 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 231 | self.model.setItem(WIDGETS.W_SLIT_HEIGHT, item) |
---|
[bde1a6b] | 232 | item = QtGui.QStandardItem("10") |
---|
[e32b120] | 233 | self.model.setItem(WIDGETS.W_NO_TERMS, item) |
---|
[bde1a6b] | 234 | item = QtGui.QStandardItem("0.0001") |
---|
[e32b120] | 235 | self.model.setItem(WIDGETS.W_REGULARIZATION, item) |
---|
[bde1a6b] | 236 | item = QtGui.QStandardItem("140.0") |
---|
[e32b120] | 237 | self.model.setItem(WIDGETS.W_MAX_DIST, item) |
---|
[bde1a6b] | 238 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 239 | self.model.setItem(WIDGETS.W_RG, item) |
---|
[bde1a6b] | 240 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 241 | self.model.setItem(WIDGETS.W_I_ZERO, item) |
---|
[bde1a6b] | 242 | item = QtGui.QStandardItem("") |
---|
| 243 | self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, item) |
---|
| 244 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 245 | self.model.setItem(WIDGETS.W_COMP_TIME, item) |
---|
[bde1a6b] | 246 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 247 | self.model.setItem(WIDGETS.W_CHI_SQUARED, item) |
---|
[bde1a6b] | 248 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 249 | self.model.setItem(WIDGETS.W_OSCILLATION, item) |
---|
[bde1a6b] | 250 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 251 | self.model.setItem(WIDGETS.W_POS_FRACTION, item) |
---|
[bde1a6b] | 252 | item = QtGui.QStandardItem("") |
---|
[e32b120] | 253 | self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, item) |
---|
[bde1a6b] | 254 | |
---|
[c00a28ff] | 255 | def setupWindow(self): |
---|
| 256 | """Initialize base window state on init""" |
---|
| 257 | self.setTabPosition(0) |
---|
| 258 | self.enableButtons() |
---|
| 259 | self.estimateBgd.setChecked(True) |
---|
| 260 | |
---|
[bde1a6b] | 261 | ###################################################################### |
---|
| 262 | # Methods for updating GUI |
---|
[8045572] | 263 | |
---|
| 264 | def enableButtons(self): |
---|
| 265 | """ |
---|
[bde1a6b] | 266 | Enable buttons when data is present, else disable them |
---|
[8045572] | 267 | """ |
---|
[c00a28ff] | 268 | self.explorerButton.setEnabled(self.logic.data_is_loaded) |
---|
| 269 | self.calculateAllButton.setEnabled(self.logic.data_is_loaded) |
---|
| 270 | self.calculateThisButton.setEnabled(self.logic.data_is_loaded) |
---|
[8045572] | 271 | |
---|
[26f42b1] | 272 | def populateDataComboBox(self, filename, data_ref): |
---|
[8045572] | 273 | """ |
---|
[bde1a6b] | 274 | Append a new file name to the data combobox |
---|
| 275 | :param data: Data1D object |
---|
[8045572] | 276 | """ |
---|
[bde1a6b] | 277 | qt_item = QtCore.QString.fromUtf8(filename) |
---|
[26f42b1] | 278 | ref = QtCore.QVariant(data_ref) |
---|
| 279 | self.dataList.addItem(qt_item, ref) |
---|
[8045572] | 280 | |
---|
[dab3351] | 281 | def acceptNoTerms(self): |
---|
| 282 | """Send estimated no of terms to input""" |
---|
| 283 | self.model.setItem(WIDGETS.W_NO_TERMS, QtGui.QStandardItem( |
---|
| 284 | self.noOfTermsSuggestionButton.text())) |
---|
| 285 | |
---|
| 286 | def acceptAlpha(self): |
---|
| 287 | """Send estimated alpha to input""" |
---|
| 288 | self.model.setItem(WIDGETS.W_REGULARIZATION, QtGui.QStandardItem( |
---|
| 289 | self.regConstantSuggestionButton.text())) |
---|
| 290 | |
---|
[d132f33] | 291 | def displayChange(self): |
---|
[dfd8233] | 292 | variant_ref = self.dataList.itemData(self.dataList.currentIndex()) |
---|
| 293 | self.setCurrentData(variant_ref.toPyObject()) |
---|
[d132f33] | 294 | |
---|
[8045572] | 295 | ###################################################################### |
---|
[dab3351] | 296 | # GUI Interaction Events |
---|
[8045572] | 297 | |
---|
[d1a4793] | 298 | def update_calculator(self): |
---|
[57ad773] | 299 | """Update all p(r) params""" |
---|
[e7651ff] | 300 | self._calculator.set_x(self._data_set.x) |
---|
| 301 | self._calculator.set_y(self._data_set.y) |
---|
| 302 | self._calculator.set_err(self._data_set.dy) |
---|
[d1a4793] | 303 | |
---|
[bde1a6b] | 304 | def model_changed(self): |
---|
| 305 | """Update the values when user makes changes""" |
---|
| 306 | if not self.mapper: |
---|
[c00a28ff] | 307 | msg = "Unable to update P{r}. The connection between the main GUI " |
---|
| 308 | msg += "and P(r) was severed. Attempting to restart P(r)." |
---|
| 309 | logging.warning(msg) |
---|
| 310 | self.setClosable(True) |
---|
| 311 | self.close() |
---|
| 312 | InversionWindow.__init__(self.parent(), self._data_list.keys()) |
---|
[570a2f73] | 313 | exit(0) |
---|
| 314 | # TODO: Only send plot first time - otherwise, update in complete |
---|
[57ad773] | 315 | if self.pr_plot is not None: |
---|
| 316 | title = self.pr_plot.name |
---|
| 317 | GuiUtils.updateModelItemWithPlot( |
---|
| 318 | self._data, QtCore.QVariant(self.pr_plot), title) |
---|
| 319 | if self.data_plot is not None: |
---|
| 320 | title = self.data_plot.name |
---|
| 321 | GuiUtils.updateModelItemWithPlot( |
---|
| 322 | self._data, QtCore.QVariant(self.data_plot), title) |
---|
[bde1a6b] | 323 | self.mapper.toFirst() |
---|
| 324 | |
---|
[e32b120] | 325 | def help(self): |
---|
[8045572] | 326 | """ |
---|
| 327 | Open the P(r) Inversion help browser |
---|
| 328 | """ |
---|
| 329 | tree_location = (GuiUtils.HELP_DIRECTORY_LOCATION + |
---|
| 330 | "user/sasgui/perspectives/pr/pr_help.html") |
---|
[e32b120] | 331 | |
---|
[8045572] | 332 | # Actual file anchor will depend on the combo box index |
---|
| 333 | # Note that we can be clusmy here, since bad current_fitter_id |
---|
| 334 | # will just make the page displayed from the top |
---|
| 335 | self._helpView.load(QtCore.QUrl(tree_location)) |
---|
| 336 | self._helpView.show() |
---|
| 337 | |
---|
[57ad773] | 338 | def toggleBgd(self): |
---|
[8045572] | 339 | """ |
---|
| 340 | Toggle the background between manual and estimated |
---|
| 341 | """ |
---|
[57ad773] | 342 | sender = self.sender() |
---|
| 343 | if sender is self.estimateBgd: |
---|
| 344 | self.backgroundInput.setEnabled(False) |
---|
| 345 | else: |
---|
| 346 | self.backgroundInput.setEnabled(True) |
---|
[e32b120] | 347 | |
---|
| 348 | def openExplorerWindow(self): |
---|
[8045572] | 349 | """ |
---|
| 350 | Open the Explorer window to see correlations between params and results |
---|
| 351 | """ |
---|
[7f5196e] | 352 | # TODO: Link Invertor() and DmaxWindow so window updates when recalculated |
---|
| 353 | from dmax import DmaxWindow |
---|
| 354 | self.dmaxWindow = DmaxWindow(self._calculator, self.getNFunc(), self) |
---|
| 355 | self.dmaxWindow.show() |
---|
[8045572] | 356 | |
---|
| 357 | ###################################################################### |
---|
| 358 | # Response Actions |
---|
| 359 | |
---|
| 360 | def setData(self, data_item=None, is_batch=False): |
---|
| 361 | """ |
---|
[c00a28ff] | 362 | Assign new data set(s) to the P(r) perspective |
---|
| 363 | Obtain a QStandardItem object and parse it to get Data1D/2D |
---|
[8045572] | 364 | Pass it over to the calculator |
---|
| 365 | """ |
---|
| 366 | assert data_item is not None |
---|
| 367 | |
---|
| 368 | if not isinstance(data_item, list): |
---|
| 369 | msg = "Incorrect type passed to the P(r) Perspective" |
---|
| 370 | raise AttributeError, msg |
---|
| 371 | |
---|
[26f42b1] | 372 | for data in data_item: |
---|
[570a2f73] | 373 | # Create initial internal mappings |
---|
| 374 | self._data_list[data] = self._calculator.clone() |
---|
| 375 | self._data_set = GuiUtils.dataFromItem(data) |
---|
| 376 | self.data_plot_list[data] = self.data_plot |
---|
| 377 | self.pr_plot_list[data] = self.pr_plot |
---|
[dfd8233] | 378 | ref_var = QtCore.QVariant(data) |
---|
| 379 | self.populateDataComboBox(self._data_set.filename, ref_var) |
---|
[570a2f73] | 380 | self.setCurrentData(data) |
---|
| 381 | |
---|
| 382 | # Estimate initial values from data |
---|
| 383 | self.performEstimate() |
---|
| 384 | self.logic = InversionLogic(self._data_set) |
---|
| 385 | |
---|
| 386 | # Estimate q range |
---|
| 387 | qmin, qmax = self.logic.computeDataRange() |
---|
| 388 | self.model.setItem(WIDGETS.W_QMIN, QtGui.QStandardItem( |
---|
| 389 | "{:.4g}".format(qmin))) |
---|
| 390 | self.model.setItem(WIDGETS.W_QMAX, QtGui.QStandardItem( |
---|
| 391 | "{:.4g}".format(qmax))) |
---|
| 392 | |
---|
| 393 | self.enableButtons() |
---|
[26f42b1] | 394 | |
---|
[7f5196e] | 395 | def getNFunc(self): |
---|
| 396 | """Get the n_func value from the GUI object""" |
---|
| 397 | return int(UI.TabbedInversionUI._fromUtf8(self.noOfTermsInput.text())) |
---|
| 398 | |
---|
[26f42b1] | 399 | def setCurrentData(self, data_ref): |
---|
[570a2f73] | 400 | """Get the current data and display as necessary""" |
---|
[26f42b1] | 401 | |
---|
| 402 | if not isinstance(data_ref, QtGui.QStandardItem): |
---|
[8045572] | 403 | msg = "Incorrect type passed to the P(r) Perspective" |
---|
| 404 | raise AttributeError, msg |
---|
| 405 | |
---|
[26f42b1] | 406 | # Data references |
---|
| 407 | self._data = data_ref |
---|
| 408 | self._data_set = GuiUtils.dataFromItem(data_ref) |
---|
[570a2f73] | 409 | self._calculator = self._data_list[data_ref] |
---|
| 410 | self.pr_plot = self.pr_plot_list[data_ref] |
---|
| 411 | self.data_plot = self.data_plot_list[data_ref] |
---|
[e7651ff] | 412 | |
---|
[dab3351] | 413 | ###################################################################### |
---|
| 414 | # Thread Creators |
---|
| 415 | |
---|
[c00a28ff] | 416 | # TODO: Move to individual class(?) |
---|
| 417 | |
---|
| 418 | def startThreadAll(self): |
---|
| 419 | for data_ref, pr in self._data_list.items(): |
---|
| 420 | self._data_set = GuiUtils.dataFromItem(data_ref) |
---|
| 421 | self._calculator = pr |
---|
| 422 | self.startThread() |
---|
| 423 | |
---|
[dab3351] | 424 | def startThread(self): |
---|
| 425 | """ |
---|
| 426 | Start a calculation thread |
---|
| 427 | """ |
---|
[e7651ff] | 428 | from Thread import CalcPr |
---|
[dab3351] | 429 | |
---|
[c00a28ff] | 430 | # Set data before running the calculations |
---|
| 431 | self.update_calculator() |
---|
| 432 | |
---|
| 433 | # If a thread is already started, stop it |
---|
| 434 | if self.calc_thread is not None and self.calc_thread.isrunning(): |
---|
| 435 | self.calc_thread.stop() |
---|
| 436 | pr = self._calculator.clone() |
---|
[7f5196e] | 437 | nfunc = self.getNFunc() |
---|
[c00a28ff] | 438 | self.calc_thread = CalcPr(pr, nfunc, |
---|
| 439 | error_func=self._threadError, |
---|
| 440 | completefn=self._completed, updatefn=None) |
---|
| 441 | self.calc_thread.queue() |
---|
| 442 | self.calc_thread.ready(2.5) |
---|
[dab3351] | 443 | |
---|
| 444 | def performEstimateNT(self): |
---|
| 445 | """ |
---|
| 446 | Perform parameter estimation |
---|
| 447 | """ |
---|
[e7651ff] | 448 | from Thread import EstimateNT |
---|
[dab3351] | 449 | |
---|
| 450 | # If a thread is already started, stop it |
---|
| 451 | if (self.estimation_thread is not None and |
---|
| 452 | self.estimation_thread.isrunning()): |
---|
| 453 | self.estimation_thread.stop() |
---|
| 454 | pr = self._calculator.clone() |
---|
| 455 | # Skip the slit settings for the estimation |
---|
| 456 | # It slows down the application and it doesn't change the estimates |
---|
| 457 | pr.slit_height = 0.0 |
---|
| 458 | pr.slit_width = 0.0 |
---|
[7f5196e] | 459 | nfunc = self.getNFunc() |
---|
[dab3351] | 460 | self.estimation_thread = EstimateNT(pr, nfunc, |
---|
| 461 | error_func=self._threadError, |
---|
| 462 | completefn=self._estimateNTCompleted, |
---|
| 463 | updatefn=None) |
---|
| 464 | self.estimation_thread.queue() |
---|
| 465 | self.estimation_thread.ready(2.5) |
---|
| 466 | |
---|
| 467 | def performEstimate(self): |
---|
| 468 | """ |
---|
| 469 | Perform parameter estimation |
---|
| 470 | """ |
---|
[e7651ff] | 471 | from Thread import EstimatePr |
---|
| 472 | |
---|
[dfd8233] | 473 | self.startThread() |
---|
[dab3351] | 474 | |
---|
| 475 | # If a thread is already started, stop it |
---|
| 476 | if (self.estimation_thread is not None and |
---|
| 477 | self.estimation_thread.isrunning()): |
---|
| 478 | self.estimation_thread.stop() |
---|
| 479 | pr = self._calculator.clone() |
---|
[7f5196e] | 480 | nfunc = self.getNFunc() |
---|
[dab3351] | 481 | self.estimation_thread = EstimatePr(pr, nfunc, |
---|
| 482 | error_func=self._threadError, |
---|
| 483 | completefn=self._estimateCompleted, |
---|
| 484 | updatefn=None) |
---|
| 485 | self.estimation_thread.queue() |
---|
| 486 | self.estimation_thread.ready(2.5) |
---|
| 487 | |
---|
| 488 | ###################################################################### |
---|
| 489 | # Thread Complete |
---|
| 490 | |
---|
| 491 | def _estimateCompleted(self, alpha, message, elapsed): |
---|
| 492 | """ |
---|
| 493 | Parameter estimation completed, |
---|
| 494 | display the results to the user |
---|
| 495 | |
---|
| 496 | :param alpha: estimated best alpha |
---|
| 497 | :param elapsed: computation time |
---|
| 498 | """ |
---|
| 499 | # Save useful info |
---|
| 500 | self.model.setItem(WIDGETS.W_COMP_TIME, |
---|
| 501 | QtGui.QStandardItem(str(elapsed))) |
---|
| 502 | self.regConstantSuggestionButton.setText(QtCore.QString(str(alpha))) |
---|
| 503 | self.regConstantSuggestionButton.setEnabled(True) |
---|
[d1a4793] | 504 | if message: |
---|
[dab3351] | 505 | logging.info(message) |
---|
| 506 | self.performEstimateNT() |
---|
| 507 | |
---|
| 508 | def _estimateNTCompleted(self, nterms, alpha, message, elapsed): |
---|
| 509 | """ |
---|
| 510 | Parameter estimation completed, |
---|
| 511 | display the results to the user |
---|
| 512 | |
---|
| 513 | :param alpha: estimated best alpha |
---|
| 514 | :param nterms: estimated number of terms |
---|
| 515 | :param elapsed: computation time |
---|
| 516 | |
---|
| 517 | """ |
---|
| 518 | # Save useful info |
---|
[d1a4793] | 519 | self.noOfTermsSuggestionButton.setText(QtCore.QString( |
---|
| 520 | "{:n}".format(nterms))) |
---|
[dab3351] | 521 | self.noOfTermsSuggestionButton.setEnabled(True) |
---|
[d1a4793] | 522 | self.regConstantSuggestionButton.setText(QtCore.QString( |
---|
| 523 | "{:.3g}".format(alpha))) |
---|
[dab3351] | 524 | self.regConstantSuggestionButton.setEnabled(True) |
---|
| 525 | self.model.setItem(WIDGETS.W_COMP_TIME, |
---|
| 526 | QtGui.QStandardItem(str(elapsed))) |
---|
[e7651ff] | 527 | self.PrTabWidget.setCurrentIndex(0) |
---|
[d1a4793] | 528 | if message: |
---|
[dab3351] | 529 | logging.info(message) |
---|
| 530 | |
---|
| 531 | def _completed(self, out, cov, pr, elapsed): |
---|
| 532 | """ |
---|
| 533 | Method called with the results when the inversion is done |
---|
| 534 | |
---|
| 535 | :param out: output coefficient for the base functions |
---|
| 536 | :param cov: covariance matrix |
---|
| 537 | :param pr: Invertor instance |
---|
| 538 | :param elapsed: time spent computing |
---|
| 539 | |
---|
| 540 | """ |
---|
| 541 | # Save useful info |
---|
| 542 | cov = np.ascontiguousarray(cov) |
---|
[57ad773] | 543 | pr.cov = cov |
---|
| 544 | pr.out = out |
---|
| 545 | pr.elapsed = elapsed |
---|
[dab3351] | 546 | |
---|
| 547 | # Show result on control panel |
---|
| 548 | |
---|
[570a2f73] | 549 | # TODO: Connect self._calculator to GUI - two-to-one connection possible? |
---|
[dab3351] | 550 | self.model.setItem(WIDGETS.W_RG, QtGui.QStandardItem(str(pr.rg(out)))) |
---|
| 551 | self.model.setItem(WIDGETS.W_I_ZERO, |
---|
| 552 | QtGui.QStandardItem(str(pr.iq0(out)))) |
---|
| 553 | self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, |
---|
[d1a4793] | 554 | QtGui.QStandardItem("{:.3f}".format(pr.background))) |
---|
[dab3351] | 555 | self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, |
---|
| 556 | QtGui.QStandardItem(str(pr.background))) |
---|
| 557 | self.model.setItem(WIDGETS.W_CHI_SQUARED, |
---|
[d1a4793] | 558 | QtGui.QStandardItem(str(pr.chi2[0]))) |
---|
[dab3351] | 559 | self.model.setItem(WIDGETS.W_COMP_TIME, |
---|
| 560 | QtGui.QStandardItem(str(elapsed))) |
---|
| 561 | self.model.setItem(WIDGETS.W_OSCILLATION, |
---|
| 562 | QtGui.QStandardItem(str(pr.oscillations(out)))) |
---|
| 563 | self.model.setItem(WIDGETS.W_POS_FRACTION, |
---|
| 564 | QtGui.QStandardItem(str(pr.get_positive(out)))) |
---|
| 565 | self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, |
---|
| 566 | QtGui.QStandardItem(str(pr.get_pos_err(out, cov)))) |
---|
| 567 | |
---|
[d1a4793] | 568 | # Display results tab |
---|
| 569 | self.PrTabWidget.setCurrentIndex(1) |
---|
[57ad773] | 570 | # Save Pr invertor |
---|
| 571 | self._calculator = pr |
---|
| 572 | # Append data to data list |
---|
[d132f33] | 573 | self._data_list[self._data] = self._calculator.clone() |
---|
[d1a4793] | 574 | |
---|
[570a2f73] | 575 | # Create new P(r) and fit plots |
---|
[57ad773] | 576 | if self.pr_plot is None: |
---|
| 577 | self.pr_plot = self.logic.newPRPlot(out, self._calculator, cov) |
---|
[570a2f73] | 578 | self.pr_plot_list[self._data] = self.pr_plot |
---|
| 579 | else: |
---|
| 580 | # FIXME: this should update the existing plot, not create a new one |
---|
| 581 | self.pr_plot = self.logic.newPRPlot(out, self._calculator, cov) |
---|
| 582 | self.pr_plot_list[self._data] = self.pr_plot |
---|
[57ad773] | 583 | if self.data_plot is None: |
---|
| 584 | self.data_plot = self.logic.new1DPlot(out, self._calculator) |
---|
[570a2f73] | 585 | self.data_plot_list[self._data] = self.data_plot |
---|
| 586 | else: |
---|
| 587 | # FIXME: this should update the existing plot, not create a new one |
---|
| 588 | self.data_plot = self.logic.new1DPlot(out, self._calculator) |
---|
| 589 | self.data_plot_list[self._data] = self.data_plot |
---|
[dab3351] | 590 | |
---|
| 591 | def _threadError(self, error): |
---|
| 592 | """ |
---|
| 593 | Call-back method for calculation errors |
---|
| 594 | """ |
---|
| 595 | logging.warning(error) |
---|