Changeset 9a7c81c in sasview for src/sas/qtgui/Perspectives/Fitting/SmearingWidget.py
- Timestamp:
- May 28, 2018 8:08:36 AM (6 years ago)
- Branches:
- ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
- Children:
- 3010f68
- Parents:
- c389557
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Perspectives/Fitting/SmearingWidget.py
rd6b8a1d r9a7c81c 2 2 Widget/logic for smearing data. 3 3 """ 4 import copy 5 import numpy as np 4 6 from PyQt5 import QtCore 5 7 from PyQt5 import QtGui 6 8 from PyQt5 import QtWidgets 7 9 10 from sas.sascalc.fit.qsmearing import smear_selection 8 11 from sas.qtgui.Plotting.PlotterData import Data1D 9 12 from sas.qtgui.Plotting.PlotterData import Data2D … … 36 39 'PINHOLE_MAX', 37 40 'ACCURACY'] 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 38 48 39 49 class SmearingWidget(QtWidgets.QWidget, Ui_SmearingWidgetUI): 50 smearingChangedSignal = QtCore.pyqtSignal() 40 51 def __init__(self, parent=None): 41 52 super(SmearingWidget, self).__init__() … … 43 54 self.setupUi(self) 44 55 45 # Have we loaded data yet? If so, what kind46 self.have_data = None47 56 # Local model for holding data 48 57 self.model = None 49 58 # Mapper for model update 50 59 self.mapper = None 51 52 self.parent = parent 60 # Data from the widget 61 self.data = None 62 self.current_smearer = None 63 53 64 # Let only floats in the line edits 54 65 self.txtSmearDown.setValidator(GuiUtils.DoubleValidator()) … … 58 69 self.cbSmearing.currentIndexChanged.connect(self.onIndexChange) 59 70 self.cbSmearing.setCurrentIndex(0) 71 self.txtSmearUp.setText(str(DEFAULT_PINHOLE_UP)) 72 self.txtSmearDown.setText(str(DEFAULT_PINHOLE_DOWN)) 60 73 61 74 self.initModel() … … 83 96 self.mapper.addMapping(self.txtSmearUp, MODEL.index('PINHOLE_MIN')) 84 97 self.mapper.addMapping(self.txtSmearDown, MODEL.index('PINHOLE_MAX')) 85 self.mapper.addMapping(self.cbSmearing, MODEL.index('SMEARING'))86 98 self.mapper.addMapping(self.cbAccuracy, MODEL.index('ACCURACY')) 87 99 88 # FIXME DOESNT WORK WITH QT5 89 #self.mapper.toFirst() 90 91 def updateSmearing(self, data=None): 92 """ 93 Update control elements based on data passed 100 self.mapper.toFirst() 101 102 def updateData(self, data=None): 103 """ 104 Update control elements based on data and model passed 94 105 """ 95 106 self.cbSmearing.clear() 96 107 self.cbSmearing.addItem("None") 97 self. cbAccuracy.setVisible(False)98 108 self.gAccuracy.setVisible(False) 109 self.data = data 99 110 if data is None: 100 111 self.setElementsVisibility(False) 101 112 elif isinstance(data, Data1D): 102 113 self.cbSmearing.addItems(SMEARING_1D) 103 self.have_data = Data1D104 114 else: 105 115 self.cbSmearing.addItems(SMEARING_2D) 106 self.have_data = Data2D107 116 self.cbSmearing.setCurrentIndex(0) 117 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 108 127 109 128 def onIndexChange(self, index): … … 113 132 if index == 0: 114 133 self.setElementsVisibility(False) 115 return134 self.current_smearer = None 116 135 elif index == 1: 117 136 self.setElementsVisibility(True) 118 137 self.setPinholeLabels() 138 self.onPinholeSmear() 119 139 elif index == 2: 120 140 self.setElementsVisibility(True) 121 141 self.setSlitLabels() 122 123 def onModelChange(self, top, bottom): 124 """ 125 Respond to model change by updating 126 """ 127 #print "MODEL CHANGED for property: %s. The value is now: %s" % \ 128 # (MODEL[top.row()], str(self.model.item(top.row()).text())) 129 pass 142 self.onSlitSmear() 143 self.smearingChangedSignal.emit() 144 145 def onModelChange(self): 146 """ 147 Respond to model change by notifying any listeners 148 """ 149 # Recalculate the smearing 150 index = self.cbSmearing.currentIndex() 151 self.onIndexChange(index) 130 152 131 153 def setElementsVisibility(self, visible): … … 137 159 self.txtSmearDown.setVisible(visible) 138 160 self.txtSmearUp.setVisible(visible) 139 self.l abel_14.setVisible(visible)140 self.l abel_15.setVisible(visible)161 self.lblUnitUp.setVisible(visible) 162 self.lblUnitDown.setVisible(visible) 141 163 self.setAccuracyVisibility() 142 164 … … 145 167 Accuracy combobox visibility 146 168 """ 147 if self.have_data == Data2Dand self.cbSmearing.currentIndex() == 1:148 self. cbAccuracy.setVisible(True)149 else: 150 self. cbAccuracy.setVisible(False)169 if isinstance(self.data, Data2D) and self.cbSmearing.currentIndex() == 1: 170 self.gAccuracy.setVisible(True) 171 else: 172 self.gAccuracy.setVisible(False) 151 173 152 174 def setPinholeLabels(self): … … 154 176 Use pinhole labels 155 177 """ 156 self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">low</span></p></body></html>') 157 self.lblSmearDown.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">high</span></p></body></html>') 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>Ã 183 <span style=" vertical-align:super;">-1</span></p></body></html>') 184 self.lblSmearUp.setText('<html><head/><body><p><dQ<span style=" vertical-align:sub;">low</span>></p></body></html>') 185 else: 186 self.lblSmearUp.setText('<html><head/><body><p>dQ<span style=" vertical-align:sub;">%</span></p></body></html>') 187 self.lblUnitUp.setText('%') 158 188 159 189 def setSlitLabels(self): … … 163 193 self.lblSmearUp.setText('Slit height') 164 194 self.lblSmearDown.setText('Slit width') 195 self.lblUnitUp.setText('<html><head/><body><p>Ã 196 <span style=" vertical-align:super;">-1</span></p></body></html>') 197 self.lblUnitDown.setText('<html><head/><body><p>Ã 198 <span style=" vertical-align:super;">-1</span></p></body></html>') 165 199 166 200 def state(self): … … 168 202 Returns current state of controls 169 203 """ 170 # or model-held values 171 smearing = str(self.model.item(MODEL.index('SMEARING')).text()) 204 smearing = self.cbSmearing.currentText() 172 205 accuracy = "" 173 206 d_down = None … … 191 224 """ 192 225 # Update the model -> controls update automatically 193 if smearing is not None:194 self.model.item(MODEL.index('SMEARING')).setText(smearing)226 #if smearing is not None: 227 #self.model.item(MODEL.index('SMEARING')).setText(smearing) 195 228 if accuracy is not None: 196 229 self.model.item(MODEL.index('ACCURACY')).setText(accuracy) … … 200 233 self.model.item(MODEL.index('PINHOLE_MAX')).setText(d_up) 201 234 235 def onPinholeSmear(self): 236 """ 237 Create a custom pinhole smear object that will change the way residuals 238 are compute when fitting 239 """ 240 _, accuracy, d_percent, _ = self.state() 241 if d_percent is None or d_percent == 0.0: 242 self.current_smearer=None 243 return 244 percent = d_percent/100.0 245 # copy data 246 data = copy.deepcopy(self.data) 247 if isinstance(self.data, Data2D): 248 len_data = len(data.data) 249 data.dqx_data = np.zeros(len_data) 250 data.dqy_data = np.zeros(len_data) 251 data.dqx_data[data.dqx_data == 0] = percent * data.qx_data 252 data.dqy_data[data.dqy_data == 0] = percent * data.qy_data 253 else: 254 len_data = len(data.x) 255 data.dx = np.zeros(len_data) 256 data.dx = percent * data.x 257 data.dxl = None 258 data.dxw = None 259 260 self.current_smearer = smear_selection(data, self.kernel_model) 261 # need to set accuracy for 2D 262 if isinstance(self.data, Data2D): 263 backend_accuracy = ACCURACY_DICT.get(accuracy) 264 if backend_accuracy: 265 self.current_smearer.set_accuracy(accuracy=backend_accuracy) 266 267 def onSlitSmear(self): 268 """ 269 Create a custom slit smear object that will change the way residuals 270 are compute when fitting 271 """ 272 _, accuracy, d_height, d_width = self.state() 273 # Check changes in slit width 274 if d_width is None: 275 d_width = 0.0 276 if d_height is None: 277 d_height = 0.0 278 279 if isinstance(self.data, Data2D): 280 return 281 # make sure once more if it is smearer 282 data = copy.deepcopy(self.data) 283 data_len = len(data.x) 284 data.dx = None 285 data.dxl = None 286 data.dxw = None 287 288 try: 289 self.dxl = d_height 290 data.dxl = self.dxl * np.ones(data_len) 291 except: 292 self.dxl = None 293 data.dxl = np.zeros(data_len) 294 try: 295 self.dxw = d_width 296 data.dxw = self.dxw * np.ones(data_len) 297 except: 298 self.dxw = None 299 data.dxw = np.zeros(data_len) 300 301 self.current_smearer = smear_selection(data, self.kernel_model)
Note: See TracChangeset
for help on using the changeset viewer.