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