[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 |
---|
[98be89f] | 10 | import thread |
---|
[e9b4cc4] | 11 | |
---|
| 12 | class 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 | |
---|
| 101 | class FitThread(CalcThread): |
---|
[ed2ea6a] | 102 | """Thread performing the fit """ |
---|
[e9b4cc4] | 103 | |
---|
| 104 | def __init__(self,parent, fn,pars=None,cpage=None, qmin=None,qmax=None,ymin=None, ymax=None, |
---|
[27561b6] | 105 | xmin=None,xmax=None, |
---|
[e9b4cc4] | 106 | completefn = None, |
---|
| 107 | updatefn = None, |
---|
| 108 | yieldtime = 0.01, |
---|
| 109 | worktime = 0.01 |
---|
| 110 | ): |
---|
| 111 | CalcThread.__init__(self,completefn, |
---|
| 112 | updatefn, |
---|
| 113 | yieldtime, |
---|
| 114 | worktime) |
---|
| 115 | self.parent = parent |
---|
| 116 | self.fitter= fn |
---|
| 117 | self.cpage= cpage |
---|
| 118 | self.pars = pars |
---|
| 119 | self.starttime = 0 |
---|
| 120 | self.qmin = qmin |
---|
| 121 | self.qmax = qmax |
---|
[27561b6] | 122 | self.xmin = xmin |
---|
| 123 | self.xmax = xmax |
---|
[e9b4cc4] | 124 | self.ymin = ymin |
---|
| 125 | self.ymax = ymax |
---|
| 126 | self.done= False |
---|
[f343069] | 127 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
[202a7dc3] | 128 | "Start the computation ",curr_thread=self,type="start")) |
---|
[e9b4cc4] | 129 | def isquit(self): |
---|
[ed2ea6a] | 130 | """ |
---|
| 131 | @raise KeyboardInterrupt: when the thread is interrupted |
---|
| 132 | """ |
---|
[e9b4cc4] | 133 | try: |
---|
| 134 | CalcThread.isquit(self) |
---|
| 135 | except KeyboardInterrupt: |
---|
| 136 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
[ed2ea6a] | 137 | "Calc %g interrupted")) |
---|
[e9b4cc4] | 138 | raise KeyboardInterrupt |
---|
| 139 | |
---|
| 140 | def update(self): |
---|
[ed2ea6a] | 141 | """ |
---|
| 142 | Is called when values of result are available |
---|
| 143 | """ |
---|
[e9b4cc4] | 144 | wx.PostEvent(self.parent, StatusEvent(status="Computing \ |
---|
[202a7dc3] | 145 | ... " ,curr_thread=self,type="update")) |
---|
[e9b4cc4] | 146 | |
---|
| 147 | def compute(self): |
---|
[ed2ea6a] | 148 | """ |
---|
| 149 | Perform a fit |
---|
| 150 | """ |
---|
[e9b4cc4] | 151 | try: |
---|
| 152 | self.starttime = time.time() |
---|
[ed2ea6a] | 153 | #Sending a progess message to the status bar |
---|
[e9b4cc4] | 154 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
[3215d32] | 155 | "Computing . ...",curr_thread=self,type="progress")) |
---|
[ed2ea6a] | 156 | #Handler used for park engine displayed message |
---|
[e9b4cc4] | 157 | handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1) |
---|
[ed2ea6a] | 158 | #Result from the fit |
---|
[e9b4cc4] | 159 | result = self.fitter.fit(handler= handler) |
---|
[ed2ea6a] | 160 | |
---|
[e9b4cc4] | 161 | elapsed = time.time()-self.starttime |
---|
| 162 | self.complete(result= result, |
---|
| 163 | pars = self.pars, |
---|
| 164 | cpage= self.cpage, |
---|
| 165 | qmin = self.qmin, |
---|
| 166 | qmax = self.qmax, |
---|
[27561b6] | 167 | xmin= self.xmin, |
---|
| 168 | xmax= self.xmax, |
---|
[e9b4cc4] | 169 | ymin = self.ymin, |
---|
| 170 | ymax = self.ymax, |
---|
| 171 | elapsed=elapsed ) |
---|
| 172 | except KeyboardInterrupt: |
---|
| 173 | # Thread was interrupted, just proceed and re-raise. |
---|
| 174 | # Real code should not print, but this is an example... |
---|
| 175 | raise |
---|
| 176 | except: |
---|
[c138160] | 177 | wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
[e9b4cc4] | 178 | return |
---|
| 179 | |
---|