[e9b4cc4] | 1 | |
---|
| 2 | import sys, wx, logging |
---|
| 3 | import string, numpy, math |
---|
| 4 | from sans.guicomm.events import NewPlotEvent, StatusEvent |
---|
| 5 | from calcthread import CalcThread |
---|
| 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, |
---|
| 112 | completefn = None, |
---|
| 113 | updatefn = None, |
---|
| 114 | yieldtime = 0.01, |
---|
| 115 | worktime = 0.01 |
---|
| 116 | ): |
---|
| 117 | CalcThread.__init__(self,completefn, |
---|
| 118 | updatefn, |
---|
| 119 | yieldtime, |
---|
| 120 | worktime) |
---|
| 121 | self.parent = parent |
---|
| 122 | self.fitter= fn |
---|
| 123 | self.cpage= cpage |
---|
| 124 | self.pars = pars |
---|
| 125 | self.starttime = 0 |
---|
| 126 | self.qmin = qmin |
---|
| 127 | self.qmax = qmax |
---|
| 128 | self.ymin = ymin |
---|
| 129 | self.ymax = ymax |
---|
| 130 | self.done= False |
---|
| 131 | def isquit(self): |
---|
| 132 | try: |
---|
| 133 | CalcThread.isquit(self) |
---|
| 134 | except KeyboardInterrupt: |
---|
| 135 | #printEVT("Calc %s interrupted" % self.model.name) |
---|
| 136 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 137 | "Calc %g interrupted" % time.time())) |
---|
| 138 | |
---|
| 139 | raise KeyboardInterrupt |
---|
| 140 | |
---|
| 141 | def update(self): |
---|
| 142 | print "updatting" |
---|
| 143 | timen= time.time() |
---|
| 144 | wx.PostEvent(self.parent, StatusEvent(status="Computing \ |
---|
| 145 | ... %g sec" % timen)) |
---|
| 146 | |
---|
| 147 | def compute(self): |
---|
| 148 | try: |
---|
| 149 | self.starttime = time.time() |
---|
| 150 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 151 | "Start the computation %g " % self.starttime)) |
---|
| 152 | handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1) |
---|
| 153 | result = self.fitter.fit(handler= handler) |
---|
| 154 | print "result", result |
---|
| 155 | |
---|
| 156 | print "in my compute function" |
---|
| 157 | elapsed = time.time()-self.starttime |
---|
| 158 | self.complete(result= result, |
---|
| 159 | pars = self.pars, |
---|
| 160 | cpage= self.cpage, |
---|
| 161 | qmin = self.qmin, |
---|
| 162 | qmax = self.qmax, |
---|
| 163 | ymin = self.ymin, |
---|
| 164 | ymax = self.ymax, |
---|
| 165 | elapsed=elapsed ) |
---|
| 166 | except KeyboardInterrupt: |
---|
| 167 | # Thread was interrupted, just proceed and re-raise. |
---|
| 168 | # Real code should not print, but this is an example... |
---|
| 169 | print "Thread stopped. " |
---|
| 170 | raise |
---|
| 171 | except: |
---|
| 172 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
| 173 | return |
---|
| 174 | |
---|