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