[e9b4cc4] | 1 | |
---|
| 2 | import sys, wx, logging |
---|
| 3 | import string, numpy, math |
---|
| 4 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
[39c8c4e] | 5 | from sans.guiframe.calcthread import CalcThread |
---|
[e9b4cc4] | 6 | import park |
---|
| 7 | from park.fitresult import FitHandler |
---|
| 8 | DEFAULT_BEAM = 0.005 |
---|
| 9 | import time |
---|
| 10 | |
---|
| 11 | import thread |
---|
| 12 | print "main",thread.get_ident() |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | class ConsoleUpdate(FitHandler): |
---|
| 16 | """ |
---|
| 17 | Print progress to the console. |
---|
| 18 | """ |
---|
| 19 | isbetter = False |
---|
| 20 | """Record whether results improved since last update""" |
---|
| 21 | progress_delta = 60 |
---|
| 22 | """Number of seconds between progress updates""" |
---|
| 23 | improvement_delta = 5 |
---|
| 24 | """Number of seconds between improvement updates""" |
---|
| 25 | def __init__(self,parent, quiet=False,progress_delta=60,improvement_delta=5): |
---|
| 26 | """ |
---|
| 27 | If quiet is true, only print out final summary, not progress and |
---|
| 28 | improvements. |
---|
| 29 | """ |
---|
| 30 | self.parent= parent |
---|
| 31 | self.progress_time = time.time() |
---|
| 32 | self.progress_percent = 0 |
---|
| 33 | self.improvement_time = self.progress_time |
---|
| 34 | self.isbetter = False |
---|
| 35 | self.quiet = quiet |
---|
| 36 | self.progress_delta = progress_delta |
---|
| 37 | self.improvement_delta = improvement_delta |
---|
| 38 | |
---|
| 39 | def progress(self, k, n): |
---|
| 40 | """ |
---|
| 41 | Report on progress. |
---|
| 42 | """ |
---|
| 43 | if self.quiet: return |
---|
| 44 | t = time.time() |
---|
| 45 | p = int((100*k)//n) |
---|
| 46 | |
---|
| 47 | # Show improvements if there are any |
---|
| 48 | dt = t - self.improvement_time |
---|
| 49 | if self.isbetter and dt > self.improvement_delta: |
---|
| 50 | self.result.print_summary() |
---|
| 51 | self.isbetter = False |
---|
| 52 | self.improvement_time = t |
---|
| 53 | |
---|
| 54 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 55 | "%d%% complete ..."%(p))) |
---|
| 56 | |
---|
| 57 | # Update percent complete |
---|
| 58 | dp = p-self.progress_percent |
---|
| 59 | if dp < 1: return |
---|
| 60 | dt = t - self.progress_time |
---|
| 61 | if dt > self.progress_delta: |
---|
| 62 | if 1 <= dp <= 2: |
---|
| 63 | print "%d%% complete"%p |
---|
| 64 | self.progress_percent = p |
---|
| 65 | self.progress_time = t |
---|
| 66 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 67 | "%d%% complete ..."%(p))) |
---|
| 68 | |
---|
| 69 | elif 2 < dp <= 5: |
---|
| 70 | if p//5 != self.progress_percent//5: |
---|
| 71 | print "%d%% complete"%(5*(p//5)) |
---|
| 72 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 73 | "%d%% complete ..."%(5*(p//5)))) |
---|
| 74 | self.progress_percent = p |
---|
| 75 | self.progress_time = t |
---|
| 76 | else: |
---|
| 77 | if p//10 != self.progress_percent//10: |
---|
| 78 | print "%d%% complete"%(10*(p//10)) |
---|
| 79 | |
---|
| 80 | self.progress_percent = p |
---|
| 81 | self.progress_time = t |
---|
| 82 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 83 | "%d%% complete ..."%(10*(p//10)))) |
---|
| 84 | |
---|
| 85 | def improvement(self): |
---|
| 86 | """ |
---|
| 87 | Called when a result is observed which is better than previous |
---|
| 88 | results from the fit. |
---|
| 89 | """ |
---|
| 90 | self.isbetter = True |
---|
| 91 | |
---|
| 92 | def error(self, msg): |
---|
| 93 | """ |
---|
| 94 | Model had an error; print traceback |
---|
| 95 | """ |
---|
| 96 | if self.isbetter: |
---|
| 97 | self.result.print_summary() |
---|
| 98 | print msg |
---|
| 99 | |
---|
| 100 | def finalize(self): |
---|
| 101 | if self.isbetter: |
---|
| 102 | self.result.print_summary() |
---|
| 103 | |
---|
| 104 | def abort(self): |
---|
| 105 | if self.isbetter: |
---|
| 106 | self.result.print_summary() |
---|
| 107 | |
---|
| 108 | class FitThread(CalcThread): |
---|
| 109 | """Compute 2D data""" |
---|
| 110 | |
---|
| 111 | def __init__(self,parent, fn,pars=None,cpage=None, qmin=None,qmax=None,ymin=None, ymax=None, |
---|
[27561b6] | 112 | xmin=None,xmax=None, |
---|
[e9b4cc4] | 113 | completefn = None, |
---|
| 114 | updatefn = None, |
---|
| 115 | yieldtime = 0.01, |
---|
| 116 | worktime = 0.01 |
---|
| 117 | ): |
---|
| 118 | CalcThread.__init__(self,completefn, |
---|
| 119 | updatefn, |
---|
| 120 | yieldtime, |
---|
| 121 | worktime) |
---|
| 122 | self.parent = parent |
---|
| 123 | self.fitter= fn |
---|
| 124 | self.cpage= cpage |
---|
| 125 | self.pars = pars |
---|
| 126 | self.starttime = 0 |
---|
| 127 | self.qmin = qmin |
---|
| 128 | self.qmax = qmax |
---|
[27561b6] | 129 | self.xmin = xmin |
---|
| 130 | self.xmax = xmax |
---|
[e9b4cc4] | 131 | self.ymin = ymin |
---|
| 132 | self.ymax = ymax |
---|
| 133 | self.done= False |
---|
[f343069] | 134 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
[202a7dc3] | 135 | "Start the computation ",curr_thread=self,type="start")) |
---|
[e9b4cc4] | 136 | def isquit(self): |
---|
| 137 | try: |
---|
| 138 | CalcThread.isquit(self) |
---|
| 139 | except KeyboardInterrupt: |
---|
| 140 | #printEVT("Calc %s interrupted" % self.model.name) |
---|
| 141 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 142 | "Calc %g interrupted" % time.time())) |
---|
| 143 | |
---|
| 144 | raise KeyboardInterrupt |
---|
| 145 | |
---|
| 146 | def update(self): |
---|
| 147 | print "updatting" |
---|
[f343069] | 148 | elapsed = time.time()-self.starttime |
---|
[e9b4cc4] | 149 | wx.PostEvent(self.parent, StatusEvent(status="Computing \ |
---|
[202a7dc3] | 150 | ... " ,curr_thread=self,type="update")) |
---|
[e9b4cc4] | 151 | |
---|
| 152 | def compute(self): |
---|
| 153 | try: |
---|
| 154 | self.starttime = time.time() |
---|
| 155 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
[3215d32] | 156 | "Computing . ...",curr_thread=self,type="progress")) |
---|
[e9b4cc4] | 157 | handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1) |
---|
| 158 | result = self.fitter.fit(handler= handler) |
---|
| 159 | print "result", result |
---|
| 160 | |
---|
| 161 | print "in my compute function" |
---|
| 162 | elapsed = time.time()-self.starttime |
---|
| 163 | self.complete(result= result, |
---|
| 164 | pars = self.pars, |
---|
| 165 | cpage= self.cpage, |
---|
| 166 | qmin = self.qmin, |
---|
| 167 | qmax = self.qmax, |
---|
[27561b6] | 168 | xmin= self.xmin, |
---|
| 169 | xmax= self.xmax, |
---|
[e9b4cc4] | 170 | ymin = self.ymin, |
---|
| 171 | ymax = self.ymax, |
---|
| 172 | elapsed=elapsed ) |
---|
| 173 | except KeyboardInterrupt: |
---|
| 174 | # Thread was interrupted, just proceed and re-raise. |
---|
| 175 | # Real code should not print, but this is an example... |
---|
| 176 | print "Thread stopped. " |
---|
| 177 | raise |
---|
| 178 | except: |
---|
[c138160] | 179 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
[e9b4cc4] | 180 | return |
---|
| 181 | |
---|