Changeset 8b74155d in sasview
- Timestamp:
- Sep 8, 2014 8:26:08 AM (10 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, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- de126d3
- Parents:
- 5b1392f (diff), 1792311 (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. - Files:
-
- 3 added
- 24 edited
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
park-1.2.1/park/expression.py
r3570545 re3efa6b3 160 160 """%("\n ".join(exprs),"\n ".join(code)) 161 161 162 #print "Function:",function 162 #print "Function:",functiondef 163 163 exec functiondef in globals,locals 164 164 retfn = locals['eval_expressions'] … … 185 185 # Check symbol rename 186 186 assert substitute(expr,{'a.b.x':'Q'}) == 'Q + sin(4*pi*a.c) + Q/a.b' 187 assert substitute(expr,{'a.b':'Q'}) == 'a.b.x + sin(4*pi*a.c) + a.b.x/Q' 187 188 188 189 … … 249 250 for expr in ['G4.cage', 'M0.cage', 'M1.G1 + *2', 250 251 'piddle', 251 ' import sys; print "p0wned"',252 '5; import sys; print "p0wned"', 252 253 '__import__("sys").argv']: 253 254 try: -
park-1.2.1/park/fitresult.py
r3570545 r95d58d3 67 67 improvements. 68 68 """ 69 #import traceback; traceback.print_stack() 69 70 self.progress_time = time.time() 70 71 self.progress_percent = 0 … … 228 229 229 230 def __str__(self): 231 #import traceback; traceback.print_stack() 230 232 if self.parameters == None: return "No results" 231 233 L = ["P%-3d %s"%(n+1,p.summarize()) for n,p in enumerate(self.parameters)] -
park-1.2.1/park/parameter.py
r3570545 rfb7180c 15 15 __all__ = ['Parameter', 'ParameterSet'] 16 16 17 import math 17 18 import numpy 18 19 import expression … … 162 163 """ 163 164 range = ['.']*10 164 lo,hi = p.range165 portion = ( p.value-lo)/(hi-lo)165 lo,hi = self.range 166 portion = (self.value-lo)/(hi-lo) 166 167 if portion < 0: portion = 0. 167 168 elif portion >= 1: portion = 0.99999999 … … 169 170 range[bar] = '|' 170 171 range = "".join(range) 171 return "%25s %s %g in [%g,%g]" % ( p.name,range,p.value,lo,hi)172 return "%25s %s %g in [%g,%g]" % (self.name,range,self.value,lo,hi) 172 173 173 174 def isfitted(self): return self.status == 'fitted' … … 273 274 for p in self: 274 275 if parts[1] == p.name: 275 if len(par s) == 2:276 if len(parts) == 2: 276 277 return p 277 278 elif isinstance(p, ParameterSet): 278 279 return p._byname(parts[1:]) 280 else: 281 raise 279 282 return None 280 283 … … 283 286 parts = name.split('.') 284 287 if parts[0] == self.name: 285 p = _byname(self,name.split('.'))288 p = self._byname(name.split('.')) 286 289 if p: return p 287 290 raise KeyError("parameter %s not in parameter set"%name) -
run.py
rbbd97e5 rf8940db 8 8 Usage: 9 9 10 ./run.py [args] 10 ./run.py [(module|script) args...] 11 12 Without arguments run.py runs sasview. With arguments, run.py will run 13 the given module or script. 11 14 """ 12 15 … … 14 17 import sys 15 18 import imp 16 from glob import glob17 19 from contextlib import contextmanager 18 20 from os.path import abspath, dirname, join as joinpath … … 49 51 return mod 50 52 51 def import_dll(modname ):53 def import_dll(modname, build_path): 52 54 """Import a DLL from the build directory""" 55 import sysconfig 56 ext = sysconfig.get_config_var('SO') 53 57 # build_path comes from context 54 path = glob(joinpath(build_path, *modname.split('.'))+'.*')[0]58 path = joinpath(build_path, *modname.split('.'))+ext 55 59 #print "importing", modname, "from", path 56 60 return imp.load_dynamic(modname, path) … … 70 74 71 75 # Make sure that we have a private version of mplconfig 72 mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig')73 os.environ['MPLCONFIGDIR'] = mplconfig74 if not os.path.exists(mplconfig): os.mkdir(mplconfig)76 #mplconfig = joinpath(abspath(dirname(__file__)), '.mplconfig') 77 #os.environ['MPLCONFIGDIR'] = mplconfig 78 #if not os.path.exists(mplconfig): os.mkdir(mplconfig) 75 79 #import matplotlib 76 80 #matplotlib.use('Agg') … … 80 84 try: import periodictable 81 85 except: addpath(joinpath(root, '..','periodictable')) 86 87 try: import bumps 88 except: addpath(joinpath(root, '..','bumps')) 82 89 83 90 # select wx version … … 107 114 sans.pr.core = import_package('sans.pr.core', 108 115 joinpath(build_path, 'sans', 'pr', 'core')) 109 #import_dll('park._modeling' )116 #import_dll('park._modeling', build_path) 110 117 111 118 #park = import_package('park',os.path.join(build_path,'park')) … … 122 129 123 130 if __name__ == "__main__": 124 # start sasview125 #import multiprocessing126 #multiprocessing.freeze_support()127 131 prepare() 128 from sans.sansview.sansview import SasView129 SasView()132 from sans.sansview.sansview import run 133 run() -
sansview/sansview.py
re2271c5 r1790664 36 36 PLUGIN_MODEL_DIR = 'plugin_models' 37 37 APP_NAME = 'SasView' 38 def run(): 39 sys.path.append(os.path.join("..","..","..")) 40 from multiprocessing import freeze_support 41 freeze_support() 42 sasview = SasView() 43 38 44 39 class SasViewApp(gui_manager.ViewApp): 45 40 """ … … 63 58 # modules. We load the fitting module here 64 59 # to ensure a complete Windows executable build. 65 60 66 61 # Fitting perspective 67 62 try: … … 72 67 logging.error("Fitting problems: " + str(inst)) 73 68 logging.error("%s: could not find Fitting plug-in module"% APP_NAME) 74 logging.error(sys.exc_value) 75 69 logging.error(sys.exc_value) 70 76 71 # P(r) perspective 77 72 try: … … 113 108 self.gui.clean_plugin_models(PLUGIN_MODEL_DIR) 114 109 # Start the main loop 115 self.gui.MainLoop() 116 110 self.gui.MainLoop() 117 111 118 112 119 if __name__ == "__main__": 113 def run(): 120 114 from multiprocessing import freeze_support 121 115 freeze_support() 122 #Process(target=SasView).start() 123 sasview = SasView() 116 if len(sys.argv) > 1: 117 ## Run sasview as an interactive python interpreter 118 #if sys.argv[1] == "-i": 119 # sys.argv = ["ipython", "--pylab"] 120 # from IPython import start_ipython 121 # sys.exit(start_ipython()) 122 thing_to_run = sys.argv[1] 123 sys.argv = sys.argv[1:] 124 import runpy 125 if os.path.exists(thing_to_run): 126 runpy.run_path(thing_to_run, run_name="__main__") 127 else: 128 runpy.run_module(thing_to_run, run_name="__main__") 129 else: 130 SasView() 124 131 125 132 if __name__ == "__main__": 133 run() 134 -
sansview/setup_exe.py
r27b7acc r6fe5100 319 319 'reportlab.platypus', 320 320 ]) 321 packages.append('IPython') 321 322 includes = ['site'] 322 323 -
setup.py
r968aa6e r8b74155d 69 69 70 70 71 enable_openmp = True 71 enable_openmp = True 72 72 73 73 if sys.platform =='darwin': … … 347 347 # 'wxPython>=2.8.11', 'pil', 348 348 # 'periodictable>=1.3.0', 'scipy>=0.7.2'] 349 required = ['lxml','periodictable>=1.3.1','pyparsing<2.0.0'] 349 required = ['lxml', 'periodictable>=1.3.1', 'pyparsing<2.0.0' 350 'bumps', 'numdifftools'] 350 351 351 352 if os.name=='nt': -
src/sans/fit/AbstractFitEngine.py
r6c00702 rbf5e985 3 3 #import logging 4 4 import sys 5 import math 5 6 import numpy 6 import math 7 import park 7 8 8 from sans.dataloader.data_info import Data1D 9 9 from sans.dataloader.data_info import Data2D 10 _SMALLVALUE = 1.0e-10 11 12 class SansParameter(park.Parameter): 13 """ 14 SANS model parameters for use in the PARK fitting service. 15 The parameter attribute value is redirected to the underlying 16 parameter value in the SANS model. 17 """ 18 def __init__(self, name, model, data): 19 """ 20 :param name: the name of the model parameter 21 :param model: the sans model to wrap as a park model 22 """ 23 park.Parameter.__init__(self, name) 24 self._model, self._name = model, name 25 self.data = data 26 self.model = model 27 #set the value for the parameter of the given name 28 self.set(model.getParam(name)) 29 30 def _getvalue(self): 31 """ 32 override the _getvalue of park parameter 33 34 :return value the parameter associates with self.name 35 36 """ 37 return self._model.getParam(self.name) 38 39 def _setvalue(self, value): 40 """ 41 override the _setvalue pf park parameter 42 43 :param value: the value to set on a given parameter 44 45 """ 46 self._model.setParam(self.name, value) 47 48 value = property(_getvalue, _setvalue) 49 50 def _getrange(self): 51 """ 52 Override _getrange of park parameter 53 return the range of parameter 54 """ 55 #if not self.name in self._model.getDispParamList(): 56 lo, hi = self._model.details[self.name][1:3] 57 if lo is None: lo = -numpy.inf 58 if hi is None: hi = numpy.inf 59 if lo > hi: 60 raise ValueError, "wrong fit range for parameters" 61 62 return lo, hi 63 64 def get_name(self): 65 """ 66 """ 67 return self._getname() 68 69 def _setrange(self, r): 70 """ 71 override _setrange of park parameter 72 73 :param r: the value of the range to set 74 75 """ 76 self._model.details[self.name][1:3] = r 77 range = property(_getrange, _setrange) 78 79 80 class Model(park.Model): 81 """ 82 PARK wrapper for SANS models. 10 _SMALLVALUE = 1.0e-10 11 12 # Note: duplicated from park 13 class FitHandler(object): 14 """ 15 Abstract interface for fit thread handler. 16 17 The methods in this class are called by the optimizer as the fit 18 progresses. 19 20 Note that it is up to the optimizer to call the fit handler correctly, 21 reporting all status changes and maintaining the 'done' flag. 22 """ 23 done = False 24 """True when the fit job is complete""" 25 result = None 26 """The current best result of the fit""" 27 28 def improvement(self): 29 """ 30 Called when a result is observed which is better than previous 31 results from the fit. 32 33 result is a FitResult object, with parameters, #calls and fitness. 34 """ 35 def error(self, msg): 36 """ 37 Model had an error; print traceback 38 """ 39 def progress(self, current, expected): 40 """ 41 Called each cycle of the fit, reporting the current and the 42 expected amount of work. The meaning of these values is 43 optimizer dependent, but they can be converted into a percent 44 complete using (100*current)//expected. 45 46 Progress is updated each iteration of the fit, whatever that 47 means for the particular optimization algorithm. It is called 48 after any calls to improvement for the iteration so that the 49 update handler can control I/O bandwidth by suppressing 50 intermediate improvements until the fit is complete. 51 """ 52 def finalize(self): 53 """ 54 Fit is complete; best results are reported 55 """ 56 def abort(self): 57 """ 58 Fit was aborted. 59 """ 60 61 # TODO: not sure how these are used, but they are needed for running the fit 62 def update_fit(self, last=False): pass 63 def set_result(self, result=None): self.result = result 64 65 class Model: 66 """ 67 Fit wrapper for SANS models. 83 68 """ 84 69 def __init__(self, sans_model, sans_data=None, **kw): 85 70 """ 86 71 :param sans_model: the sans model to wrap using park interface 87 88 """ 89 park.Model.__init__(self, **kw) 72 73 """ 90 74 self.model = sans_model 91 75 self.name = sans_model.name 92 76 self.data = sans_data 93 #list of parameters names 94 self.sansp = sans_model.getParamList() 95 #list of park parameter 96 self.parkp = [SansParameter(p, sans_model, sans_data) for p in self.sansp] 97 #list of parameter set 98 self.parameterset = park.ParameterSet(sans_model.name, pars=self.parkp) 99 self.pars = [] 100 77 101 78 def get_params(self, fitparams): 102 79 """ 103 80 return a list of value of paramter to fit 104 81 105 82 :param fitparams: list of paramaters name to fit 106 107 """ 108 list_params = [] 109 self.pars = [] 110 self.pars = fitparams 111 for item in fitparams: 112 for element in self.parkp: 113 if element.name == str(item): 114 list_params.append(element.value) 115 return list_params 116 83 84 """ 85 return [self.model.getParam(k) for k in fitparams] 86 117 87 def set_params(self, paramlist, params): 118 88 """ 119 89 Set value for parameters to fit 120 90 121 91 :param params: list of value for parameters to fit 122 123 """ 124 try: 125 for i in range(len(self.parkp)): 126 for j in range(len(paramlist)): 127 if self.parkp[i].name == paramlist[j]: 128 self.parkp[i].value = params[j] 129 self.model.setParam(self.parkp[i].name, params[j]) 130 except: 131 raise 132 92 93 """ 94 for k,v in zip(paramlist, params): 95 self.model.setParam(k,v) 96 97 def set(self, **kw): 98 self.set_params(*zip(*kw.items())) 99 133 100 def eval(self, x): 134 101 """ 135 102 Override eval method of park model. 136 103 137 104 :param x: the x value used to compute a function 138 105 """ … … 141 108 except: 142 109 raise 143 110 144 111 def eval_derivs(self, x, pars=[]): 145 112 """ … … 154 121 instead of calling eval. 155 122 """ 156 return [] 157 158 123 raise NotImplementedError('no derivatives available') 124 125 def __call__(self, x): 126 return self.eval(x) 127 159 128 class FitData1D(Data1D): 160 129 """ … … 185 154 """ 186 155 Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy) 156 self.num_points = len(x) 187 157 self.sans_data = data 188 158 self.smearer = smearer … … 251 221 """ 252 222 return self.qmin, self.qmax 253 223 224 def size(self): 225 """ 226 Number of measurement points in data set after masking, etc. 227 """ 228 return len(self.x) 229 254 230 def residuals(self, fn): 255 231 """ … … 293 269 def __init__(self, sans_data2d, data=None, err_data=None): 294 270 Data2D.__init__(self, data=data, err_data=err_data) 295 """ 296 Data can be initital with a data (sans plottable) 297 or with vectors. 298 """ 271 # Data can be initialized with a sans plottable or with vectors. 299 272 self.res_err_image = [] 273 self.num_points = 0 # will be set by set_data 300 274 self.idx = [] 301 275 self.qmin = None … … 339 313 self.idx = (self.idx) & (self.mask) 340 314 self.idx = (self.idx) & (numpy.isfinite(self.data)) 315 self.num_points = numpy.sum(self.idx) 341 316 342 317 def set_smearer(self, smearer): … … 372 347 """ 373 348 return self.qmin, self.qmax 374 349 350 def size(self): 351 """ 352 Number of measurement points in data set after masking, etc. 353 """ 354 return numpy.sum(self.idx) 355 375 356 def residuals(self, fn): 376 357 """ … … 409 390 410 391 411 class SansAssembly: 412 """ 413 Sans Assembly class a class wrapper to be call in optimizer.leastsq method 414 """ 415 def __init__(self, paramlist, model=None, data=None, fitresult=None, 416 handler=None, curr_thread=None, msg_q=None): 417 """ 418 :param Model: the model wrapper fro sans -model 419 :param Data: the data wrapper for sans data 420 421 """ 422 self.model = model 423 self.data = data 424 self.paramlist = paramlist 425 self.msg_q = msg_q 426 self.curr_thread = curr_thread 427 self.handler = handler 428 self.fitresult = fitresult 429 self.res = [] 430 self.true_res = [] 431 self.func_name = "Functor" 432 self.theory = None 433 434 def chisq(self): 435 """ 436 Calculates chi^2 437 438 :param params: list of parameter values 439 440 :return: chi^2 441 442 """ 443 total = 0 444 for item in self.true_res: 445 total += item * item 446 if len(self.true_res) == 0: 447 return None 448 return total / len(self.true_res) 449 450 def __call__(self, params): 451 """ 452 Compute residuals 453 :param params: value of parameters to fit 454 """ 455 #import thread 456 self.model.set_params(self.paramlist, params) 457 #print "params", params 458 self.true_res, theory = self.data.residuals(self.model.eval) 459 self.theory = copy.deepcopy(theory) 460 # check parameters range 461 if self.check_param_range(): 462 # if the param value is outside of the bound 463 # just silent return res = inf 464 return self.res 465 self.res = self.true_res 466 467 if self.fitresult is not None: 468 self.fitresult.set_model(model=self.model) 469 self.fitresult.residuals = self.true_res 470 self.fitresult.iterations += 1 471 self.fitresult.theory = theory 472 473 #fitness = self.chisq(params=params) 474 fitness = self.chisq() 475 self.fitresult.pvec = params 476 self.fitresult.set_fitness(fitness=fitness) 477 if self.msg_q is not None: 478 self.msg_q.put(self.fitresult) 479 480 if self.handler is not None: 481 self.handler.set_result(result=self.fitresult) 482 self.handler.update_fit() 483 484 if self.curr_thread != None: 485 try: 486 self.curr_thread.isquit() 487 except: 488 #msg = "Fitting: Terminated... Note: Forcing to stop " 489 #msg += "fitting may cause a 'Functor error message' " 490 #msg += "being recorded in the log file....." 491 #self.handler.stop(msg) 492 raise 493 494 return self.res 495 496 def check_param_range(self): 497 """ 498 Check the lower and upper bound of the parameter value 499 and set res to the inf if the value is outside of the 500 range 501 :limitation: the initial values must be within range. 502 """ 503 504 #time.sleep(0.01) 505 is_outofbound = False 506 # loop through the fit parameters 507 for p in self.model.parameterset: 508 param_name = p.get_name() 509 if param_name in self.paramlist: 510 511 # if the range was defined, check the range 512 if numpy.isfinite(p.range[0]): 513 if p.value == 0: 514 # This value works on Scipy 515 # Do not change numbers below 516 value = _SMALLVALUE 517 else: 518 value = p.value 519 # For leastsq, it needs a bit step back from the boundary 520 val = p.range[0] - value * _SMALLVALUE 521 if p.value < val: 522 self.res *= 1e+6 523 524 is_outofbound = True 525 break 526 if numpy.isfinite(p.range[1]): 527 # This value works on Scipy 528 # Do not change numbers below 529 if p.value == 0: 530 value = _SMALLVALUE 531 else: 532 value = p.value 533 # For leastsq, it needs a bit step back from the boundary 534 val = p.range[1] + value * _SMALLVALUE 535 if p.value > val: 536 self.res *= 1e+6 537 is_outofbound = True 538 break 539 540 return is_outofbound 541 542 392 543 393 class FitEngine: 544 394 def __init__(self): … … 546 396 Base class for scipy and park fit engine 547 397 """ 548 #List of parameter names to fit549 self.param_list = []550 398 #Dictionnary of fitArrange element (fit problems) 551 399 self.fit_arrange_dict = {} … … 571 419 572 420 """ 573 if model == None: 574 raise ValueError, "AbstractFitEngine: Need to set model to fit" 575 576 new_model = model 421 if not pars: 422 raise ValueError("no fitting parameters") 423 424 if model is None: 425 raise ValueError("no model to fit") 426 577 427 if not issubclass(model.__class__, Model): 578 new_model = Model(model, data) 579 580 if len(constraints) > 0: 581 for constraint in constraints: 582 name, value = constraint 583 try: 584 new_model.parameterset[str(name)].set(str(value)) 585 except: 586 msg = "Fit Engine: Error occurs when setting the constraint" 587 msg += " %s for parameter %s " % (value, name) 588 raise ValueError, msg 589 590 if len(pars) > 0: 591 temp = [] 592 for item in pars: 593 if item in new_model.model.getParamList(): 594 temp.append(item) 595 self.param_list.append(item) 596 else: 597 598 msg = "wrong parameter %s used " % str(item) 599 msg += "to set model %s. Choose " % str(new_model.model.name) 600 msg += "parameter name within %s" % \ 601 str(new_model.model.getParamList()) 602 raise ValueError, msg 603 604 #A fitArrange is already created but contains data_list only at id 605 if self.fit_arrange_dict.has_key(id): 606 self.fit_arrange_dict[id].set_model(new_model) 607 self.fit_arrange_dict[id].pars = pars 608 else: 609 #no fitArrange object has been create with this id 610 fitproblem = FitArrange() 611 fitproblem.set_model(new_model) 612 fitproblem.pars = pars 613 self.fit_arrange_dict[id] = fitproblem 614 vals = [] 615 for name in pars: 616 vals.append(new_model.model.getParam(name)) 617 self.fit_arrange_dict[id].vals = vals 618 else: 619 raise ValueError, "park_integration:missing parameters" 620 428 model = Model(model, data) 429 430 sasmodel = model.model 431 available_parameters = sasmodel.getParamList() 432 for p in pars: 433 if p not in available_parameters: 434 raise ValueError("parameter %s not available in model %s; use one of [%s] instead" 435 %(p, sasmodel.name, ", ".join(available_parameters))) 436 437 if id not in self.fit_arrange_dict: 438 self.fit_arrange_dict[id] = FitArrange() 439 440 self.fit_arrange_dict[id].set_model(model) 441 self.fit_arrange_dict[id].pars = pars 442 self.fit_arrange_dict[id].vals = [sasmodel.getParam(name) for name in pars] 443 self.fit_arrange_dict[id].constraints = constraints 444 621 445 def set_data(self, data, id, smearer=None, qmin=None, qmax=None): 622 446 """ … … 700 524 self.vals = [] 701 525 self.selected = 0 702 526 703 527 def set_model(self, model): 704 528 """ … … 752 576 """ 753 577 return self.selected 754 755 756 IS_MAC = True757 if sys.platform.count("win32") > 0:758 IS_MAC = False759 760 578 761 579 class FResult(object): … … 765 583 def __init__(self, model=None, param_list=None, data=None): 766 584 self.calls = None 767 self.pars = []768 585 self.fitness = None 769 586 self.chisqr = None … … 776 593 self.residuals = [] 777 594 self.index = [] 778 self.parameters = None779 self.is_mac = IS_MAC780 595 self.model = model 781 596 self.data = data … … 803 618 if self.pvec == None and self.model is None and self.param_list is None: 804 619 return "No results" 805 n = len(self.model.parameterset) 806 807 result_param = zip(xrange(n), self.model.parameterset) 808 msg1 = ["[Iteration #: %s ]" % self.iterations] 809 msg3 = ["=== goodness of fit: %s ===" % (str(self.fitness))] 810 if not self.is_mac: 811 msg2 = ["P%-3d %s......|.....%s" % \ 812 (p[0], p[1], p[1].value)\ 813 for p in result_param if p[1].name in self.param_list] 814 msg = msg1 + msg3 + msg2 815 else: 816 msg = msg1 + msg3 817 msg = "\n".join(msg) 818 return msg 620 621 sasmodel = self.model.model 622 pars = enumerate(sasmodel.getParamList()) 623 msg1 = "[Iteration #: %s ]" % self.iterations 624 msg3 = "=== goodness of fit: %s ===" % (str(self.fitness)) 625 msg2 = ["P%-3d %s......|.....%s" % (i, v, sasmodel.getParam(v)) 626 for i,v in pars if v in self.param_list] 627 msg = [msg1, msg3] + msg2 628 return "\n".join(msg) 819 629 820 630 def print_summary(self): 821 631 """ 822 632 """ 823 print s elf633 print str(self) -
src/sans/fit/Fitting.py
r5777106 re3efa6b3 8 8 from sans.fit.ScipyFitting import ScipyFit 9 9 from sans.fit.ParkFitting import ParkFit 10 from sans.fit.BumpsFitting import BumpsFit 10 11 12 ENGINES={ 13 'scipy': ScipyFit, 14 'park': ParkFit, 15 'bumps': BumpsFit, 16 } 11 17 12 18 class Fit(object): … … 26 32 27 33 """ 28 def __init__(self, engine='scipy' ):34 def __init__(self, engine='scipy', *args, **kw): 29 35 """ 30 36 """ … … 32 38 self._engine = None 33 39 self.fitter_id = None 34 self.set_engine(engine )40 self.set_engine(engine, *args, **kw) 35 41 36 42 def __setattr__(self, name, value): … … 49 55 self.__dict__[name] = value 50 56 51 def set_engine(self, word ):57 def set_engine(self, word, *args, **kw): 52 58 """ 53 59 Select the type of Fit … … 59 65 60 66 """ 61 if word == "scipy": 62 self._engine = ScipyFit() 63 elif word == "park": 64 self._engine = ParkFit() 65 else: 66 raise ValueError, "enter the keyword scipy or park" 67 try: 68 self._engine = ENGINES[word](*args, **kw) 69 except KeyError, exc: 70 raise KeyError("fit engine should be one of scipy, park or bumps") 67 71 68 72 def fit(self, msg_q=None, q=None, handler=None, -
src/sans/fit/Loader.py
r5777106 r6fe5100 8 8 This class is loading values from given file or value giving by the user 9 9 """ 10 11 10 def __init__(self, x=None, y=None, dx=None, dy=None): 11 raise NotImplementedError("a code search shows that this code is not active, and you are not seeing this message") 12 12 # variable to store loaded values 13 13 self.x = x -
src/sans/fit/ParkFitting.py
r9d6d5ba r8d074d9 24 24 from sans.fit.AbstractFitEngine import FitEngine 25 25 from sans.fit.AbstractFitEngine import FResult 26 26 27 class SansParameter(park.Parameter): 28 """ 29 SANS model parameters for use in the PARK fitting service. 30 The parameter attribute value is redirected to the underlying 31 parameter value in the SANS model. 32 """ 33 def __init__(self, name, model, data): 34 """ 35 :param name: the name of the model parameter 36 :param model: the sans model to wrap as a park model 37 """ 38 park.Parameter.__init__(self, name) 39 #self._model, self._name = model, name 40 self.data = data 41 self.model = model 42 #set the value for the parameter of the given name 43 self.set(model.getParam(name)) 44 45 # TODO: model is missing parameter ranges for dispersion parameters 46 if name not in model.details: 47 #print "setting details for",name 48 model.details[name] = ["", None, None] 49 50 def _getvalue(self): 51 """ 52 override the _getvalue of park parameter 53 54 :return value the parameter associates with self.name 55 56 """ 57 return self.model.getParam(self.name) 58 59 def _setvalue(self, value): 60 """ 61 override the _setvalue pf park parameter 62 63 :param value: the value to set on a given parameter 64 65 """ 66 self.model.setParam(self.name, value) 67 68 value = property(_getvalue, _setvalue) 69 70 def _getrange(self): 71 """ 72 Override _getrange of park parameter 73 return the range of parameter 74 """ 75 #if not self.name in self._model.getDispParamList(): 76 lo, hi = self.model.details[self.name][1:3] 77 if lo is None: lo = -numpy.inf 78 if hi is None: hi = numpy.inf 79 if lo > hi: 80 raise ValueError, "wrong fit range for parameters" 81 82 return lo, hi 83 84 def get_name(self): 85 """ 86 """ 87 return self._getname() 88 89 def _setrange(self, r): 90 """ 91 override _setrange of park parameter 92 93 :param r: the value of the range to set 94 95 """ 96 self.model.details[self.name][1:3] = r 97 range = property(_getrange, _setrange) 98 99 100 class ParkModel(park.Model): 101 """ 102 PARK wrapper for SANS models. 103 """ 104 def __init__(self, sans_model, sans_data=None, **kw): 105 """ 106 :param sans_model: the sans model to wrap using park interface 107 108 """ 109 park.Model.__init__(self, **kw) 110 self.model = sans_model 111 self.name = sans_model.name 112 self.data = sans_data 113 #list of parameters names 114 self.sansp = sans_model.getParamList() 115 #list of park parameter 116 self.parkp = [SansParameter(p, sans_model, sans_data) for p in self.sansp] 117 #list of parameter set 118 self.parameterset = park.ParameterSet(sans_model.name, pars=self.parkp) 119 self.pars = [] 120 121 def get_params(self, fitparams): 122 """ 123 return a list of value of paramter to fit 124 125 :param fitparams: list of paramaters name to fit 126 127 """ 128 list_params = [] 129 self.pars = fitparams 130 for item in fitparams: 131 for element in self.parkp: 132 if element.name == str(item): 133 list_params.append(element.value) 134 return list_params 135 136 def set_params(self, paramlist, params): 137 """ 138 Set value for parameters to fit 139 140 :param params: list of value for parameters to fit 141 142 """ 143 try: 144 for i in range(len(self.parkp)): 145 for j in range(len(paramlist)): 146 if self.parkp[i].name == paramlist[j]: 147 self.parkp[i].value = params[j] 148 self.model.setParam(self.parkp[i].name, params[j]) 149 except: 150 raise 151 152 def eval(self, x): 153 """ 154 Override eval method of park model. 155 156 :param x: the x value used to compute a function 157 """ 158 try: 159 return self.model.evalDistribution(x) 160 except: 161 raise 162 163 def eval_derivs(self, x, pars=[]): 164 """ 165 Evaluate the model and derivatives wrt pars at x. 166 167 pars is a list of the names of the parameters for which derivatives 168 are desired. 169 170 This method needs to be specialized in the model to evaluate the 171 model function. Alternatively, the model can implement is own 172 version of residuals which calculates the residuals directly 173 instead of calling eval. 174 """ 175 return [] 176 177 27 178 class SansFitResult(fitresult.FitResult): 28 179 def __init__(self, *args, **kwrds): … … 244 395 return fitpars 245 396 246 def all_results(self, result):397 def extend_results_with_calculated_parameters(self, result): 247 398 """ 248 399 Extend result from the fit with the calculated parameters. … … 292 443 # dividing residuals by N in order to be consistent with Scipy 293 444 m.chisq = numpy.sum(m.residuals**2/N) 294 resid.append(m.weight*m.residuals /math.sqrt(N))445 resid.append(m.weight*m.residuals) 295 446 self.residuals = numpy.hstack(resid) 296 447 N = len(self.residuals) 297 448 self.degrees_of_freedom = N-k if N>k else 1 298 449 self.chisq = numpy.sum(self.residuals**2) 299 return self.chisq 450 return self.chisq/self.degrees_of_freedom 300 451 301 452 class ParkFit(FitEngine): … … 354 505 if fproblem.get_to_fit() == 1: 355 506 fitproblems.append(fproblem) 356 if len(fitproblems) == 0: 507 if len(fitproblems) == 0: 357 508 raise RuntimeError, "No Assembly scheduled for Park fitting." 358 return359 509 for item in fitproblems: 360 parkmodel = item.get_model() 510 model = item.get_model() 511 parkmodel = ParkModel(model.model, model.data) 512 parkmodel.pars = item.pars 361 513 if reset_flag: 362 514 # reset the initial value; useful for batch … … 364 516 ind = item.pars.index(name) 365 517 parkmodel.model.setParam(name, item.vals[ind]) 518 519 # set the constraints into the model 520 for p,v in item.constraints: 521 parkmodel.parameterset[str(p)].set(str(v)) 366 522 367 523 for p in parkmodel.parameterset: 368 524 ## does not allow status change for constraint parameters 369 525 if p.status != 'computed': 370 if p.get_name() in item.pars:526 if p.get_name() in item.pars: 371 527 ## make parameters selected for 372 528 #fit will be between boundaries … … 383 539 def fit(self, msg_q=None, 384 540 q=None, handler=None, curr_thread=None, 385 541 ftol=1.49012e-8, reset_flag=False): 386 542 """ 387 543 Performs fit with park.fit module.It can perform fit with one model … … 407 563 localfit = SansFitSimplex() 408 564 localfit.ftol = ftol 409 565 localfit.xtol = 1e-6 566 410 567 # See `park.fitresult.FitHandler` for details. 411 568 fitter = SansFitMC(localfit=localfit, start_points=1) … … 416 573 try: 417 574 result = fit.fit(self.problem, fitter=fitter, handler=handler) 418 self.problem. all_results(result)575 self.problem.extend_results_with_calculated_parameters(result) 419 576 420 577 except LinAlgError: 421 578 raise ValueError, "SVD did not converge" 579 580 if result is None: 581 raise RuntimeError("park did not return a fit result") 422 582 423 583 for m in self.problem.parts: … … 427 587 small_result.theory = theory 428 588 small_result.residuals = residuals 429 small_result.pvec = [] 430 small_result.cov = [] 431 small_result.stderr = [] 432 small_result.param_list = [] 433 small_result.residuals = m.residuals 434 if result is not None: 435 for p in result.parameters: 436 if p.data.name == small_result.data.name and \ 437 p.model.name == small_result.model.name: 438 small_result.index = m.data.idx 439 small_result.fitness = result.fitness 440 small_result.pvec.append(p.value) 441 small_result.stderr.append(p.stderr) 442 name_split = p.name.split('.') 443 name = name_split[1].strip() 444 if len(name_split) > 2: 445 name += '.' + name_split[2].strip() 446 small_result.param_list.append(name) 589 small_result.index = m.data.idx 590 small_result.fitness = result.fitness 591 592 # Extract the parameters that are part of this model; make sure 593 # they match the fitted parameters for this model, and place them 594 # in the same order as they occur in the model. 595 pars = {} 596 for p in result.parameters: 597 #if p.data.name == small_result.data.name and 598 if p.model.name == small_result.model.name: 599 model_name, par_name = p.name.split('.', 1) 600 pars[par_name] = (p.value, p.stderr) 601 #assert len(pars.keys()) == len(m.model.pars) 602 v,dv = zip(*[pars[p] for p in m.model.pars]) 603 small_result.pvec = v 604 small_result.stderr = dv 605 small_result.param_list = m.model.pars 606 607 # normalize chisq by degrees of freedom 608 dof = len(small_result.residuals)-len(small_result.pvec) 609 small_result.fitness = numpy.sum(residuals**2)/dof 610 447 611 result_list.append(small_result) 448 612 if q != None: -
src/sans/fit/ScipyFitting.py
r5777106 r1792311 1 2 3 1 """ 4 2 ScipyFitting module contains FitArrange , ScipyFit, … … 6 4 simple fit with scipy optimizer. 7 5 """ 6 import sys 7 import copy 8 8 9 9 import numpy 10 import sys11 12 10 13 11 from sans.fit.AbstractFitEngine import FitEngine 14 from sans.fit.AbstractFitEngine import SansAssembly 15 from sans.fit.AbstractFitEngine import FitAbort 16 from sans.fit.AbstractFitEngine import Model 17 from sans.fit.AbstractFitEngine import FResult 12 from sans.fit.AbstractFitEngine import FResult 13 14 _SMALLVALUE = 1.0e-10 15 16 class SansAssembly: 17 """ 18 Sans Assembly class a class wrapper to be call in optimizer.leastsq method 19 """ 20 def __init__(self, paramlist, model=None, data=None, fitresult=None, 21 handler=None, curr_thread=None, msg_q=None): 22 """ 23 :param Model: the model wrapper fro sans -model 24 :param Data: the data wrapper for sans data 25 26 """ 27 self.model = model 28 self.data = data 29 self.paramlist = paramlist 30 self.msg_q = msg_q 31 self.curr_thread = curr_thread 32 self.handler = handler 33 self.fitresult = fitresult 34 self.res = [] 35 self.true_res = [] 36 self.func_name = "Functor" 37 self.theory = None 38 39 def chisq(self): 40 """ 41 Calculates chi^2 42 43 :param params: list of parameter values 44 45 :return: chi^2 46 47 """ 48 total = 0 49 for item in self.true_res: 50 total += item * item 51 if len(self.true_res) == 0: 52 return None 53 return total / (len(self.true_res) - len(self.paramlist)) 54 55 def __call__(self, params): 56 """ 57 Compute residuals 58 :param params: value of parameters to fit 59 """ 60 #import thread 61 self.model.set_params(self.paramlist, params) 62 #print "params", params 63 self.true_res, theory = self.data.residuals(self.model.eval) 64 self.theory = copy.deepcopy(theory) 65 # check parameters range 66 if self.check_param_range(): 67 # if the param value is outside of the bound 68 # just silent return res = inf 69 return self.res 70 self.res = self.true_res 71 72 if self.fitresult is not None: 73 self.fitresult.set_model(model=self.model) 74 self.fitresult.residuals = self.true_res 75 self.fitresult.iterations += 1 76 self.fitresult.theory = theory 77 78 #fitness = self.chisq(params=params) 79 fitness = self.chisq() 80 self.fitresult.pvec = params 81 self.fitresult.set_fitness(fitness=fitness) 82 if self.msg_q is not None: 83 self.msg_q.put(self.fitresult) 84 85 if self.handler is not None: 86 self.handler.set_result(result=self.fitresult) 87 self.handler.update_fit() 88 89 if self.curr_thread != None: 90 try: 91 self.curr_thread.isquit() 92 except: 93 #msg = "Fitting: Terminated... Note: Forcing to stop " 94 #msg += "fitting may cause a 'Functor error message' " 95 #msg += "being recorded in the log file....." 96 #self.handler.stop(msg) 97 raise 98 99 return self.res 100 101 def check_param_range(self): 102 """ 103 Check the lower and upper bound of the parameter value 104 and set res to the inf if the value is outside of the 105 range 106 :limitation: the initial values must be within range. 107 """ 108 109 #time.sleep(0.01) 110 is_outofbound = False 111 # loop through the fit parameters 112 model = self.model.model 113 for p in self.paramlist: 114 value = model.getParam(p) 115 low,high = model.details[p][1:3] 116 if low is not None and numpy.isfinite(low): 117 if p.value == 0: 118 # This value works on Scipy 119 # Do not change numbers below 120 value = _SMALLVALUE 121 # For leastsq, it needs a bit step back from the boundary 122 val = low - value * _SMALLVALUE 123 if value < val: 124 self.res *= 1e+6 125 is_outofbound = True 126 break 127 if high is not None and numpy.isfinite(high): 128 # This value works on Scipy 129 # Do not change numbers below 130 if value == 0: 131 value = _SMALLVALUE 132 # For leastsq, it needs a bit step back from the boundary 133 val = high + value * _SMALLVALUE 134 if value > val: 135 self.res *= 1e+6 136 is_outofbound = True 137 break 138 139 return is_outofbound 18 140 19 141 class ScipyFit(FitEngine): … … 50 172 """ 51 173 FitEngine.__init__(self) 52 self.fit_arrange_dict = {}53 self.param_list = []54 174 self.curr_thread = None 55 175 #def fit(self, *args, **kw): … … 68 188 msg = "Scipy can't fit more than a single fit problem at a time." 69 189 raise RuntimeError, msg 70 return 71 elif len(fitproblem) == 0 : 190 elif len(fitproblem) == 0 : 72 191 raise RuntimeError, "No Assembly scheduled for Scipy fitting." 73 return74 192 model = fitproblem[0].get_model() 193 pars = fitproblem[0].pars 75 194 if reset_flag: 76 195 # reset the initial value; useful for batch … … 87 206 88 207 # Check the initial value if it is within range 89 self._check_param_range(model)208 _check_param_range(model.model, pars) 90 209 91 result = FResult(model=model, data=data, param_list=self.param_list) 92 result.pars = fitproblem[0].pars 210 result = FResult(model=model.model, data=data, param_list=pars) 93 211 result.fitter_id = self.fitter_id 94 212 if handler is not None: 95 213 handler.set_result(result=result) 214 functor = SansAssembly(paramlist=pars, 215 model=model, 216 data=data, 217 handler=handler, 218 fitresult=result, 219 curr_thread=curr_thread, 220 msg_q=msg_q) 96 221 try: 97 222 # This import must be here; otherwise it will be confused when more … … 99 224 from scipy import optimize 100 225 101 functor = SansAssembly(paramlist=self.param_list,102 model=model,103 data=data,104 handler=handler,105 fitresult=result,106 curr_thread=curr_thread,107 msg_q=msg_q)108 226 out, cov_x, _, mesg, success = optimize.leastsq(functor, 109 model.get_params( self.param_list),110 111 227 model.get_params(pars), 228 ftol=ftol, 229 full_output=1) 112 230 except: 113 231 if hasattr(sys, 'last_type') and sys.last_type == KeyboardInterrupt: … … 142 260 143 261 144 def _check_param_range(self, model): 145 """ 146 Check parameter range and set the initial value inside 147 if it is out of range. 148 149 : model: park model object 150 """ 151 is_outofbound = False 152 # loop through parameterset 153 for p in model.parameterset: 154 param_name = p.get_name() 155 # proceed only if the parameter name is in the list of fitting 156 if param_name in self.param_list: 157 # if the range was defined, check the range 158 if numpy.isfinite(p.range[0]): 159 if p.value <= p.range[0]: 160 # 10 % backing up from the border if not zero 161 # for Scipy engine to work properly. 162 shift = self._get_zero_shift(p.range[0]) 163 new_value = p.range[0] + shift 164 p.value = new_value 165 is_outofbound = True 166 if numpy.isfinite(p.range[1]): 167 if p.value >= p.range[1]: 168 shift = self._get_zero_shift(p.range[1]) 169 # 10 % backing up from the border if not zero 170 # for Scipy engine to work properly. 171 new_value = p.range[1] - shift 172 # Check one more time if the new value goes below 173 # the low bound, If so, re-evaluate the value 174 # with the mean of the range. 175 if numpy.isfinite(p.range[0]): 176 if new_value < p.range[0]: 177 new_value = (p.range[0] + p.range[1]) / 2.0 178 # Todo: 179 # Need to think about when both min and max are same. 180 p.value = new_value 181 is_outofbound = True 182 183 return is_outofbound 184 185 def _get_zero_shift(self, range): 186 """ 187 Get 10% shift of the param value = 0 based on the range value 188 189 : param range: min or max value of the bounds 190 """ 191 if range == 0: 192 shift = 0.1 193 else: 194 shift = 0.1 * range 195 196 return shift 197 262 def _check_param_range(model, pars): 263 """ 264 Check parameter range and set the initial value inside 265 if it is out of range. 266 267 : model: park model object 268 """ 269 # loop through parameterset 270 for p in pars: 271 value = model.getParam(p) 272 low,high = model.details.setdefault(p,["",None,None])[1:3] 273 # if the range was defined, check the range 274 if low is not None and value <= low: 275 value = low + _get_zero_shift(low) 276 if high is not None and value > high: 277 value = high - _get_zero_shift(high) 278 # Check one more time if the new value goes below 279 # the low bound, If so, re-evaluate the value 280 # with the mean of the range. 281 if low is not None and value < low: 282 value = 0.5 * (low+high) 283 model.setParam(p, value) 284 285 def _get_zero_shift(limit): 286 """ 287 Get 10% shift of the param value = 0 based on the range value 288 289 : param range: min or max value of the bounds 290 """ 291 return 0.1 * (limit if limit != 0.0 else 1.0) 292 198 293 199 294 #def profile(fn, *args, **kw): -
src/sans/fit/__init__.py
r5777106 r6fe5100 1 from .AbstractFitEngine import FitHandler -
src/sans/guiframe/gui_statusbar.py
r8001331 r0e29289 211 211 """ 212 212 """ 213 wxStatusB.SetStatusText(self, text , number)213 wxStatusB.SetStatusText(self, text.split('\n',1)[0], number) 214 214 self.list_msg.append(text) 215 215 self.status_color.SetBackgroundColour(GREEN) -
src/sans/perspectives/fitting/basepage.py
r116e1a7 rbf5e985 96 96 self.num_points = None 97 97 ## default fitengine type 98 self.engine_type = ' scipy'98 self.engine_type = 'bumps' 99 99 ## smear default 100 100 self.current_smearer = None … … 835 835 infor = "warning" 836 836 else: 837 msg = "Error was occured"838 msg += " :No valid parameter values to paste from the clipboard..."837 msg = "Error occured: " 838 msg += "No valid parameter values to paste from the clipboard..." 839 839 infor = "error" 840 840 wx.PostEvent(self._manager.parent, … … 2183 2183 else: 2184 2184 tcrtl.SetBackgroundColour("pink") 2185 msg = "Model Error: wrong value entered: %s" % sys.exc_value2185 msg = "Model Error: wrong value entered: %s" % sys.exc_value 2186 2186 wx.PostEvent(self.parent, StatusEvent(status=msg)) 2187 2187 return 2188 2188 except: 2189 2189 tcrtl.SetBackgroundColour("pink") 2190 msg = "Model Error: wrong value entered: %s" % sys.exc_value2190 msg = "Model Error: wrong value entered: %s" % sys.exc_value 2191 2191 wx.PostEvent(self.parent, StatusEvent(status=msg)) 2192 2192 return … … 2199 2199 #is_modified = True 2200 2200 else: 2201 msg = "Cannot Plot :No npts in that Qrange!!! "2201 msg = "Cannot plot: No points in Q range!!! " 2202 2202 wx.PostEvent(self.parent, StatusEvent(status=msg)) 2203 2203 else: 2204 2204 tcrtl.SetBackgroundColour("pink") 2205 msg = "Model Error: wrong value entered!!!"2205 msg = "Model Error: wrong value entered!!!" 2206 2206 wx.PostEvent(self.parent, StatusEvent(status=msg)) 2207 2207 self.save_current_state() … … 2240 2240 else: 2241 2241 tcrtl.SetBackgroundColour("pink") 2242 msg = "Model Error: wrong value entered: %s" % sys.exc_value2242 msg = "Model Error: wrong value entered: %s" % sys.exc_value 2243 2243 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2244 2244 return 2245 2245 except: 2246 2246 tcrtl.SetBackgroundColour("pink") 2247 msg = "Model Error: wrong value entered: %s" % sys.exc_value2247 msg = "Model Error: wrong value entered: %s" % sys.exc_value 2248 2248 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2249 2249 return … … 2256 2256 is_modified = True 2257 2257 else: 2258 msg = "Cannot Plot :No npts in that Qrange!!! "2258 msg = "Cannot Plot: No points in Q range!!! " 2259 2259 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2260 2260 else: 2261 2261 tcrtl.SetBackgroundColour("pink") 2262 msg = "Model Error: wrong value entered!!!"2262 msg = "Model Error: wrong value entered!!!" 2263 2263 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2264 2264 self.save_current_state() … … 2431 2431 self.qmax.SetBackgroundColour("pink") 2432 2432 self.qmax.Refresh() 2433 msg = " Npts of Data Error :"2434 msg += " No or too little npts of%s." % data.name2433 msg = "Data Error: " 2434 msg += "Too few points in %s." % data.name 2435 2435 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2436 2436 self.fitrange = False … … 2466 2466 self.qmax.SetBackgroundColour("pink") 2467 2467 self.qmax.Refresh() 2468 msg = " Npts of Data Error :"2469 msg += " No or too little npts of%s." % data.name2468 msg = "Data Error: " 2469 msg += "Too few points in %s." % data.name 2470 2470 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) 2471 2471 self.fitrange = False … … 2518 2518 2519 2519 except: 2520 msg = "Wrong Fit parameter range entered"2520 msg = "Wrong fit parameter range entered" 2521 2521 wx.PostEvent(self._manager.parent, 2522 2522 StatusEvent(status=msg)) … … 2986 2986 self.qmin.SetValue(str(self.qmin_x)) 2987 2987 self.qmax.SetValue(str(self.qmax_x)) 2988 self.s et_npts2fit()2988 self.show_npts2fit() 2989 2989 # At this point, some button and variables satatus (disabled?) 2990 2990 # should be checked such as color that should be reset to … … 3874 3874 to fit if implemented 3875 3875 """ 3876 def s et_npts2fit(self):3876 def show_npts2fit(self): 3877 3877 """ 3878 3878 setValue Npts for fitting if implemented -
src/sans/perspectives/fitting/console.py
r5777106 r35086c3 5 5 import time 6 6 import wx 7 import park 8 from park.fitresult import FitHandler 7 from sans.fit import FitHandler 9 8 10 9 class ConsoleUpdate(FitHandler): … … 88 87 Print result object 89 88 """ 90 msg = " \n %s \n" % s elf.result.__str__()89 msg = " \n %s \n" % str(self.result) 91 90 wx.PostEvent(self.parent, StatusEvent(status=msg)) 92 91 … … 129 128 130 129 131 def update_fit(self, msg="",last=False):130 def update_fit(self, last=False): 132 131 """ 133 132 """ … … 136 135 self.update_duration = t1 137 136 self.fit_duration += self.elapsed_time 138 str_time = time.strftime("% a, %d %b %Y%H:%M:%S ", time.localtime(t1))139 UPDATE_INTERVAL = 0.5137 str_time = time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime(t1)) 138 UPDATE_INTERVAL = 5.0 140 139 u_flag = False 141 140 if self.fit_duration >= UPDATE_INTERVAL: 142 141 self.fit_duration = 0 143 142 u_flag = True 144 if not last: 145 msg += "Fit Updates ... %s \n" % str_time 146 else: 147 msg += "Final updates ........." 143 msg = str_time 148 144 if u_flag or last: 149 145 if self.result is not None: … … 164 160 msg += str(self.result) 165 161 msg += "\n" 166 if not last:167 msg += "About %s s elapsed......... \n" % \168 str (UPDATE_INTERVAL)169 162 else: 170 163 msg += "No result available\n" -
src/sans/perspectives/fitting/fit_thread.py
ra855fec re3efa6b3 18 18 19 19 def __init__(self, 20 21 22 23 24 batch_inputs=None,25 20 fn, 21 page_id, 22 handler, 23 batch_outputs, 24 batch_inputs=None, 25 pars=None, 26 26 completefn = None, 27 27 updatefn = None, … … 30 30 ftol = None, 31 31 reset_flag = False): 32 CalcThread.__init__(self,completefn, 32 CalcThread.__init__(self, 33 completefn, 33 34 updatefn, 34 35 yieldtime, … … 80 81 list_map_get_attr.append(map_getattr) 81 82 #from multiprocessing import Pool 82 inputs = zip(list_map_get_attr, self.fitter, list_fit_function,83 83 inputs = zip(list_map_get_attr, self.fitter, list_fit_function, 84 list_q, list_q, list_handler,list_curr_thread,list_ftol, 84 85 list_reset_flag) 85 86 result = map(map_apply, inputs) … … 87 88 self.complete(result=result, 88 89 batch_inputs=self.batch_inputs, 89 90 batch_outputs=self.batch_outputs, 90 91 page_id=self.page_id, 91 92 pars = self.pars, -
src/sans/perspectives/fitting/fitpage.py
rd44648e rbf5e985 661 661 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 662 662 663 if self.engine_type == "park": 664 self.text_disp_max.Show(True) 665 self.text_disp_min.Show(True) 663 self.text_disp_max.Show(True) 664 self.text_disp_min.Show(True) 666 665 667 666 for item in self.model.dispersion.keys(): … … 738 737 wx.EXPAND | wx.ADJUST_MINSIZE, 0) 739 738 740 if self.engine_type == "park": 741 ctl3.Show(True) 742 ctl4.Show(True) 739 ctl3.Show(True) 740 ctl4.Show(True) 743 741 744 742 elif p == "npts": … … 1005 1003 return 1006 1004 1007 if len(self._manager.fit_thread_list) > 0 and\1008 self._manager._fit_engine != "park" and\1009 self._manager.sim_page != None and \1010 self._manager.sim_page.uid == self.uid:1005 if (len(self._manager.fit_thread_list) > 0 1006 and self._manager._fit_engine not in ("park","bumps") 1007 and self._manager.sim_page != None 1008 and self._manager.sim_page.uid == self.uid): 1011 1009 msg = "The FitEnging will be set to 'ParkMC'\n" 1012 1010 msg += " to fit with more than one data set..." … … 1268 1266 if check_float(tcrtl): 1269 1267 flag = self._onparamEnter_helper() 1270 self.s et_npts2fit()1268 self.show_npts2fit() 1271 1269 if self.fitrange: 1272 1270 temp_smearer = None … … 1468 1466 #self.data.mask = index_data 1469 1467 #self.Npts_fit.SetValue(str(len(self.data.mask))) 1470 self.s et_npts2fit()1468 self.show_npts2fit() 1471 1469 else: 1472 1470 index_data = ((self.qmin_x <= self.data.x) & \ … … 1766 1764 # try re draw the model plot if it exists 1767 1765 self._draw_model() 1768 self.s et_npts2fit()1766 self.show_npts2fit() 1769 1767 elif self.model == None: 1770 1768 self.panel.MakeModal(False) 1771 1769 event.Skip() 1772 self.s et_npts2fit()1770 self.show_npts2fit() 1773 1771 msg = "No model is found on updating MASK in the model plot... " 1774 1772 wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) … … 2056 2054 return npts2fit 2057 2055 2058 def s et_npts2fit(self):2056 def show_npts2fit(self): 2059 2057 """ 2060 2058 setValue Npts for fitting … … 2068 2066 return self.tcChi.GetValue() 2069 2067 2070 def get_param_list(self):2071 """2072 :return self.param_toFit: list containing references to TextCtrl2073 checked.Theses TextCtrl will allow reference to parameters to fit.2074 2075 :raise: if return an empty list of parameter fit will nnote work2076 properly so raise ValueError,"missing parameter to fit"2077 """2078 if self.param_toFit != []:2079 return self.param_toFit2080 else:2081 msg = "missing parameters to fit"2082 wx.MessageBox(msg, 'warning')2083 return False2084 2085 2068 def onsetValues(self, chisqr, p_name, out, cov): 2086 2069 """ … … 2108 2091 if chisqr != None and numpy.isfinite(chisqr): 2109 2092 #format chi2 2110 if self.engine_type == "park":2111 npt_fit = float(self.get_npts2fit())2112 2093 chi2 = format_number(chisqr, True) 2113 2094 self.tcChi.SetValue(chi2) -
src/sans/perspectives/fitting/fitpanel.py
rb6a181b reff93b8 64 64 self.sim_page = None 65 65 self.batch_page = None 66 self.fit_engine_type = " scipy"66 self.fit_engine_type = "bumps" 67 67 ## get the state of a page 68 68 self.Bind(basepage.EVT_PAGE_INFO, self._onGetstate) -
src/sans/perspectives/fitting/fitproblem.py
r5777106 r5bf0331 454 454 return self.itervalues() 455 455 456 def 456 def set_result(self, result, fid): 457 457 """ 458 458 """ -
src/sans/perspectives/fitting/fitting.py
r9afebe1 ra9807efa 36 36 from .fitproblem import FitProblemDictionary 37 37 from .fitpanel import FitPanel 38 from .resultpanel import ResultPanel, PlotResultEvent 39 38 40 from .fit_thread import FitThread 39 41 from .pagestate import Reader … … 42 44 from sans.perspectives.calculator.model_editor import EditorWindow 43 45 from sans.guiframe.gui_manager import MDIFrame 46 47 # TODO: remove globals from interface to bumps options! 48 # Default bumps to use the levenberg-marquardt optimizer 49 import bumps.fitters 50 bumps.fitters.FIT_DEFAULT = 'lm' 44 51 45 52 MAX_NBR_DATA = 4 … … 78 85 # Start with a good default 79 86 self.elapsed = 0.022 80 # the type of optimizer selected, park or scipy81 self.fitter = None82 87 self.fit_panel = None 83 #let fit ready84 self.fitproblem_count = None85 88 #Flag to let the plug-in know that it is running stand alone 86 89 self.standalone = True … … 88 91 self.closed_page_dict = {} 89 92 ## Fit engine 90 self._fit_engine = ' scipy'93 self._fit_engine = 'bumps' 91 94 self._gui_engine = None 92 95 ## Relative error desired in the sum of squares (float); scipy only … … 111 114 self.scipy_id = wx.NewId() 112 115 self.park_id = wx.NewId() 116 self.bumps_id = wx.NewId() 113 117 self.menu1 = None 114 118 self.new_model_frame = None … … 198 202 wx.EVT_MENU(owner, self.park_id, self._onset_engine_park) 199 203 200 self.menu1.FindItemById(self.scipy_id).Check(True) 201 self.menu1.FindItemById(self.park_id).Check(False) 204 bumps_help = "Bumps: fitting and uncertainty analysis. More in Help window...." 205 self.menu1.AppendCheckItem(self.bumps_id, "Bumps fit", 206 bumps_help) 207 wx.EVT_MENU(owner, self.bumps_id, self._onset_engine_bumps) 208 209 self.menu1.FindItemById(self.scipy_id).Check(self._fit_engine=="scipy") 210 self.menu1.FindItemById(self.park_id).Check(self._fit_engine=="park") 211 self.menu1.FindItemById(self.bumps_id).Check(self._fit_engine=="bumps") 202 212 self.menu1.AppendSeparator() 203 213 self.id_tol = wx.NewId() … … 207 217 ftol_help) 208 218 wx.EVT_MENU(owner, self.id_tol, self.show_ftol_dialog) 219 220 self.id_bumps_options = wx.NewId() 221 bopts_help = "Bumps fitting options" 222 self.menu1.Append(self.id_bumps_options, 'Bumps &Options', bopts_help) 223 wx.EVT_MENU(owner, self.id_bumps_options, self.on_bumps_options) 224 self.bumps_options_menu = self.menu1.FindItemById(self.id_bumps_options) 225 self.bumps_options_menu.Enable(True) 226 227 self.id_result_panel = wx.NewId() 228 self.menu1.Append(self.id_result_panel, "Fit Results", "Show fit results panel") 229 wx.EVT_MENU(owner, self.id_result_panel, lambda ev: self.result_frame.Show()) 209 230 self.menu1.AppendSeparator() 210 231 … … 511 532 self.perspective = [] 512 533 self.perspective.append(self.fit_panel.window_name) 534 535 self.result_frame = MDIFrame(self.parent, None, ResultPanel.window_caption, (220, 200)) 536 self.result_panel = ResultPanel(parent=self.result_frame, manager=self) 537 self.perspective.append(self.result_panel.window_name) 513 538 514 539 #index number to create random model name … … 525 550 #Send the fitting panel to guiframe 526 551 self.mypanels.append(self.fit_panel) 552 self.mypanels.append(self.result_panel) 527 553 return self.mypanels 528 554 … … 818 844 StatusEvent(status=msg, info='warning')) 819 845 dialog.Destroy() 846 847 def on_bumps_options(self, event=None): 848 from bumps.gui.fit_dialog import OpenFitOptions 849 OpenFitOptions() 820 850 821 851 def stop_fit(self, uid): … … 951 981 :param uid: id related to the panel currently calling this fit function. 952 982 """ 953 flag = True 954 ## count the number of fitproblem schedule to fit 955 fitproblem_count = 0 956 for value in self.page_finder.values(): 957 if value.get_scheduled() == 1: 958 fitproblem_count += 1 959 self._gui_engine = self._return_engine_type() 960 self.fitproblem_count = fitproblem_count 961 if self._fit_engine == "park": 962 engineType = "Simultaneous Fit" 983 if uid is None: raise RuntimeError("no page to fit") # Should never happen 984 985 # Remember the user selected fit engine before the fit. Simultaneous 986 # fitting may change the selected engine, so it needs to be restored 987 # when the fit is complete. 988 self._gui_engine = self._fit_engine 989 990 sim_page_uid = getattr(self.sim_page, 'uid', None) 991 batch_page_uid = getattr(self.batch_page, 'uid', None) 992 993 if uid == sim_page_uid: 994 fit_type = 'simultaneous' 995 elif uid == batch_page_uid: 996 fit_type = 'combined_batch' 963 997 else: 964 engineType = "Single Fit" 998 fit_type = 'single' 999 1000 # if constrained fit, don't use scipy leastsq directly 1001 if fit_type == 'simultaneous': 1002 if self._fit_engine not in ("park","bumps"): 1003 self._on_change_engine(engine='bumps') 1004 1005 965 1006 fitter_list = [] 966 1007 sim_fitter = None 967 is_single_fit = True 968 batch_on = False 969 if self.sim_page is not None and self.sim_page.uid == uid: 1008 if fit_type == 'simultaneous': 970 1009 #simulatanous fit only one engine need to be created 971 ## if simultaneous fit change automatically the engine to park972 self._on_change_engine(engine='park')973 1010 sim_fitter = Fit(self._fit_engine) 974 1011 sim_fitter.fitter_id = self.sim_page.uid 975 1012 fitter_list.append(sim_fitter) 976 is_single_fit = False 977 batch_on = self.sim_page.batch_on 978 979 self.fitproblem_count = fitproblem_count 980 if self._fit_engine == "park": 981 engineType = "Simultaneous Fit" 982 else: 983 engineType = "Single Fit" 984 1013 985 1014 self.current_pg = None 986 1015 list_page_id = [] 987 1016 fit_id = 0 988 batch_inputs = {} 989 batch_outputs = {} 990 for page_id, value in self.page_finder.iteritems(): 1017 for page_id, page_info in self.page_finder.iteritems(): 991 1018 # For simulfit (uid give with None), do for-loop 992 1019 # if uid is specified (singlefit), do it only on the page. 993 if engineType == "Single Fit": 994 #combine more than 1 batch page on single mode 995 if self.batch_page is None or self.batch_page.uid != uid: 996 if page_id != uid: 997 continue 1020 if page_id in (sim_page_uid, batch_page_uid): continue 1021 if fit_type == "single" and page_id != uid: continue 1022 998 1023 try: 999 if value.get_scheduled() == 1: 1000 value.nbr_residuals_computed = 0 1001 #Get list of parameters name to fit 1002 pars = [] 1003 templist = [] 1024 if page_info.get_scheduled() == 1: 1025 page_info.nbr_residuals_computed = 0 1004 1026 page = self.fit_panel.get_page_by_id(page_id) 1005 1027 self.set_fit_weight(uid=page.uid, 1006 1028 flag=page.get_weight_flag(), 1007 1029 is2d=page._is_2D()) 1008 templist = page.get_param_list() 1009 flag = page._update_paramv_on_fit() 1010 if not flag: 1030 if not page.param_toFit: 1031 msg = "No fitting parameters for %s"%page.window_caption 1032 wx.PostEvent(page.parent.parent, 1033 StatusEvent(status=msg, info="error", 1034 type="stop")) 1035 return False 1036 if not page._update_paramv_on_fit(): 1011 1037 msg = "Fitting range or parameter values are" 1012 1038 msg += " invalid in %s" % \ … … 1015 1041 StatusEvent(status=msg, info="error", 1016 1042 type="stop")) 1017 return flag 1018 for element in templist: 1019 name = str(element[1]) 1020 pars.append(name) 1021 fitproblem_list = value.values() 1043 return False 1044 1045 pars = [str(element[1]) for element in page.param_toFit] 1046 fitproblem_list = page_info.values() 1022 1047 for fitproblem in fitproblem_list: 1023 1048 if sim_fitter is None: 1024 1049 fitter = Fit(self._fit_engine) 1025 1050 fitter.fitter_id = page_id 1026 self._fit_helper(fitproblem=fitproblem,1027 pars=pars,1028 fitter=fitter,1029 fit_id=fit_id,1030 batch_inputs=batch_inputs,1031 batch_outputs=batch_outputs)1032 1051 fitter_list.append(fitter) 1033 1052 else: 1034 1053 fitter = sim_fitter 1035 self._fit_helper(fitproblem=fitproblem,1054 self._add_problem_to_fit(fitproblem=fitproblem, 1036 1055 pars=pars, 1037 1056 fitter=fitter, 1038 fit_id=fit_id, 1039 batch_inputs=batch_inputs, 1040 batch_outputs=batch_outputs) 1057 fit_id=fit_id) 1041 1058 fit_id += 1 1042 1059 list_page_id.append(page_id) 1043 current_page_id = page_id 1044 value.clear_model_param() 1060 page_info.clear_model_param() 1045 1061 except KeyboardInterrupt: 1046 flag = True1047 1062 msg = "Fitting terminated" 1048 1063 wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", 1049 1064 type="stop")) 1050 return flag1065 return True 1051 1066 except: 1052 flag = False 1053 msg = "%s error: %s" % (engineType, sys.exc_value) 1067 msg = "Fitting error: %s" % str(sys.exc_value) 1054 1068 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", 1055 1069 type="stop")) 1056 return flag1070 return False 1057 1071 ## If a thread is already started, stop it 1058 1072 #if self.calc_fit!= None and self.calc_fit.isrunning(): … … 1066 1080 improvement_delta=0.1) 1067 1081 self._mac_sleep(0.2) 1068 ## perform single fit 1069 try: 1082 1083 # batch fit 1084 batch_inputs = {} 1085 batch_outputs = {} 1086 if fit_type == "simultaneous": 1087 page = self.sim_page 1088 elif fit_type == "combined_batch": 1089 page = self.batch_page 1090 else: 1070 1091 page = self.fit_panel.get_page_by_id(uid) 1071 batch_on = page.batch_on 1072 except: 1073 try: 1074 #if the id cannot be found then we deal with a self.sim_page 1075 #or a self.batch_page 1076 if self.sim_page is not None and uid == self.sim_page.uid: 1077 batch_on = self.sim_page.batch_on 1078 if self.batch_page is not None and uid == self.batch_page.uid: 1079 batch_on = self.batch_page.batch_on 1080 except: 1081 batch_on = False 1082 1083 # batch fit 1084 if batch_on: 1092 if page.batch_on: 1085 1093 calc_fit = FitThread(handler=handler, 1086 1094 fn=fitter_list, … … 1093 1101 reset_flag=self.batch_reset_flag) 1094 1102 else: 1095 # single fit: not batch and not simul fit1096 if not is_single_fit:1097 current_page_id = self.sim_page.uid1098 1103 ## Perform more than 1 fit at the time 1099 1104 calc_fit = FitThread(handler=handler, … … 1105 1110 completefn=self._fit_completed, 1106 1111 ftol=self.ftol) 1107 self.fit_thread_list[current_page_id] = calc_fit 1112 #self.fit_thread_list[current_page_id] = calc_fit 1113 self.fit_thread_list[uid] = calc_fit 1108 1114 calc_fit.queue() 1115 calc_fit.ready(2.5) 1109 1116 msg = "Fitting is in progress..." 1110 1117 wx.PostEvent(self.parent, StatusEvent(status=msg, type="progress")) 1111 1118 1112 self.ready_fit(calc_fit=calc_fit) 1113 return flag 1114 1115 def ready_fit(self, calc_fit): 1116 """ 1117 Ready for another fit 1118 """ 1119 if self.fitproblem_count != None and self.fitproblem_count > 1: 1120 calc_fit.ready(2.5) 1121 else: 1122 time.sleep(0.4) 1123 1119 return True 1120 1124 1121 def remove_plot(self, uid, fid=None, theory=False): 1125 1122 """ … … 1261 1258 self.page_finder[uid].schedule_tofit(value) 1262 1259 1263 def _fit_helper(self, fitproblem, pars, fitter, fit_id, 1264 batch_inputs, batch_outputs): 1260 def _add_problem_to_fit(self, fitproblem, pars, fitter, fit_id): 1265 1261 """ 1266 1262 Create and set fit engine with series of data and model … … 1571 1567 wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", 1572 1568 type="stop")) 1569 wx.PostEvent(self.result_panel, PlotResultEvent(result=result)) 1573 1570 # reset fit_engine if changed by simul_fit 1574 1571 if self._fit_engine != self._gui_engine: … … 1682 1679 self._on_change_engine('scipy') 1683 1680 1681 def _onset_engine_bumps(self, event): 1682 """ 1683 set engine to bumps 1684 """ 1685 self._on_change_engine('bumps') 1686 1684 1687 def _on_slicer_event(self, event): 1685 1688 """ … … 1714 1717 break 1715 1718 1716 def _return_engine_type(self):1717 """1718 return the current type of engine1719 """1720 return self._fit_engine1721 1722 1719 def _on_change_engine(self, engine='park'): 1723 1720 """ … … 1733 1730 self.menu1.FindItemById(self.park_id).Check(True) 1734 1731 self.menu1.FindItemById(self.scipy_id).Check(False) 1732 self.menu1.FindItemById(self.bumps_id).Check(False) 1733 elif engine == "scipy": 1734 self.menu1.FindItemById(self.park_id).Check(False) 1735 self.menu1.FindItemById(self.scipy_id).Check(True) 1736 self.menu1.FindItemById(self.bumps_id).Check(False) 1735 1737 else: 1736 1738 self.menu1.FindItemById(self.park_id).Check(False) 1737 self.menu1.FindItemById(self.scipy_id).Check(True) 1739 self.menu1.FindItemById(self.scipy_id).Check(False) 1740 self.menu1.FindItemById(self.bumps_id).Check(True) 1738 1741 ## post a message to status bar 1739 1742 msg = "Engine set to: %s" % self._fit_engine -
src/sans/perspectives/fitting/simfitpage.py
r5777106 rbf5e985 138 138 self.Layout() 139 139 break 140 self._onAdd_constraint(None) 140 141 #self._onAdd_constraint(None) 141 142 142 143 def onFit(self, event): … … 152 153 ## making sure all parameters content a constraint 153 154 ## validity of the constraint expression is own by fit engine 154 if self.parent._manager._fit_engine != "park"and flag:155 if self.parent._manager._fit_engine not in ("park","bumps") and flag: 155 156 msg = "The FitEnging will be set to 'Park' fit engine\n" 156 157 msg += " for the simultaneous fit..." … … 378 379 box_description = wx.StaticBox(self, -1,"Easy Setup ") 379 380 boxsizer = wx.StaticBoxSizer(box_description, wx.HORIZONTAL) 380 sizer_constraint = wx.BoxSizer(wx.HORIZONTAL |wx.LEFT|wx.RIGHT|wx.EXPAND)381 sizer_constraint = wx.BoxSizer(wx.HORIZONTAL) 381 382 self.model_cbox_left = wx.ComboBox(self, -1, style=wx.CB_READONLY) 382 383 self.model_cbox_left.Clear() … … 814 815 815 816 for fid in self.page_finder[id].iterkeys(): 816 self.page_finder[id].set_model_param(param, 817 constraint, fid=fid) 817 # wrap in param/constraint in str() to remove unicode 818 self.page_finder[id].set_model_param(str(param), 819 str(constraint), fid=fid) 818 820 break 819 821 return True -
test/park_integration/test/batch_fit.py
r5777106 re3efa6b3 1 2 3 import math4 import numpy5 1 import copy 6 2 import time … … 15 11 16 12 17 18 19 13 def classMapper(classInstance, classFunc, *args): 20 14 """ … … 28 22 29 23 30 31 class BatchScipyFit: 24 class BatchFit: 32 25 """ 33 26 test fit module … … 69 62 model.setParam('cyl_phi.npts', 3) 70 63 model.setParam('cyl_theta.nsigmas', 10) 71 """ for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg]"""64 # for 2 data cyl_theta = 60.0 [deg] cyl_phi= 60.0 [deg] 72 65 fitter.set_model(model, i, self.param_to_fit, 73 66 self.list_of_constraints) … … 84 77 self.list_of_mapper.append(classMapper) 85 78 86 def reset_value(self ):79 def reset_value(self, engine='scipy'): 87 80 """ 88 81 Initialize inputs for the map function … … 93 86 self.list_of_constraints = [] 94 87 self.list_of_mapper = [] 95 engine ="scipy" 96 88 97 89 path = "testdata_line3.txt" 98 90 self._reset_helper(path=path, engine=engine, npts=NPTS) … … 113 105 114 106 115 def test_map_fit(self ):107 def test_map_fit(self, n=0): 116 108 """ 117 """ 118 results = map(classMapper,self.list_of_fitter, self.list_of_function) 119 print len(results) 120 for result in results: 121 print result.fitness, result.stderr, result.pvec 109 """ 110 if n > 0: 111 self._test_process_map_fit(n=n) 112 else: 113 results = map(classMapper,self.list_of_fitter, self.list_of_function) 114 print len(results) 115 for result in results: 116 print result.fitness, result.stderr, result.pvec 122 117 123 118 def test_process_map_fit(self, n=1): … … 144 139 """ 145 140 def setUp(self): 146 self.test = Batch ScipyFit(qmin=None, qmax=None)141 self.test = BatchFit(qmin=None, qmax=None) 147 142 148 143 149 def __test_fit1(self):144 def test_fit1(self): 150 145 """test fit with python built in map function---- full range of each data""" 151 146 self.test.test_map_fit() 152 147 153 def __test_fit2(self):148 def test_fit2(self): 154 149 """test fit with python built in map function---- common range for all data""" 155 150 self.test.set_range(qmin=0.013, qmax=0.05) 156 151 self.test.reset_value() 157 152 self.test.test_map_fit() 153 raise Exception("fail") 158 154 159 155 def test_fit3(self): … … 161 157 self.test.set_range(qmin=None, qmax=None) 162 158 self.test.reset_value() 163 self.test.test_ process_map_fit(n=2)159 self.test.test_map_fit(n=1) 164 160 165 161 def test_fit4(self): … … 167 163 self.test.set_range(qmin=-1, qmax=10) 168 164 self.test.reset_value() 169 self.test.test_ process_map_fit(n=1)165 self.test.test_map_fit(n=3) 170 166 171 167 -
test/park_integration/test/utest_fit_cylinder.py
rda5d8e8 reff93b8 10 10 from sans.dataloader.loader import Loader 11 11 12 #@unittest.skip("") 12 13 class TestSingleFit(unittest.TestCase): 13 14 """ test single fitting """ … … 33 34 fitter.set_model(self.model,1,self.pars1) 34 35 fitter.select_problem_for_fit(id=1,value=1) 35 return fitter.fit() 36 36 result1, = fitter.fit() 37 37 38 def test_scipy(self):39 """ Simple cylinder model fit (scipy) """40 41 result1, = self._fit("scipy")42 43 38 self.assert_(result1) 44 39 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 45 40 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 46 41 47 42 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 48 43 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 49 44 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 50 45 self.assertTrue( result1.fitness < 1.0 ) 51 52 46 47 48 def test_scipy(self): 49 """ Simple cylinder model fit (scipy) """ 50 self._fit("scipy") 51 52 53 53 def test_park(self): 54 54 """ Simple cylinder model fit (park) """ 55 #raise NotImplementedError() 56 result1, = self._fit("park") 57 58 self.assert_(result1) 59 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 60 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 61 62 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 63 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 64 self.assertTrue( math.fabs(result1.pvec[2]-1.0)/3.0 < result1.stderr[2] ) 65 self.assertTrue( result1.fitness < 1.0 ) 66 67 55 self._fit("park") 56 57 def test_bumps(self): 58 """ Simple cylinder model fit (park) """ 59 self._fit("bumps") 60 61 68 62 69 63 class TestSimultaneousFit(unittest.TestCase): … … 82 76 self.model1.set(scale= 1.0) 83 77 self.model1.set(radius=18) 84 self.model1.set(length= 396)78 self.model1.set(length=200) 85 79 self.model1.set(sldCyl=3e-006, sldSolv=0.0) 86 80 self.model1.set(background=0.0) 87 81 88 82 cyl2 = CylinderModel() 89 83 cyl2.name = "C2" … … 91 85 self.model2.set(scale= 1.0) 92 86 self.model2.set(radius=37) 93 self.model2.set(length= 'C1.length')87 self.model2.set(length=300) 94 88 self.model2.set(sldCyl=3e-006, sldSolv=0.0) 95 89 self.model2.set(background=0.0) 96 97 def _fit(self, name="park"): 90 91 92 def test_constrained_bumps(self): 93 """ Simultaneous cylinder model fit (park) """ 94 self._run_fit(Fit('bumps')) 95 96 #@unittest.skip("") 97 def test_constrained_park(self): 98 """ Simultaneous cylinder model fit (park) """ 99 self._run_fit(Fit('park')) 100 101 def _run_fit(self, fitter): 102 result1, result2 = self._fit(fitter) 103 self.assert_(result1) 104 self.assertTrue(len(result1.pvec)>0) 105 self.assertTrue(len(result1.stderr)>0) 106 107 for n, v, dv in zip(result1.param_list, result1.pvec, result1.stderr): 108 print "%s M1.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv) 109 if n == "length": 110 self.assertTrue( math.fabs(v-400.0)/3.0 < dv ) 111 elif n=='radius': 112 self.assertTrue( math.fabs(v-20.0)/3.0 < dv ) 113 elif n=='scale': 114 self.assertTrue( math.fabs(v-1.0)/3.0 < dv ) 115 for n, v, dv in zip(result2.param_list, result2.pvec, result2.stderr): 116 print "%s M2.%s = %s +/- %s"%(fitter._engine.__class__.__name__,n,v,dv) 117 if n=='radius': 118 self.assertTrue( math.fabs(v-40.0)/3.0 < dv ) 119 elif n=='scale': 120 self.assertTrue( math.fabs(v-1.0)/3.0 < dv ) 121 122 def _fit(self, fitter): 98 123 """ return fit result """ 99 fitter = Fit(name)100 124 fitter.set_data(self.data1,1) 101 125 fitter.set_model(self.model1, 1, ['length','radius','scale']) 102 126 103 127 fitter.set_data(self.data2,2) 104 fitter.set_model(self.model2, 2, ['radius','scale']) 128 fitter.set_model(self.model2, 2, ['length','radius','scale'], 129 constraints=[("length","C1.length")]) 105 130 fitter.select_problem_for_fit(id=1,value=1) 106 131 fitter.select_problem_for_fit(id=2,value=1) 107 132 return fitter.fit() 108 109 110 def test_park2(self): 111 """ Simultaneous cylinder model fit (park) """ 112 raise NotImplementedError() 113 result1,= self._fit('park') 114 self.assert_(result1) 115 self.assertTrue(len(result1.pvec)>=0 ) 116 self.assertTrue(len(result1.stderr)>= 0) 117 118 for par in result1.parameters: 119 if par.name=='C1.length': 120 print par.name, par.value 121 self.assertTrue( math.fabs(par.value-400.0)/3.0 < par.stderr ) 122 elif par.name=='C1.radius': 123 print par.name, par.value 124 self.assertTrue( math.fabs(par.value-20.0)/3.0 < par.stderr ) 125 elif par.name=='C2.radius': 126 print par.name, par.value 127 self.assertTrue( math.fabs(par.value-40.0)/3.0 < par.stderr ) 128 elif par.name=='C1.scale': 129 print par.name, par.value 130 self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) 131 elif par.name=='C2.scale': 132 print par.name, par.value 133 self.assertTrue( math.fabs(par.value-1.0)/3.0 < par.stderr ) 134 133 135 134 136 135 if __name__ == '__main__': -
test/park_integration/test/utest_fit_line.py
rda5d8e8 re3efa6b3 4 4 """ 5 5 import unittest 6 7 from sans.fit.AbstractFitEngine import Model 6 import math 7 8 from sans.fit.AbstractFitEngine import Model, FitHandler 8 9 from sans.dataloader.loader import Loader 9 10 from sans.fit.Fitting import Fit … … 11 12 from sans.models.Constant import Constant 12 13 13 import math14 14 class testFitModule(unittest.TestCase): 15 15 """ test fitting """ 16 17 def test 1(self):18 """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """19 #load data 16 17 def test_bad_pars(self): 18 fitter = Fit('bumps') 19 20 20 data = Loader().load("testdata_line.txt") 21 21 data.name = data.filename 22 #Importing the Fit module 23 fitter = Fit('scipy') 22 fitter.set_data(data,1) 23 24 model1 = LineModel() 25 model1.name = "M1" 26 model = Model(model1, data) 27 pars1= ['param1','param2'] 28 try: 29 fitter.set_model(model,1,pars1) 30 except ValueError,exc: 31 #print "ValueError was correctly raised: "+str(msg) 32 assert str(exc).startswith('parameter param1') 33 else: 34 raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 35 36 def fit_single(self, fitter_name, isdream=False): 37 fitter = Fit(fitter_name) 38 39 data = Loader().load("testdata_line.txt") 40 data.name = data.filename 41 fitter.set_data(data,1) 42 24 43 # Receives the type of model for the fitting 25 44 model1 = LineModel() … … 27 46 model = Model(model1,data) 28 47 #fit with scipy test 29 30 pars1= ['param1','param2'] 31 fitter.set_data(data,1) 32 try:fitter.set_model(model,1,pars1) 33 except ValueError,exc: 34 #print "ValueError was correctly raised: "+str(msg) 35 assert str(exc).startswith('wrong parameter') 36 else: raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 48 37 49 pars1= ['A','B'] 38 50 fitter.set_model(model,1,pars1) 39 51 fitter.select_problem_for_fit(id=1,value=1) 40 result1, = fitter.fit() 41 42 self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] ) 43 self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 44 self.assertTrue( result1.fitness/len(data.x) < 2 ) 45 46 #fit with park test 47 fitter = Fit('park') 48 fitter.set_data(data,1) 49 fitter.set_model(model,1,pars1) 50 fitter.select_problem_for_fit(id=1,value=1) 51 result2, = fitter.fit() 52 53 self.assert_(result2) 54 self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] ) 55 self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 56 self.assertTrue( result2.fitness/len(data.x) < 2) 57 # compare fit result result for scipy and park 58 self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) 59 self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) 60 self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 61 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 62 self.assertAlmostEquals( result1.fitness, 63 result2.fitness/len(data.x),1 ) 64 52 result1, = fitter.fit(handler=FitHandler()) 53 54 # The target values were generated from the following statements 55 p,s,fx = result1.pvec, result1.stderr, result1.fitness 56 #print "p0,p1,s0,s1,fx = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx) 57 p0,p1,s0,s1,fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189 58 59 if isdream: 60 # Dream is not a minimizer: just check that the fit is within 61 # uncertainty 62 self.assertTrue( abs(p[0]-p0) <= s0 ) 63 self.assertTrue( abs(p[1]-p1) <= s1 ) 64 else: 65 self.assertTrue( abs(p[0]-p0) <= 1e-5 ) 66 self.assertTrue( abs(p[1]-p1) <= 1e-5 ) 67 self.assertTrue( abs(fx-fx_) <= 1e-5 ) 68 69 def fit_bumps(self, alg, **opts): 70 #Importing the Fit module 71 from bumps import fitters 72 fitters.FIT_DEFAULT = alg 73 fitters.FIT_OPTIONS[alg].options.update(opts) 74 fitters.FIT_OPTIONS[alg].options.update(monitors=[]) 75 #print "fitting",alg,opts 76 #kprint "options",fitters.FIT_OPTIONS[alg].__dict__ 77 self.fit_single('bumps', isdream=(alg=='dream')) 78 79 def test_bumps_de(self): 80 self.fit_bumps('de') 81 82 def test_bumps_dream(self): 83 self.fit_bumps('dream', burn=500, steps=100) 84 85 def test_bumps_amoeba(self): 86 self.fit_bumps('amoeba') 87 88 def test_bumps_newton(self): 89 self.fit_bumps('newton') 90 91 def test_bumps_lm(self): 92 self.fit_bumps('lm') 93 94 def test_scipy(self): 95 #print "fitting scipy" 96 self.fit_single('scipy') 97 98 def test_park(self): 99 #print "fitting park" 100 self.fit_single('park') 101 65 102 66 103 def test2(self): … … 93 130 fitter.select_problem_for_fit(id=2,value=0) 94 131 95 try: result1, = fitter.fit( )132 try: result1, = fitter.fit(handler=FitHandler()) 96 133 except RuntimeError,msg: 97 134 assert str(msg)=="No Assembly scheduled for Scipy fitting." … … 99 136 fitter.select_problem_for_fit(id=1,value=1) 100 137 fitter.select_problem_for_fit(id=2,value=1) 101 try: result1, = fitter.fit( )138 try: result1, = fitter.fit(handler=FitHandler()) 102 139 except RuntimeError,msg: 103 140 assert str(msg)=="Scipy can't fit more than a single fit problem at a time." 104 141 else: raise AssertionError,"No error raised for scipy fitting with more than 2 models" 105 142 106 143 #fit with park test 107 144 fitter = Fit('park') … … 112 149 fitter.select_problem_for_fit(id=1,value=1) 113 150 fitter.select_problem_for_fit(id=2,value=1) 114 R1,R2 = fitter.fit( )151 R1,R2 = fitter.fit(handler=FitHandler()) 115 152 116 153 self.assertTrue( math.fabs(R1.pvec[0]-4)/3 <= R1.stderr[0] ) … … 157 194 fitter.select_problem_for_fit(id=2,value=1) 158 195 159 R1,R2 = fitter.fit( )196 R1,R2 = fitter.fit(handler=FitHandler()) 160 197 self.assertTrue( math.fabs(R1.pvec[0]-4.0)/3. <= R1.stderr[0]) 161 198 self.assertTrue( math.fabs(R1.pvec[1]-2.5)/3. <= R1.stderr[1]) … … 188 225 fitter.select_problem_for_fit(id=1,value=1) 189 226 190 result1, = fitter.fit() 227 result1, = fitter.fit(handler=FitHandler()) 228 #print(result1) 191 229 self.assert_(result1) 192 230 … … 194 232 self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 195 233 self.assertTrue( result1.fitness/len(data1.x) < 2 ) 196 234 197 235 #fit with park test 198 236 fitter = Fit('park') … … 201 239 fitter.set_data(data2,1,qmin=1,qmax=10) 202 240 fitter.select_problem_for_fit(id=1,value=1) 203 result2, = fitter.fit( )241 result2, = fitter.fit(handler=FitHandler()) 204 242 205 243 self.assert_(result2) … … 213 251 self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 214 252 self.assertTrue( result2.fitness/(len(data2.x)+len(data1.x)) < 2 ) 215 216 253 254 255 if __name__ == "__main__": 256 unittest.main() 217 257 218 -
test/park_integration/test/utest_fit_smeared.py
r6c00702 re3efa6b3 5 5 import unittest 6 6 import math 7 7 8 import numpy 8 9 from sans.fit.AbstractFitEngine import Model … … 21 22 out=Loader().load("cyl_400_20.txt") 22 23 # This data file has not error, add them 23 out.dy = out.y24 #out.dy = out.y 24 25 25 26 fitter = Fit('scipy') … … 28 29 # Receives the type of model for the fitting 29 30 model1 = CylinderModel() 30 model1.setParam('sldCyl', 3.0e-6) 31 model1.setParam('sldSolv', 0.0) 31 model1.setParam("scale", 1.0) 32 model1.setParam("radius",18) 33 model1.setParam("length", 397) 34 model1.setParam("sldCyl",3e-006 ) 35 model1.setParam("sldSolv",0.0 ) 36 model1.setParam("background", 0.0) 32 37 model = Model(model1) 33 model.set(scale=1e-10)34 38 pars1 =['length','radius','scale'] 35 39 fitter.set_model(model,1,pars1) … … 38 42 fitter.select_problem_for_fit(id=1,value=1) 39 43 result1, = fitter.fit() 40 44 #print "result1",result1 45 41 46 self.assert_(result1) 42 self.assertTrue(len(result1.pvec) >0 or len(result1.pvec)==0)43 self.assertTrue(len(result1.stderr) > 0 or len(result1.stderr)==0)47 self.assertTrue(len(result1.pvec) > 0) 48 self.assertTrue(len(result1.stderr) > 0) 44 49 45 50 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 46 51 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 47 self.assertTrue( math.fabs(result1.pvec[2]- 9.0e-12)/3.0 < result1.stderr[2] )52 self.assertTrue( math.fabs(result1.pvec[2]-1)/3.0 < result1.stderr[2] ) 48 53 self.assertTrue( result1.fitness < 1.0 ) 49 54 55 def test_park_dispersion(self): 56 """ 57 Cylinder fit with dispersion 58 """ 59 self._dispersion(fitter = Fit('park')) 60 61 def test_bumps_dispersion(self): 62 """ 63 Cylinder fit with dispersion 64 """ 65 alg = 'lm' 66 from bumps import fitters 67 fitters.FIT_DEFAULT = alg 68 #fitters.FIT_OPTIONS[alg].options.update(opts) 69 fitters.FIT_OPTIONS[alg].options.update(monitors=[]) 70 self._dispersion(fitter = Fit('bumps')) 71 50 72 def test_scipy_dispersion(self): 51 73 """ 52 74 Cylinder fit with dispersion 53 75 """ 76 self._dispersion(fitter = Fit('scipy')) 77 78 def _dispersion(self, fitter): 54 79 # Load data 55 80 # This data is for a cylinder with … … 60 85 out.dy[i] = math.sqrt(out.y[i]) 61 86 62 # Set up the fit63 fitter = Fit('scipy')64 87 # Receives the type of model for the fitting 65 88 model1 = CylinderModel() 66 model1.setParam('sldCyl', 3.0e-6) 67 model1.setParam('sldSolv', 0.0) 89 model1.setParam("scale", 10.0) 90 model1.setParam("radius",18) 91 model1.setParam("length", 397) 92 model1.setParam("sldCyl",3e-006 ) 93 model1.setParam("sldSolv",0.0 ) 94 model1.setParam("background", 0.0) 68 95 69 96 # Dispersion parameters 70 model1.dispersion['radius']['width'] = 0. 00171 model1.dispersion['radius']['npts'] = 50 72 97 model1.dispersion['radius']['width'] = 0.25 98 model1.dispersion['radius']['npts'] = 50 99 73 100 model = Model(model1) 74 101 75 102 pars1 =['length','radius','scale','radius.width'] 76 103 fitter.set_data(out,1) 77 model.set(scale=1e-10)78 104 fitter.set_model(model,1,pars1) 79 105 fitter.select_problem_for_fit(id=1,value=1) 80 result1, = fitter.fit() 106 #import time; T0 = time.time() 107 result1, = fitter.fit() 108 #print "time",time.time()-T0,fitter._engine.__class__.__name__ 81 109 82 110 self.assert_(result1) 83 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) 84 self.assertTrue(len(result1.stderr)> 0 or len(result1.stderr)==0) 85 86 self.assertTrue( math.fabs(result1.pvec[0]-400.0)/3.0 < result1.stderr[0] ) 87 self.assertTrue( math.fabs(result1.pvec[1]-20.0)/3.0 < result1.stderr[1] ) 88 self.assertTrue( math.fabs(result1.pvec[2]-1.0e-10)/3.0 < result1.stderr[2] ) 89 self.assertTrue( math.fabs(result1.pvec[3]-5.0)/3.0 < result1.stderr[3] ) 111 self.assertTrue(len(result1.pvec)>0) 112 self.assertTrue(len(result1.stderr)>0) 113 114 #print [z for z in zip(result1.param_list,result1.pvec,result1.stderr)] 115 self.assertTrue( math.fabs(result1.pvec[0]-399.8)/3.0 < result1.stderr[0] ) 116 self.assertTrue( math.fabs(result1.pvec[1]-17.5)/3.0 < result1.stderr[1] ) 117 self.assertTrue( math.fabs(result1.pvec[2]-11.1)/3.0 < result1.stderr[2] ) 118 self.assertTrue( math.fabs(result1.pvec[3]-0.276)/3.0 < result1.stderr[3] ) 90 119 self.assertTrue( result1.fitness < 1.0 ) 91 120 … … 98 127 """ 99 128 def setUp(self): 100 print "TEST DONE WITHOUT PROPER OUTPUT CHECK:"101 print " ---> TEST NEEDS TO BE COMPLETED"102 129 data = Loader().load("latex_smeared.xml") 103 130 self.data_res = data[0] … … 105 132 106 133 self.sphere = SphereModel() 134 self.sphere.setParam('background', 0) 107 135 self.sphere.setParam('radius', 5000.0) 108 self.sphere.setParam('scale', 1.0e-13) 109 self.sphere.setParam('radius.npts', 30) 110 self.sphere.setParam('radius.width',500) 111 136 self.sphere.setParam('scale', 0.4) 137 self.sphere.setParam('sldSolv',0) 138 self.sphere.setParam('sldSph',1e-6) 139 #self.sphere.setParam('radius.npts', 30) 140 #self.sphere.setParam('radius.width',50) 141 112 142 def test_reso(self): 113 143 … … 125 155 fitter.set_data(self.data_res,1) 126 156 fitter._engine.fit_arrange_dict[1].data_list[0].smearer = smear 127 print "smear ",smear 157 128 158 # Model: maybe there's a better way to do this. 129 159 # Ideally we should have to create a new model from our sans model. 130 fitter.set_model(Model(self.sphere),1, ['radius','scale' ])160 fitter.set_model(Model(self.sphere),1, ['radius','scale', 'background']) 131 161 132 162 # Why do we have to do this...? 133 163 fitter.select_problem_for_fit(id=1,value=1) 134 164 135 165 # Perform the fit (might take a while) 136 166 result1, = fitter.fit() 137 167 138 # Replace this with proper test once we know what the 139 # result should be 140 print result1.pvec 141 print result1.stderr 142 168 #print "v",result1.pvec 169 #print "dv",result1.stderr 170 #print "chisq(v)",result1.fitness 171 172 self.assertTrue( math.fabs(result1.pvec[0]-5000) < 20 ) 173 self.assertTrue( math.fabs(result1.pvec[1]-0.48) < 0.02 ) 174 self.assertTrue( math.fabs(result1.pvec[2]-0.060) < 0.002 ) 175 176 143 177 def test_slit(self): 144 178 smear = smear_selection(self.data_slit) 145 179 self.assertEqual(smear.__class__.__name__, 'SlitSmearer') 146 180 147 # Fit148 181 fitter = Fit('scipy') 149 182 … … 161 194 result1, = fitter.fit() 162 195 163 # Replace this with proper test once we know what the164 # result should be165 print result1.pvec166 print result1.stderr167 168 169 196 #print "v",result1.pvec 197 #print "dv",result1.stderr 198 #print "chisq(v)",result1.fitness 199 200 self.assertTrue( math.fabs(result1.pvec[0]-2340) < 20 ) 201 self.assertTrue( math.fabs(result1.pvec[1]-0.010) < 0.002 ) 202 170 203 if __name__ == '__main__': 171 204 unittest.main() -
test/park_integration/test/utest_park_scipy.py
r6c00702 re544c84 48 48 self.assertTrue(len(result1.stderr)>= 0) 49 49 50 print "results",list(zip(result1.pvec, result1.stderr))51 self.assertTrue( math.fabs(result1.pvec[0]-6 05)/3.0 <= result1.stderr[0] )52 self.assertTrue( math.fabs(result1.pvec[1]-20. 0)/3.0 <= result1.stderr[1] )53 self.assertTrue( math.fabs(result1.pvec[2]- 1)/3.0 <= result1.stderr[2] )50 #print "results",list(zip(result1.pvec, result1.stderr)) 51 self.assertTrue( math.fabs(result1.pvec[0]-612)/3.0 <= result1.stderr[0] ) 52 self.assertTrue( math.fabs(result1.pvec[1]-20.3)/3.0 <= result1.stderr[1] ) 53 self.assertTrue( math.fabs(result1.pvec[2]-25)/3.0 <= result1.stderr[2] ) 54 54 55 55 self.assertTrue( result1.fitness/len(data.x) < 1.0 ) -
test/park_integration/test/utest_small_test.py
r5777106 r76f132a 5 5 import math 6 6 from sans.fit.Fitting import Fit 7 from DataLoader.loader import Loader7 from sans.dataloader.loader import Loader 8 8 9 9 class testFitModule(unittest.TestCase): … … 18 18 from sans.models.CylinderModel import CylinderModel 19 19 model = CylinderModel() 20 model.setParam('contrast', 1) 21 20 model.setParam('sldCyl', 1) 21 model.setParam('sldSolv', 0) 22 22 23 pars1 =['length','radius','scale'] 23 24 fitter.set_data(out,1) 24 25 model.setParam('scale', 1e-10) 25 26 fitter.set_model(model,1,pars1, constraints=()) 26 fitter.select_problem_for_fit(Uid=1,value=1) 27 result1 = fitter.fit() 28 27 fitter.select_problem_for_fit(id=1,value=1) 28 result1, = fitter.fit() 29 #print result1 30 #print result1.__dict__ 31 29 32 self.assert_(result1) 30 33 self.assertTrue(len(result1.pvec)>0 or len(result1.pvec)==0 ) -
test/run_one.py
r95fb3e8 r76f132a 1 1 #!/usr/bin/env python 2 2 3 import os 3 4 import sys … … 8 9 9 10 run_py = joinpath(dirname(dirname(abspath(__file__))), 'run.py') 10 #print run_py11 11 run = imp.load_source('sasview_run', run_py) 12 12 run.prepare() 13 13 #print "\n".join(sys.path) 14 test_path,test_file = splitpath( sys.argv[1])15 print " test file",sys.argv[1]14 test_path,test_file = splitpath(abspath(sys.argv[1])) 15 print "=== testing:",sys.argv[1] 16 16 #print test_path, test_file 17 17 sys.argv = [sys.argv[0]] … … 20 20 test = imp.load_source('tests',test_file) 21 21 unittest.main(test, testRunner=xmlrunner.XMLTestRunner(output='logs')) 22
Note: See TracChangeset
for help on using the changeset viewer.