Changeset b46f285 in sasview for src/sas/qtgui/LinearFit.py
- Timestamp:
- Jan 9, 2017 9:49:16 AM (8 years ago)
- Branches:
- ESS_GUI, ESS_GUI_Docs, 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:
- 87cc73a
- Parents:
- a66ff280
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/LinearFit.py
rfed94a2 rb46f285 38 38 self.yLabel = ylabel 39 39 40 self.x_is_log = self.xLabel == "log10(x)" 41 self.y_is_log = self.yLabel == "log10(y)" 42 40 43 self.txtFitRangeMin.setValidator(QtGui.QDoubleValidator()) 41 44 self.txtFitRangeMax.setValidator(QtGui.QDoubleValidator()) … … 108 111 self.xminFit, self.xmaxFit = self.range() 109 112 110 xminView = self.xminFit 111 xmaxView = self.xmaxFit 112 xmin = xminView 113 xmax = xmaxView 113 xmin = self.xminFit 114 xmax = self.xmaxFit 115 xminView = xmin 116 xmaxView = xmax 117 114 118 # Set the qmin and qmax in the panel that matches the 115 119 # transformed min and max 116 120 #value_xmin = X_VAL_DICT[self.xLabel].floatTransform(xmin) 117 121 #value_xmax = X_VAL_DICT[self.xLabel].floatTransform(xmax) 122 118 123 value_xmin = self.floatInvTransform(xmin) 119 124 value_xmax = self.floatInvTransform(xmax) … … 121 126 self.txtRangeMax.setText(formatNumber(value_xmax)) 122 127 128 tempx, tempy, tempdy = self.origData() 129 130 # Find the fitting parameters 131 self.cstA = fittings.Parameter(self.model, 'A', self.default_A) 132 self.cstB = fittings.Parameter(self.model, 'B', self.default_B) 133 tempdy = numpy.asarray(tempdy) 134 tempdy[tempdy == 0] = 1 135 136 if self.x_is_log: 137 xmin = numpy.log10(xmin) 138 xmax = numpy.log10(xmax) 139 140 chisqr, out, cov = fittings.sasfit(self.model, 141 [self.cstA, self.cstB], 142 tempx, tempy, tempdy, 143 xmin, xmax) 144 # Use chi2/dof 145 if len(tempx) > 0: 146 chisqr = chisqr / len(tempx) 147 148 # Check that cov and out are iterable before displaying them 149 errA = numpy.sqrt(cov[0][0]) if cov is not None else 0 150 errB = numpy.sqrt(cov[1][1]) if cov is not None else 0 151 cstA = out[0] if out is not None else 0.0 152 cstB = out[1] if out is not None else 0.0 153 154 # Reset model with the right values of A and B 155 self.model.setParam('A', float(cstA)) 156 self.model.setParam('B', float(cstB)) 157 158 tempx = [] 159 tempy = [] 160 y_model = 0.0 161 162 # load tempy with the minimum transformation 163 y_model = self.model.run(xmin) 164 tempx.append(xminView) 165 tempy.append(numpy.power(10, y_model) if self.y_is_log else y_model) 166 167 # load tempy with the maximum transformation 168 y_model = self.model.run(xmax) 169 tempx.append(xmaxView) 170 tempy.append(numpy.power(10, y_model) if self.y_is_log else y_model) 171 172 # Set the fit parameter display when FitDialog is opened again 173 self.Avalue = cstA 174 self.Bvalue = cstB 175 self.ErrAvalue = errA 176 self.ErrBvalue = errB 177 self.Chivalue = chisqr 178 179 # Update the widget 180 self.txtA.setText(formatNumber(self.Avalue)) 181 self.txtAerr.setText(formatNumber(self.ErrAvalue)) 182 self.txtB.setText(formatNumber(self.Bvalue)) 183 self.txtBerr.setText(formatNumber(self.ErrBvalue)) 184 self.txtChi2.setText(formatNumber(self.Chivalue)) 185 186 #self.parent.updatePlot.emit((tempx, tempy)) 187 self.parent.emit(QtCore.SIGNAL('updatePlot'), (tempx, tempy)) 188 189 def origData(self): 123 190 # Store the transformed values of view x, y and dy before the fit 124 xmin_check = numpy.log10(xmin) 191 xmin_check = numpy.log10(self.xminFit) 192 # Local shortcuts 125 193 x = self.data.view.x 126 194 y = self.data.view.y 127 195 dy = self.data.view.dy 128 196 129 if self.y Label == "log10(y)":130 if self.x Label == "log10(x)":197 if self.y_is_log: 198 if self.x_is_log: 131 199 tempy = [numpy.log10(y[i]) 132 200 for i in range(len(x)) if x[i] >= xmin_check] … … 140 208 tempdy = dy 141 209 142 if self.x Label == "log10(x)":210 if self.x_is_log: 143 211 tempx = [numpy.log10(x) for x in self.data.view.x if x > xmin_check] 144 212 else: 145 tempx = self.data.view.x 146 147 # Find the fitting parameters 148 # Always use the same defaults, so that fit history 149 # doesn't play a role! 150 self.cstA = fittings.Parameter(self.model, 'A', self.default_A) 151 self.cstB = fittings.Parameter(self.model, 'B', self.default_B) 152 tempdy = numpy.asarray(tempdy) 153 tempdy[tempdy == 0] = 1 154 155 if self.xLabel == "log10(x)": 156 chisqr, out, cov = fittings.sasfit(self.model, 157 [self.cstA, self.cstB], 158 tempx, tempy, 159 tempdy, 160 numpy.log10(xmin), 161 numpy.log10(xmax)) 162 else: 163 chisqr, out, cov = fittings.sasfit(self.model, 164 [self.cstA, self.cstB], 165 tempx, tempy, tempdy, 166 xminView, xmaxView) 167 # Use chi2/dof 168 if len(tempx) > 0: 169 chisqr = chisqr / len(tempx) 170 171 # Check that cov and out are iterable before displaying them 172 errA = numpy.sqrt(cov[0][0]) if cov is not None else 0 173 errB = numpy.sqrt(cov[1][1]) if cov is not None else 0 174 cstA = out[0] if out is not None else 0.0 175 cstB = out[1] if out is not None else 0.0 176 177 # Reset model with the right values of A and B 178 self.model.setParam('A', float(cstA)) 179 self.model.setParam('B', float(cstB)) 180 181 tempx = [] 182 tempy = [] 183 y_model = 0.0 184 # load tempy with the minimum transformation 185 if self.xLabel == "log10(x)": 186 y_model = self.model.run(numpy.log10(xmin)) 187 tempx.append(xmin) 188 else: 189 y_model = self.model.run(xminView) 190 tempx.append(xminView) 191 192 if self.yLabel == "log10(y)": 193 tempy.append(numpy.power(10, y_model)) 194 else: 195 tempy.append(y_model) 196 197 # load tempy with the maximum transformation 198 if self.xLabel == "log10(x)": 199 y_model = self.model.run(numpy.log10(xmax)) 200 tempx.append(xmax) 201 else: 202 y_model = self.model.run(xmaxView) 203 tempx.append(xmaxView) 204 205 if self.yLabel == "log10(y)": 206 tempy.append(numpy.power(10, y_model)) 207 else: 208 tempy.append(y_model) 209 # Set the fit parameter display when FitDialog is opened again 210 self.Avalue = cstA 211 self.Bvalue = cstB 212 self.ErrAvalue = errA 213 self.ErrBvalue = errB 214 self.Chivalue = chisqr 215 216 # Update the widget 217 self.txtA.setText(formatNumber(self.Avalue)) 218 self.txtAerr.setText(formatNumber(self.ErrAvalue)) 219 self.txtB.setText(formatNumber(self.Bvalue)) 220 self.txtBerr.setText(formatNumber(self.ErrBvalue)) 221 self.txtChi2.setText(formatNumber(self.Chivalue)) 222 223 #self.parent.updatePlot.emit((tempx, tempy)) 224 self.parent.emit(QtCore.SIGNAL('updatePlot'), (tempx, tempy)) 213 tempx = x 214 215 return tempx, tempy, tempdy 225 216 226 217 def checkFitValues(self, item): … … 234 225 p_pink = item.palette() 235 226 p_pink.setColor(item.backgroundRole(), QtGui.QColor(255, 128, 128)) 227 item.setAutoFillBackground(True) 236 228 # Check for possible values entered 237 if self.x Label == "log10(x)":229 if self.x_is_log: 238 230 if float(value) > 0: 239 231 item.setPalette(p_white) … … 259 251 return numpy.sqrt(x) 260 252 elif self.xLabel == "x^(4)": 261 return numpy.sqrt( math.sqrt(x))253 return numpy.sqrt(numpy.sqrt(x)) 262 254 elif self.xLabel == "log10(x)": 263 255 return numpy.power(10, x)
Note: See TracChangeset
for help on using the changeset viewer.