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