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