[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 |
---|
| 9 | |
---|
| 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 |
---|
| 38 | self.elapsed_time = self.progress_time |
---|
| 39 | |
---|
| 40 | def progress(self, k, n): |
---|
| 41 | """ |
---|
| 42 | Report on progress. |
---|
| 43 | """ |
---|
| 44 | if self.quiet: return |
---|
| 45 | |
---|
| 46 | t = time.time() |
---|
| 47 | p = int((100*k)//n) |
---|
| 48 | |
---|
| 49 | # Show improvements if there are any |
---|
| 50 | dt = t - self.improvement_time |
---|
| 51 | if self.isbetter and dt > self.improvement_delta: |
---|
| 52 | self.result.print_summary() |
---|
| 53 | self.isbetter = False |
---|
| 54 | self.improvement_time = t |
---|
| 55 | |
---|
[8b481a51] | 56 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 57 | "%d%% complete ..."%(p),type="progress")) |
---|
[e54d2c32] | 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 |
---|
[8b481a51] | 67 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 68 | "%d%% complete ..."%(p), |
---|
| 69 | type="progress")) |
---|
[e54d2c32] | 70 | |
---|
| 71 | elif 2 < dp <= 5: |
---|
| 72 | if p//5 != self.progress_percent//5: |
---|
[8b481a51] | 73 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 74 | "%d%% complete ..."%(5*(p//5)),type="progress")) |
---|
[e54d2c32] | 75 | self.progress_percent = p |
---|
| 76 | self.progress_time = t |
---|
| 77 | else: |
---|
| 78 | if p//10 != self.progress_percent//10: |
---|
| 79 | self.progress_percent = p |
---|
| 80 | self.progress_time = t |
---|
[8b481a51] | 81 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 82 | "%d%% complete ..."%(10*(p//10)),type="progress")) |
---|
[e54d2c32] | 83 | |
---|
| 84 | def improvement(self): |
---|
| 85 | """ |
---|
| 86 | Called when a result is observed which is better than previous |
---|
| 87 | results from the fit. |
---|
| 88 | """ |
---|
| 89 | self.isbetter = True |
---|
| 90 | |
---|
| 91 | def error(self, msg): |
---|
| 92 | """ |
---|
| 93 | Model had an error; print traceback |
---|
| 94 | """ |
---|
| 95 | if self.isbetter: |
---|
| 96 | self.result.print_summary() |
---|
[40953a9] | 97 | message = "Fit Abort:" |
---|
[8b481a51] | 98 | message = str(msg)+ " \n %s"%self.result.__str__() |
---|
| 99 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 100 | info="error", type="stop")) |
---|
[e54d2c32] | 101 | def finalize(self): |
---|
[5062bbf] | 102 | """ |
---|
| 103 | """ |
---|
[e54d2c32] | 104 | if self.isbetter: |
---|
| 105 | self.result.print_summary() |
---|
| 106 | |
---|
| 107 | def abort(self): |
---|
[5062bbf] | 108 | """ |
---|
| 109 | """ |
---|
[e54d2c32] | 110 | if self.isbetter: |
---|
| 111 | self.result.print_summary() |
---|
| 112 | |
---|
[66ff250] | 113 | |
---|
[e54d2c32] | 114 | def update_fit(self, msg=""): |
---|
[5062bbf] | 115 | """ |
---|
| 116 | """ |
---|
[2296316] | 117 | self.elapsed_time = time.time() - self.elapsed_time |
---|
| 118 | msg = " Updating fit ...\n result:\n %s \n"%self.result.__str__() |
---|
| 119 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 120 | type="progress")) |
---|
| 121 | time.sleep(0.01) |
---|
[e54d2c32] | 122 | |
---|
| 123 | def starting_fit(self): |
---|
[5062bbf] | 124 | """ |
---|
| 125 | """ |
---|
[8b481a51] | 126 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 127 | info="info",type="progress")) |
---|
[e54d2c32] | 128 | |
---|
| 129 | def set_result(self, result): |
---|
[5062bbf] | 130 | """ |
---|
| 131 | """ |
---|
[e54d2c32] | 132 | self.result = result |
---|
[66ff250] | 133 | |
---|
| 134 | def get_result(self): |
---|
| 135 | """ |
---|
| 136 | """ |
---|
| 137 | return self.result |
---|
[e54d2c32] | 138 | |
---|
| 139 | |
---|