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 finalize(self): |
---|
105 | """ |
---|
106 | """ |
---|
107 | if self.isbetter: |
---|
108 | #self.result.print_summary() |
---|
109 | self.update_fit() |
---|
110 | |
---|
111 | def abort(self): |
---|
112 | """ |
---|
113 | """ |
---|
114 | if self.isbetter: |
---|
115 | #self.result.print_summary() |
---|
116 | self.update_fit() |
---|
117 | |
---|
118 | |
---|
119 | def update_fit(self, msg="", last=False): |
---|
120 | """ |
---|
121 | """ |
---|
122 | t1 = time.time() |
---|
123 | self.elapsed_time = t1 - self.update_duration |
---|
124 | self.update_duration = t1 |
---|
125 | self.fit_duration += self.elapsed_time |
---|
126 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
127 | UPDATE_INTERVAL = 0.5 |
---|
128 | u_flag = False |
---|
129 | if self.fit_duration >= UPDATE_INTERVAL: |
---|
130 | self.fit_duration = 0 |
---|
131 | u_flag = True |
---|
132 | if not last: |
---|
133 | msg += "Fit Updates ... %s \n" % str_time |
---|
134 | else: |
---|
135 | msg += "Final updates ........." |
---|
136 | if u_flag or last: |
---|
137 | if self.result is not None: |
---|
138 | data_name, model_name = None, None |
---|
139 | d_flag = (hasattr(self.result, "data") and \ |
---|
140 | self.result.data is not None and \ |
---|
141 | hasattr(self.result.data, "sans_data") and |
---|
142 | self.result.data.sans_data is not None) |
---|
143 | m_flag = (hasattr(self.result, "model") and \ |
---|
144 | self.result.model is not None) |
---|
145 | if d_flag: |
---|
146 | data_name = self.result.data.sans_data.name |
---|
147 | if m_flag: |
---|
148 | model_name = str(self.result.model.name) |
---|
149 | if m_flag and d_flag: |
---|
150 | msg += "Data : %s \n" % (str(data_name))#, |
---|
151 | #str(model_name)) |
---|
152 | msg += str(self.result) |
---|
153 | msg += "\n" |
---|
154 | if not last: |
---|
155 | msg += "About %s s elapsed......... \n" % \ |
---|
156 | str (UPDATE_INTERVAL) |
---|
157 | else: |
---|
158 | msg += "No result available\n" |
---|
159 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
160 | type="progress")) |
---|
161 | |
---|
162 | def starting_fit(self): |
---|
163 | """ |
---|
164 | """ |
---|
165 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
166 | info="info",type="progress")) |
---|
167 | |
---|
168 | def set_result(self, result): |
---|
169 | """ |
---|
170 | """ |
---|
171 | self.result = result |
---|
172 | |
---|
173 | def get_result(self): |
---|
174 | """ |
---|
175 | """ |
---|
176 | return self.result |
---|
177 | |
---|
178 | |
---|