[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")) |
---|
[e54d2c32] | 104 | def finalize(self): |
---|
[5062bbf] | 105 | """ |
---|
| 106 | """ |
---|
[e54d2c32] | 107 | if self.isbetter: |
---|
[2bb37c3] | 108 | #self.result.print_summary() |
---|
| 109 | self.update_fit() |
---|
[e54d2c32] | 110 | |
---|
| 111 | def abort(self): |
---|
[5062bbf] | 112 | """ |
---|
| 113 | """ |
---|
[e54d2c32] | 114 | if self.isbetter: |
---|
[2bb37c3] | 115 | #self.result.print_summary() |
---|
| 116 | self.update_fit() |
---|
[e54d2c32] | 117 | |
---|
[66ff250] | 118 | |
---|
[2bb37c3] | 119 | def update_fit(self, msg="", last=False): |
---|
| 120 | """ |
---|
| 121 | """ |
---|
| 122 | t1 = time.time() |
---|
| 123 | self.elapsed_time = t1 - self.update_duration |
---|
| 124 | self.update_duration = t1 |
---|
| 125 | self.fit_duration += self.elapsed_time |
---|
| 126 | str_time = time.strftime("%a, %d %b %Y %H:%M:%S ", time.localtime(t1)) |
---|
| 127 | UPDATE_INTERVAL = 0.5 |
---|
| 128 | u_flag = False |
---|
| 129 | if self.fit_duration >= UPDATE_INTERVAL: |
---|
| 130 | self.fit_duration = 0 |
---|
| 131 | u_flag = True |
---|
| 132 | if not last: |
---|
[afac721] | 133 | msg += "Fit Updates ... %s \n" % str_time |
---|
[2bb37c3] | 134 | else: |
---|
[afac721] | 135 | msg += "Final updates ........." |
---|
[2bb37c3] | 136 | if u_flag or last: |
---|
| 137 | if self.result is not None: |
---|
| 138 | data_name, model_name = None, None |
---|
| 139 | d_flag = (hasattr(self.result, "data") and \ |
---|
| 140 | self.result.data is not None and \ |
---|
| 141 | hasattr(self.result.data, "sans_data") and |
---|
| 142 | self.result.data.sans_data is not None) |
---|
| 143 | m_flag = (hasattr(self.result, "model") and \ |
---|
| 144 | self.result.model is not None) |
---|
| 145 | if d_flag: |
---|
| 146 | data_name = self.result.data.sans_data.name |
---|
| 147 | if m_flag: |
---|
| 148 | model_name = str(self.result.model.name) |
---|
| 149 | if m_flag and d_flag: |
---|
[9c9a224] | 150 | msg += "Data : %s \n" % (str(data_name))#, |
---|
| 151 | #str(model_name)) |
---|
[2bb37c3] | 152 | msg += str(self.result) |
---|
| 153 | msg += "\n" |
---|
[9c9a224] | 154 | if not last: |
---|
| 155 | msg += "About %s s elapsed......... \n" % \ |
---|
| 156 | str (UPDATE_INTERVAL) |
---|
[2bb37c3] | 157 | else: |
---|
| 158 | msg += "No result available\n" |
---|
[ccb0f39] | 159 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
[2296316] | 160 | type="progress")) |
---|
[2bb37c3] | 161 | |
---|
[e54d2c32] | 162 | def starting_fit(self): |
---|
[5062bbf] | 163 | """ |
---|
| 164 | """ |
---|
[8b481a51] | 165 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 166 | info="info",type="progress")) |
---|
[e54d2c32] | 167 | |
---|
| 168 | def set_result(self, result): |
---|
[5062bbf] | 169 | """ |
---|
| 170 | """ |
---|
[e54d2c32] | 171 | self.result = result |
---|
[66ff250] | 172 | |
---|
| 173 | def get_result(self): |
---|
| 174 | """ |
---|
| 175 | """ |
---|
| 176 | return self.result |
---|
[e54d2c32] | 177 | |
---|
| 178 | |
---|