[1bc27f1] | 1 | from copy import deepcopy |
---|
| 2 | |
---|
[4d457df] | 3 | from PyQt4 import QtGui |
---|
| 4 | from PyQt4 import QtCore |
---|
| 5 | |
---|
[6fd4e36] | 6 | import numpy |
---|
| 7 | |
---|
| 8 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 9 | from sas.sasgui.guiframe.dataFitting import Data2D |
---|
| 10 | |
---|
[4d457df] | 11 | def replaceShellName(param_name, value): |
---|
| 12 | """ |
---|
| 13 | Updates parameter name from <param_name>[n_shell] to <param_name>value |
---|
| 14 | """ |
---|
| 15 | assert '[' in param_name |
---|
| 16 | return param_name[:param_name.index('[')]+str(value) |
---|
| 17 | |
---|
| 18 | def getIterParams(model): |
---|
| 19 | """ |
---|
| 20 | Returns a list of all multi-shell parameters in 'model' |
---|
| 21 | """ |
---|
| 22 | return list(filter(lambda par: "[" in par.name, model.iq_parameters)) |
---|
| 23 | |
---|
| 24 | def getMultiplicity(model): |
---|
| 25 | """ |
---|
| 26 | Finds out if 'model' has multishell parameters. |
---|
| 27 | If so, returns the name of the counter parameter and the number of shells |
---|
| 28 | """ |
---|
| 29 | iter_params = getIterParams(model) |
---|
[a9b568c] | 30 | param_name = "" |
---|
| 31 | param_length = 0 |
---|
| 32 | if iter_params: |
---|
| 33 | param_length = iter_params[0].length |
---|
| 34 | param_name = iter_params[0].length_control |
---|
| 35 | if param_name is None and '[' in iter_params[0].name: |
---|
| 36 | param_name = iter_params[0].name[:iter_params[0].name.index('[')] |
---|
| 37 | return (param_name, param_length) |
---|
[4d457df] | 38 | |
---|
[1970780] | 39 | def addParametersToModel(parameters, is2D): |
---|
[4d457df] | 40 | """ |
---|
| 41 | Update local ModelModel with sasmodel parameters |
---|
| 42 | """ |
---|
| 43 | multishell_parameters = getIterParams(parameters) |
---|
| 44 | multishell_param_name, _ = getMultiplicity(parameters) |
---|
[1970780] | 45 | params = parameters.iqxy_parameters if is2D else parameters.iq_parameters |
---|
| 46 | item = [] |
---|
| 47 | for param in params: |
---|
[4d457df] | 48 | # don't include shell parameters |
---|
| 49 | if param.name == multishell_param_name: |
---|
| 50 | continue |
---|
| 51 | # Modify parameter name from <param>[n] to <param>1 |
---|
| 52 | item_name = param.name |
---|
| 53 | if param in multishell_parameters: |
---|
[b1e36a3] | 54 | continue |
---|
| 55 | # item_name = replaceShellName(param.name, 1) |
---|
[4d457df] | 56 | |
---|
| 57 | item1 = QtGui.QStandardItem(item_name) |
---|
| 58 | item1.setCheckable(True) |
---|
[2add354] | 59 | item1.setEditable(False) |
---|
[1bc27f1] | 60 | # item_err = QtGui.QStandardItem() |
---|
[4d457df] | 61 | # check for polydisp params |
---|
| 62 | if param.polydisperse: |
---|
| 63 | poly_item = QtGui.QStandardItem("Polydispersity") |
---|
[2add354] | 64 | poly_item.setEditable(False) |
---|
[4d457df] | 65 | item1_1 = QtGui.QStandardItem("Distribution") |
---|
[2add354] | 66 | item1_1.setEditable(False) |
---|
[4d457df] | 67 | # Find param in volume_params |
---|
| 68 | for p in parameters.form_volume_parameters: |
---|
| 69 | if p.name != param.name: |
---|
| 70 | continue |
---|
| 71 | item1_2 = QtGui.QStandardItem(str(p.default)) |
---|
[2add354] | 72 | item1_2.setEditable(False) |
---|
[4d457df] | 73 | item1_3 = QtGui.QStandardItem(str(p.limits[0])) |
---|
[2add354] | 74 | item1_3.setEditable(False) |
---|
[4d457df] | 75 | item1_4 = QtGui.QStandardItem(str(p.limits[1])) |
---|
[2add354] | 76 | item1_4.setEditable(False) |
---|
[4d457df] | 77 | item1_5 = QtGui.QStandardItem(p.units) |
---|
[2add354] | 78 | item1_5.setEditable(False) |
---|
[4d457df] | 79 | poly_item.appendRow([item1_1, item1_2, item1_3, item1_4, item1_5]) |
---|
| 80 | break |
---|
| 81 | # Add the polydisp item as a child |
---|
| 82 | item1.appendRow([poly_item]) |
---|
| 83 | # Param values |
---|
| 84 | item2 = QtGui.QStandardItem(str(param.default)) |
---|
| 85 | # TODO: the error column. |
---|
| 86 | # Either add a proxy model or a custom view delegate |
---|
| 87 | #item_err = QtGui.QStandardItem() |
---|
| 88 | item3 = QtGui.QStandardItem(str(param.limits[0])) |
---|
| 89 | item4 = QtGui.QStandardItem(str(param.limits[1])) |
---|
| 90 | item5 = QtGui.QStandardItem(param.units) |
---|
[2add354] | 91 | item5.setEditable(False) |
---|
[1970780] | 92 | item.append([item1, item2, item3, item4, item5]) |
---|
| 93 | return item |
---|
[4d457df] | 94 | |
---|
[1970780] | 95 | def addSimpleParametersToModel(parameters, is2D): |
---|
[4d457df] | 96 | """ |
---|
| 97 | Update local ModelModel with sasmodel parameters |
---|
| 98 | """ |
---|
[1970780] | 99 | params = parameters.iqxy_parameters if is2D else parameters.iq_parameters |
---|
| 100 | item = [] |
---|
| 101 | for param in params: |
---|
[7248d75d] | 102 | # Create the top level, checkable item |
---|
[4d457df] | 103 | item_name = param.name |
---|
| 104 | item1 = QtGui.QStandardItem(item_name) |
---|
| 105 | item1.setCheckable(True) |
---|
[2add354] | 106 | item1.setEditable(False) |
---|
[4d457df] | 107 | # Param values |
---|
[2add354] | 108 | # TODO: add delegate for validation of cells |
---|
[4d457df] | 109 | item2 = QtGui.QStandardItem(str(param.default)) |
---|
[f182f93] | 110 | item4 = QtGui.QStandardItem(str(param.limits[0])) |
---|
| 111 | item5 = QtGui.QStandardItem(str(param.limits[1])) |
---|
| 112 | item6 = QtGui.QStandardItem(param.units) |
---|
[2add354] | 113 | item6.setEditable(False) |
---|
[1970780] | 114 | item.append([item1, item2, item4, item5, item6]) |
---|
| 115 | return item |
---|
[4d457df] | 116 | |
---|
| 117 | def addCheckedListToModel(model, param_list): |
---|
| 118 | """ |
---|
| 119 | Add a QItem to model. Makes the QItem checkable |
---|
| 120 | """ |
---|
| 121 | assert isinstance(model, QtGui.QStandardItemModel) |
---|
| 122 | item_list = [QtGui.QStandardItem(item) for item in param_list] |
---|
| 123 | item_list[0].setCheckable(True) |
---|
| 124 | model.appendRow(item_list) |
---|
| 125 | |
---|
| 126 | def addHeadersToModel(model): |
---|
| 127 | """ |
---|
| 128 | Adds predefined headers to the model |
---|
| 129 | """ |
---|
| 130 | model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter")) |
---|
| 131 | model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("Value")) |
---|
| 132 | model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min")) |
---|
| 133 | model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max")) |
---|
| 134 | model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("[Units]")) |
---|
| 135 | |
---|
[f182f93] | 136 | def addErrorHeadersToModel(model): |
---|
| 137 | """ |
---|
| 138 | Adds predefined headers to the model |
---|
| 139 | """ |
---|
| 140 | model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter")) |
---|
| 141 | model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("Value")) |
---|
| 142 | model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Error")) |
---|
| 143 | model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Min")) |
---|
| 144 | model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("Max")) |
---|
| 145 | model.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("[Units]")) |
---|
| 146 | |
---|
[4d457df] | 147 | def addPolyHeadersToModel(model): |
---|
| 148 | """ |
---|
| 149 | Adds predefined headers to the model |
---|
| 150 | """ |
---|
| 151 | model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("Parameter")) |
---|
| 152 | model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("PD[ratio]")) |
---|
| 153 | model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("Min")) |
---|
| 154 | model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("Max")) |
---|
| 155 | model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("Npts")) |
---|
| 156 | model.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("Nsigs")) |
---|
| 157 | model.setHeaderData(6, QtCore.Qt.Horizontal, QtCore.QVariant("Function")) |
---|
| 158 | |
---|
| 159 | def addShellsToModel(parameters, model, index): |
---|
| 160 | """ |
---|
| 161 | Find out multishell parameters and update the model with the requested number of them |
---|
| 162 | """ |
---|
| 163 | multishell_parameters = getIterParams(parameters) |
---|
| 164 | |
---|
| 165 | for i in xrange(index): |
---|
| 166 | for par in multishell_parameters: |
---|
[b1e36a3] | 167 | # Create the name: <param>[<i>], e.g. "sld1" for parameter "sld[n]" |
---|
| 168 | param_name = replaceShellName(par.name, i+1) |
---|
[4d457df] | 169 | item1 = QtGui.QStandardItem(param_name) |
---|
| 170 | item1.setCheckable(True) |
---|
| 171 | # check for polydisp params |
---|
| 172 | if par.polydisperse: |
---|
| 173 | poly_item = QtGui.QStandardItem("Polydispersity") |
---|
| 174 | item1_1 = QtGui.QStandardItem("Distribution") |
---|
| 175 | # Find param in volume_params |
---|
| 176 | for p in parameters.form_volume_parameters: |
---|
| 177 | if p.name != par.name: |
---|
| 178 | continue |
---|
| 179 | item1_2 = QtGui.QStandardItem(str(p.default)) |
---|
| 180 | item1_3 = QtGui.QStandardItem(str(p.limits[0])) |
---|
| 181 | item1_4 = QtGui.QStandardItem(str(p.limits[1])) |
---|
| 182 | item1_5 = QtGui.QStandardItem(p.units) |
---|
| 183 | poly_item.appendRow([item1_1, item1_2, item1_3, item1_4, item1_5]) |
---|
| 184 | break |
---|
| 185 | item1.appendRow([poly_item]) |
---|
| 186 | |
---|
| 187 | item2 = QtGui.QStandardItem(str(par.default)) |
---|
| 188 | item3 = QtGui.QStandardItem(str(par.limits[0])) |
---|
| 189 | item4 = QtGui.QStandardItem(str(par.limits[1])) |
---|
| 190 | item5 = QtGui.QStandardItem(par.units) |
---|
| 191 | model.appendRow([item1, item2, item3, item4, item5]) |
---|
| 192 | |
---|
[6fd4e36] | 193 | def calculateChi2(reference_data, current_data): |
---|
| 194 | """ |
---|
| 195 | Calculate Chi2 value between two sets of data |
---|
| 196 | """ |
---|
| 197 | |
---|
| 198 | # WEIGHING INPUT |
---|
| 199 | #from sas.sasgui.perspectives.fitting.utils import get_weight |
---|
| 200 | #flag = self.get_weight_flag() |
---|
| 201 | #weight = get_weight(data=self.data, is2d=self._is_2D(), flag=flag) |
---|
[1bc27f1] | 202 | chisqr = None |
---|
| 203 | if reference_data is None: |
---|
| 204 | return chisqr |
---|
[6fd4e36] | 205 | |
---|
| 206 | # temporary default values for index and weight |
---|
| 207 | index = None |
---|
| 208 | weight = None |
---|
| 209 | |
---|
| 210 | # Get data: data I, theory I, and data dI in order |
---|
| 211 | if isinstance(reference_data, Data2D): |
---|
[1bc27f1] | 212 | if index is None: |
---|
[6fd4e36] | 213 | index = numpy.ones(len(current_data.data), dtype=bool) |
---|
[1bc27f1] | 214 | if weight is not None: |
---|
[6fd4e36] | 215 | current_data.err_data = weight |
---|
| 216 | # get rid of zero error points |
---|
| 217 | index = index & (current_data.err_data != 0) |
---|
| 218 | index = index & (numpy.isfinite(current_data.data)) |
---|
| 219 | fn = current_data.data[index] |
---|
| 220 | gn = reference_data.data[index] |
---|
| 221 | en = current_data.err_data[index] |
---|
| 222 | else: |
---|
| 223 | # 1 d theory from model_thread is only in the range of index |
---|
[1bc27f1] | 224 | if index is None: |
---|
[6fd4e36] | 225 | index = numpy.ones(len(current_data.y), dtype=bool) |
---|
[1bc27f1] | 226 | if weight is not None: |
---|
[6fd4e36] | 227 | current_data.dy = weight |
---|
[1bc27f1] | 228 | if current_data.dy is None or current_data.dy == []: |
---|
[6fd4e36] | 229 | dy = numpy.ones(len(current_data.y)) |
---|
| 230 | else: |
---|
| 231 | ## Set consistently w/AbstractFitengine: |
---|
| 232 | # But this should be corrected later. |
---|
| 233 | dy = deepcopy(current_data.dy) |
---|
| 234 | dy[dy == 0] = 1 |
---|
| 235 | fn = current_data.y[index] |
---|
| 236 | gn = reference_data.y |
---|
| 237 | en = dy[index] |
---|
| 238 | # Calculate the residual |
---|
| 239 | try: |
---|
| 240 | res = (fn - gn) / en |
---|
| 241 | except ValueError: |
---|
[180bd54] | 242 | #print "Chi2 calculations: Unmatched lengths %s, %s, %s" % (len(fn), len(gn), len(en)) |
---|
[0268aed] | 243 | return None |
---|
[6fd4e36] | 244 | |
---|
| 245 | residuals = res[numpy.isfinite(res)] |
---|
| 246 | chisqr = numpy.average(residuals * residuals) |
---|
| 247 | |
---|
| 248 | return chisqr |
---|
| 249 | |
---|
[0268aed] | 250 | def residualsData1D(reference_data, current_data): |
---|
| 251 | """ |
---|
[7d077d1] | 252 | Calculate the residuals for difference of two Data1D sets |
---|
[0268aed] | 253 | """ |
---|
| 254 | # temporary default values for index and weight |
---|
| 255 | index = None |
---|
| 256 | weight = None |
---|
| 257 | |
---|
| 258 | # 1d theory from model_thread is only in the range of index |
---|
[180bd54] | 259 | if current_data.dy is None or current_data.dy == []: |
---|
[0268aed] | 260 | dy = numpy.ones(len(current_data.y)) |
---|
| 261 | else: |
---|
[180bd54] | 262 | dy = weight if weight is not None else numpy.ones(len(current_data.y)) |
---|
[0268aed] | 263 | dy[dy == 0] = 1 |
---|
| 264 | fn = current_data.y[index][0] |
---|
| 265 | gn = reference_data.y |
---|
| 266 | en = dy[index][0] |
---|
| 267 | # build residuals |
---|
| 268 | residuals = Data1D() |
---|
[180bd54] | 269 | if len(fn) == len(gn): |
---|
[0268aed] | 270 | y = (fn - gn)/en |
---|
| 271 | residuals.y = -y |
---|
[180bd54] | 272 | else: |
---|
[d48cc19] | 273 | # TODO: fix case where applying new data from file on top of existing model data |
---|
[0268aed] | 274 | y = (fn - gn[index][0]) / en |
---|
| 275 | residuals.y = y |
---|
[180bd54] | 276 | |
---|
| 277 | #try: |
---|
| 278 | # y = (fn - gn)/en |
---|
| 279 | # residuals.y = -y |
---|
| 280 | #except ValueError: |
---|
| 281 | # msg = "ResidualPlot Error: different number of data points in theory" |
---|
| 282 | # print msg |
---|
| 283 | # y = (fn - gn[index][0]) / en |
---|
| 284 | # residuals.y = y |
---|
[0268aed] | 285 | residuals.x = current_data.x[index][0] |
---|
| 286 | residuals.dy = numpy.ones(len(residuals.y)) |
---|
| 287 | residuals.dx = None |
---|
| 288 | residuals.dxl = None |
---|
| 289 | residuals.dxw = None |
---|
| 290 | residuals.ytransform = 'y' |
---|
[1bc27f1] | 291 | # For latter scale changes |
---|
[0268aed] | 292 | residuals.xaxis('\\rm{Q} ', 'A^{-1}') |
---|
| 293 | residuals.yaxis('\\rm{Residuals} ', 'normalized') |
---|
| 294 | |
---|
| 295 | return residuals |
---|
| 296 | |
---|
| 297 | def residualsData2D(reference_data, current_data): |
---|
| 298 | """ |
---|
[7d077d1] | 299 | Calculate the residuals for difference of two Data2D sets |
---|
[0268aed] | 300 | """ |
---|
| 301 | # temporary default values for index and weight |
---|
[1bc27f1] | 302 | # index = None |
---|
[0268aed] | 303 | weight = None |
---|
| 304 | |
---|
| 305 | # build residuals |
---|
| 306 | residuals = Data2D() |
---|
| 307 | # Not for trunk the line below, instead use the line above |
---|
| 308 | current_data.clone_without_data(len(current_data.data), residuals) |
---|
| 309 | residuals.data = None |
---|
| 310 | fn = current_data.data |
---|
| 311 | gn = reference_data.data |
---|
[180bd54] | 312 | en = current_data.err_data if weight is None else weight |
---|
[0268aed] | 313 | residuals.data = (fn - gn) / en |
---|
| 314 | residuals.qx_data = current_data.qx_data |
---|
| 315 | residuals.qy_data = current_data.qy_data |
---|
| 316 | residuals.q_data = current_data.q_data |
---|
| 317 | residuals.err_data = numpy.ones(len(residuals.data)) |
---|
| 318 | residuals.xmin = min(residuals.qx_data) |
---|
| 319 | residuals.xmax = max(residuals.qx_data) |
---|
| 320 | residuals.ymin = min(residuals.qy_data) |
---|
| 321 | residuals.ymax = max(residuals.qy_data) |
---|
| 322 | residuals.q_data = current_data.q_data |
---|
| 323 | residuals.mask = current_data.mask |
---|
| 324 | residuals.scale = 'linear' |
---|
| 325 | # check the lengths |
---|
| 326 | if len(residuals.data) != len(residuals.q_data): |
---|
| 327 | return None |
---|
| 328 | return residuals |
---|
| 329 | |
---|
| 330 | def plotResiduals(reference_data, current_data): |
---|
| 331 | """ |
---|
| 332 | Create Data1D/Data2D with residuals, ready for plotting |
---|
| 333 | """ |
---|
| 334 | data_copy = deepcopy(current_data) |
---|
| 335 | # Get data: data I, theory I, and data dI in order |
---|
| 336 | method_name = current_data.__class__.__name__ |
---|
| 337 | residuals_dict = {"Data1D": residualsData1D, |
---|
| 338 | "Data2D": residualsData2D} |
---|
| 339 | |
---|
| 340 | residuals = residuals_dict[method_name](reference_data, data_copy) |
---|
| 341 | |
---|
| 342 | theory_name = str(current_data.name.split()[0]) |
---|
| 343 | residuals.name = "Residuals for " + str(theory_name) + "[" + \ |
---|
| 344 | str(reference_data.filename) + "]" |
---|
| 345 | residuals.title = residuals.name |
---|
[f182f93] | 346 | residuals.ytransform = 'y' |
---|
| 347 | |
---|
[0268aed] | 348 | # when 2 data have the same id override the 1 st plotted |
---|
| 349 | # include the last part if keeping charts for separate models is required |
---|
| 350 | residuals.id = "res" + str(reference_data.id) # + str(theory_name) |
---|
| 351 | # group_id specify on which panel to plot this data |
---|
| 352 | group_id = reference_data.group_id |
---|
| 353 | residuals.group_id = "res" + str(group_id) |
---|
[1bc27f1] | 354 | |
---|
[0268aed] | 355 | # Symbol |
---|
| 356 | residuals.symbol = 0 |
---|
| 357 | residuals.hide_error = False |
---|
| 358 | |
---|
| 359 | return residuals |
---|
| 360 | |
---|
| 361 | |
---|
[6fd4e36] | 362 | def binary_encode(i, digits): |
---|
| 363 | return [i >> d & 1 for d in xrange(digits)] |
---|
| 364 | |
---|