1 | |
---|
2 | |
---|
3 | |
---|
4 | from sans.guiframe.events import StatusEvent |
---|
5 | import time |
---|
6 | import wx |
---|
7 | import park |
---|
8 | from park.fitresult import FitHandler |
---|
9 | |
---|
10 | class ConsoleUpdate(FitHandler): |
---|
11 | """ |
---|
12 | Print progress to the console. |
---|
13 | """ |
---|
14 | isbetter = False |
---|
15 | """Record whether results improved since last update""" |
---|
16 | progress_delta = 60 |
---|
17 | """Number of seconds between progress updates""" |
---|
18 | improvement_delta = 5 |
---|
19 | """Number of seconds between improvement updates""" |
---|
20 | def __init__(self, parent, manager=None, |
---|
21 | quiet=False,progress_delta=60,improvement_delta=5): |
---|
22 | """ |
---|
23 | If quiet is true, only print out final summary, not progress and |
---|
24 | improvements. |
---|
25 | |
---|
26 | :attr parent: the object that handle the messages |
---|
27 | |
---|
28 | """ |
---|
29 | self.parent= parent |
---|
30 | self.manager = manager |
---|
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 | self.elapsed_time = time.time() |
---|
39 | self.update_duration = time.time() |
---|
40 | self.fit_duration = 0 |
---|
41 | |
---|
42 | |
---|
43 | def progress(self, k, n): |
---|
44 | """ |
---|
45 | Report on progress. |
---|
46 | """ |
---|
47 | if self.quiet: return |
---|
48 | |
---|
49 | t = time.time() |
---|
50 | p = int((100*k)//n) |
---|
51 | |
---|
52 | # Show improvements if there are any |
---|
53 | dt = t - self.improvement_time |
---|
54 | if self.isbetter and dt > self.improvement_delta: |
---|
55 | #self.result.print_summary() |
---|
56 | self.update_fit() |
---|
57 | self.isbetter = False |
---|
58 | self.improvement_time = t |
---|
59 | |
---|
60 | # Update percent complete |
---|
61 | dp = p-self.progress_percent |
---|
62 | if dp < 1: return |
---|
63 | dt = t - self.progress_time |
---|
64 | if dt > self.progress_delta: |
---|
65 | if 1 <= dp <= 2: |
---|
66 | self.progress_percent = p |
---|
67 | self.progress_time = t |
---|
68 | self.update_fit() |
---|
69 | elif 2 < dp <= 5: |
---|
70 | if p//5 != self.progress_percent//5: |
---|
71 | self.progress_percent = p |
---|
72 | self.progress_time = t |
---|
73 | else: |
---|
74 | if p//10 != self.progress_percent//10: |
---|
75 | self.progress_percent = p |
---|
76 | self.progress_time = t |
---|
77 | self.update_fit() |
---|
78 | |
---|
79 | def improvement(self): |
---|
80 | """ |
---|
81 | Called when a result is observed which is better than previous |
---|
82 | results from the fit. |
---|
83 | """ |
---|
84 | self.isbetter = True |
---|
85 | |
---|
86 | def print_result(self): |
---|
87 | """ |
---|
88 | Print result object |
---|
89 | """ |
---|
90 | msg = " \n %s \n" % self.result.__str__() |
---|
91 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
92 | |
---|
93 | def error(self, msg): |
---|
94 | """ |
---|
95 | Model had an error; print traceback |
---|
96 | """ |
---|
97 | if self.isbetter: |
---|
98 | #self.result.print_summary() |
---|
99 | self.update_fit() |
---|
100 | |
---|
101 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
102 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
103 | info="error", type="stop")) |
---|
104 | def stop(self, msg): |
---|
105 | """ |
---|
106 | Post event msg and stop |
---|
107 | """ |
---|
108 | if self.isbetter: |
---|
109 | #self.result.print_summary() |
---|
110 | self.update_fit() |
---|
111 | |
---|
112 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
113 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
114 | info="info", type="stop")) |
---|
115 | |
---|
116 | def finalize(self): |
---|
117 | """ |
---|
118 | """ |
---|
119 | if self.isbetter: |
---|
120 | #self.result.print_summary() |
---|
121 | self.update_fit() |
---|
122 | |
---|
123 | def abort(self): |
---|
124 | """ |
---|
125 | """ |
---|
126 | if self.isbetter: |
---|
127 | #self.result.print_summary() |
---|
128 | self.update_fit() |
---|
129 | |
---|
130 | |
---|
131 | def update_fit(self, msg="", last=False): |
---|
132 | """ |
---|
133 | """ |
---|
134 | t1 = time.time() |
---|
135 | self.elapsed_time = t1 - self.update_duration |
---|
136 | self.update_duration = t1 |
---|
137 | self.fit_duration += self.elapsed_time |
---|
138 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
139 | UPDATE_INTERVAL = 0.5 |
---|
140 | u_flag = False |
---|
141 | if self.fit_duration >= UPDATE_INTERVAL: |
---|
142 | self.fit_duration = 0 |
---|
143 | u_flag = True |
---|
144 | if not last: |
---|
145 | msg += "Fit Updates ... %s \n" % str_time |
---|
146 | else: |
---|
147 | msg += "Final updates ........." |
---|
148 | if u_flag or last: |
---|
149 | if self.result is not None: |
---|
150 | data_name, model_name = None, None |
---|
151 | d_flag = (hasattr(self.result, "data") and \ |
---|
152 | self.result.data is not None and \ |
---|
153 | hasattr(self.result.data, "sans_data") and |
---|
154 | self.result.data.sans_data is not None) |
---|
155 | m_flag = (hasattr(self.result, "model") and \ |
---|
156 | self.result.model is not None) |
---|
157 | if d_flag: |
---|
158 | data_name = self.result.data.sans_data.name |
---|
159 | if m_flag: |
---|
160 | model_name = str(self.result.model.name) |
---|
161 | if m_flag and d_flag: |
---|
162 | msg += "Data : %s \n" % (str(data_name))#, |
---|
163 | #str(model_name)) |
---|
164 | msg += str(self.result) |
---|
165 | msg += "\n" |
---|
166 | if not last: |
---|
167 | msg += "About %s s elapsed......... \n" % \ |
---|
168 | str (UPDATE_INTERVAL) |
---|
169 | else: |
---|
170 | msg += "No result available\n" |
---|
171 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
172 | type="progress")) |
---|
173 | |
---|
174 | def starting_fit(self): |
---|
175 | """ |
---|
176 | """ |
---|
177 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
178 | info="info",type="progress")) |
---|
179 | |
---|
180 | def set_result(self, result): |
---|
181 | """ |
---|
182 | """ |
---|
183 | self.result = result |
---|
184 | |
---|
185 | def get_result(self): |
---|
186 | """ |
---|
187 | """ |
---|
188 | return self.result |
---|
189 | |
---|
190 | |
---|