[f3d51f6] | 1 | import sys, time |
---|
[1ebb590] | 2 | from data_util.calcthread import CalcThread |
---|
[f3d51f6] | 3 | |
---|
| 4 | class CalcPr(CalcThread): |
---|
| 5 | """ |
---|
[7116b6e0] | 6 | Compute P(r) |
---|
[f3d51f6] | 7 | """ |
---|
| 8 | |
---|
| 9 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
| 10 | completefn = None, |
---|
| 11 | updatefn = None, |
---|
| 12 | yieldtime = 0.01, |
---|
| 13 | worktime = 0.01 |
---|
| 14 | ): |
---|
[7116b6e0] | 15 | """ |
---|
| 16 | """ |
---|
[f3d51f6] | 17 | CalcThread.__init__(self,completefn, |
---|
| 18 | updatefn, |
---|
| 19 | yieldtime, |
---|
| 20 | worktime) |
---|
| 21 | self.pr = pr |
---|
| 22 | self.nfunc = nfunc |
---|
| 23 | self.error_func = error_func |
---|
| 24 | self.starttime = 0 |
---|
| 25 | |
---|
| 26 | def compute(self): |
---|
[4318af7f] | 27 | """ |
---|
[7116b6e0] | 28 | Perform P(r) inversion |
---|
[4318af7f] | 29 | """ |
---|
[f3d51f6] | 30 | try: |
---|
| 31 | self.starttime = time.time() |
---|
[7cb0353] | 32 | out, cov = self.pr.invert(self.nfunc) |
---|
| 33 | #out, cov = self.pr.lstsq(self.nfunc) |
---|
| 34 | #out, cov = self.pr.invert_optimize(self.nfunc) |
---|
[f3d51f6] | 35 | elapsed = time.time()-self.starttime |
---|
| 36 | self.complete(out=out, cov=cov, pr=self.pr, elapsed=elapsed) |
---|
[2a92852] | 37 | except KeyboardInterrupt: |
---|
| 38 | # Thread was interrupted, just proceed |
---|
[4318af7f] | 39 | pass |
---|
[f3d51f6] | 40 | except: |
---|
| 41 | if not self.error_func==None: |
---|
| 42 | self.error_func("CalcPr.compute: %s" % sys.exc_value) |
---|
| 43 | |
---|
| 44 | class EstimatePr(CalcThread): |
---|
| 45 | """ |
---|
[7116b6e0] | 46 | Estimate P(r) |
---|
[f3d51f6] | 47 | """ |
---|
| 48 | |
---|
| 49 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
| 50 | completefn = None, |
---|
| 51 | updatefn = None, |
---|
| 52 | yieldtime = 0.01, |
---|
| 53 | worktime = 0.01 |
---|
| 54 | ): |
---|
| 55 | CalcThread.__init__(self,completefn, |
---|
| 56 | updatefn, |
---|
| 57 | yieldtime, |
---|
| 58 | worktime) |
---|
| 59 | self.pr = pr |
---|
| 60 | self.nfunc = nfunc |
---|
| 61 | self.error_func = error_func |
---|
| 62 | self.starttime = 0 |
---|
| 63 | |
---|
| 64 | def compute(self): |
---|
[634f1cf] | 65 | """ |
---|
[7116b6e0] | 66 | Calculates the estimate |
---|
[634f1cf] | 67 | """ |
---|
[32dffae4] | 68 | try: |
---|
[634f1cf] | 69 | alpha, message, elapsed = self.pr.estimate_alpha(self.nfunc) |
---|
[35adaf6] | 70 | self.isquit() |
---|
[634f1cf] | 71 | self.complete(alpha=alpha, message=message, elapsed=elapsed) |
---|
[2a92852] | 72 | except KeyboardInterrupt: |
---|
| 73 | # Thread was interrupted, just proceed |
---|
[4318af7f] | 74 | pass |
---|
[f3d51f6] | 75 | except: |
---|
| 76 | if not self.error_func==None: |
---|
[2a92852] | 77 | self.error_func("EstimatePr.compute: %s" % sys.exc_value) |
---|
[f3d51f6] | 78 | |
---|
[35adaf6] | 79 | class EstimateNT(CalcThread): |
---|
[7116b6e0] | 80 | """ |
---|
| 81 | """ |
---|
[35adaf6] | 82 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
| 83 | completefn = None, |
---|
| 84 | updatefn = None, |
---|
| 85 | yieldtime = 0.01, |
---|
| 86 | worktime = 0.01 |
---|
| 87 | ): |
---|
| 88 | CalcThread.__init__(self,completefn, |
---|
| 89 | updatefn, |
---|
| 90 | yieldtime, |
---|
| 91 | worktime) |
---|
| 92 | self.pr = pr |
---|
| 93 | self.nfunc = nfunc |
---|
| 94 | self.error_func = error_func |
---|
| 95 | self.starttime = 0 |
---|
[357b79b] | 96 | self._time_for_sleep = 0 |
---|
| 97 | self._sleep_delay = 1.0 |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | def isquit(self): |
---|
[7116b6e0] | 101 | """ |
---|
| 102 | """ |
---|
[357b79b] | 103 | CalcThread.isquit(self) |
---|
| 104 | if time.time()>self._time_for_sleep+self._sleep_delay: |
---|
| 105 | time.sleep(.2) |
---|
| 106 | self._time_for_sleep = time.time() |
---|
| 107 | |
---|
[35adaf6] | 108 | def compute(self): |
---|
| 109 | """ |
---|
[7116b6e0] | 110 | Calculates the estimate |
---|
[35adaf6] | 111 | """ |
---|
| 112 | try: |
---|
| 113 | t_0 = time.time() |
---|
[357b79b] | 114 | self._time_for_sleep = t_0 |
---|
[35adaf6] | 115 | nterms, alpha, message = self.pr.estimate_numterms(self.isquit) |
---|
| 116 | t_1 = time.time()-t_0 |
---|
| 117 | self.isquit() |
---|
| 118 | self.complete(nterms=nterms, alpha=alpha, message=message, elapsed=t_1) |
---|
[2a92852] | 119 | except KeyboardInterrupt: |
---|
| 120 | # Thread was interrupted, just proceed |
---|
[4318af7f] | 121 | pass |
---|
[35adaf6] | 122 | except: |
---|
| 123 | if not self.error_func==None: |
---|
[2a92852] | 124 | self.error_func("EstimatePr2.compute: %s" % sys.exc_value) |
---|