[e54d2c32] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
[79492222] | 4 | from sas.guiframe.events import StatusEvent |
---|
[e54d2c32] | 5 | import time |
---|
| 6 | import wx |
---|
[79492222] | 7 | from sas.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""" |
---|
| 15 | progress_delta = 60 |
---|
| 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, |
---|
| 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. |
---|
[5062bbf] | 24 | |
---|
| 25 | :attr parent: the object that handle the messages |
---|
| 26 | |
---|
[e54d2c32] | 27 | """ |
---|
| 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 |
---|
| 40 | |
---|
[e54d2c32] | 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: |
---|
[2bb37c3] | 54 | #self.result.print_summary() |
---|
| 55 | self.update_fit() |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 67 | self.update_fit() |
---|
[e54d2c32] | 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 |
---|
[2bb37c3] | 76 | self.update_fit() |
---|
[e54d2c32] | 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 | |
---|
[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)) |
---|
| 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")) |
---|
| 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() |
---|
[e54d2c32] | 128 | |
---|
[66ff250] | 129 | |
---|
[35086c3] | 130 | def update_fit(self, last=False): |
---|
[2bb37c3] | 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 |
---|
[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 \ |
---|
[b9a5f0e] | 149 | hasattr(self.result.data, "sas_data") and |
---|
| 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: |
---|
[9c9a224] | 158 | msg += "Data : %s \n" % (str(data_name))#, |
---|
| 159 | #str(model_name)) |
---|
[2bb37c3] | 160 | msg += str(self.result) |
---|
| 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")) |
---|
[2bb37c3] | 166 | |
---|
[644ca73] | 167 | def starting_fit(self): |
---|
[5062bbf] | 168 | """ |
---|
| 169 | """ |
---|
[8b481a51] | 170 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 171 | info="info",type="progress")) |
---|
[e54d2c32] | 172 | |
---|
| 173 | def set_result(self, result): |
---|
[5062bbf] | 174 | """ |
---|
| 175 | """ |
---|
[e54d2c32] | 176 | self.result = result |
---|
[66ff250] | 177 | |
---|
| 178 | def get_result(self): |
---|
| 179 | """ |
---|
| 180 | """ |
---|
| 181 | return self.result |
---|
[e54d2c32] | 182 | |
---|
| 183 | |
---|