[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""" |
---|
| 20 | def __init__(self,parent, quiet=False,progress_delta=60,improvement_delta=5): |
---|
| 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 |
---|
| 29 | self.progress_time = time.time() |
---|
| 30 | self.progress_percent = 0 |
---|
| 31 | self.improvement_time = self.progress_time |
---|
| 32 | self.isbetter = False |
---|
| 33 | self.quiet = quiet |
---|
| 34 | self.progress_delta = progress_delta |
---|
| 35 | self.improvement_delta = improvement_delta |
---|
| 36 | self.elapsed_time = self.progress_time |
---|
| 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.isbetter = False |
---|
| 52 | self.improvement_time = t |
---|
| 53 | |
---|
| 54 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 55 | "%d%% complete ..."%(p),type="progress")) |
---|
| 56 | |
---|
| 57 | # Update percent complete |
---|
| 58 | dp = p-self.progress_percent |
---|
| 59 | if dp < 1: return |
---|
| 60 | dt = t - self.progress_time |
---|
| 61 | if dt > self.progress_delta: |
---|
| 62 | if 1 <= dp <= 2: |
---|
| 63 | self.progress_percent = p |
---|
| 64 | self.progress_time = t |
---|
| 65 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 66 | "%d%% complete ..."%(p), |
---|
| 67 | type="progress")) |
---|
| 68 | |
---|
| 69 | elif 2 < dp <= 5: |
---|
| 70 | if p//5 != self.progress_percent//5: |
---|
| 71 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 72 | "%d%% complete ..."%(5*(p//5)),type="progress")) |
---|
| 73 | self.progress_percent = p |
---|
| 74 | self.progress_time = t |
---|
| 75 | else: |
---|
| 76 | if p//10 != self.progress_percent//10: |
---|
| 77 | self.progress_percent = p |
---|
| 78 | self.progress_time = t |
---|
| 79 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 80 | "%d%% complete ..."%(10*(p//10)),type="progress")) |
---|
| 81 | |
---|
| 82 | def improvement(self): |
---|
| 83 | """ |
---|
| 84 | Called when a result is observed which is better than previous |
---|
| 85 | results from the fit. |
---|
| 86 | """ |
---|
| 87 | self.isbetter = True |
---|
| 88 | |
---|
| 89 | def error(self, msg): |
---|
| 90 | """ |
---|
| 91 | Model had an error; print traceback |
---|
| 92 | """ |
---|
| 93 | if self.isbetter: |
---|
| 94 | self.result.print_summary() |
---|
| 95 | message = "fit Error" |
---|
| 96 | message = str(msg)+ " \n %s"%self.result.__str__() |
---|
| 97 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 98 | info="error", type="stop")) |
---|
| 99 | |
---|
| 100 | def finalize(self): |
---|
[5062bbf] | 101 | """ |
---|
| 102 | """ |
---|
[e54d2c32] | 103 | if self.isbetter: |
---|
| 104 | self.result.print_summary() |
---|
| 105 | |
---|
| 106 | def abort(self): |
---|
[5062bbf] | 107 | """ |
---|
| 108 | """ |
---|
[e54d2c32] | 109 | if self.isbetter: |
---|
| 110 | self.result.print_summary() |
---|
| 111 | |
---|
| 112 | def update_fit(self, msg=""): |
---|
[5062bbf] | 113 | """ |
---|
| 114 | """ |
---|
[e54d2c32] | 115 | self.elapsed_time = time.time() - self.elapsed_time |
---|
| 116 | msg = " Updating fit ...\n result:\n %s \n"%self.result.__str__() |
---|
| 117 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 118 | type="progress")) |
---|
| 119 | |
---|
| 120 | def starting_fit(self): |
---|
[5062bbf] | 121 | """ |
---|
| 122 | """ |
---|
[e54d2c32] | 123 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 124 | info="info",type="progress")) |
---|
| 125 | |
---|
| 126 | def set_result(self, result): |
---|
[5062bbf] | 127 | """ |
---|
| 128 | """ |
---|
[e54d2c32] | 129 | self.result = result |
---|
| 130 | |
---|
| 131 | |
---|