Changeset 69363c7 in sasview for src/sas/sascalc/pr
- Timestamp:
- Sep 22, 2017 10:29:48 AM (7 years ago)
- Branches:
- master, 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- ab0b93f
- Parents:
- 1386b2f (diff), d76c43a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - git-author:
- Paul Kienzle <pkienzle@…> (09/22/17 10:28:48)
- git-committer:
- Paul Kienzle <pkienzle@…> (09/22/17 10:29:48)
- Location:
- src/sas/sascalc/pr
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/pr/c_extensions/Cinvertor.c
r959eb01 rcb62bd5 294 294 } 295 295 296 const char set_ has_bck_doc[] =296 const char set_est_bck_doc[] = 297 297 "Sets background flag\n"; 298 298 … … 300 300 * Sets the maximum distance 301 301 */ 302 static PyObject * set_ has_bck(Cinvertor *self, PyObject *args) {303 int has_bck;304 305 if (!PyArg_ParseTuple(args, "i", & has_bck)) return NULL;306 self->params. has_bck = has_bck;307 return Py_BuildValue("i", self->params. has_bck);308 } 309 310 const char get_ has_bck_doc[] =302 static PyObject * set_est_bck(Cinvertor *self, PyObject *args) { 303 int est_bck; 304 305 if (!PyArg_ParseTuple(args, "i", &est_bck)) return NULL; 306 self->params.est_bck = est_bck; 307 return Py_BuildValue("i", self->params.est_bck); 308 } 309 310 const char get_est_bck_doc[] = 311 311 "Gets background flag\n"; 312 312 … … 314 314 * Gets the maximum distance 315 315 */ 316 static PyObject * get_ has_bck(Cinvertor *self, PyObject *args) {317 return Py_BuildValue("i", self->params. has_bck);316 static PyObject * get_est_bck(Cinvertor *self, PyObject *args) { 317 return Py_BuildValue("i", self->params.est_bck); 318 318 } 319 319 … … 882 882 sqrt_alpha = sqrt(self->params.alpha); 883 883 pi = acos(-1.0); 884 offset = (self->params. has_bck==1) ? 0 : 1;884 offset = (self->params.est_bck==1) ? 0 : 1; 885 885 886 886 for (j=0; j<nfunc; j++) { … … 892 892 } 893 893 if (accept_q(self, self->params.x[i])){ 894 if (self->params. has_bck==1 && j==0) {894 if (self->params.est_bck==1 && j==0) { 895 895 a[i*nfunc+j] = 1.0/self->params.err[i]; 896 896 } else { … … 906 906 } 907 907 for (i_r=0; i_r<nr; i_r++){ 908 if (self->params. has_bck==1 && j==0) {908 if (self->params.est_bck==1 && j==0) { 909 909 a[(i_r+self->params.npoints)*nfunc+j] = 0.0; 910 910 } else { … … 1029 1029 {"set_slit_height", (PyCFunction)set_slit_height, METH_VARARGS, set_slit_height_doc}, 1030 1030 {"get_slit_height", (PyCFunction)get_slit_height, METH_VARARGS, get_slit_height_doc}, 1031 {"set_ has_bck", (PyCFunction)set_has_bck, METH_VARARGS, set_has_bck_doc},1032 {"get_ has_bck", (PyCFunction)get_has_bck, METH_VARARGS, get_has_bck_doc},1031 {"set_est_bck", (PyCFunction)set_est_bck, METH_VARARGS, set_est_bck_doc}, 1032 {"get_est_bck", (PyCFunction)get_est_bck, METH_VARARGS, get_est_bck_doc}, 1033 1033 {"get_nx", (PyCFunction)get_nx, METH_VARARGS, get_nx_doc}, 1034 1034 {"get_ny", (PyCFunction)get_ny, METH_VARARGS, get_ny_doc}, -
src/sas/sascalc/pr/c_extensions/invertor.c
r959eb01 rcb62bd5 20 20 pars->q_min = -1.0; 21 21 pars->q_max = -1.0; 22 pars-> has_bck = 0;22 pars->est_bck = 0; 23 23 } 24 24 … … 313 313 return sqrt(sum_r2/(2.0*sum)); 314 314 } 315 -
src/sas/sascalc/pr/c_extensions/invertor.h
r959eb01 rcb62bd5 27 27 double q_max; 28 28 /// Flag for whether or not to evalute a constant background while inverting 29 int has_bck;29 int est_bck; 30 30 /// Slit height in units of q [A-1] 31 31 double slit_height; -
src/sas/sascalc/pr/invertor.py
r45dffa69 rcb62bd5 121 121 self.q_min, self.q_max, 122 122 self.x, self.y, 123 self.err, self. has_bck,123 self.err, self.est_bck, 124 124 self.slit_height, self.slit_width) = state 125 125 … … 133 133 self.q_min, self.q_max, 134 134 self.x, self.y, 135 self.err, self. has_bck,135 self.err, self.est_bck, 136 136 self.slit_height, self.slit_width, 137 137 ) … … 175 175 elif name == 'slit_width': 176 176 return self.set_slit_width(value) 177 elif name == ' has_bck':177 elif name == 'est_bck': 178 178 if value == True: 179 return self.set_ has_bck(1)179 return self.set_est_bck(1) 180 180 elif value == False: 181 return self.set_ has_bck(0)181 return self.set_est_bck(0) 182 182 else: 183 raise ValueError, "Invertor: has_bck can only be True or False"183 raise ValueError, "Invertor: est_bck can only be True or False" 184 184 185 185 return Cinvertor.__setattr__(self, name, value) … … 220 220 elif name == 'slit_width': 221 221 return self.get_slit_width() 222 elif name == ' has_bck':223 value = self.get_ has_bck()222 elif name == 'est_bck': 223 value = self.get_est_bck() 224 224 if value == 1: 225 225 return True … … 248 248 invertor.y = self.y 249 249 invertor.err = self.err 250 invertor.has_bck = self.has_bck 250 invertor.est_bck = self.est_bck 251 invertor.background = self.background 251 252 invertor.slit_height = self.slit_height 252 253 invertor.slit_width = self.slit_width … … 290 291 """ 291 292 # Reset the background value before proceeding 292 self.background = 0.0 293 return self.lstsq(nfunc, nr=nr) 293 # self.background = 0.0 294 if not self.est_bck: 295 self.y -= self.background 296 out, cov = self.lstsq(nfunc, nr=nr) 297 if not self.est_bck: 298 self.y += self.background 299 return out, cov 294 300 295 301 def iq(self, out, q): … … 454 460 455 461 # If we need to fit the background, add a term 456 if self. has_bck == True:462 if self.est_bck == True: 457 463 nfunc_0 = nfunc 458 464 nfunc += 1 … … 500 506 501 507 # Keep a copy of the last output 502 if self.has_bck == False: 503 self.background = 0 508 if self.est_bck == False: 504 509 self.out = c 505 510 self.cov = err … … 653 658 file.write("#slit_width=%g\n" % self.slit_width) 654 659 file.write("#background=%g\n" % self.background) 655 if self. has_bck == True:660 if self.est_bck == True: 656 661 file.write("#has_bck=1\n") 657 662 else: … … 734 739 toks = line.split('=') 735 740 if int(toks[1]) == 1: 736 self. has_bck = True741 self.est_bck = True 737 742 else: 738 self. has_bck = False743 self.est_bck = False 739 744 740 745 # Now read in the parameters -
src/sas/sascalc/pr/fit/AbstractFitEngine.py
ra1b8fee r50fcb09 137 137 that will smear the theory data (slit smearing or resolution 138 138 smearing) when set. 139 139 140 140 The proper way to set the smearing object would be to 141 141 do the following: :: 142 143 from sas.sascalc. data_util.qsmearing import smear_selection142 143 from sas.sascalc.fit.qsmearing import smear_selection 144 144 smearer = smear_selection(some_data) 145 145 fitdata1d = FitData1D( x= [1,3,..,], … … 147 147 dx=None, 148 148 dy=[1,2...], smearer= smearer) 149 149 150 150 :Note: that some_data _HAS_ to be of 151 151 class DataLoader.data_info.Data1D 152 152 Setting it back to None will turn smearing off. 153 153 154 154 """ 155 155 Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) … … 176 176 ## Max Q-value 177 177 self.qmax = max(self.x) 178 178 179 179 # Range used for input to smearing 180 180 self._qmin_unsmeared = self.qmin … … 184 184 self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ 185 185 & (self.x <= self._qmax_unsmeared) 186 186 187 187 def set_fit_range(self, qmin=None, qmax=None): 188 188 """ to set the fit range""" … … 199 199 self._qmin_unsmeared = self.qmin 200 200 self._qmax_unsmeared = self.qmax 201 201 202 202 self._first_unsmeared_bin = 0 203 203 self._last_unsmeared_bin = len(self.x) - 1 204 204 205 205 if self.smearer is not None: 206 206 self._first_unsmeared_bin, self._last_unsmeared_bin = \ … … 208 208 self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 209 209 self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 210 210 211 211 # Identify the bin range for the unsmeared and smeared spaces 212 212 self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) … … 231 231 """ 232 232 Compute residuals. 233 233 234 234 If self.smearer has been set, use if to smear 235 235 the data before computing chi squared. 236 236 237 237 :param fn: function that return model value 238 238 239 239 :return: residuals 240 240 """ … … 242 242 fx = np.zeros(len(self.x)) 243 243 fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) 244 244 245 245 ## Smear theory data 246 246 if self.smearer is not None: … … 253 253 raise RuntimeError, msg 254 254 return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 255 255 256 256 def residuals_deriv(self, model, pars=[]): 257 257 """ 258 258 :return: residuals derivatives . 259 260 :note: in this case just return empty array 259 260 :note: in this case just return empty array 261 261 """ 262 262 return [] … … 293 293 x_max = max(math.fabs(sas_data2d.xmin), math.fabs(sas_data2d.xmax)) 294 294 y_max = max(math.fabs(sas_data2d.ymin), math.fabs(sas_data2d.ymax)) 295 295 296 296 ## fitting range 297 297 if qmin is None: … … 305 305 self.res_err_data = copy.deepcopy(self.err_data) 306 306 #self.res_err_data[self.res_err_data==0]=1 307 307 308 308 self.radius = np.sqrt(self.qx_data**2 + self.qy_data**2) 309 309 310 310 # Note: mask = True: for MASK while mask = False for NOT to mask 311 311 self.idx = ((self.qmin <= self.radius) &\ … … 371 371 372 372 return res, gn 373 373 374 374 def residuals_deriv(self, model, pars=[]): 375 375 """ 376 376 :return: residuals derivatives . 377 377 378 378 :note: in this case just return empty array 379 379 380 380 """ 381 381 return [] 382 383 382 383 384 384 class FitAbort(Exception): 385 385 """ … … 399 399 self.fit_arrange_dict = {} 400 400 self.fitter_id = None 401 401 402 402 def set_model(self, model, id, pars=[], constraints=[], data=None): 403 403 """ 404 404 set a model on a given in the fit engine. 405 406 :param model: sas.models type 405 406 :param model: sas.models type 407 407 :param id: is the key of the fitArrange dictionary where model is saved as a value 408 :param pars: the list of parameters to fit 409 :param constraints: list of 408 :param pars: the list of parameters to fit 409 :param constraints: list of 410 410 tuple (name of parameter, value of parameters) 411 411 the value of parameter must be a string to constraint 2 different 412 412 parameters. 413 Example: 413 Example: 414 414 we want to fit 2 model M1 and M2 both have parameters A and B. 415 415 constraints can be ``constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]`` 416 417 416 417 418 418 :note: pars must contains only name of existing model's parameters 419 419 420 420 """ 421 421 if not pars: … … 448 448 in a FitArrange object and adds that object in a dictionary 449 449 with key id. 450 450 451 451 :param data: data added 452 452 :param id: unique key corresponding to a fitArrange object with data … … 459 459 dx=data.dx, dy=data.dy, smearer=smearer) 460 460 fitdata.sas_data = data 461 461 462 462 fitdata.set_fit_range(qmin=qmin, qmax=qmax) 463 463 #A fitArrange is already created but contains model only at id … … 469 469 fitproblem.add_data(fitdata) 470 470 self.fit_arrange_dict[id] = fitproblem 471 471 472 472 def get_model(self, id): 473 473 """ 474 474 :param id: id is key in the dictionary containing the model to return 475 475 476 476 :return: a model at this id or None if no FitArrange element was 477 477 created with this id … … 481 481 else: 482 482 return None 483 483 484 484 def remove_fit_problem(self, id): 485 485 """remove fitarrange in id""" 486 486 if id in self.fit_arrange_dict: 487 487 del self.fit_arrange_dict[id] 488 488 489 489 def select_problem_for_fit(self, id, value): 490 490 """ 491 491 select a couple of model and data at the id position in dictionary 492 492 and set in self.selected value to value 493 493 494 494 :param value: the value to allow fitting. 495 495 can only have the value one or zero … … 497 497 if id in self.fit_arrange_dict: 498 498 self.fit_arrange_dict[id].set_to_fit(value) 499 499 500 500 def get_problem_to_fit(self, id): 501 501 """ 502 502 return the self.selected value of the fit problem of id 503 503 504 504 :param id: the id of the problem 505 505 """ 506 506 if id in self.fit_arrange_dict: 507 507 self.fit_arrange_dict[id].get_to_fit() 508 509 508 509 510 510 class FitArrange: 511 511 def __init__(self): … … 514 514 to perform the Fit.FitArrange must contain exactly one model 515 515 and at least one data for the fit to be performed. 516 516 517 517 model: the model selected by the user 518 518 Ldata: a list of data what the user wants to fit 519 519 520 520 """ 521 521 self.model = None … … 528 528 """ 529 529 set_model save a copy of the model 530 530 531 531 :param model: the model being set 532 532 """ 533 533 self.model = model 534 534 535 535 def add_data(self, data): 536 536 """ 537 537 add_data fill a self.data_list with data to fit 538 538 539 539 :param data: Data to add in the list 540 540 """ 541 541 if not data in self.data_list: 542 542 self.data_list.append(data) 543 543 544 544 def get_model(self): 545 545 """ … … 547 547 """ 548 548 return self.model 549 549 550 550 def get_data(self): 551 551 """ … … 553 553 """ 554 554 return self.data_list[0] 555 555 556 556 def remove_data(self, data): 557 557 """ 558 558 Remove one element from the list 559 559 560 560 :param data: Data to remove from data_list 561 561 """ 562 562 if data in self.data_list: 563 563 self.data_list.remove(data) 564 564 565 565 def set_to_fit(self, value=0): 566 566 """ 567 567 set self.selected to 0 or 1 for other values raise an exception 568 568 569 569 :param value: integer between 0 or 1 570 570 """ 571 571 self.selected = value 572 572 573 573 def get_to_fit(self): 574 574 """ … … 602 602 if self.model is not None and self.data is not None: 603 603 self.inputs = [(self.model, self.data)] 604 604 605 605 def set_model(self, model): 606 606 """ 607 607 """ 608 608 self.model = model 609 609 610 610 def set_fitness(self, fitness): 611 611 """ 612 612 """ 613 613 self.fitness = fitness 614 614 615 615 def __str__(self): 616 616 """ … … 627 627 msg = [msg1, msg3] + msg2 628 628 return "\n".join(msg) 629 629 630 630 def print_summary(self): 631 631 """
Note: See TracChangeset
for help on using the changeset viewer.