[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 | |
---|
[66ff250] | 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 |
---|
[66ff250] | 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: |
---|
[66ff250] | 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 |
---|
[66ff250] | 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() |
---|
[66ff250] | 97 | #message = "fit Error" |
---|
| 98 | #message = str(msg)+ " \n %s"%self.result.__str__() |
---|
| 99 | #wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 100 | # info="error", type="stop")) |
---|
[e54d2c32] | 101 | |
---|
| 102 | def finalize(self): |
---|
[5062bbf] | 103 | """ |
---|
| 104 | """ |
---|
[e54d2c32] | 105 | if self.isbetter: |
---|
| 106 | self.result.print_summary() |
---|
| 107 | |
---|
| 108 | def abort(self): |
---|
[5062bbf] | 109 | """ |
---|
| 110 | """ |
---|
[e54d2c32] | 111 | if self.isbetter: |
---|
| 112 | self.result.print_summary() |
---|
| 113 | |
---|
[66ff250] | 114 | |
---|
[e54d2c32] | 115 | def update_fit(self, msg=""): |
---|
[5062bbf] | 116 | """ |
---|
| 117 | """ |
---|
[2296316] | 118 | self.elapsed_time = time.time() - self.elapsed_time |
---|
| 119 | msg = " Updating fit ...\n result:\n %s \n"%self.result.__str__() |
---|
| 120 | wx.PostEvent(self.parent, StatusEvent(status=msg, info="info", |
---|
| 121 | type="progress")) |
---|
| 122 | time.sleep(0.01) |
---|
[e54d2c32] | 123 | |
---|
| 124 | def starting_fit(self): |
---|
[5062bbf] | 125 | """ |
---|
| 126 | """ |
---|
[66ff250] | 127 | #wx.PostEvent(self.parent, StatusEvent(status="Starting the Fit...", |
---|
| 128 | # info="info",type="progress")) |
---|
[e54d2c32] | 129 | |
---|
| 130 | def set_result(self, result): |
---|
[5062bbf] | 131 | """ |
---|
| 132 | """ |
---|
[e54d2c32] | 133 | self.result = result |
---|
[66ff250] | 134 | |
---|
| 135 | def get_result(self): |
---|
| 136 | """ |
---|
| 137 | """ |
---|
| 138 | return self.result |
---|
[e54d2c32] | 139 | |
---|
| 140 | |
---|