[e1e3e09] | 1 | """ |
---|
| 2 | Widget/logic for smearing data. |
---|
| 3 | """ |
---|
[9a7c81c] | 4 | import copy |
---|
| 5 | import numpy as np |
---|
[4992ff2] | 6 | from PyQt5 import QtCore |
---|
| 7 | from PyQt5 import QtGui |
---|
| 8 | from PyQt5 import QtWidgets |
---|
[e1e3e09] | 9 | |
---|
[9a7c81c] | 10 | from sas.sascalc.fit.qsmearing import smear_selection |
---|
[dc5ef15] | 11 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 12 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[d6b8a1d] | 13 | import sas.qtgui.Utilities.GuiUtils as GuiUtils |
---|
[e1e3e09] | 14 | |
---|
| 15 | # Local UI |
---|
[1bc27f1] | 16 | from sas.qtgui.Perspectives.Fitting.UI.SmearingWidgetUI import Ui_SmearingWidgetUI |
---|
[e1e3e09] | 17 | |
---|
[4992ff2] | 18 | class DataWidgetMapper(QtWidgets.QDataWidgetMapper): |
---|
[180bd54] | 19 | """ |
---|
| 20 | Custom version of the standard QDataWidgetMapper allowing for proper |
---|
| 21 | response to index change in comboboxes |
---|
| 22 | """ |
---|
| 23 | def addMapping(self, widget, section, propertyName=None): |
---|
| 24 | if propertyName is None: |
---|
| 25 | super(DataWidgetMapper, self).addMapping(widget, section) |
---|
| 26 | else: |
---|
| 27 | super(DataWidgetMapper, self).addMapping(widget, section, propertyName) |
---|
| 28 | |
---|
[4992ff2] | 29 | if isinstance(widget, QtWidgets.QComboBox): |
---|
[180bd54] | 30 | delegate = self.itemDelegate() |
---|
[e1e3e09] | 31 | widget.currentIndexChanged.connect(lambda: delegate.commitData.emit(widget)) |
---|
| 32 | |
---|
| 33 | SMEARING_1D = ["Custom Pinhole Smear", "Custom Slit Smear"] |
---|
| 34 | SMEARING_2D = ["Custom Pinhole Smear"] |
---|
| 35 | |
---|
| 36 | MODEL = [ |
---|
| 37 | 'SMEARING', |
---|
| 38 | 'PINHOLE_MIN', |
---|
| 39 | 'PINHOLE_MAX', |
---|
| 40 | 'ACCURACY'] |
---|
[9a7c81c] | 41 | ACCURACY_DICT={'Low': 'low', |
---|
| 42 | 'Medium': 'med', |
---|
| 43 | 'High': 'high', |
---|
| 44 | 'Extra high': 'xhigh'} |
---|
| 45 | |
---|
| 46 | DEFAULT_PINHOLE_UP=0.0 |
---|
| 47 | DEFAULT_PINHOLE_DOWN=0.0 |
---|
[e1e3e09] | 48 | |
---|
[4992ff2] | 49 | class SmearingWidget(QtWidgets.QWidget, Ui_SmearingWidgetUI): |
---|
[9a7c81c] | 50 | smearingChangedSignal = QtCore.pyqtSignal() |
---|
[e1e3e09] | 51 | def __init__(self, parent=None): |
---|
| 52 | super(SmearingWidget, self).__init__() |
---|
| 53 | |
---|
| 54 | self.setupUi(self) |
---|
[98b13f72] | 55 | |
---|
| 56 | # Local model for holding data |
---|
[e1e3e09] | 57 | self.model = None |
---|
[98b13f72] | 58 | # Mapper for model update |
---|
[e1e3e09] | 59 | self.mapper = None |
---|
[9a7c81c] | 60 | # Data from the widget |
---|
| 61 | self.data = None |
---|
| 62 | self.current_smearer = None |
---|
[e1e3e09] | 63 | |
---|
[98b13f72] | 64 | # Let only floats in the line edits |
---|
[d6b8a1d] | 65 | self.txtSmearDown.setValidator(GuiUtils.DoubleValidator()) |
---|
| 66 | self.txtSmearUp.setValidator(GuiUtils.DoubleValidator()) |
---|
[98b13f72] | 67 | |
---|
| 68 | # Attach slots |
---|
| 69 | self.cbSmearing.currentIndexChanged.connect(self.onIndexChange) |
---|
| 70 | self.cbSmearing.setCurrentIndex(0) |
---|
[9a7c81c] | 71 | self.txtSmearUp.setText(str(DEFAULT_PINHOLE_UP)) |
---|
| 72 | self.txtSmearDown.setText(str(DEFAULT_PINHOLE_DOWN)) |
---|
[98b13f72] | 73 | |
---|
[e1e3e09] | 74 | self.initModel() |
---|
| 75 | self.initMapper() |
---|
| 76 | |
---|
| 77 | def initModel(self): |
---|
| 78 | """ |
---|
| 79 | Initialize the state |
---|
| 80 | """ |
---|
| 81 | self.model = QtGui.QStandardItemModel() |
---|
[b3e8629] | 82 | for model_item in range(len(MODEL)): |
---|
[e1e3e09] | 83 | self.model.setItem(model_item, QtGui.QStandardItem()) |
---|
[98b13f72] | 84 | # Attach slot |
---|
[e1e3e09] | 85 | self.model.dataChanged.connect(self.onModelChange) |
---|
| 86 | |
---|
| 87 | def initMapper(self): |
---|
| 88 | """ |
---|
| 89 | Initialize model item <-> UI element mapping |
---|
| 90 | """ |
---|
| 91 | self.mapper = DataWidgetMapper(self) |
---|
| 92 | |
---|
| 93 | self.mapper.setModel(self.model) |
---|
| 94 | self.mapper.setOrientation(QtCore.Qt.Vertical) |
---|
| 95 | |
---|
| 96 | self.mapper.addMapping(self.txtSmearUp, MODEL.index('PINHOLE_MIN')) |
---|
| 97 | self.mapper.addMapping(self.txtSmearDown, MODEL.index('PINHOLE_MAX')) |
---|
| 98 | self.mapper.addMapping(self.cbAccuracy, MODEL.index('ACCURACY')) |
---|
[4992ff2] | 99 | |
---|
[9a7c81c] | 100 | self.mapper.toFirst() |
---|
[e1e3e09] | 101 | |
---|
[9a7c81c] | 102 | def updateData(self, data=None): |
---|
[e1e3e09] | 103 | """ |
---|
[9a7c81c] | 104 | Update control elements based on data and model passed |
---|
[e1e3e09] | 105 | """ |
---|
| 106 | self.cbSmearing.clear() |
---|
| 107 | self.cbSmearing.addItem("None") |
---|
[9a7c81c] | 108 | self.gAccuracy.setVisible(False) |
---|
| 109 | self.data = data |
---|
[e1e3e09] | 110 | if data is None: |
---|
| 111 | self.setElementsVisibility(False) |
---|
| 112 | elif isinstance(data, Data1D): |
---|
| 113 | self.cbSmearing.addItems(SMEARING_1D) |
---|
| 114 | else: |
---|
| 115 | self.cbSmearing.addItems(SMEARING_2D) |
---|
| 116 | self.cbSmearing.setCurrentIndex(0) |
---|
| 117 | |
---|
[9a7c81c] | 118 | def updateKernelModel(self, kernel_model=None): |
---|
| 119 | """ |
---|
| 120 | Update the model |
---|
| 121 | """ |
---|
| 122 | self.kernel_model = kernel_model |
---|
| 123 | |
---|
| 124 | def smearer(self): |
---|
| 125 | """ Returns the current smearer """ |
---|
| 126 | return self.current_smearer |
---|
| 127 | |
---|
[e1e3e09] | 128 | def onIndexChange(self, index): |
---|
| 129 | """ |
---|
| 130 | Callback for smearing combobox index change |
---|
| 131 | """ |
---|
| 132 | if index == 0: |
---|
| 133 | self.setElementsVisibility(False) |
---|
[9a7c81c] | 134 | self.current_smearer = None |
---|
[e1e3e09] | 135 | elif index == 1: |
---|
| 136 | self.setElementsVisibility(True) |
---|
| 137 | self.setPinholeLabels() |
---|
[9a7c81c] | 138 | self.onPinholeSmear() |
---|
[e1e3e09] | 139 | elif index == 2: |
---|
| 140 | self.setElementsVisibility(True) |
---|
| 141 | self.setSlitLabels() |
---|
[9a7c81c] | 142 | self.onSlitSmear() |
---|
| 143 | self.smearingChangedSignal.emit() |
---|
[e1e3e09] | 144 | |
---|
[9a7c81c] | 145 | def onModelChange(self): |
---|
[e1e3e09] | 146 | """ |
---|
[9a7c81c] | 147 | Respond to model change by notifying any listeners |
---|
[e1e3e09] | 148 | """ |
---|
[9a7c81c] | 149 | # Recalculate the smearing |
---|
| 150 | index = self.cbSmearing.currentIndex() |
---|
| 151 | self.onIndexChange(index) |
---|
[e1e3e09] | 152 | |
---|
| 153 | def setElementsVisibility(self, visible): |
---|
| 154 | """ |
---|
| 155 | Labels and linedits visibility control |
---|
| 156 | """ |
---|
| 157 | self.lblSmearDown.setVisible(visible) |
---|
| 158 | self.lblSmearUp.setVisible(visible) |
---|
| 159 | self.txtSmearDown.setVisible(visible) |
---|
| 160 | self.txtSmearUp.setVisible(visible) |
---|
[9a7c81c] | 161 | self.lblUnitUp.setVisible(visible) |
---|
| 162 | self.lblUnitDown.setVisible(visible) |
---|
[e1e3e09] | 163 | self.setAccuracyVisibility() |
---|
| 164 | |
---|
| 165 | def setAccuracyVisibility(self): |
---|
| 166 | """ |
---|
| 167 | Accuracy combobox visibility |
---|
| 168 | """ |
---|
[9a7c81c] | 169 | if isinstance(self.data, Data2D) and self.cbSmearing.currentIndex() == 1: |
---|
| 170 | self.gAccuracy.setVisible(True) |
---|
[e1e3e09] | 171 | else: |
---|
[9a7c81c] | 172 | self.gAccuracy.setVisible(False) |
---|
[e1e3e09] | 173 | |
---|
| 174 | def setPinholeLabels(self): |
---|
| 175 | """ |
---|
| 176 | Use pinhole labels |
---|
| 177 | """ |
---|
[9a7c81c] | 178 | self.txtSmearDown.setVisible(False) |
---|
| 179 | self.lblSmearDown.setText('') |
---|
| 180 | self.lblUnitDown.setText('') |
---|
| 181 | if isinstance(self.data, Data2D): |
---|
| 182 | self.lblUnitUp.setText('<html><head/><body><p>Ã
<span style=" vertical-align:super;">-1</span></p></body></html>') |
---|
| 183 | self.lblSmearUp.setText('<html><head/><body><p><dQ<span style=" vertical-align:sub;">low</span>></p></body></html>') |
---|
| 184 | else: |
---|
| 185 | self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">%</span></p></body></html>') |
---|
| 186 | self.lblUnitUp.setText('%') |
---|
[e1e3e09] | 187 | |
---|
| 188 | def setSlitLabels(self): |
---|
| 189 | """ |
---|
| 190 | Use pinhole labels |
---|
| 191 | """ |
---|
| 192 | self.lblSmearUp.setText('Slit height') |
---|
| 193 | self.lblSmearDown.setText('Slit width') |
---|
[9a7c81c] | 194 | self.lblUnitUp.setText('<html><head/><body><p>Ã
<span style=" vertical-align:super;">-1</span></p></body></html>') |
---|
| 195 | self.lblUnitDown.setText('<html><head/><body><p>Ã
<span style=" vertical-align:super;">-1</span></p></body></html>') |
---|
[e1e3e09] | 196 | |
---|
| 197 | def state(self): |
---|
| 198 | """ |
---|
| 199 | Returns current state of controls |
---|
| 200 | """ |
---|
[9a7c81c] | 201 | smearing = self.cbSmearing.currentText() |
---|
[180bd54] | 202 | accuracy = "" |
---|
| 203 | d_down = None |
---|
| 204 | d_up = None |
---|
| 205 | if smearing != "None": |
---|
| 206 | accuracy = str(self.model.item(MODEL.index('ACCURACY')).text()) |
---|
| 207 | try: |
---|
| 208 | d_down = float(self.model.item(MODEL.index('PINHOLE_MIN')).text()) |
---|
| 209 | except ValueError: |
---|
| 210 | d_down = None |
---|
| 211 | try: |
---|
| 212 | d_up = float(self.model.item(MODEL.index('PINHOLE_MAX')).text()) |
---|
| 213 | except ValueError: |
---|
| 214 | d_up = None |
---|
[98b13f72] | 215 | |
---|
| 216 | return (smearing, accuracy, d_down, d_up) |
---|
[672b8ab] | 217 | |
---|
| 218 | def setState(self, smearing, accuracy, d_down, d_up): |
---|
| 219 | """ |
---|
| 220 | Sets new values for the controls |
---|
| 221 | """ |
---|
| 222 | # Update the model -> controls update automatically |
---|
[9a7c81c] | 223 | #if smearing is not None: |
---|
| 224 | #self.model.item(MODEL.index('SMEARING')).setText(smearing) |
---|
[672b8ab] | 225 | if accuracy is not None: |
---|
| 226 | self.model.item(MODEL.index('ACCURACY')).setText(accuracy) |
---|
| 227 | if d_down is not None: |
---|
| 228 | self.model.item(MODEL.index('PINHOLE_MIN')).setText(d_down) |
---|
| 229 | if d_up is not None: |
---|
| 230 | self.model.item(MODEL.index('PINHOLE_MAX')).setText(d_up) |
---|
| 231 | |
---|
[9a7c81c] | 232 | def onPinholeSmear(self): |
---|
| 233 | """ |
---|
| 234 | Create a custom pinhole smear object that will change the way residuals |
---|
| 235 | are compute when fitting |
---|
| 236 | """ |
---|
| 237 | _, accuracy, d_percent, _ = self.state() |
---|
| 238 | if d_percent is None or d_percent == 0.0: |
---|
| 239 | self.current_smearer=None |
---|
| 240 | return |
---|
| 241 | percent = d_percent/100.0 |
---|
| 242 | # copy data |
---|
| 243 | data = copy.deepcopy(self.data) |
---|
| 244 | if isinstance(self.data, Data2D): |
---|
| 245 | len_data = len(data.data) |
---|
| 246 | data.dqx_data = np.zeros(len_data) |
---|
| 247 | data.dqy_data = np.zeros(len_data) |
---|
| 248 | data.dqx_data[data.dqx_data == 0] = percent * data.qx_data |
---|
| 249 | data.dqy_data[data.dqy_data == 0] = percent * data.qy_data |
---|
| 250 | else: |
---|
| 251 | len_data = len(data.x) |
---|
| 252 | data.dx = np.zeros(len_data) |
---|
| 253 | data.dx = percent * data.x |
---|
| 254 | data.dxl = None |
---|
| 255 | data.dxw = None |
---|
| 256 | |
---|
| 257 | self.current_smearer = smear_selection(data, self.kernel_model) |
---|
| 258 | # need to set accuracy for 2D |
---|
| 259 | if isinstance(self.data, Data2D): |
---|
| 260 | backend_accuracy = ACCURACY_DICT.get(accuracy) |
---|
| 261 | if backend_accuracy: |
---|
| 262 | self.current_smearer.set_accuracy(accuracy=backend_accuracy) |
---|
| 263 | |
---|
| 264 | def onSlitSmear(self): |
---|
| 265 | """ |
---|
| 266 | Create a custom slit smear object that will change the way residuals |
---|
| 267 | are compute when fitting |
---|
| 268 | """ |
---|
| 269 | _, accuracy, d_height, d_width = self.state() |
---|
| 270 | # Check changes in slit width |
---|
| 271 | if d_width is None: |
---|
| 272 | d_width = 0.0 |
---|
| 273 | if d_height is None: |
---|
| 274 | d_height = 0.0 |
---|
| 275 | |
---|
| 276 | if isinstance(self.data, Data2D): |
---|
| 277 | return |
---|
| 278 | # make sure once more if it is smearer |
---|
| 279 | data = copy.deepcopy(self.data) |
---|
| 280 | data_len = len(data.x) |
---|
| 281 | data.dx = None |
---|
| 282 | data.dxl = None |
---|
| 283 | data.dxw = None |
---|
| 284 | |
---|
| 285 | try: |
---|
| 286 | self.dxl = d_height |
---|
| 287 | data.dxl = self.dxl * np.ones(data_len) |
---|
| 288 | except: |
---|
| 289 | self.dxl = None |
---|
| 290 | data.dxl = np.zeros(data_len) |
---|
| 291 | try: |
---|
| 292 | self.dxw = d_width |
---|
| 293 | data.dxw = self.dxw * np.ones(data_len) |
---|
| 294 | except: |
---|
| 295 | self.dxw = None |
---|
| 296 | data.dxw = np.zeros(data_len) |
---|
| 297 | |
---|
| 298 | self.current_smearer = smear_selection(data, self.kernel_model) |
---|