1 | import sys, time |
---|
2 | from data_util.calcthread import CalcThread |
---|
3 | |
---|
4 | class CalcPr(CalcThread): |
---|
5 | """ |
---|
6 | Compute P(r) |
---|
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 | """ |
---|
16 | """ |
---|
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): |
---|
27 | """ |
---|
28 | Perform P(r) inversion |
---|
29 | """ |
---|
30 | try: |
---|
31 | self.starttime = time.time() |
---|
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) |
---|
35 | elapsed = time.time()-self.starttime |
---|
36 | self.complete(out=out, cov=cov, pr=self.pr, elapsed=elapsed) |
---|
37 | except KeyboardInterrupt: |
---|
38 | # Thread was interrupted, just proceed |
---|
39 | pass |
---|
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 | """ |
---|
46 | Estimate P(r) |
---|
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): |
---|
65 | """ |
---|
66 | Calculates the estimate |
---|
67 | """ |
---|
68 | try: |
---|
69 | alpha, message, elapsed = self.pr.estimate_alpha(self.nfunc) |
---|
70 | self.isquit() |
---|
71 | self.complete(alpha=alpha, message=message, elapsed=elapsed) |
---|
72 | except KeyboardInterrupt: |
---|
73 | # Thread was interrupted, just proceed |
---|
74 | pass |
---|
75 | except: |
---|
76 | if not self.error_func==None: |
---|
77 | self.error_func("EstimatePr.compute: %s" % sys.exc_value) |
---|
78 | |
---|
79 | class EstimateNT(CalcThread): |
---|
80 | """ |
---|
81 | """ |
---|
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 |
---|
96 | self._time_for_sleep = 0 |
---|
97 | self._sleep_delay = 1.0 |
---|
98 | |
---|
99 | |
---|
100 | def isquit(self): |
---|
101 | """ |
---|
102 | """ |
---|
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 | |
---|
108 | def compute(self): |
---|
109 | """ |
---|
110 | Calculates the estimate |
---|
111 | """ |
---|
112 | try: |
---|
113 | t_0 = time.time() |
---|
114 | self._time_for_sleep = t_0 |
---|
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) |
---|
119 | except KeyboardInterrupt: |
---|
120 | # Thread was interrupted, just proceed |
---|
121 | pass |
---|
122 | except: |
---|
123 | if not self.error_func==None: |
---|
124 | self.error_func("EstimatePr2.compute: %s" % sys.exc_value) |
---|