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 | xmin=None,xmax=None, |
---|
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 |
---|
129 | self.xmin = xmin |
---|
130 | self.xmax = xmax |
---|
131 | self.ymin = ymin |
---|
132 | self.ymax = ymax |
---|
133 | self.done= False |
---|
134 | def isquit(self): |
---|
135 | try: |
---|
136 | CalcThread.isquit(self) |
---|
137 | except KeyboardInterrupt: |
---|
138 | #printEVT("Calc %s interrupted" % self.model.name) |
---|
139 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
140 | "Calc %g interrupted" % time.time())) |
---|
141 | |
---|
142 | raise KeyboardInterrupt |
---|
143 | |
---|
144 | def update(self): |
---|
145 | print "updatting" |
---|
146 | timen= time.time() |
---|
147 | wx.PostEvent(self.parent, StatusEvent(status="Computing \ |
---|
148 | ... %g sec" % timen)) |
---|
149 | |
---|
150 | def compute(self): |
---|
151 | try: |
---|
152 | self.starttime = time.time() |
---|
153 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
154 | "Start the computation %g " % self.starttime)) |
---|
155 | handler= ConsoleUpdate(parent= self.parent,improvement_delta=0.1) |
---|
156 | result = self.fitter.fit(handler= handler) |
---|
157 | print "result", result |
---|
158 | |
---|
159 | print "in my compute function" |
---|
160 | elapsed = time.time()-self.starttime |
---|
161 | self.complete(result= result, |
---|
162 | pars = self.pars, |
---|
163 | cpage= self.cpage, |
---|
164 | qmin = self.qmin, |
---|
165 | qmax = self.qmax, |
---|
166 | xmin= self.xmin, |
---|
167 | xmax= self.xmax, |
---|
168 | ymin = self.ymin, |
---|
169 | ymax = self.ymax, |
---|
170 | elapsed=elapsed ) |
---|
171 | except KeyboardInterrupt: |
---|
172 | # Thread was interrupted, just proceed and re-raise. |
---|
173 | # Real code should not print, but this is an example... |
---|
174 | print "Thread stopped. " |
---|
175 | raise |
---|
176 | except: |
---|
177 | raise |
---|
178 | #wx.PostEvent(self.parent, StatusEvent(status="Fitting error: %s" % sys.exc_value)) |
---|
179 | return |
---|
180 | |
---|