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