[3e3ab46] | 1 | ################################################################################ |
---|
| 2 | #This software was developed by the University of Tennessee as part of the |
---|
| 3 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | #project funded by the US National Science Foundation. |
---|
| 5 | # |
---|
| 6 | #See the license text in license.txt |
---|
| 7 | # |
---|
| 8 | #copyright 2009, University of Tennessee |
---|
| 9 | ################################################################################ |
---|
[e88ebfd] | 10 | import copy |
---|
[3e3ab46] | 11 | from sans.models.qsmearing import smear_selection |
---|
[d89f09b] | 12 | |
---|
[3e3ab46] | 13 | |
---|
| 14 | class FitProblemComponent(object): |
---|
| 15 | """ |
---|
| 16 | Inferface containing information to store data, model, range of data, etc... |
---|
| 17 | and retreive this information. This is an inferface |
---|
| 18 | for a fitProblem i.e relationship between data and model. |
---|
| 19 | """ |
---|
| 20 | def enable_smearing(self, flag=False): |
---|
| 21 | """ |
---|
| 22 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
| 23 | flag is 0 ingore smear value. |
---|
| 24 | """ |
---|
| 25 | def get_smearer(self): |
---|
| 26 | """ |
---|
| 27 | return smear object |
---|
| 28 | """ |
---|
| 29 | def save_model_name(self, name): |
---|
| 30 | """ |
---|
| 31 | """ |
---|
| 32 | def get_name(self): |
---|
| 33 | """ |
---|
| 34 | """ |
---|
| 35 | def set_model(self, model): |
---|
| 36 | """ |
---|
| 37 | associates each model with its new created name |
---|
| 38 | :param model: model selected |
---|
| 39 | :param name: name created for model |
---|
| 40 | """ |
---|
| 41 | def get_model(self): |
---|
| 42 | """ |
---|
| 43 | :return: saved model |
---|
| 44 | """ |
---|
| 45 | def set_theory_data(self, data): |
---|
| 46 | """ |
---|
| 47 | save a copy of the data select to fit |
---|
| 48 | :param data: data selected |
---|
| 49 | """ |
---|
| 50 | def get_theory_data(self): |
---|
| 51 | """ |
---|
| 52 | :return: list of data dList |
---|
| 53 | """ |
---|
| 54 | def set_fit_data(self, data): |
---|
| 55 | """ |
---|
| 56 | Store of list of data and create by create new fitproblem of each data |
---|
| 57 | id , if there was existing information about model, this information |
---|
| 58 | get copy to the new fitproblem |
---|
| 59 | :param data: list of data selected |
---|
| 60 | """ |
---|
| 61 | def get_fit_data(self): |
---|
| 62 | """ |
---|
| 63 | """ |
---|
| 64 | def set_model_param(self, name, value=None): |
---|
| 65 | """ |
---|
| 66 | Store the name and value of a parameter of this fitproblem's model |
---|
| 67 | :param name: name of the given parameter |
---|
| 68 | :param value: value of that parameter |
---|
| 69 | """ |
---|
| 70 | def get_model_param(self): |
---|
| 71 | """ |
---|
| 72 | return list of couple of parameter name and value |
---|
| 73 | """ |
---|
| 74 | def schedule_tofit(self, schedule=0): |
---|
| 75 | """ |
---|
| 76 | set schedule to true to decide if this fit must be performed |
---|
| 77 | """ |
---|
| 78 | def get_scheduled(self): |
---|
| 79 | """ |
---|
| 80 | return true or false if a problem as being schedule for fitting |
---|
| 81 | """ |
---|
| 82 | def set_range(self, qmin=None, qmax=None): |
---|
| 83 | """ |
---|
| 84 | set fitting range |
---|
| 85 | """ |
---|
| 86 | def get_range(self): |
---|
| 87 | """ |
---|
| 88 | :return: fitting range |
---|
| 89 | """ |
---|
| 90 | def clear_model_param(self): |
---|
| 91 | """ |
---|
| 92 | clear constraint info |
---|
| 93 | """ |
---|
| 94 | def set_fit_tab_caption(self, caption): |
---|
| 95 | """ |
---|
| 96 | store the caption of the page associated with object |
---|
| 97 | """ |
---|
| 98 | def get_fit_tab_caption(self): |
---|
| 99 | """ |
---|
| 100 | Return the caption of the page associated with object |
---|
| 101 | """ |
---|
| 102 | |
---|
| 103 | |
---|
| 104 | class FitProblemDictionary(FitProblemComponent, dict): |
---|
| 105 | """ |
---|
| 106 | This module implements a dictionary of fitproblem objects |
---|
| 107 | """ |
---|
| 108 | def __init__(self): |
---|
| 109 | FitProblemComponent.__init__(self) |
---|
| 110 | dict.__init__(self) |
---|
| 111 | ## the current model |
---|
| 112 | self.model = None |
---|
| 113 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
| 114 | ## it will not be selected for fit |
---|
| 115 | self.schedule = 0 |
---|
| 116 | ##list containing parameter name and value |
---|
| 117 | self.list_param = [] |
---|
| 118 | ## fitting range |
---|
| 119 | self.qmin = None |
---|
| 120 | self.qmax = None |
---|
| 121 | self._smear_on = False |
---|
| 122 | self.scheduled = 0 |
---|
| 123 | self.fit_tab_caption = '' |
---|
| 124 | |
---|
| 125 | def enable_smearing(self, flag=False, fid=None): |
---|
| 126 | """ |
---|
| 127 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
| 128 | flag is 0 ingore smear value. |
---|
| 129 | """ |
---|
| 130 | self._smear_on = flag |
---|
| 131 | if fid is None: |
---|
| 132 | for value in self.itervalues(): |
---|
| 133 | value.enable_smearing(flag) |
---|
| 134 | else: |
---|
| 135 | if fid in self.iterkeys(): |
---|
| 136 | self[fid].enable_smearing(flag) |
---|
| 137 | |
---|
| 138 | def set_smearer(self, smearer, fid=None): |
---|
| 139 | """ |
---|
| 140 | save reference of smear object on fitdata |
---|
| 141 | :param smear: smear object from DataLoader |
---|
| 142 | """ |
---|
| 143 | if fid is None: |
---|
| 144 | for value in self.itervalues(): |
---|
| 145 | value.set_smearer(smearer) |
---|
| 146 | else: |
---|
| 147 | if fid in self.iterkeys(): |
---|
| 148 | self[fid].set_smearer(smearer) |
---|
| 149 | |
---|
| 150 | def get_smearer(self, fid=None): |
---|
| 151 | """ |
---|
| 152 | return smear object |
---|
| 153 | """ |
---|
| 154 | result = [] |
---|
| 155 | if fid is None: |
---|
| 156 | for value in self.itervalues(): |
---|
| 157 | result.append(value.get_smearer()) |
---|
| 158 | else: |
---|
| 159 | if fid in self.iterkeys(): |
---|
| 160 | result.append(self[fid].get_smearer()) |
---|
| 161 | return result |
---|
| 162 | |
---|
| 163 | def save_model_name(self, name, fid=None): |
---|
| 164 | """ |
---|
| 165 | """ |
---|
| 166 | if fid is None: |
---|
| 167 | for value in self.itervalues(): |
---|
| 168 | value.save_model_name(name) |
---|
| 169 | else: |
---|
| 170 | if fid in self.iterkeys(): |
---|
| 171 | self[fid].save_model_name(name) |
---|
| 172 | |
---|
| 173 | def get_name(self, fid=None): |
---|
| 174 | """ |
---|
| 175 | """ |
---|
| 176 | result = [] |
---|
| 177 | if fid is None: |
---|
| 178 | for value in self.itervalues(): |
---|
| 179 | result.append(value.get_name()) |
---|
| 180 | else: |
---|
| 181 | if fid in self.iterkeys(): |
---|
| 182 | result.append(self[fid].get_name()) |
---|
| 183 | return result |
---|
| 184 | |
---|
| 185 | def set_model(self, model, fid=None): |
---|
| 186 | """ |
---|
| 187 | associates each model with its new created name |
---|
| 188 | :param model: model selected |
---|
| 189 | :param name: name created for model |
---|
| 190 | """ |
---|
| 191 | self.model = model |
---|
| 192 | if fid is None: |
---|
| 193 | for value in self.itervalues(): |
---|
| 194 | value.set_model(self.model) |
---|
| 195 | else: |
---|
| 196 | if fid in self.iterkeys(): |
---|
| 197 | self[fid].set_model(self.model) |
---|
| 198 | |
---|
| 199 | def get_model(self, fid): |
---|
| 200 | """ |
---|
| 201 | :return: saved model |
---|
| 202 | """ |
---|
| 203 | if fid in self.iterkeys(): |
---|
| 204 | self[fid].get_model() |
---|
| 205 | |
---|
| 206 | def set_fit_tab_caption(self, caption): |
---|
| 207 | """ |
---|
| 208 | store the caption of the page associated with object |
---|
| 209 | """ |
---|
| 210 | self.fit_tab_caption = caption |
---|
| 211 | |
---|
| 212 | def get_fit_tab_caption(self): |
---|
| 213 | """ |
---|
| 214 | Return the caption of the page associated with object |
---|
| 215 | """ |
---|
| 216 | return self.fit_tab_caption |
---|
| 217 | |
---|
| 218 | def set_theory_data(self, fid, data=None): |
---|
| 219 | """ |
---|
| 220 | save a copy of the data select to fit |
---|
| 221 | :param data: data selected |
---|
| 222 | """ |
---|
| 223 | if fid in self.iterkeys(): |
---|
| 224 | self[fid].set_theory_data(data) |
---|
| 225 | |
---|
| 226 | def get_theory_data(self, fid): |
---|
| 227 | """ |
---|
| 228 | :return: list of data dList |
---|
| 229 | """ |
---|
| 230 | if fid in self.iterkeys(): |
---|
| 231 | return self[fid].get_theory_data() |
---|
| 232 | |
---|
| 233 | def add_data(self, data): |
---|
| 234 | """ |
---|
| 235 | Add data to the current dictionary of fitproblem. if data id does not |
---|
| 236 | exist create a new fit problem. |
---|
| 237 | :note: only data changes in the fit problem |
---|
| 238 | """ |
---|
| 239 | if data.id not in self.iterkeys(): |
---|
| 240 | self[data.id] = FitProblem() |
---|
| 241 | self[data.id].set_fit_data(data) |
---|
| 242 | |
---|
| 243 | def set_fit_data(self, data): |
---|
| 244 | """ |
---|
| 245 | save a copy of the data select to fit |
---|
| 246 | :param data: data selected |
---|
| 247 | |
---|
| 248 | """ |
---|
| 249 | self.clear() |
---|
| 250 | if data is None: |
---|
| 251 | data = [] |
---|
| 252 | for d in data: |
---|
| 253 | if (d is not None): |
---|
| 254 | if (d.id not in self.iterkeys()): |
---|
| 255 | self[d.id] = FitProblem() |
---|
| 256 | self[d.id].set_fit_data(d) |
---|
| 257 | self[d.id].set_model(self.model) |
---|
| 258 | self[d.id].set_range(self.qmin, self.qmax) |
---|
| 259 | |
---|
| 260 | def get_fit_data(self, fid): |
---|
| 261 | """ |
---|
| 262 | |
---|
| 263 | return data for the given fitproblem id |
---|
| 264 | :param fid: is key representing a fitproblem. usually extract from data |
---|
| 265 | id |
---|
| 266 | """ |
---|
| 267 | if fid in self.iterkeys(): |
---|
| 268 | return self[fid].get_fit_data() |
---|
| 269 | |
---|
| 270 | def set_model_param(self, name, value=None, fid=None): |
---|
| 271 | """ |
---|
| 272 | Store the name and value of a parameter of this fitproblem's model |
---|
| 273 | :param name: name of the given parameter |
---|
| 274 | :param value: value of that parameter |
---|
| 275 | """ |
---|
| 276 | if fid is None: |
---|
| 277 | for value in self.itervalues(): |
---|
| 278 | value.set_model_param(name, value) |
---|
| 279 | else: |
---|
| 280 | if fid in self.iterkeys(): |
---|
| 281 | self[fid].set_model_param(name, value) |
---|
| 282 | |
---|
| 283 | def get_model_param(self, fid): |
---|
| 284 | """ |
---|
| 285 | return list of couple of parameter name and value |
---|
| 286 | """ |
---|
| 287 | if fid in self.iterkeys(): |
---|
| 288 | return self[fid].get_model_param() |
---|
| 289 | |
---|
| 290 | def schedule_tofit(self, schedule=0): |
---|
| 291 | """ |
---|
| 292 | set schedule to true to decide if this fit must be performed |
---|
| 293 | """ |
---|
| 294 | self.scheduled = schedule |
---|
| 295 | for value in self.itervalues(): |
---|
| 296 | value.schedule_tofit(schedule) |
---|
| 297 | |
---|
| 298 | def get_scheduled(self): |
---|
| 299 | """ |
---|
| 300 | return true or false if a problem as being schedule for fitting |
---|
| 301 | """ |
---|
| 302 | return self.scheduled |
---|
| 303 | |
---|
| 304 | def set_range(self, qmin=None, qmax=None, fid=None): |
---|
| 305 | """ |
---|
| 306 | set fitting range |
---|
| 307 | """ |
---|
| 308 | self.qmin = qmin |
---|
| 309 | self.qmax = qmax |
---|
| 310 | if fid is None: |
---|
| 311 | for value in self.itervalues(): |
---|
| 312 | value.set_range(self.qmin, self.qmax) |
---|
| 313 | else: |
---|
| 314 | if fid in self.iterkeys(): |
---|
| 315 | self[fid].value.set_range(self.qmin, self.qmax) |
---|
| 316 | |
---|
| 317 | def get_range(self, fid): |
---|
| 318 | """ |
---|
| 319 | :return: fitting range |
---|
| 320 | """ |
---|
| 321 | if fid in self.iterkeys(): |
---|
| 322 | return self[fid].get_range() |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | def clear_model_param(self, fid=None): |
---|
| 326 | """ |
---|
| 327 | clear constraint info |
---|
| 328 | """ |
---|
| 329 | if fid is None: |
---|
| 330 | for value in self.itervalues(): |
---|
| 331 | value.clear_model_param() |
---|
| 332 | else: |
---|
| 333 | if fid in self.iterkeys(): |
---|
| 334 | self[fid].clear_model_param() |
---|
| 335 | |
---|
| 336 | def get_fit_problem(self): |
---|
| 337 | """ |
---|
| 338 | return fitproblem contained in this dictionary |
---|
| 339 | """ |
---|
| 340 | return self.itervalues() |
---|
| 341 | |
---|
| 342 | def set_result(self, result): |
---|
| 343 | """ |
---|
| 344 | set a list of result |
---|
| 345 | """ |
---|
| 346 | self.result = result |
---|
| 347 | |
---|
| 348 | def get_result(self): |
---|
| 349 | """ |
---|
| 350 | get result |
---|
| 351 | """ |
---|
| 352 | return self.result |
---|
| 353 | |
---|
| 354 | |
---|
| 355 | class FitProblem(FitProblemComponent): |
---|
[d89f09b] | 356 | """ |
---|
[5062bbf] | 357 | FitProblem class allows to link a model with the new name created in _on_model, |
---|
| 358 | a name theory created with that model and the data fitted with the model. |
---|
| 359 | FitProblem is mostly used as value of the dictionary by fitting module. |
---|
[d89f09b] | 360 | """ |
---|
| 361 | def __init__(self): |
---|
[3e3ab46] | 362 | FitProblemComponent.__init__(self) |
---|
[d89f09b] | 363 | """ |
---|
[5062bbf] | 364 | contains information about data and model to fit |
---|
[d89f09b] | 365 | """ |
---|
[925a30e] | 366 | ## data used for fitting |
---|
[6bbeacd4] | 367 | self.fit_data = None |
---|
| 368 | self.theory_data = None |
---|
[2140e68] | 369 | ## the current model |
---|
| 370 | self.model = None |
---|
[925a30e] | 371 | ## if 1 this fit problem will be selected to fit , if 0 |
---|
| 372 | ## it will not be selected for fit |
---|
[6bbeacd4] | 373 | self.schedule = 0 |
---|
[925a30e] | 374 | ##list containing parameter name and value |
---|
[6bbeacd4] | 375 | self.list_param = [] |
---|
[925a30e] | 376 | ## smear object to smear or not data1D |
---|
[3e3ab46] | 377 | self.smearer_compute_count = 0 |
---|
| 378 | self.smearer_enable = False |
---|
| 379 | self.smearer_computer_value = None |
---|
[2140e68] | 380 | ## fitting range |
---|
| 381 | self.qmin = None |
---|
| 382 | self.qmax = None |
---|
[08b9c6c8] | 383 | |
---|
[ba1f0b2] | 384 | |
---|
[3e3ab46] | 385 | def enable_smearing(self, flag=False): |
---|
[9853ad0] | 386 | """ |
---|
[3e3ab46] | 387 | :param flag: bool.When flag is 1 get the computer smear value. When |
---|
| 388 | flag is 0 ingore smear value. |
---|
[9853ad0] | 389 | """ |
---|
[3e3ab46] | 390 | self.smearer_enable = flag |
---|
[9853ad0] | 391 | |
---|
[08b9c6c8] | 392 | def set_smearer(self, smearer): |
---|
[925a30e] | 393 | """ |
---|
[5062bbf] | 394 | save reference of smear object on fitdata |
---|
| 395 | |
---|
| 396 | :param smear: smear object from DataLoader |
---|
| 397 | |
---|
[925a30e] | 398 | """ |
---|
[3e3ab46] | 399 | self.smearer_computer_value = smearer |
---|
[925a30e] | 400 | |
---|
[08b9c6c8] | 401 | def get_smearer(self): |
---|
[925a30e] | 402 | """ |
---|
[5062bbf] | 403 | return smear object |
---|
[925a30e] | 404 | """ |
---|
[3e3ab46] | 405 | if not self.smearer_enable: |
---|
| 406 | return None |
---|
| 407 | if self.smearer_computer_value is None and \ |
---|
| 408 | self.smearer_compute_count > 1: |
---|
| 409 | #smeari_selection should be call only once per fitproblem |
---|
| 410 | self.smearer_computer_value = smear_selection(self.fit_data, |
---|
| 411 | self.model) |
---|
| 412 | self.smearer_compute_count += 1 |
---|
| 413 | return self.smearer_computer_value |
---|
[08b9c6c8] | 414 | |
---|
[5062bbf] | 415 | def save_model_name(self, name): |
---|
| 416 | """ |
---|
| 417 | """ |
---|
[bb18ef1] | 418 | self.name_per_page= name |
---|
| 419 | |
---|
| 420 | def get_name(self): |
---|
[5062bbf] | 421 | """ |
---|
| 422 | """ |
---|
[bb18ef1] | 423 | return self.name_per_page |
---|
[08b9c6c8] | 424 | |
---|
[2140e68] | 425 | def set_model(self,model): |
---|
[d89f09b] | 426 | """ |
---|
[5062bbf] | 427 | associates each model with its new created name |
---|
| 428 | :param model: model selected |
---|
| 429 | :param name: name created for model |
---|
[d89f09b] | 430 | """ |
---|
[2140e68] | 431 | self.model= model |
---|
[bb18ef1] | 432 | |
---|
[2140e68] | 433 | def get_model(self): |
---|
[5062bbf] | 434 | """ |
---|
| 435 | :return: saved model |
---|
| 436 | """ |
---|
[2140e68] | 437 | return self.model |
---|
| 438 | |
---|
[6bbeacd4] | 439 | def set_theory_data(self, data): |
---|
[d89f09b] | 440 | """ |
---|
[5062bbf] | 441 | save a copy of the data select to fit |
---|
| 442 | |
---|
| 443 | :param data: data selected |
---|
| 444 | |
---|
[d89f09b] | 445 | """ |
---|
[e88ebfd] | 446 | self.theory_data = copy.deepcopy(data) |
---|
[ba1f0b2] | 447 | |
---|
[3e3ab46] | 448 | |
---|
[ba1f0b2] | 449 | |
---|
[6bbeacd4] | 450 | def get_theory_data(self): |
---|
[5062bbf] | 451 | """ |
---|
[3e3ab46] | 452 | :return: theory generated with the current model and data of this class |
---|
[5062bbf] | 453 | """ |
---|
[6bbeacd4] | 454 | return self.theory_data |
---|
[5062bbf] | 455 | |
---|
[3e3ab46] | 456 | def set_fit_data(self, data): |
---|
[2a8fac1] | 457 | """ |
---|
[3e3ab46] | 458 | Store data associated with this class |
---|
| 459 | :param data: list of data selected |
---|
[2a8fac1] | 460 | """ |
---|
| 461 | self.fit_data = data |
---|
[d89f09b] | 462 | |
---|
[2a8fac1] | 463 | def get_fit_data(self): |
---|
[5062bbf] | 464 | """ |
---|
[3e3ab46] | 465 | :return: data associate with this class |
---|
[5062bbf] | 466 | """ |
---|
[2a8fac1] | 467 | return self.fit_data |
---|
| 468 | |
---|
[2140e68] | 469 | def set_model_param(self,name,value=None): |
---|
[d89f09b] | 470 | """ |
---|
[5062bbf] | 471 | Store the name and value of a parameter of this fitproblem's model |
---|
| 472 | :param name: name of the given parameter |
---|
| 473 | :param value: value of that parameter |
---|
[d89f09b] | 474 | """ |
---|
[8e81af0] | 475 | self.list_param.append([name,value]) |
---|
[925a30e] | 476 | |
---|
[00561739] | 477 | def get_model_param(self): |
---|
| 478 | """ |
---|
[5062bbf] | 479 | return list of couple of parameter name and value |
---|
[00561739] | 480 | """ |
---|
[8e81af0] | 481 | return self.list_param |
---|
[d89f09b] | 482 | |
---|
[948add7] | 483 | def schedule_tofit(self, schedule=0): |
---|
[3b19ac9] | 484 | """ |
---|
[5062bbf] | 485 | set schedule to true to decide if this fit must be performed |
---|
[3b19ac9] | 486 | """ |
---|
[3e3ab46] | 487 | self.schedule = schedule |
---|
[6bcdad1] | 488 | |
---|
[3b19ac9] | 489 | def get_scheduled(self): |
---|
[5062bbf] | 490 | """ |
---|
| 491 | return true or false if a problem as being schedule for fitting |
---|
| 492 | """ |
---|
[3b19ac9] | 493 | return self.schedule |
---|
[925a30e] | 494 | |
---|
[2140e68] | 495 | def set_range(self, qmin=None, qmax=None): |
---|
| 496 | """ |
---|
[3e3ab46] | 497 | set fitting range |
---|
| 498 | :param qmin: minimum value to consider for the fit range |
---|
| 499 | :param qmax: maximum value to consider for the fit range |
---|
[2140e68] | 500 | """ |
---|
| 501 | self.qmin = qmin |
---|
| 502 | self.qmax = qmax |
---|
| 503 | |
---|
| 504 | def get_range(self): |
---|
| 505 | """ |
---|
[5062bbf] | 506 | :return: fitting range |
---|
| 507 | |
---|
[2140e68] | 508 | """ |
---|
| 509 | return self.qmin, self.qmax |
---|
[925a30e] | 510 | |
---|
[9e27de9] | 511 | def clear_model_param(self): |
---|
| 512 | """ |
---|
| 513 | clear constraint info |
---|
| 514 | """ |
---|
[3e3ab46] | 515 | self.list_param = [] |
---|
[6bbeacd4] | 516 | |
---|
| 517 | def set_fit_tab_caption(self, caption): |
---|
| 518 | """ |
---|
| 519 | """ |
---|
| 520 | self.fit_tab_caption = str(caption) |
---|
| 521 | |
---|
| 522 | def get_fit_tab_caption(self): |
---|
| 523 | """ |
---|
| 524 | """ |
---|
| 525 | return self.fit_tab_caption |
---|
[ba1f0b2] | 526 | |
---|
[3e3ab46] | 527 | |
---|
[5062bbf] | 528 | |
---|