Changeset e54d2c32 in sasview
- Timestamp:
- Apr 13, 2010 5:48:35 PM (15 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:
- 90c9cdf
- Parents:
- e0072082
- Location:
- sansview/perspectives/fitting
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fit_thread.py
r25e3fc9 re54d2c32 1 1 2 import sys, wx, logging 3 import string, numpy, math 4 from sans.guicomm.events import NewPlotEvent, StatusEvent 2 5 3 from data_util.calcthread import CalcThread 6 import park7 from park.fitresult import FitHandler8 DEFAULT_BEAM = 0.0059 import time10 import thread11 12 class ConsoleUpdate(FitHandler):13 """14 Print progress to the console.15 """16 isbetter = False17 """Record whether results improved since last update"""18 progress_delta = 6019 """Number of seconds between progress updates"""20 improvement_delta = 521 """Number of seconds between improvement updates"""22 def __init__(self,parent, quiet=False,progress_delta=60,improvement_delta=5):23 """24 If quiet is true, only print out final summary, not progress and25 improvements.26 """27 self.parent= parent28 self.progress_time = time.time()29 self.progress_percent = 030 self.improvement_time = self.progress_time31 self.isbetter = False32 self.quiet = quiet33 self.progress_delta = progress_delta34 self.improvement_delta = improvement_delta35 36 def progress(self, k, n):37 """38 Report on progress.39 """40 if self.quiet: return41 t = time.time()42 p = int((100*k)//n)43 44 # Show improvements if there are any45 dt = t - self.improvement_time46 if self.isbetter and dt > self.improvement_delta:47 self.result.print_summary()48 self.isbetter = False49 self.improvement_time = t50 51 wx.PostEvent(self.parent, StatusEvent(status=\52 "%d%% complete ..."%(p),type="update"))53 54 # Update percent complete55 dp = p-self.progress_percent56 if dp < 1: return57 dt = t - self.progress_time58 if dt > self.progress_delta:59 if 1 <= dp <= 2:60 self.progress_percent = p61 self.progress_time = t62 wx.PostEvent(self.parent, StatusEvent(status=\63 "%d%% complete ..."%(p),type="update"))64 65 elif 2 < dp <= 5:66 if p//5 != self.progress_percent//5:67 wx.PostEvent(self.parent, StatusEvent(status=\68 "%d%% complete ..."%(5*(p//5)),type="update"))69 self.progress_percent = p70 self.progress_time = t71 else:72 if p//10 != self.progress_percent//10:73 self.progress_percent = p74 self.progress_time = t75 wx.PostEvent(self.parent, StatusEvent(status=\76 "%d%% complete ..."%(10*(p//10)),type="update"))77 78 def improvement(self):79 """80 Called when a result is observed which is better than previous81 results from the fit.82 """83 self.isbetter = True84 85 def error(self, msg):86 """87 Model had an error; print traceback88 """89 if self.isbetter:90 self.result.print_summary()91 print msg92 93 def finalize(self):94 if self.isbetter:95 self.result.print_summary()96 97 def abort(self):98 if self.isbetter:99 self.result.print_summary()100 4 101 5 class FitThread(CalcThread): 102 6 """Thread performing the fit """ 103 7 104 def __init__(self,parent, fn,pars=None,cpage=None, 8 def __init__(self, parent, 9 fn, 10 handler, 11 pars=None, 12 cpage=None, 105 13 completefn = None, 106 14 updatefn = None, … … 113 21 worktime) 114 22 self.parent = parent 115 self.fitter= fn 23 self.handler = handler 24 self.fitter = fn 116 25 self.cpage= cpage 117 26 self.pars = pars 118 27 self.starttime = 0 119 120 self.done= False 121 28 self.updatefn = updatefn 29 122 30 def isquit(self): 123 31 """ … … 134 42 """ 135 43 try: 136 self.starttime = time.time() 137 138 #Handler used for park engine displayed message 139 handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1) 44 self.handler.starting_fit() 45 self.updatefn() 140 46 #Result from the fit 141 result = self.fitter.fit(handler= handler, curr_thread=self) 142 143 elapsed = time.time()-self.starttime 47 result = self.fitter.fit(handler=self.handler, curr_thread=self) 48 self.updatefn() 144 49 self.complete(result= result, 145 50 pars = self.pars, 146 cpage= self.cpage, 147 elapsed=elapsed ) 51 cpage= self.cpage) 148 52 149 except KeyboardInterrupt :53 except KeyboardInterrupt, msg: 150 54 # Thread was interrupted, just proceed and re-raise. 151 55 # Real code should not print, but this is an example... 152 56 #print "keyboard exception" 153 raise 57 #Stop on exception during fitting. Todo: need to put some mssg and reset progress bar. 58 self.handler.error(msg=msg) 154 59 155 except : 156 #Stop on exception during fitting. Todo: need to put some mssg and reset progress bar. 157 msg= " Fit Error occurred... %s"%sys.exc_value 158 wx.PostEvent(self.parent, StatusEvent(status= msg,type="stop")) 60 61 159 62 160 63 -
sansview/perspectives/fitting/fitting.py
r018a0bd re54d2c32 29 29 from sans.fit.AbstractFitEngine import Model 30 30 from sans.fit.AbstractFitEngine import FitAbort 31 31 from console import ConsoleUpdate 32 32 33 33 from fitproblem import FitProblem … … 462 462 type="stop")) 463 463 return 464 465 #Do the simultaneous fit 466 try: 467 ## If a thread is already started, stop it 468 #if self.calc_fit!= None and self.calc_fit.isrunning(): 469 # self.calc_fit.stop() 470 471 wx.PostEvent(self.parent, StatusEvent(status="Start the computation", 472 curr_thread=self.calc_fit,type="start")) 473 time.sleep(0.5) 474 wx.PostEvent(self.parent, StatusEvent(status="Computing...", 475 curr_thread=self.calc_fit,type="progress")) 476 time.sleep(0.5) 477 ## perform single fit 478 if fitproblem_count == 1: 479 #qmin, qmax= self.current_pg.get_range() 480 #print "went here fitproblem_count == 1",fitproblem_count == 1 481 calc_fit=FitThread(parent =self.parent, 482 fn= self.fitter, 483 cpage=self.current_pg, 484 pars= pars, 485 completefn= self._single_fit_completed, 486 updatefn=self._updateFit) 487 488 489 else: 490 ## Perform more than 1 fit at the time 491 calc_fit=FitThread(parent =self.parent, 492 fn= self.fitter, 493 completefn= self._simul_fit_completed, 494 updatefn=self._updateFit) 495 496 calc_fit.queue() 497 self.ready_fit(calc_fit=calc_fit) 498 499 except FitAbort: 500 print "in pluging" 501 except: 502 msg= "%s error: %s" % (engineType,sys.exc_value) 503 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error", 504 type="stop")) 505 return 506 464 ## If a thread is already started, stop it 465 #if self.calc_fit!= None and self.calc_fit.isrunning(): 466 # self.calc_fit.stop() 467 #Handler used for park engine displayed message 468 handler = ConsoleUpdate(parent=self.parent,improvement_delta=0.1) 469 ## perform single fit 470 if fitproblem_count == 1: 471 calc_fit=FitThread(parent =self.parent, 472 handler = handler, 473 fn= self.fitter, 474 cpage=self.current_pg, 475 pars= pars, 476 updatefn=handler.update_fit, 477 completefn= self._single_fit_completed) 478 else: 479 ## Perform more than 1 fit at the time 480 calc_fit=FitThread(parent=self.parent, 481 handler=handler, 482 fn= self.fitter, 483 completefn= self._simul_fit_completed, 484 updatefn=handler.update_fit) 485 486 calc_fit.queue() 487 self.ready_fit(calc_fit=calc_fit) 488 507 489 def ready_fit(self, calc_fit): 508 490 """ … … 740 722 data = self.create_fittable_data2D(data=plottable) 741 723 self.add_fit_page(data=data) 742 743 def _updateFit(self):744 """745 Is called when values of result are available746 """747 ##Sending a progess message to the status bar748 wx.PostEvent(self.parent, StatusEvent(status="Computing..."))749 724 750 725 def _single_fit_completed(self,result,pars,cpage, elapsed=None):
Note: See TracChangeset
for help on using the changeset viewer.