[fa81e94] | 1 | import logging |
---|
| 2 | import numpy as np |
---|
| 3 | |
---|
| 4 | from PyQt5 import QtGui, QtCore, QtWidgets |
---|
| 5 | |
---|
| 6 | # sas-global |
---|
| 7 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
| 8 | |
---|
| 9 | # pr inversion GUI elements |
---|
| 10 | from .InversionUtils import WIDGETS |
---|
| 11 | from .UI.TabbedInversionUI import Ui_PrInversion |
---|
| 12 | from .InversionLogic import InversionLogic |
---|
| 13 | |
---|
| 14 | # pr inversion calculation elements |
---|
| 15 | from sas.sascalc.pr.invertor import Invertor |
---|
[effdd98] | 16 | # Batch calculation display |
---|
| 17 | from sas.qtgui.Utilities.GridPanel import BatchInversionOutputPanel |
---|
| 18 | |
---|
[b9e89d5] | 19 | |
---|
[fa81e94] | 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 | |
---|
[b9e89d5] | 27 | |
---|
[50bfab0] | 28 | NUMBER_OF_TERMS = 10 |
---|
| 29 | REGULARIZATION = 0.0001 |
---|
| 30 | BACKGROUND_INPUT = 0.0 |
---|
| 31 | MAX_DIST = 140.0 |
---|
[edd6720] | 32 | DICT_KEYS = ["Calculator", "PrPlot", "DataPlot"] |
---|
[47bf906] | 33 | |
---|
[6da860a] | 34 | logger = logging.getLogger(__name__) |
---|
| 35 | |
---|
[fa81e94] | 36 | |
---|
[d4881f6a] | 37 | class InversionWindow(QtWidgets.QDialog, Ui_PrInversion): |
---|
[fa81e94] | 38 | """ |
---|
| 39 | The main window for the P(r) Inversion perspective. |
---|
| 40 | """ |
---|
| 41 | |
---|
| 42 | name = "Inversion" |
---|
[f1ec901] | 43 | estimateSignal = QtCore.pyqtSignal(tuple) |
---|
| 44 | estimateNTSignal = QtCore.pyqtSignal(tuple) |
---|
[34cf92c] | 45 | estimateDynamicNTSignal = QtCore.pyqtSignal(tuple) |
---|
| 46 | estimateDynamicSignal = QtCore.pyqtSignal(tuple) |
---|
[f1ec901] | 47 | calculateSignal = QtCore.pyqtSignal(tuple) |
---|
[fa81e94] | 48 | |
---|
| 49 | def __init__(self, parent=None, data=None): |
---|
| 50 | super(InversionWindow, self).__init__() |
---|
| 51 | self.setupUi(self) |
---|
| 52 | |
---|
| 53 | self.setWindowTitle("P(r) Inversion Perspective") |
---|
| 54 | |
---|
| 55 | self._manager = parent |
---|
[b9e89d5] | 56 | self.communicate = parent.communicator() |
---|
| 57 | self.communicate.dataDeletedSignal.connect(self.removeData) |
---|
[fa81e94] | 58 | |
---|
| 59 | self.logic = InversionLogic() |
---|
| 60 | |
---|
| 61 | # The window should not close |
---|
[ae34d30] | 62 | self._allowClose = False |
---|
[fa81e94] | 63 | |
---|
[ae34d30] | 64 | # Visible data items |
---|
[fa81e94] | 65 | # current QStandardItem showing on the panel |
---|
| 66 | self._data = None |
---|
[47bf906] | 67 | # Reference to Dmax window for self._data |
---|
| 68 | self.dmaxWindow = None |
---|
| 69 | # p(r) calculator for self._data |
---|
[fa81e94] | 70 | self._calculator = Invertor() |
---|
[304e42f] | 71 | # Default to background estimate |
---|
[bb6b037] | 72 | self._calculator.est_bck = True |
---|
[47bf906] | 73 | # plots of self._data |
---|
[ae34d30] | 74 | self.prPlot = None |
---|
| 75 | self.dataPlot = None |
---|
[e51e078] | 76 | # suggested nTerms |
---|
| 77 | self.nTermsSuggested = NUMBER_OF_TERMS |
---|
[47bf906] | 78 | |
---|
| 79 | # Calculation threads used by all data items |
---|
[ae34d30] | 80 | self.calcThread = None |
---|
| 81 | self.estimationThread = None |
---|
| 82 | self.estimationThreadNT = None |
---|
[72ecbdf2] | 83 | self.isCalculating = False |
---|
[fa81e94] | 84 | |
---|
[47bf906] | 85 | # Mapping for all data items |
---|
[ae34d30] | 86 | # Dictionary mapping data to all parameters |
---|
| 87 | self._dataList = {} |
---|
[fa81e94] | 88 | if not isinstance(data, list): |
---|
| 89 | data_list = [data] |
---|
| 90 | if data is not None: |
---|
| 91 | for datum in data_list: |
---|
[e51e078] | 92 | self.updateDataList(datum) |
---|
[f1ec901] | 93 | |
---|
[044454d] | 94 | self.dataDeleted = False |
---|
| 95 | |
---|
[fa81e94] | 96 | self.model = QtGui.QStandardItemModel(self) |
---|
| 97 | self.mapper = QtWidgets.QDataWidgetMapper(self) |
---|
[8f83719f] | 98 | |
---|
[ae34d30] | 99 | # Batch fitting parameters |
---|
| 100 | self.isBatch = False |
---|
| 101 | self.batchResultsWindow = None |
---|
[98485fe] | 102 | self.batchResults = {} |
---|
| 103 | self.batchComplete = [] |
---|
[effdd98] | 104 | |
---|
[8f83719f] | 105 | # Add validators |
---|
| 106 | self.setupValidators() |
---|
[fa81e94] | 107 | # Link user interactions with methods |
---|
| 108 | self.setupLinks() |
---|
| 109 | # Set values |
---|
| 110 | self.setupModel() |
---|
| 111 | # Set up the Widget Map |
---|
| 112 | self.setupMapper() |
---|
| 113 | # Set base window state |
---|
| 114 | self.setupWindow() |
---|
| 115 | |
---|
| 116 | ###################################################################### |
---|
| 117 | # Base Perspective Class Definitions |
---|
| 118 | |
---|
| 119 | def communicator(self): |
---|
| 120 | return self.communicate |
---|
| 121 | |
---|
| 122 | def allowBatch(self): |
---|
| 123 | return True |
---|
| 124 | |
---|
| 125 | def setClosable(self, value=True): |
---|
| 126 | """ |
---|
| 127 | Allow outsiders close this widget |
---|
| 128 | """ |
---|
| 129 | assert isinstance(value, bool) |
---|
[ae34d30] | 130 | self._allowClose = value |
---|
[fa81e94] | 131 | |
---|
[917eba5] | 132 | def isClosable(self): |
---|
| 133 | """ |
---|
| 134 | Allow outsiders close this widget |
---|
| 135 | """ |
---|
| 136 | return self._allowClose |
---|
| 137 | |
---|
[fa81e94] | 138 | def closeEvent(self, event): |
---|
| 139 | """ |
---|
| 140 | Overwrite QDialog close method to allow for custom widget close |
---|
| 141 | """ |
---|
[ae34d30] | 142 | # Close report widgets before closing/minimizing main widget |
---|
| 143 | self.closeDMax() |
---|
| 144 | self.closeBatchResults() |
---|
| 145 | if self._allowClose: |
---|
[fa81e94] | 146 | # reset the closability flag |
---|
| 147 | self.setClosable(value=False) |
---|
[d4881f6a] | 148 | # Tell the MdiArea to close the container |
---|
| 149 | self.parentWidget().close() |
---|
[fa81e94] | 150 | event.accept() |
---|
| 151 | else: |
---|
| 152 | event.ignore() |
---|
| 153 | # Maybe we should just minimize |
---|
| 154 | self.setWindowState(QtCore.Qt.WindowMinimized) |
---|
| 155 | |
---|
[ae34d30] | 156 | def closeDMax(self): |
---|
| 157 | if self.dmaxWindow is not None: |
---|
| 158 | self.dmaxWindow.close() |
---|
| 159 | |
---|
| 160 | def closeBatchResults(self): |
---|
| 161 | if self.batchResultsWindow is not None: |
---|
| 162 | self.batchResultsWindow.close() |
---|
| 163 | |
---|
[fa81e94] | 164 | ###################################################################### |
---|
| 165 | # Initialization routines |
---|
| 166 | |
---|
| 167 | def setupLinks(self): |
---|
| 168 | """Connect the use controls to their appropriate methods""" |
---|
| 169 | self.dataList.currentIndexChanged.connect(self.displayChange) |
---|
[f1ec901] | 170 | self.calculateAllButton.clicked.connect(self.startThreadAll) |
---|
| 171 | self.calculateThisButton.clicked.connect(self.startThread) |
---|
[72ecbdf2] | 172 | self.stopButton.clicked.connect(self.stopCalculation) |
---|
[fa81e94] | 173 | self.removeButton.clicked.connect(self.removeData) |
---|
| 174 | self.helpButton.clicked.connect(self.help) |
---|
| 175 | self.estimateBgd.toggled.connect(self.toggleBgd) |
---|
| 176 | self.manualBgd.toggled.connect(self.toggleBgd) |
---|
| 177 | self.regConstantSuggestionButton.clicked.connect(self.acceptAlpha) |
---|
| 178 | self.noOfTermsSuggestionButton.clicked.connect(self.acceptNoTerms) |
---|
| 179 | self.explorerButton.clicked.connect(self.openExplorerWindow) |
---|
[f1ec901] | 180 | |
---|
[d79bb7e] | 181 | self.backgroundInput.textChanged.connect( |
---|
[bb6b037] | 182 | lambda: self.set_background(self.backgroundInput.text())) |
---|
[d79bb7e] | 183 | self.minQInput.textChanged.connect( |
---|
[8f83719f] | 184 | lambda: self._calculator.set_qmin(is_float(self.minQInput.text()))) |
---|
[d79bb7e] | 185 | self.regularizationConstantInput.textChanged.connect( |
---|
[8f83719f] | 186 | lambda: self._calculator.set_alpha(is_float(self.regularizationConstantInput.text()))) |
---|
[d79bb7e] | 187 | self.maxDistanceInput.textChanged.connect( |
---|
[8f83719f] | 188 | lambda: self._calculator.set_dmax(is_float(self.maxDistanceInput.text()))) |
---|
[d79bb7e] | 189 | self.maxQInput.textChanged.connect( |
---|
[8f83719f] | 190 | lambda: self._calculator.set_qmax(is_float(self.maxQInput.text()))) |
---|
[d79bb7e] | 191 | self.slitHeightInput.textChanged.connect( |
---|
[8f83719f] | 192 | lambda: self._calculator.set_slit_height(is_float(self.slitHeightInput.text()))) |
---|
[d79bb7e] | 193 | self.slitWidthInput.textChanged.connect( |
---|
| 194 | lambda: self._calculator.set_slit_width(is_float(self.slitWidthInput.text()))) |
---|
[f1ec901] | 195 | |
---|
[fa81e94] | 196 | self.model.itemChanged.connect(self.model_changed) |
---|
[f1ec901] | 197 | self.estimateNTSignal.connect(self._estimateNTUpdate) |
---|
[34cf92c] | 198 | self.estimateDynamicNTSignal.connect(self._estimateDynamicNTUpdate) |
---|
| 199 | self.estimateDynamicSignal.connect(self._estimateDynamicUpdate) |
---|
[f1ec901] | 200 | self.estimateSignal.connect(self._estimateUpdate) |
---|
| 201 | self.calculateSignal.connect(self._calculateUpdate) |
---|
[fa81e94] | 202 | |
---|
[34cf92c] | 203 | self.maxDistanceInput.textEdited.connect(self.performEstimateDynamic) |
---|
| 204 | |
---|
[fa81e94] | 205 | def setupMapper(self): |
---|
| 206 | # Set up the mapper. |
---|
| 207 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
| 208 | self.mapper.setModel(self.model) |
---|
| 209 | |
---|
| 210 | # Filename |
---|
| 211 | self.mapper.addMapping(self.dataList, WIDGETS.W_FILENAME) |
---|
| 212 | # Background |
---|
| 213 | self.mapper.addMapping(self.backgroundInput, WIDGETS.W_BACKGROUND_INPUT) |
---|
| 214 | self.mapper.addMapping(self.estimateBgd, WIDGETS.W_ESTIMATE) |
---|
| 215 | self.mapper.addMapping(self.manualBgd, WIDGETS.W_MANUAL_INPUT) |
---|
| 216 | |
---|
| 217 | # Qmin/Qmax |
---|
| 218 | self.mapper.addMapping(self.minQInput, WIDGETS.W_QMIN) |
---|
| 219 | self.mapper.addMapping(self.maxQInput, WIDGETS.W_QMAX) |
---|
| 220 | |
---|
| 221 | # Slit Parameter items |
---|
| 222 | self.mapper.addMapping(self.slitWidthInput, WIDGETS.W_SLIT_WIDTH) |
---|
| 223 | self.mapper.addMapping(self.slitHeightInput, WIDGETS.W_SLIT_HEIGHT) |
---|
| 224 | |
---|
| 225 | # Parameter Items |
---|
[8f83719f] | 226 | self.mapper.addMapping(self.regularizationConstantInput, WIDGETS.W_REGULARIZATION) |
---|
| 227 | self.mapper.addMapping(self.regConstantSuggestionButton, WIDGETS.W_REGULARIZATION_SUGGEST) |
---|
[fa81e94] | 228 | self.mapper.addMapping(self.explorerButton, WIDGETS.W_EXPLORE) |
---|
| 229 | self.mapper.addMapping(self.maxDistanceInput, WIDGETS.W_MAX_DIST) |
---|
| 230 | self.mapper.addMapping(self.noOfTermsInput, WIDGETS.W_NO_TERMS) |
---|
[8f83719f] | 231 | self.mapper.addMapping(self.noOfTermsSuggestionButton, WIDGETS.W_NO_TERMS_SUGGEST) |
---|
[fa81e94] | 232 | |
---|
| 233 | # Output |
---|
| 234 | self.mapper.addMapping(self.rgValue, WIDGETS.W_RG) |
---|
| 235 | self.mapper.addMapping(self.iQ0Value, WIDGETS.W_I_ZERO) |
---|
| 236 | self.mapper.addMapping(self.backgroundValue, WIDGETS.W_BACKGROUND_OUTPUT) |
---|
| 237 | self.mapper.addMapping(self.computationTimeValue, WIDGETS.W_COMP_TIME) |
---|
| 238 | self.mapper.addMapping(self.chiDofValue, WIDGETS.W_CHI_SQUARED) |
---|
| 239 | self.mapper.addMapping(self.oscillationValue, WIDGETS.W_OSCILLATION) |
---|
| 240 | self.mapper.addMapping(self.posFractionValue, WIDGETS.W_POS_FRACTION) |
---|
[8f83719f] | 241 | self.mapper.addMapping(self.sigmaPosFractionValue, WIDGETS.W_SIGMA_POS_FRACTION) |
---|
[fa81e94] | 242 | |
---|
| 243 | # Main Buttons |
---|
| 244 | self.mapper.addMapping(self.removeButton, WIDGETS.W_REMOVE) |
---|
[f1ec901] | 245 | self.mapper.addMapping(self.calculateAllButton, WIDGETS.W_CALCULATE_ALL) |
---|
[8f83719f] | 246 | self.mapper.addMapping(self.calculateThisButton, WIDGETS.W_CALCULATE_VISIBLE) |
---|
[fa81e94] | 247 | self.mapper.addMapping(self.helpButton, WIDGETS.W_HELP) |
---|
| 248 | |
---|
| 249 | self.mapper.toFirst() |
---|
| 250 | |
---|
| 251 | def setupModel(self): |
---|
| 252 | """ |
---|
| 253 | Update boxes with initial values |
---|
| 254 | """ |
---|
[ae34d30] | 255 | bgd_item = QtGui.QStandardItem(str(BACKGROUND_INPUT)) |
---|
| 256 | self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, bgd_item) |
---|
[d79bb7e] | 257 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 258 | self.model.setItem(WIDGETS.W_QMIN, blank_item) |
---|
[d79bb7e] | 259 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 260 | self.model.setItem(WIDGETS.W_QMAX, blank_item) |
---|
[d79bb7e] | 261 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 262 | self.model.setItem(WIDGETS.W_SLIT_WIDTH, blank_item) |
---|
[d79bb7e] | 263 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 264 | self.model.setItem(WIDGETS.W_SLIT_HEIGHT, blank_item) |
---|
[d79bb7e] | 265 | no_terms_item = QtGui.QStandardItem(str(NUMBER_OF_TERMS)) |
---|
[ae34d30] | 266 | self.model.setItem(WIDGETS.W_NO_TERMS, no_terms_item) |
---|
[d79bb7e] | 267 | reg_item = QtGui.QStandardItem(str(REGULARIZATION)) |
---|
[ae34d30] | 268 | self.model.setItem(WIDGETS.W_REGULARIZATION, reg_item) |
---|
[d79bb7e] | 269 | max_dist_item = QtGui.QStandardItem(str(MAX_DIST)) |
---|
[ae34d30] | 270 | self.model.setItem(WIDGETS.W_MAX_DIST, max_dist_item) |
---|
[d79bb7e] | 271 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 272 | self.model.setItem(WIDGETS.W_RG, blank_item) |
---|
[d79bb7e] | 273 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 274 | self.model.setItem(WIDGETS.W_I_ZERO, blank_item) |
---|
[d79bb7e] | 275 | bgd_item = QtGui.QStandardItem(str(BACKGROUND_INPUT)) |
---|
[ae34d30] | 276 | self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, bgd_item) |
---|
[d79bb7e] | 277 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 278 | self.model.setItem(WIDGETS.W_COMP_TIME, blank_item) |
---|
[d79bb7e] | 279 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 280 | self.model.setItem(WIDGETS.W_CHI_SQUARED, blank_item) |
---|
[d79bb7e] | 281 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 282 | self.model.setItem(WIDGETS.W_OSCILLATION, blank_item) |
---|
[d79bb7e] | 283 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 284 | self.model.setItem(WIDGETS.W_POS_FRACTION, blank_item) |
---|
[d79bb7e] | 285 | blank_item = QtGui.QStandardItem("") |
---|
[ae34d30] | 286 | self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, blank_item) |
---|
[fa81e94] | 287 | |
---|
| 288 | def setupWindow(self): |
---|
| 289 | """Initialize base window state on init""" |
---|
| 290 | self.enableButtons() |
---|
| 291 | self.estimateBgd.setChecked(True) |
---|
| 292 | |
---|
[8f83719f] | 293 | def setupValidators(self): |
---|
| 294 | """Apply validators to editable line edits""" |
---|
| 295 | self.noOfTermsInput.setValidator(QtGui.QIntValidator()) |
---|
| 296 | self.regularizationConstantInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 297 | self.maxDistanceInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 298 | self.minQInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 299 | self.maxQInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 300 | self.slitHeightInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 301 | self.slitWidthInput.setValidator(GuiUtils.DoubleValidator()) |
---|
| 302 | |
---|
[fa81e94] | 303 | ###################################################################### |
---|
| 304 | # Methods for updating GUI |
---|
| 305 | |
---|
| 306 | def enableButtons(self): |
---|
| 307 | """ |
---|
| 308 | Enable buttons when data is present, else disable them |
---|
| 309 | """ |
---|
[ae34d30] | 310 | self.calculateAllButton.setEnabled(len(self._dataList) > 1 |
---|
[72ecbdf2] | 311 | and not self.isBatch |
---|
| 312 | and not self.isCalculating) |
---|
[ae34d30] | 313 | self.calculateThisButton.setEnabled(self.logic.data_is_loaded |
---|
[72ecbdf2] | 314 | and not self.isBatch |
---|
| 315 | and not self.isCalculating) |
---|
[fa81e94] | 316 | self.removeButton.setEnabled(self.logic.data_is_loaded) |
---|
[b1f6063] | 317 | self.explorerButton.setEnabled(self.logic.data_is_loaded) |
---|
[72ecbdf2] | 318 | self.stopButton.setVisible(self.isCalculating) |
---|
[b685c7b] | 319 | self.regConstantSuggestionButton.setEnabled( |
---|
[d79bb7e] | 320 | self.logic.data_is_loaded and |
---|
[b685c7b] | 321 | self._calculator.suggested_alpha != self._calculator.alpha) |
---|
| 322 | self.noOfTermsSuggestionButton.setEnabled( |
---|
[d79bb7e] | 323 | self.logic.data_is_loaded and |
---|
[b685c7b] | 324 | self._calculator.nfunc != self.nTermsSuggested) |
---|
[fa81e94] | 325 | |
---|
| 326 | def populateDataComboBox(self, filename, data_ref): |
---|
| 327 | """ |
---|
| 328 | Append a new file name to the data combobox |
---|
| 329 | :param filename: data filename |
---|
| 330 | :param data_ref: QStandardItem reference for data set to be added |
---|
| 331 | """ |
---|
[6a3e1fe] | 332 | self.dataList.addItem(filename, data_ref) |
---|
[fa81e94] | 333 | |
---|
| 334 | def acceptNoTerms(self): |
---|
| 335 | """Send estimated no of terms to input""" |
---|
| 336 | self.model.setItem(WIDGETS.W_NO_TERMS, QtGui.QStandardItem( |
---|
| 337 | self.noOfTermsSuggestionButton.text())) |
---|
| 338 | |
---|
| 339 | def acceptAlpha(self): |
---|
| 340 | """Send estimated alpha to input""" |
---|
| 341 | self.model.setItem(WIDGETS.W_REGULARIZATION, QtGui.QStandardItem( |
---|
| 342 | self.regConstantSuggestionButton.text())) |
---|
| 343 | |
---|
[edd6720] | 344 | def displayChange(self, data_index=0): |
---|
[47bf906] | 345 | """Switch to another item in the data list""" |
---|
[044454d] | 346 | if self.dataDeleted: |
---|
| 347 | return |
---|
[edd6720] | 348 | self.updateDataList(self._data) |
---|
| 349 | self.setCurrentData(self.dataList.itemData(data_index)) |
---|
[fa81e94] | 350 | |
---|
| 351 | ###################################################################### |
---|
| 352 | # GUI Interaction Events |
---|
| 353 | |
---|
[ae34d30] | 354 | def updateCalculator(self): |
---|
[fa81e94] | 355 | """Update all p(r) params""" |
---|
[edd6720] | 356 | self._calculator.set_x(self.logic.data.x) |
---|
| 357 | self._calculator.set_y(self.logic.data.y) |
---|
| 358 | self._calculator.set_err(self.logic.data.dy) |
---|
[441a03f] | 359 | self.set_background(self.backgroundInput.text()) |
---|
| 360 | |
---|
| 361 | def set_background(self, value): |
---|
| 362 | self._calculator.background = is_float(value) |
---|
[fa81e94] | 363 | |
---|
| 364 | def model_changed(self): |
---|
| 365 | """Update the values when user makes changes""" |
---|
| 366 | if not self.mapper: |
---|
| 367 | msg = "Unable to update P{r}. The connection between the main GUI " |
---|
| 368 | msg += "and P(r) was severed. Attempting to restart P(r)." |
---|
[6da860a] | 369 | logger.warning(msg) |
---|
[fa81e94] | 370 | self.setClosable(True) |
---|
| 371 | self.close() |
---|
[ae34d30] | 372 | InversionWindow.__init__(self.parent(), list(self._dataList.keys())) |
---|
[fa81e94] | 373 | exit(0) |
---|
[8f83719f] | 374 | if self.dmaxWindow is not None: |
---|
[47bf906] | 375 | self.dmaxWindow.nfunc = self.getNFunc() |
---|
[bb6b037] | 376 | self.dmaxWindow.pr_state = self._calculator |
---|
[edd6720] | 377 | self.mapper.toLast() |
---|
[fa81e94] | 378 | |
---|
| 379 | def help(self): |
---|
| 380 | """ |
---|
| 381 | Open the P(r) Inversion help browser |
---|
| 382 | """ |
---|
[aed0532] | 383 | tree_location = "/user/qtgui/Perspectives/Inversion/pr_help.html" |
---|
[fa81e94] | 384 | |
---|
| 385 | # Actual file anchor will depend on the combo box index |
---|
| 386 | # Note that we can be clusmy here, since bad current_fitter_id |
---|
| 387 | # will just make the page displayed from the top |
---|
[e90988c] | 388 | self._manager.showHelp(tree_location) |
---|
[fa81e94] | 389 | |
---|
| 390 | def toggleBgd(self): |
---|
| 391 | """ |
---|
| 392 | Toggle the background between manual and estimated |
---|
| 393 | """ |
---|
[edd6720] | 394 | if self.estimateBgd.isChecked(): |
---|
| 395 | self.manualBgd.setChecked(False) |
---|
[fa81e94] | 396 | self.backgroundInput.setEnabled(False) |
---|
[304e42f] | 397 | self._calculator.set_est_bck = True |
---|
[edd6720] | 398 | elif self.manualBgd.isChecked(): |
---|
| 399 | self.estimateBgd.setChecked(False) |
---|
[fa81e94] | 400 | self.backgroundInput.setEnabled(True) |
---|
[304e42f] | 401 | self._calculator.set_est_bck = False |
---|
[441a03f] | 402 | else: |
---|
| 403 | pass |
---|
[fa81e94] | 404 | |
---|
| 405 | def openExplorerWindow(self): |
---|
| 406 | """ |
---|
| 407 | Open the Explorer window to see correlations between params and results |
---|
| 408 | """ |
---|
| 409 | from .DMaxExplorerWidget import DmaxWindow |
---|
[6da860a] | 410 | self.dmaxWindow = DmaxWindow(pr_state=self._calculator, |
---|
| 411 | nfunc=self.getNFunc(), |
---|
| 412 | parent=self) |
---|
[fa81e94] | 413 | self.dmaxWindow.show() |
---|
| 414 | |
---|
[98485fe] | 415 | def showBatchOutput(self): |
---|
[76567bb] | 416 | """ |
---|
| 417 | Display the batch output in tabular form |
---|
| 418 | :param output_data: Dictionary mapping filename -> P(r) instance |
---|
| 419 | """ |
---|
[ae34d30] | 420 | if self.batchResultsWindow is None: |
---|
| 421 | self.batchResultsWindow = BatchInversionOutputPanel( |
---|
[98485fe] | 422 | parent=self, output_data=self.batchResults) |
---|
[76567bb] | 423 | else: |
---|
[ae34d30] | 424 | self.batchResultsWindow.setupTable(self.batchResults) |
---|
| 425 | self.batchResultsWindow.show() |
---|
[effdd98] | 426 | |
---|
[72ecbdf2] | 427 | def stopCalculation(self): |
---|
| 428 | """ Stop all threads, return to the base state and update GUI """ |
---|
[b0ba43e] | 429 | self.stopCalcThread() |
---|
| 430 | self.stopEstimationThread() |
---|
| 431 | self.stopEstimateNTThread() |
---|
[72ecbdf2] | 432 | # Show any batch calculations that successfully completed |
---|
| 433 | if self.isBatch and self.batchResultsWindow is not None: |
---|
| 434 | self.showBatchOutput() |
---|
| 435 | self.isBatch = False |
---|
| 436 | self.isCalculating = False |
---|
| 437 | self.updateGuiValues() |
---|
| 438 | |
---|
[fa81e94] | 439 | ###################################################################### |
---|
| 440 | # Response Actions |
---|
| 441 | |
---|
| 442 | def setData(self, data_item=None, is_batch=False): |
---|
| 443 | """ |
---|
| 444 | Assign new data set(s) to the P(r) perspective |
---|
| 445 | Obtain a QStandardItem object and parse it to get Data1D/2D |
---|
| 446 | Pass it over to the calculator |
---|
| 447 | """ |
---|
| 448 | assert data_item is not None |
---|
| 449 | |
---|
| 450 | if not isinstance(data_item, list): |
---|
| 451 | msg = "Incorrect type passed to the P(r) Perspective" |
---|
[47bf906] | 452 | raise AttributeError(msg) |
---|
[fa81e94] | 453 | |
---|
| 454 | for data in data_item: |
---|
[ae34d30] | 455 | if data in self._dataList.keys(): |
---|
[8f83719f] | 456 | # Don't add data if it's already in |
---|
[edd6720] | 457 | continue |
---|
[fa81e94] | 458 | # Create initial internal mappings |
---|
[edd6720] | 459 | self.logic.data = GuiUtils.dataFromItem(data) |
---|
[e51e078] | 460 | # Estimate q range |
---|
| 461 | qmin, qmax = self.logic.computeDataRange() |
---|
| 462 | self._calculator.set_qmin(qmin) |
---|
| 463 | self._calculator.set_qmax(qmax) |
---|
| 464 | self.updateDataList(data) |
---|
[edd6720] | 465 | self.populateDataComboBox(self.logic.data.filename, data) |
---|
| 466 | self.dataList.setCurrentIndex(len(self.dataList) - 1) |
---|
| 467 | self.setCurrentData(data) |
---|
[fa81e94] | 468 | |
---|
[47bf906] | 469 | def updateDataList(self, dataRef): |
---|
| 470 | """Save the current data state of the window into self._data_list""" |
---|
[e51e078] | 471 | if dataRef is None: |
---|
| 472 | return |
---|
[ae34d30] | 473 | self._dataList[dataRef] = { |
---|
[e51e078] | 474 | DICT_KEYS[0]: self._calculator, |
---|
[ae34d30] | 475 | DICT_KEYS[1]: self.prPlot, |
---|
| 476 | DICT_KEYS[2]: self.dataPlot |
---|
[47bf906] | 477 | } |
---|
[98485fe] | 478 | # Update batch results window when finished |
---|
| 479 | self.batchResults[self.logic.data.filename] = self._calculator |
---|
[ae34d30] | 480 | if self.batchResultsWindow is not None: |
---|
[98485fe] | 481 | self.showBatchOutput() |
---|
[47bf906] | 482 | |
---|
[fa81e94] | 483 | def getNFunc(self): |
---|
| 484 | """Get the n_func value from the GUI object""" |
---|
[50bfab0] | 485 | try: |
---|
| 486 | nfunc = int(self.noOfTermsInput.text()) |
---|
| 487 | except ValueError: |
---|
[6da860a] | 488 | logger.error("Incorrect number of terms specified: %s" |
---|
[edd6720] | 489 | %self.noOfTermsInput.text()) |
---|
[50bfab0] | 490 | self.noOfTermsInput.setText(str(NUMBER_OF_TERMS)) |
---|
| 491 | nfunc = NUMBER_OF_TERMS |
---|
| 492 | return nfunc |
---|
[fa81e94] | 493 | |
---|
| 494 | def setCurrentData(self, data_ref): |
---|
[47bf906] | 495 | """Get the data by reference and display as necessary""" |
---|
[304e42f] | 496 | if data_ref is None: |
---|
| 497 | return |
---|
[fa81e94] | 498 | if not isinstance(data_ref, QtGui.QStandardItem): |
---|
| 499 | msg = "Incorrect type passed to the P(r) Perspective" |
---|
[e51e078] | 500 | raise AttributeError(msg) |
---|
[fa81e94] | 501 | # Data references |
---|
| 502 | self._data = data_ref |
---|
[edd6720] | 503 | self.logic.data = GuiUtils.dataFromItem(data_ref) |
---|
[ae34d30] | 504 | self._calculator = self._dataList[data_ref].get(DICT_KEYS[0]) |
---|
| 505 | self.prPlot = self._dataList[data_ref].get(DICT_KEYS[1]) |
---|
| 506 | self.dataPlot = self._dataList[data_ref].get(DICT_KEYS[2]) |
---|
[edd6720] | 507 | self.performEstimate() |
---|
[e51e078] | 508 | |
---|
[34cf92c] | 509 | def updateDynamicGuiValues(self): |
---|
| 510 | pr = self._calculator |
---|
| 511 | alpha = self._calculator.suggested_alpha |
---|
| 512 | self.model.setItem(WIDGETS.W_MAX_DIST, |
---|
| 513 | QtGui.QStandardItem("{:.4g}".format(pr.get_dmax()))) |
---|
| 514 | self.regConstantSuggestionButton.setText("{:-3.2g}".format(alpha)) |
---|
| 515 | self.noOfTermsSuggestionButton.setText( |
---|
| 516 | "{:n}".format(self.nTermsSuggested)) |
---|
| 517 | |
---|
| 518 | self.enableButtons() |
---|
| 519 | |
---|
[e51e078] | 520 | def updateGuiValues(self): |
---|
| 521 | pr = self._calculator |
---|
| 522 | out = self._calculator.out |
---|
| 523 | cov = self._calculator.cov |
---|
| 524 | elapsed = self._calculator.elapsed |
---|
| 525 | alpha = self._calculator.suggested_alpha |
---|
| 526 | self.model.setItem(WIDGETS.W_QMIN, |
---|
| 527 | QtGui.QStandardItem("{:.4g}".format(pr.get_qmin()))) |
---|
| 528 | self.model.setItem(WIDGETS.W_QMAX, |
---|
| 529 | QtGui.QStandardItem("{:.4g}".format(pr.get_qmax()))) |
---|
| 530 | self.model.setItem(WIDGETS.W_BACKGROUND_INPUT, |
---|
[effdd98] | 531 | QtGui.QStandardItem("{:.3g}".format(pr.background))) |
---|
[e51e078] | 532 | self.model.setItem(WIDGETS.W_BACKGROUND_OUTPUT, |
---|
| 533 | QtGui.QStandardItem("{:.3g}".format(pr.background))) |
---|
| 534 | self.model.setItem(WIDGETS.W_COMP_TIME, |
---|
| 535 | QtGui.QStandardItem("{:.4g}".format(elapsed))) |
---|
[ae34d30] | 536 | self.model.setItem(WIDGETS.W_MAX_DIST, |
---|
| 537 | QtGui.QStandardItem("{:.4g}".format(pr.get_dmax()))) |
---|
[4d959c8] | 538 | #FIXME: At the initial initialization trigger Dynamic estimate instead of this one |
---|
| 539 | #self.regConstantSuggestionButton.setText("{:-3.2g}".format(alpha)) |
---|
| 540 | #self.noOfTermsSuggestionButton.setText( |
---|
| 541 | # "{:n}".format(self.nTermsSuggested)) |
---|
[e51e078] | 542 | |
---|
[304e42f] | 543 | if isinstance(pr.chi2, np.ndarray): |
---|
[e51e078] | 544 | self.model.setItem(WIDGETS.W_CHI_SQUARED, |
---|
| 545 | QtGui.QStandardItem("{:.3g}".format(pr.chi2[0]))) |
---|
| 546 | if out is not None: |
---|
| 547 | self.model.setItem(WIDGETS.W_RG, |
---|
| 548 | QtGui.QStandardItem("{:.3g}".format(pr.rg(out)))) |
---|
| 549 | self.model.setItem(WIDGETS.W_I_ZERO, |
---|
| 550 | QtGui.QStandardItem( |
---|
| 551 | "{:.3g}".format(pr.iq0(out)))) |
---|
| 552 | self.model.setItem(WIDGETS.W_OSCILLATION, QtGui.QStandardItem( |
---|
| 553 | "{:.3g}".format(pr.oscillations(out)))) |
---|
| 554 | self.model.setItem(WIDGETS.W_POS_FRACTION, QtGui.QStandardItem( |
---|
| 555 | "{:.3g}".format(pr.get_positive(out)))) |
---|
| 556 | if cov is not None: |
---|
| 557 | self.model.setItem(WIDGETS.W_SIGMA_POS_FRACTION, |
---|
| 558 | QtGui.QStandardItem( |
---|
| 559 | "{:.3g}".format( |
---|
| 560 | pr.get_pos_err(out, cov)))) |
---|
[ae34d30] | 561 | if self.prPlot is not None: |
---|
| 562 | title = self.prPlot.name |
---|
| 563 | GuiUtils.updateModelItemWithPlot(self._data, self.prPlot, title) |
---|
[2b8286c] | 564 | self.communicate.plotRequestedSignal.emit([self.prPlot], None) |
---|
[ae34d30] | 565 | if self.dataPlot is not None: |
---|
| 566 | title = self.dataPlot.name |
---|
| 567 | GuiUtils.updateModelItemWithPlot(self._data, self.dataPlot, title) |
---|
[2b8286c] | 568 | self.communicate.plotRequestedSignal.emit([self.dataPlot], None) |
---|
[b685c7b] | 569 | self.enableButtons() |
---|
[47bf906] | 570 | |
---|
[b9e89d5] | 571 | def removeData(self, data_list=None): |
---|
[47bf906] | 572 | """Remove the existing data reference from the P(r) Persepective""" |
---|
[044454d] | 573 | self.dataDeleted = True |
---|
[d79bb7e] | 574 | self.batchResults = {} |
---|
[b9e89d5] | 575 | if not data_list: |
---|
| 576 | data_list = [self._data] |
---|
[ae34d30] | 577 | self.closeDMax() |
---|
[b9e89d5] | 578 | for data in data_list: |
---|
[ae34d30] | 579 | self._dataList.pop(data) |
---|
[76567bb] | 580 | self._data = None |
---|
[044454d] | 581 | length = len(self.dataList) |
---|
| 582 | for index in reversed(range(length)): |
---|
[b9e89d5] | 583 | if self.dataList.itemData(index) in data_list: |
---|
| 584 | self.dataList.removeItem(index) |
---|
[e51e078] | 585 | # Last file removed |
---|
[044454d] | 586 | self.dataDeleted = False |
---|
[ae34d30] | 587 | if len(self._dataList) == 0: |
---|
| 588 | self.prPlot = None |
---|
| 589 | self.dataPlot = None |
---|
[edd6720] | 590 | self.logic.data = None |
---|
[ae34d30] | 591 | self._calculator = Invertor() |
---|
| 592 | self.closeBatchResults() |
---|
[b685c7b] | 593 | self.nTermsSuggested = NUMBER_OF_TERMS |
---|
| 594 | self.noOfTermsSuggestionButton.setText("{:n}".format( |
---|
| 595 | self.nTermsSuggested)) |
---|
| 596 | self.regConstantSuggestionButton.setText("{:-3.2g}".format( |
---|
| 597 | REGULARIZATION)) |
---|
[edd6720] | 598 | self.updateGuiValues() |
---|
[b685c7b] | 599 | self.setupModel() |
---|
[ba4e3ba] | 600 | else: |
---|
| 601 | self.dataList.setCurrentIndex(0) |
---|
| 602 | self.updateGuiValues() |
---|
[fa81e94] | 603 | |
---|
| 604 | ###################################################################### |
---|
| 605 | # Thread Creators |
---|
[98485fe] | 606 | |
---|
[fa81e94] | 607 | def startThreadAll(self): |
---|
[72ecbdf2] | 608 | self.isCalculating = True |
---|
[98485fe] | 609 | self.isBatch = True |
---|
[d79bb7e] | 610 | self.batchComplete = [] |
---|
| 611 | self.calculateAllButton.setText("Calculating...") |
---|
| 612 | self.enableButtons() |
---|
| 613 | self.batchResultsWindow = BatchInversionOutputPanel( |
---|
| 614 | parent=self, output_data=self.batchResults) |
---|
[98485fe] | 615 | self.performEstimate() |
---|
| 616 | |
---|
| 617 | def startNextBatchItem(self): |
---|
| 618 | self.isBatch = False |
---|
[ae34d30] | 619 | for index in range(len(self._dataList)): |
---|
[98485fe] | 620 | if index not in self.batchComplete: |
---|
| 621 | self.dataList.setCurrentIndex(index) |
---|
| 622 | self.isBatch = True |
---|
[6da860a] | 623 | # Add the index before calculating in case calculation fails |
---|
| 624 | self.batchComplete.append(index) |
---|
[98485fe] | 625 | break |
---|
| 626 | if self.isBatch: |
---|
[76567bb] | 627 | self.performEstimate() |
---|
[ae34d30] | 628 | else: |
---|
| 629 | # If no data sets left, end batch calculation |
---|
[72ecbdf2] | 630 | self.isCalculating = False |
---|
[d79bb7e] | 631 | self.batchComplete = [] |
---|
[ae34d30] | 632 | self.calculateAllButton.setText("Calculate All") |
---|
[5a5e371] | 633 | self.showBatchOutput() |
---|
[ae34d30] | 634 | self.enableButtons() |
---|
[fa81e94] | 635 | |
---|
| 636 | def startThread(self): |
---|
| 637 | """ |
---|
| 638 | Start a calculation thread |
---|
| 639 | """ |
---|
| 640 | from .Thread import CalcPr |
---|
| 641 | |
---|
| 642 | # Set data before running the calculations |
---|
[72ecbdf2] | 643 | self.isCalculating = True |
---|
| 644 | self.enableButtons() |
---|
[ae34d30] | 645 | self.updateCalculator() |
---|
[6da860a] | 646 | # Disable calculation buttons to prevent thread interference |
---|
[fa81e94] | 647 | |
---|
[b0ba43e] | 648 | # If the thread is already started, stop it |
---|
| 649 | self.stopCalcThread() |
---|
| 650 | |
---|
[fa81e94] | 651 | pr = self._calculator.clone() |
---|
| 652 | nfunc = self.getNFunc() |
---|
[ae34d30] | 653 | self.calcThread = CalcPr(pr, nfunc, |
---|
| 654 | error_func=self._threadError, |
---|
| 655 | completefn=self._calculateCompleted, |
---|
| 656 | updatefn=None) |
---|
| 657 | self.calcThread.queue() |
---|
| 658 | self.calcThread.ready(2.5) |
---|
[fa81e94] | 659 | |
---|
[b1f6063] | 660 | #Perform estimate should be done on value enter this should solve delay problem |
---|
[4d959c8] | 661 | #self.performEstimate() |
---|
[b1f6063] | 662 | |
---|
[b0ba43e] | 663 | def stopCalcThread(self): |
---|
| 664 | """ Stops a thread if it exists and is running """ |
---|
| 665 | if self.calcThread is not None and self.calcThread.isrunning(): |
---|
| 666 | self.calcThread.stop() |
---|
| 667 | |
---|
[fa81e94] | 668 | def performEstimateNT(self): |
---|
| 669 | """ |
---|
[f1ec901] | 670 | Perform parameter estimation |
---|
[fa81e94] | 671 | """ |
---|
| 672 | from .Thread import EstimateNT |
---|
| 673 | |
---|
[ae34d30] | 674 | self.updateCalculator() |
---|
[edd6720] | 675 | |
---|
[fa81e94] | 676 | # If a thread is already started, stop it |
---|
[b0ba43e] | 677 | self.stopEstimateNTThread() |
---|
| 678 | |
---|
[fa81e94] | 679 | pr = self._calculator.clone() |
---|
| 680 | # Skip the slit settings for the estimation |
---|
| 681 | # It slows down the application and it doesn't change the estimates |
---|
| 682 | pr.slit_height = 0.0 |
---|
| 683 | pr.slit_width = 0.0 |
---|
| 684 | nfunc = self.getNFunc() |
---|
[f1ec901] | 685 | |
---|
[ae34d30] | 686 | self.estimationThreadNT = EstimateNT(pr, nfunc, |
---|
| 687 | error_func=self._threadError, |
---|
| 688 | completefn=self._estimateNTCompleted, |
---|
| 689 | updatefn=None) |
---|
| 690 | self.estimationThreadNT.queue() |
---|
| 691 | self.estimationThreadNT.ready(2.5) |
---|
[fa81e94] | 692 | |
---|
[34cf92c] | 693 | def performEstimateDynamicNT(self): |
---|
| 694 | """ |
---|
| 695 | Perform parameter estimation |
---|
| 696 | """ |
---|
| 697 | from .Thread import EstimateNT |
---|
| 698 | |
---|
| 699 | self.updateCalculator() |
---|
| 700 | |
---|
| 701 | # If a thread is already started, stop it |
---|
| 702 | self.stopEstimateNTThread() |
---|
| 703 | |
---|
| 704 | pr = self._calculator.clone() |
---|
| 705 | # Skip the slit settings for the estimation |
---|
| 706 | # It slows down the application and it doesn't change the estimates |
---|
| 707 | pr.slit_height = 0.0 |
---|
| 708 | pr.slit_width = 0.0 |
---|
| 709 | nfunc = self.getNFunc() |
---|
| 710 | |
---|
| 711 | self.estimationThreadNT = EstimateNT(pr, nfunc, |
---|
| 712 | error_func=self._threadError, |
---|
| 713 | completefn=self._estimateDynamicNTCompleted, |
---|
| 714 | updatefn=None) |
---|
| 715 | self.estimationThreadNT.queue() |
---|
| 716 | self.estimationThreadNT.ready(2.5) |
---|
| 717 | |
---|
[b0ba43e] | 718 | def stopEstimateNTThread(self): |
---|
| 719 | if (self.estimationThreadNT is not None and |
---|
| 720 | self.estimationThreadNT.isrunning()): |
---|
| 721 | self.estimationThreadNT.stop() |
---|
| 722 | |
---|
[fa81e94] | 723 | def performEstimate(self): |
---|
| 724 | """ |
---|
| 725 | Perform parameter estimation |
---|
| 726 | """ |
---|
| 727 | from .Thread import EstimatePr |
---|
| 728 | |
---|
| 729 | # If a thread is already started, stop it |
---|
[b0ba43e] | 730 | self.stopEstimationThread() |
---|
| 731 | |
---|
[ae34d30] | 732 | self.estimationThread = EstimatePr(self._calculator.clone(), |
---|
| 733 | self.getNFunc(), |
---|
| 734 | error_func=self._threadError, |
---|
| 735 | completefn=self._estimateCompleted, |
---|
| 736 | updatefn=None) |
---|
| 737 | self.estimationThread.queue() |
---|
| 738 | self.estimationThread.ready(2.5) |
---|
[fa81e94] | 739 | |
---|
[34cf92c] | 740 | def performEstimateDynamic(self): |
---|
| 741 | """ |
---|
| 742 | Perform parameter estimation |
---|
| 743 | """ |
---|
| 744 | from .Thread import EstimatePr |
---|
| 745 | |
---|
| 746 | # If a thread is already started, stop it |
---|
| 747 | self.stopEstimationThread() |
---|
| 748 | |
---|
| 749 | self.estimationThread = EstimatePr(self._calculator.clone(), |
---|
| 750 | self.getNFunc(), |
---|
| 751 | error_func=self._threadError, |
---|
| 752 | completefn=self._estimateDynamicCompleted, |
---|
| 753 | updatefn=None) |
---|
| 754 | self.estimationThread.queue() |
---|
| 755 | self.estimationThread.ready(2.5) |
---|
| 756 | |
---|
[b0ba43e] | 757 | def stopEstimationThread(self): |
---|
| 758 | """ Stop the estimation thread if it exists and is running """ |
---|
| 759 | if (self.estimationThread is not None and |
---|
| 760 | self.estimationThread.isrunning()): |
---|
| 761 | self.estimationThread.stop() |
---|
| 762 | |
---|
[fa81e94] | 763 | ###################################################################### |
---|
| 764 | # Thread Complete |
---|
| 765 | |
---|
| 766 | def _estimateCompleted(self, alpha, message, elapsed): |
---|
[f1ec901] | 767 | ''' Send a signal to the main thread for model update''' |
---|
| 768 | self.estimateSignal.emit((alpha, message, elapsed)) |
---|
| 769 | |
---|
[34cf92c] | 770 | def _estimateDynamicCompleted(self, alpha, message, elapsed): |
---|
| 771 | ''' Send a signal to the main thread for model update''' |
---|
| 772 | self.estimateDynamicSignal.emit((alpha, message, elapsed)) |
---|
| 773 | |
---|
[f1ec901] | 774 | def _estimateUpdate(self, output_tuple): |
---|
[fa81e94] | 775 | """ |
---|
| 776 | Parameter estimation completed, |
---|
| 777 | display the results to the user |
---|
| 778 | |
---|
| 779 | :param alpha: estimated best alpha |
---|
| 780 | :param elapsed: computation time |
---|
| 781 | """ |
---|
[f1ec901] | 782 | alpha, message, elapsed = output_tuple |
---|
[6da860a] | 783 | self._calculator.alpha = alpha |
---|
| 784 | self._calculator.elapsed += self._calculator.elapsed |
---|
[fa81e94] | 785 | if message: |
---|
[6da860a] | 786 | logger.info(message) |
---|
[98485fe] | 787 | self.performEstimateNT() |
---|
[fa81e94] | 788 | |
---|
[34cf92c] | 789 | def _estimateDynamicUpdate(self, output_tuple): |
---|
| 790 | """ |
---|
| 791 | Parameter estimation completed, |
---|
| 792 | display the results to the user |
---|
| 793 | |
---|
| 794 | :param alpha: estimated best alpha |
---|
| 795 | :param elapsed: computation time |
---|
| 796 | """ |
---|
| 797 | alpha, message, elapsed = output_tuple |
---|
| 798 | self._calculator.alpha = alpha |
---|
| 799 | self._calculator.elapsed += self._calculator.elapsed |
---|
| 800 | if message: |
---|
| 801 | logger.info(message) |
---|
| 802 | self.performEstimateDynamicNT() |
---|
| 803 | |
---|
[fa81e94] | 804 | def _estimateNTCompleted(self, nterms, alpha, message, elapsed): |
---|
[f1ec901] | 805 | ''' Send a signal to the main thread for model update''' |
---|
| 806 | self.estimateNTSignal.emit((nterms, alpha, message, elapsed)) |
---|
| 807 | |
---|
[34cf92c] | 808 | def _estimateDynamicNTCompleted(self, nterms, alpha, message, elapsed): |
---|
| 809 | ''' Send a signal to the main thread for model update''' |
---|
| 810 | self.estimateDynamicNTSignal.emit((nterms, alpha, message, elapsed)) |
---|
| 811 | |
---|
[f1ec901] | 812 | def _estimateNTUpdate(self, output_tuple): |
---|
[fa81e94] | 813 | """ |
---|
| 814 | Parameter estimation completed, |
---|
| 815 | display the results to the user |
---|
| 816 | |
---|
| 817 | :param alpha: estimated best alpha |
---|
| 818 | :param nterms: estimated number of terms |
---|
| 819 | :param elapsed: computation time |
---|
| 820 | """ |
---|
[f1ec901] | 821 | nterms, alpha, message, elapsed = output_tuple |
---|
[6da860a] | 822 | self._calculator.elapsed += elapsed |
---|
[e51e078] | 823 | self._calculator.suggested_alpha = alpha |
---|
| 824 | self.nTermsSuggested = nterms |
---|
[fa81e94] | 825 | # Save useful info |
---|
[e51e078] | 826 | self.updateGuiValues() |
---|
[fa81e94] | 827 | if message: |
---|
[6da860a] | 828 | logger.info(message) |
---|
[98485fe] | 829 | if self.isBatch: |
---|
| 830 | self.acceptAlpha() |
---|
| 831 | self.acceptNoTerms() |
---|
| 832 | self.startThread() |
---|
[fa81e94] | 833 | |
---|
[34cf92c] | 834 | def _estimateDynamicNTUpdate(self, output_tuple): |
---|
| 835 | """ |
---|
| 836 | Parameter estimation completed, |
---|
| 837 | display the results to the user |
---|
| 838 | |
---|
| 839 | :param alpha: estimated best alpha |
---|
| 840 | :param nterms: estimated number of terms |
---|
| 841 | :param elapsed: computation time |
---|
| 842 | """ |
---|
| 843 | nterms, alpha, message, elapsed = output_tuple |
---|
| 844 | self._calculator.elapsed += elapsed |
---|
| 845 | self._calculator.suggested_alpha = alpha |
---|
| 846 | self.nTermsSuggested = nterms |
---|
| 847 | # Save useful info |
---|
| 848 | self.updateDynamicGuiValues() |
---|
| 849 | if message: |
---|
| 850 | logger.info(message) |
---|
| 851 | if self.isBatch: |
---|
| 852 | self.acceptAlpha() |
---|
| 853 | self.acceptNoTerms() |
---|
| 854 | self.startThread() |
---|
| 855 | |
---|
[f1ec901] | 856 | def _calculateCompleted(self, out, cov, pr, elapsed): |
---|
| 857 | ''' Send a signal to the main thread for model update''' |
---|
| 858 | self.calculateSignal.emit((out, cov, pr, elapsed)) |
---|
| 859 | |
---|
| 860 | def _calculateUpdate(self, output_tuple): |
---|
[fa81e94] | 861 | """ |
---|
| 862 | Method called with the results when the inversion is done |
---|
| 863 | |
---|
| 864 | :param out: output coefficient for the base functions |
---|
| 865 | :param cov: covariance matrix |
---|
| 866 | :param pr: Invertor instance |
---|
| 867 | :param elapsed: time spent computing |
---|
| 868 | """ |
---|
[f1ec901] | 869 | out, cov, pr, elapsed = output_tuple |
---|
[fa81e94] | 870 | # Save useful info |
---|
| 871 | cov = np.ascontiguousarray(cov) |
---|
| 872 | pr.cov = cov |
---|
| 873 | pr.out = out |
---|
| 874 | pr.elapsed = elapsed |
---|
| 875 | |
---|
| 876 | # Save Pr invertor |
---|
| 877 | self._calculator = pr |
---|
[f1ec901] | 878 | |
---|
[318b353e] | 879 | # Update P(r) and fit plots |
---|
[ae34d30] | 880 | self.prPlot = self.logic.newPRPlot(out, self._calculator, cov) |
---|
| 881 | self.prPlot.filename = self.logic.data.filename |
---|
| 882 | self.dataPlot = self.logic.new1DPlot(out, self._calculator) |
---|
| 883 | self.dataPlot.filename = self.logic.data.filename |
---|
[318b353e] | 884 | |
---|
| 885 | # Udpate internals and GUI |
---|
[e51e078] | 886 | self.updateDataList(self._data) |
---|
[98485fe] | 887 | if self.isBatch: |
---|
| 888 | self.batchComplete.append(self.dataList.currentIndex()) |
---|
| 889 | self.startNextBatchItem() |
---|
[72ecbdf2] | 890 | else: |
---|
| 891 | self.isCalculating = False |
---|
| 892 | self.updateGuiValues() |
---|
[fa81e94] | 893 | |
---|
| 894 | def _threadError(self, error): |
---|
| 895 | """ |
---|
| 896 | Call-back method for calculation errors |
---|
| 897 | """ |
---|
[6da860a] | 898 | logger.error(error) |
---|
[b0ba43e] | 899 | if self.isBatch: |
---|
| 900 | self.startNextBatchItem() |
---|
| 901 | else: |
---|
| 902 | self.stopCalculation() |
---|