source: sasview/sansview/perspectives/fitting/fit_thread.py @ 018a0bd

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 018a0bd was 25e3fc9, checked in by Gervaise Alina <gervyh@…>, 15 years ago

remove raise and pritn statement

  • Property mode set to 100644
File size: 5.2 KB
RevLine 
[e9b4cc4]1
2import sys, wx, logging
3import string, numpy, math
4from sans.guicomm.events import NewPlotEvent, StatusEvent 
[e733619]5from data_util.calcthread import CalcThread
[e9b4cc4]6import park
7from park.fitresult import FitHandler
8DEFAULT_BEAM = 0.005
9import time
[98be89f]10import thread
[e9b4cc4]11 
12class ConsoleUpdate(FitHandler):
13    """
14    Print progress to the console.
15    """
16    isbetter = False
17    """Record whether results improved since last update"""
18    progress_delta =  60
19    """Number of seconds between progress updates"""
20    improvement_delta = 5
21    """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 and
25        improvements.
26        """
27        self.parent= parent
28        self.progress_time = time.time()
29        self.progress_percent = 0
30        self.improvement_time = self.progress_time
31        self.isbetter = False
32        self.quiet = quiet
33        self.progress_delta = progress_delta
34        self.improvement_delta = improvement_delta
35
36    def progress(self, k, n):
37        """
38        Report on progress.
39        """
40        if self.quiet: return
41        t = time.time()
42        p = int((100*k)//n)
43       
44        # Show improvements if there are any
45        dt = t - self.improvement_time
46        if self.isbetter and dt > self.improvement_delta:
47            self.result.print_summary()
48            self.isbetter = False
49            self.improvement_time = t
50           
51            wx.PostEvent(self.parent, StatusEvent(status=\
[ed2ea6a]52             "%d%% complete ..."%(p),type="update"))
[e9b4cc4]53       
54        # Update percent complete
55        dp = p-self.progress_percent
56        if dp < 1: return
57        dt = t - self.progress_time
58        if dt > self.progress_delta:
59            if 1 <= dp <= 2:
60                self.progress_percent = p
61                self.progress_time = t
62                wx.PostEvent(self.parent, StatusEvent(status=\
[ed2ea6a]63                                                      "%d%% complete ..."%(p),type="update"))
[e9b4cc4]64       
65            elif 2 < dp <= 5:
66                if p//5 != self.progress_percent//5:
67                    wx.PostEvent(self.parent, StatusEvent(status=\
[ed2ea6a]68                       "%d%% complete ..."%(5*(p//5)),type="update"))
[e9b4cc4]69                    self.progress_percent = p
70                    self.progress_time = t
71            else:
72                if p//10 != self.progress_percent//10:
73                    self.progress_percent = p
74                    self.progress_time = t
75                    wx.PostEvent(self.parent, StatusEvent(status=\
[ed2ea6a]76                   "%d%% complete ..."%(10*(p//10)),type="update"))
[e9b4cc4]77       
78    def improvement(self):
79        """
80        Called when a result is observed which is better than previous
81        results from the fit.
82        """
83        self.isbetter = True
84   
85    def error(self, msg):
86        """
87        Model had an error; print traceback
88        """
89        if self.isbetter:
90            self.result.print_summary()
91        print msg
92
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
101class FitThread(CalcThread):
[ed2ea6a]102    """Thread performing the fit """
[e9b4cc4]103   
[ca7a626]104    def __init__(self,parent, fn,pars=None,cpage=None,
[e9b4cc4]105                 completefn = None,
106                 updatefn   = None,
107                 yieldtime  = 0.01,
108                 worktime   = 0.01
109                 ):
110        CalcThread.__init__(self,completefn,
111                 updatefn,
112                 yieldtime,
113                 worktime)
114        self.parent = parent
115        self.fitter= fn
116        self.cpage= cpage
117        self.pars = pars
118        self.starttime = 0
[c77d859]119       
[e9b4cc4]120        self.done= False
[1c1436d]121       
[e9b4cc4]122    def isquit(self):
[ed2ea6a]123        """
124             @raise KeyboardInterrupt: when the thread is interrupted
125        """
[e9b4cc4]126        try:
127            CalcThread.isquit(self)
128        except KeyboardInterrupt:
129            raise KeyboardInterrupt
130       
131    def compute(self):
[ed2ea6a]132        """
133            Perform a fit
134        """
[e9b4cc4]135        try: 
136            self.starttime = time.time()
[1c1436d]137       
[ed2ea6a]138            #Handler used for park engine displayed message
[e9b4cc4]139            handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1)
[ed2ea6a]140            #Result from the fit
[ad6dd4c]141            result = self.fitter.fit(handler= handler, curr_thread=self)
[ed2ea6a]142       
[e9b4cc4]143            elapsed = time.time()-self.starttime
144            self.complete(result= result,
145                          pars = self.pars,
146                          cpage= self.cpage,
147                          elapsed=elapsed )
[785c8233]148           
[e9b4cc4]149        except KeyboardInterrupt:
150            # Thread was interrupted, just proceed and re-raise.
151            # Real code should not print, but this is an example...
[ad6dd4c]152            #print "keyboard exception"
153            raise
154       
155        except :
[a6a7e8a]156            #Stop on exception during fitting. Todo: need to put some mssg and reset progress bar.
[25e3fc9]157            msg= " Fit Error occurred... %s"%sys.exc_value
158            wx.PostEvent(self.parent, StatusEvent(status= msg,type="stop"))
159           
[e9b4cc4]160   
Note: See TracBrowser for help on using the repository browser.