[e54d2c32] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | from sans.guicomm.events import StatusEvent |
---|
| 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. |
---|
| 24 | """ |
---|
| 25 | self.parent= parent |
---|
| 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 = self.progress_time |
---|
| 34 | |
---|
| 35 | def progress(self, k, n): |
---|
| 36 | """ |
---|
| 37 | Report on progress. |
---|
| 38 | """ |
---|
| 39 | if self.quiet: return |
---|
| 40 | |
---|
| 41 | t = time.time() |
---|
| 42 | p = int((100*k)//n) |
---|
| 43 | |
---|
| 44 | # Show improvements if there are any |
---|
| 45 | dt = t - self.improvement_time |
---|
| 46 | if self.isbetter and dt > self.improvement_delta: |
---|
| 47 | self.result.print_summary() |
---|
| 48 | self.isbetter = False |
---|
| 49 | self.improvement_time = t |
---|
| 50 | |
---|
| 51 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 52 | "%d%% complete ..."%(p),type="progress")) |
---|
| 53 | |
---|
| 54 | # Update percent complete |
---|
| 55 | dp = p-self.progress_percent |
---|
| 56 | if dp < 1: return |
---|
| 57 | dt = t - self.progress_time |
---|
| 58 | if dt > self.progress_delta: |
---|
| 59 | if 1 <= dp <= 2: |
---|
| 60 | self.progress_percent = p |
---|
| 61 | self.progress_time = t |
---|
| 62 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 63 | "%d%% complete ..."%(p), |
---|
| 64 | type="progress")) |
---|
| 65 | |
---|
| 66 | elif 2 < dp <= 5: |
---|
| 67 | if p//5 != self.progress_percent//5: |
---|
| 68 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 69 | "%d%% complete ..."%(5*(p//5)),type="progress")) |
---|
| 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 |
---|
| 76 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
| 77 | "%d%% complete ..."%(10*(p//10)),type="progress")) |
---|
| 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 | |
---|
| 86 | def error(self, msg): |
---|
| 87 | """ |
---|
| 88 | Model had an error; print traceback |
---|
| 89 | """ |
---|
| 90 | if self.isbetter: |
---|
| 91 | self.result.print_summary() |
---|
| 92 | message = "fit Error" |
---|
| 93 | message = str(msg)+ " \n %s"%self.result.__str__() |
---|
| 94 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 95 | info="error", type="stop")) |
---|
| 96 | |
---|
| 97 | def finalize(self): |
---|
| 98 | if self.isbetter: |
---|
| 99 | self.result.print_summary() |
---|
| 100 | |
---|
| 101 | def abort(self): |
---|
| 102 | if self.isbetter: |
---|
| 103 | self.result.print_summary() |
---|
| 104 | |
---|
| 105 | def update_fit(self, msg=""): |
---|
| 106 | self.elapsed_time = time.time() - self.elapsed_time |
---|
| 107 | msg = " Updating fit ...\n result:\n %s \n"%self.result.__str__() |
---|
| 108 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 109 | type="progress")) |
---|
| 110 | |
---|
| 111 | def starting_fit(self): |
---|
| 112 | wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 113 | info="info",type="progress")) |
---|
| 114 | |
---|
| 115 | def set_result(self, result): |
---|
| 116 | self.result = result |
---|
| 117 | |
---|
| 118 | |
---|