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