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 = self.progress_time |
---|
39 | |
---|
40 | def progress(self, k, n): |
---|
41 | """ |
---|
42 | Report on progress. |
---|
43 | """ |
---|
44 | if self.quiet: return |
---|
45 | |
---|
46 | t = time.time() |
---|
47 | p = int((100*k)//n) |
---|
48 | |
---|
49 | # Show improvements if there are any |
---|
50 | dt = t - self.improvement_time |
---|
51 | if self.isbetter and dt > self.improvement_delta: |
---|
52 | self.result.print_summary() |
---|
53 | self.isbetter = False |
---|
54 | self.improvement_time = t |
---|
55 | |
---|
56 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
57 | "%d%% complete ..."%(p),type="progress")) |
---|
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 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
68 | "%d%% complete ..."%(p), |
---|
69 | type="progress")) |
---|
70 | |
---|
71 | elif 2 < dp <= 5: |
---|
72 | if p//5 != self.progress_percent//5: |
---|
73 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
74 | "%d%% complete ..."%(5*(p//5)),type="progress")) |
---|
75 | self.progress_percent = p |
---|
76 | self.progress_time = t |
---|
77 | else: |
---|
78 | if p//10 != self.progress_percent//10: |
---|
79 | self.progress_percent = p |
---|
80 | self.progress_time = t |
---|
81 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
82 | "%d%% complete ..."%(10*(p//10)),type="progress")) |
---|
83 | |
---|
84 | def improvement(self): |
---|
85 | """ |
---|
86 | Called when a result is observed which is better than previous |
---|
87 | results from the fit. |
---|
88 | """ |
---|
89 | self.isbetter = True |
---|
90 | |
---|
91 | def error(self, msg): |
---|
92 | """ |
---|
93 | Model had an error; print traceback |
---|
94 | """ |
---|
95 | if self.isbetter: |
---|
96 | self.result.print_summary() |
---|
97 | message = "fit Error" |
---|
98 | message = str(msg)+ " \n %s"%self.result.__str__() |
---|
99 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
100 | info="error", type="stop")) |
---|
101 | |
---|
102 | def finalize(self): |
---|
103 | """ |
---|
104 | """ |
---|
105 | if self.isbetter: |
---|
106 | self.result.print_summary() |
---|
107 | |
---|
108 | def abort(self): |
---|
109 | """ |
---|
110 | """ |
---|
111 | if self.isbetter: |
---|
112 | self.result.print_summary() |
---|
113 | |
---|
114 | |
---|
115 | def update_fit(self, msg=""): |
---|
116 | """ |
---|
117 | """ |
---|
118 | self.elapsed_time = time.time() - self.elapsed_time |
---|
119 | msg = " Updating fit ...\n result:\n %s \n"%self.result.__str__() |
---|
120 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
121 | type="progress")) |
---|
122 | time.sleep(0.01) |
---|
123 | |
---|
124 | def starting_fit(self): |
---|
125 | """ |
---|
126 | """ |
---|
127 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
128 | info="info",type="progress")) |
---|
129 | |
---|
130 | def set_result(self, result): |
---|
131 | """ |
---|
132 | """ |
---|
133 | self.result = result |
---|
134 | |
---|
135 | def get_result(self): |
---|
136 | """ |
---|
137 | """ |
---|
138 | return self.result |
---|
139 | |
---|
140 | |
---|