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