[e54d2c32] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[d85c194] | 4 | from sas.sasgui.guiframe.events import StatusEvent |
---|
[e54d2c32] | 5 | import time |
---|
| 6 | import wx |
---|
[b699768] | 7 | from sas.sascalc.fit import FitHandler |
---|
[5011193] | 8 | |
---|
[e54d2c32] | 9 | class ConsoleUpdate(FitHandler): |
---|
| 10 | """ |
---|
| 11 | Print progress to the console. |
---|
| 12 | """ |
---|
| 13 | isbetter = False |
---|
| 14 | """Record whether results improved since last update""" |
---|
[2f4b430] | 15 | progress_delta = 60 |
---|
[e54d2c32] | 16 | """Number of seconds between progress updates""" |
---|
| 17 | improvement_delta = 5 |
---|
| 18 | """Number of seconds between improvement updates""" |
---|
[66ff250] | 19 | def __init__(self, parent, manager=None, |
---|
[2f4b430] | 20 | quiet=False, progress_delta=60, improvement_delta=5): |
---|
[e54d2c32] | 21 | """ |
---|
| 22 | If quiet is true, only print out final summary, not progress and |
---|
| 23 | improvements. |
---|
[2f4b430] | 24 | |
---|
[5062bbf] | 25 | :attr parent: the object that handle the messages |
---|
[2f4b430] | 26 | |
---|
[e54d2c32] | 27 | """ |
---|
[2f4b430] | 28 | self.parent = parent |
---|
[66ff250] | 29 | self.manager = manager |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 37 | self.elapsed_time = time.time() |
---|
| 38 | self.update_duration = time.time() |
---|
| 39 | self.fit_duration = 0 |
---|
[2f4b430] | 40 | |
---|
| 41 | |
---|
[e54d2c32] | 42 | def progress(self, k, n): |
---|
| 43 | """ |
---|
| 44 | Report on progress. |
---|
| 45 | """ |
---|
| 46 | if self.quiet: return |
---|
| 47 | |
---|
| 48 | t = time.time() |
---|
[2f4b430] | 49 | p = int((100 * k) // n) |
---|
| 50 | |
---|
[e54d2c32] | 51 | # Show improvements if there are any |
---|
| 52 | dt = t - self.improvement_time |
---|
| 53 | if self.isbetter and dt > self.improvement_delta: |
---|
[2bb37c3] | 54 | #self.result.print_summary() |
---|
| 55 | self.update_fit() |
---|
[e54d2c32] | 56 | self.isbetter = False |
---|
| 57 | self.improvement_time = t |
---|
[2f4b430] | 58 | |
---|
[e54d2c32] | 59 | # Update percent complete |
---|
[2f4b430] | 60 | dp = p - self.progress_percent |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 67 | self.update_fit() |
---|
[e54d2c32] | 68 | elif 2 < dp <= 5: |
---|
[2f4b430] | 69 | if p // 5 != self.progress_percent // 5: |
---|
[e54d2c32] | 70 | self.progress_percent = p |
---|
| 71 | self.progress_time = t |
---|
| 72 | else: |
---|
[2f4b430] | 73 | if p // 10 != self.progress_percent // 10: |
---|
[e54d2c32] | 74 | self.progress_percent = p |
---|
| 75 | self.progress_time = t |
---|
[2bb37c3] | 76 | self.update_fit() |
---|
[2f4b430] | 77 | |
---|
[e54d2c32] | 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 |
---|
[2f4b430] | 84 | |
---|
[2bb37c3] | 85 | def print_result(self): |
---|
| 86 | """ |
---|
| 87 | Print result object |
---|
| 88 | """ |
---|
[85f17f6] | 89 | msg = " \n %s \n" % str(self.result) |
---|
[2bb37c3] | 90 | wx.PostEvent(self.parent, StatusEvent(status=msg)) |
---|
[2f4b430] | 91 | |
---|
[e54d2c32] | 92 | def error(self, msg): |
---|
| 93 | """ |
---|
| 94 | Model had an error; print traceback |
---|
| 95 | """ |
---|
| 96 | if self.isbetter: |
---|
[2bb37c3] | 97 | #self.result.print_summary() |
---|
| 98 | self.update_fit() |
---|
| 99 | |
---|
| 100 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
[8b481a51] | 101 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 102 | info="error", type="stop")) |
---|
[986da97] | 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")) |
---|
[2f4b430] | 114 | |
---|
[e54d2c32] | 115 | def finalize(self): |
---|
[5062bbf] | 116 | """ |
---|
| 117 | """ |
---|
[e54d2c32] | 118 | if self.isbetter: |
---|
[2bb37c3] | 119 | #self.result.print_summary() |
---|
| 120 | self.update_fit() |
---|
[e54d2c32] | 121 | |
---|
| 122 | def abort(self): |
---|
[5062bbf] | 123 | """ |
---|
| 124 | """ |
---|
[e54d2c32] | 125 | if self.isbetter: |
---|
[2bb37c3] | 126 | #self.result.print_summary() |
---|
| 127 | self.update_fit() |
---|
[2f4b430] | 128 | |
---|
| 129 | |
---|
[35086c3] | 130 | def update_fit(self, last=False): |
---|
[2bb37c3] | 131 | """ |
---|
| 132 | """ |
---|
| 133 | t1 = time.time() |
---|
[2f4b430] | 134 | self.elapsed_time = t1 - self.update_duration |
---|
[2bb37c3] | 135 | self.update_duration = t1 |
---|
| 136 | self.fit_duration += self.elapsed_time |
---|
[35086c3] | 137 | str_time = time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime(t1)) |
---|
[85f17f6] | 138 | UPDATE_INTERVAL = 5.0 |
---|
[2bb37c3] | 139 | u_flag = False |
---|
| 140 | if self.fit_duration >= UPDATE_INTERVAL: |
---|
| 141 | self.fit_duration = 0 |
---|
| 142 | u_flag = True |
---|
[35086c3] | 143 | msg = str_time |
---|
[2bb37c3] | 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 \ |
---|
[2f4b430] | 149 | hasattr(self.result.data, "sas_data") and |
---|
[b9a5f0e] | 150 | self.result.data.sas_data is not None) |
---|
[2bb37c3] | 151 | m_flag = (hasattr(self.result, "model") and \ |
---|
| 152 | self.result.model is not None) |
---|
| 153 | if d_flag: |
---|
[b9a5f0e] | 154 | data_name = self.result.data.sas_data.name |
---|
[2bb37c3] | 155 | if m_flag: |
---|
| 156 | model_name = str(self.result.model.name) |
---|
| 157 | if m_flag and d_flag: |
---|
[2f4b430] | 158 | msg += "Data : %s \n" % (str(data_name))#, |
---|
[9c9a224] | 159 | #str(model_name)) |
---|
[2f4b430] | 160 | msg += str(self.result) |
---|
[2bb37c3] | 161 | msg += "\n" |
---|
| 162 | else: |
---|
| 163 | msg += "No result available\n" |
---|
[ccb0f39] | 164 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
[2296316] | 165 | type="progress")) |
---|
[2f4b430] | 166 | |
---|
[644ca73] | 167 | def starting_fit(self): |
---|
[5062bbf] | 168 | """ |
---|
| 169 | """ |
---|
[8b481a51] | 170 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
[2f4b430] | 171 | info="info", type="progress")) |
---|
| 172 | |
---|
[e54d2c32] | 173 | def set_result(self, result): |
---|
[5062bbf] | 174 | """ |
---|
| 175 | """ |
---|
[e54d2c32] | 176 | self.result = result |
---|
[2f4b430] | 177 | |
---|
[66ff250] | 178 | def get_result(self): |
---|
| 179 | """ |
---|
| 180 | """ |
---|
| 181 | return self.result |
---|
[2f4b430] | 182 | |
---|
| 183 | |
---|