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 | 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): |
---|
25 | """ |
---|
26 | Perform P(r) inversion |
---|
27 | """ |
---|
28 | try: |
---|
29 | self.starttime = time.time() |
---|
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) |
---|
33 | elapsed = time.time()-self.starttime |
---|
34 | self.complete(out=out, cov=cov, pr=self.pr, elapsed=elapsed) |
---|
35 | except KeyboardInterrupt: |
---|
36 | # Thread was interrupted, just proceed |
---|
37 | pass |
---|
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 | """ |
---|
44 | Estimate P(r) |
---|
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): |
---|
63 | """ |
---|
64 | Calculates the estimate |
---|
65 | """ |
---|
66 | try: |
---|
67 | alpha, message, elapsed = self.pr.estimate_alpha(self.nfunc) |
---|
68 | self.isquit() |
---|
69 | self.complete(alpha=alpha, message=message, elapsed=elapsed) |
---|
70 | except KeyboardInterrupt: |
---|
71 | # Thread was interrupted, just proceed |
---|
72 | pass |
---|
73 | except: |
---|
74 | if not self.error_func==None: |
---|
75 | self.error_func("EstimatePr.compute: %s" % sys.exc_value) |
---|
76 | |
---|
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 |
---|
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 | |
---|
103 | def compute(self): |
---|
104 | """ |
---|
105 | Calculates the estimate |
---|
106 | """ |
---|
107 | try: |
---|
108 | t_0 = time.time() |
---|
109 | self._time_for_sleep = t_0 |
---|
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) |
---|
114 | except KeyboardInterrupt: |
---|
115 | # Thread was interrupted, just proceed |
---|
116 | pass |
---|
117 | except: |
---|
118 | if not self.error_func==None: |
---|
119 | self.error_func("EstimatePr2.compute: %s" % sys.exc_value) |
---|