[7adc2a8] | 1 | import time |
---|
| 2 | import logging |
---|
| 3 | from sas.sascalc.fit import FitHandler |
---|
| 4 | |
---|
| 5 | class ConsoleUpdate(FitHandler): |
---|
| 6 | """ |
---|
| 7 | Print progress to the console. |
---|
| 8 | """ |
---|
| 9 | isbetter = False |
---|
| 10 | """Record whether results improved since last update""" |
---|
| 11 | progress_delta = 60 |
---|
| 12 | """Number of seconds between progress updates""" |
---|
| 13 | improvement_delta = 5 |
---|
| 14 | """Number of seconds between improvement updates""" |
---|
| 15 | def __init__(self, parent, manager=None, |
---|
| 16 | quiet=False, progress_delta=60, improvement_delta=5): |
---|
| 17 | """ |
---|
| 18 | If quiet is true, only print out final summary, not progress and |
---|
| 19 | improvements. |
---|
| 20 | |
---|
| 21 | :attr parent: the object that handle the messages |
---|
| 22 | |
---|
| 23 | """ |
---|
| 24 | self.parent = parent |
---|
| 25 | self.manager = manager |
---|
| 26 | self.progress_time = time.time() |
---|
| 27 | self.progress_percent = 0 |
---|
| 28 | self.improvement_time = self.progress_time |
---|
| 29 | self.isbetter = False |
---|
| 30 | self.quiet = quiet |
---|
| 31 | self.progress_delta = progress_delta |
---|
| 32 | self.improvement_delta = improvement_delta |
---|
| 33 | self.elapsed_time = time.time() |
---|
| 34 | self.update_duration = time.time() |
---|
| 35 | self.fit_duration = 0 |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | def progress(self, k, n): |
---|
| 39 | """ |
---|
| 40 | Report on progress. |
---|
| 41 | """ |
---|
| 42 | if self.quiet: return |
---|
| 43 | |
---|
| 44 | t = time.time() |
---|
| 45 | p = int((100 * k) // n) |
---|
| 46 | |
---|
| 47 | # Show improvements if there are any |
---|
| 48 | dt = t - self.improvement_time |
---|
| 49 | if self.isbetter and dt > self.improvement_delta: |
---|
| 50 | #self.result.print_summary() |
---|
| 51 | self.update_fit() |
---|
| 52 | self.isbetter = False |
---|
| 53 | self.improvement_time = t |
---|
| 54 | |
---|
| 55 | # Update percent complete |
---|
| 56 | dp = p - self.progress_percent |
---|
| 57 | if dp < 1: return |
---|
| 58 | dt = t - self.progress_time |
---|
| 59 | if dt > self.progress_delta: |
---|
| 60 | if 1 <= dp <= 2: |
---|
| 61 | self.progress_percent = p |
---|
| 62 | self.progress_time = t |
---|
| 63 | self.update_fit() |
---|
| 64 | elif 2 < dp <= 5: |
---|
| 65 | if p // 5 != self.progress_percent // 5: |
---|
| 66 | self.progress_percent = p |
---|
| 67 | self.progress_time = t |
---|
| 68 | else: |
---|
| 69 | if p // 10 != self.progress_percent // 10: |
---|
| 70 | self.progress_percent = p |
---|
| 71 | self.progress_time = t |
---|
| 72 | self.update_fit() |
---|
| 73 | |
---|
| 74 | def improvement(self): |
---|
| 75 | """ |
---|
| 76 | Called when a result is observed which is better than previous |
---|
| 77 | results from the fit. |
---|
| 78 | """ |
---|
| 79 | self.isbetter = True |
---|
| 80 | |
---|
| 81 | def print_result(self): |
---|
| 82 | """ |
---|
| 83 | Print result object |
---|
| 84 | """ |
---|
| 85 | msg = " \n %s \n" % str(self.result) |
---|
| 86 | logging.info(msg) |
---|
| 87 | |
---|
| 88 | def error(self, msg): |
---|
| 89 | """ |
---|
| 90 | Model had an error; print traceback |
---|
| 91 | """ |
---|
| 92 | if self.isbetter: |
---|
| 93 | self.update_fit() |
---|
| 94 | |
---|
| 95 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
| 96 | logging.error(message) |
---|
| 97 | |
---|
| 98 | def stop(self, msg): |
---|
| 99 | """ |
---|
| 100 | Post event msg and stop |
---|
| 101 | """ |
---|
| 102 | if self.isbetter: |
---|
| 103 | self.update_fit() |
---|
| 104 | |
---|
| 105 | message = str(msg) + " \n %s \n" % self.result.__str__() |
---|
| 106 | logging.info(message) |
---|
| 107 | |
---|
| 108 | def finalize(self): |
---|
| 109 | """ |
---|
| 110 | """ |
---|
| 111 | if self.isbetter: |
---|
| 112 | self.update_fit() |
---|
| 113 | |
---|
| 114 | def abort(self): |
---|
| 115 | """ |
---|
| 116 | """ |
---|
| 117 | if self.isbetter: |
---|
| 118 | self.update_fit() |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | def update_fit(self, last=False): |
---|
| 122 | """ |
---|
| 123 | """ |
---|
| 124 | t1 = time.time() |
---|
| 125 | self.elapsed_time = t1 - self.update_duration |
---|
| 126 | self.update_duration = t1 |
---|
| 127 | self.fit_duration += self.elapsed_time |
---|
| 128 | str_time = time.strftime("%Y-%m-%d %H:%M:%S ", time.localtime(t1)) |
---|
| 129 | UPDATE_INTERVAL = 5.0 |
---|
| 130 | u_flag = False |
---|
| 131 | if self.fit_duration >= UPDATE_INTERVAL: |
---|
| 132 | self.fit_duration = 0 |
---|
| 133 | u_flag = True |
---|
| 134 | msg = str_time |
---|
| 135 | if u_flag or last: |
---|
| 136 | if self.result is not None: |
---|
| 137 | data_name, model_name = None, None |
---|
| 138 | d_flag = (hasattr(self.result, "data") and \ |
---|
| 139 | self.result.data is not None and \ |
---|
| 140 | hasattr(self.result.data, "sas_data") and |
---|
| 141 | self.result.data.sas_data is not None) |
---|
| 142 | m_flag = (hasattr(self.result, "model") and \ |
---|
| 143 | self.result.model is not None) |
---|
| 144 | if d_flag: |
---|
| 145 | data_name = self.result.data.sas_data.name |
---|
| 146 | if m_flag: |
---|
| 147 | model_name = str(self.result.model.name) |
---|
| 148 | if m_flag and d_flag: |
---|
| 149 | msg += "Data : %s \n" % (str(data_name))#, |
---|
| 150 | #str(model_name)) |
---|
| 151 | msg += str(self.result) |
---|
| 152 | msg += "\n" |
---|
| 153 | else: |
---|
| 154 | msg += "No result available\n" |
---|
| 155 | |
---|
| 156 | logging.info(msg) |
---|
| 157 | |
---|
| 158 | def set_result(self, result): |
---|
| 159 | """ |
---|
| 160 | """ |
---|
| 161 | self.result = result |
---|
| 162 | |
---|
| 163 | def get_result(self): |
---|
| 164 | """ |
---|
| 165 | """ |
---|
| 166 | return self.result |
---|