[e54d2c32] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[4ce74917] | 4 | from sans.guiframe.events import StatusEvent |
---|
[e54d2c32] | 5 | import time |
---|
| 6 | import wx |
---|
| 7 | import park |
---|
| 8 | from park.fitresult import FitHandler |
---|
[5011193] | 9 | |
---|
[e54d2c32] | 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""" |
---|
[66ff250] | 20 | def __init__(self, parent, manager=None, |
---|
| 21 | quiet=False,progress_delta=60,improvement_delta=5): |
---|
[e54d2c32] | 22 | """ |
---|
| 23 | If quiet is true, only print out final summary, not progress and |
---|
| 24 | improvements. |
---|
[5062bbf] | 25 | |
---|
| 26 | :attr parent: the object that handle the messages |
---|
| 27 | |
---|
[e54d2c32] | 28 | """ |
---|
| 29 | self.parent= parent |
---|
[66ff250] | 30 | self.manager = manager |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 38 | self.elapsed_time = time.time() |
---|
| 39 | self.update_duration = time.time() |
---|
| 40 | self.fit_duration = 0 |
---|
| 41 | |
---|
[e54d2c32] | 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: |
---|
[2bb37c3] | 55 | #self.result.print_summary() |
---|
| 56 | self.update_fit() |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 68 | self.update_fit() |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 77 | self.update_fit() |
---|
[e54d2c32] | 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 | |
---|
[2bb37c3] | 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 | |
---|
[e54d2c32] | 93 | def error(self, msg): |
---|
| 94 | """ |
---|
| 95 | Model had an error; print traceback |
---|
| 96 | """ |
---|
| 97 | if self.isbetter: |
---|
[2bb37c3] | 98 | #self.result.print_summary() |
---|
| 99 | self.update_fit() |
---|
| 100 | |
---|
| 101 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
[8b481a51] | 102 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 103 | info="error", type="stop")) |
---|
[986da97] | 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 | |
---|
[e54d2c32] | 116 | def finalize(self): |
---|
[5062bbf] | 117 | """ |
---|
| 118 | """ |
---|
[e54d2c32] | 119 | if self.isbetter: |
---|
[2bb37c3] | 120 | #self.result.print_summary() |
---|
| 121 | self.update_fit() |
---|
[e54d2c32] | 122 | |
---|
| 123 | def abort(self): |
---|
[5062bbf] | 124 | """ |
---|
| 125 | """ |
---|
[e54d2c32] | 126 | if self.isbetter: |
---|
[2bb37c3] | 127 | #self.result.print_summary() |
---|
| 128 | self.update_fit() |
---|
[e54d2c32] | 129 | |
---|
[66ff250] | 130 | |
---|
[2bb37c3] | 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: |
---|
[afac721] | 145 | msg += "Fit Updates ... %s \n" % str_time |
---|
[2bb37c3] | 146 | else: |
---|
[afac721] | 147 | msg += "Final updates ........." |
---|
[2bb37c3] | 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: |
---|
[9c9a224] | 162 | msg += "Data : %s \n" % (str(data_name))#, |
---|
| 163 | #str(model_name)) |
---|
[2bb37c3] | 164 | msg += str(self.result) |
---|
| 165 | msg += "\n" |
---|
[9c9a224] | 166 | if not last: |
---|
| 167 | msg += "About %s s elapsed......... \n" % \ |
---|
| 168 | str (UPDATE_INTERVAL) |
---|
[2bb37c3] | 169 | else: |
---|
| 170 | msg += "No result available\n" |
---|
[ccb0f39] | 171 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
[2296316] | 172 | type="progress")) |
---|
[2bb37c3] | 173 | |
---|
[e54d2c32] | 174 | def starting_fit(self): |
---|
[5062bbf] | 175 | """ |
---|
| 176 | """ |
---|
[8b481a51] | 177 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 178 | info="info",type="progress")) |
---|
[e54d2c32] | 179 | |
---|
| 180 | def set_result(self, result): |
---|
[5062bbf] | 181 | """ |
---|
| 182 | """ |
---|
[e54d2c32] | 183 | self.result = result |
---|
[66ff250] | 184 | |
---|
| 185 | def get_result(self): |
---|
| 186 | """ |
---|
| 187 | """ |
---|
| 188 | return self.result |
---|
[e54d2c32] | 189 | |
---|
| 190 | |
---|