1 | import sys, time |
---|
2 | from calcthread import CalcThread |
---|
3 | |
---|
4 | class CalcPr(CalcThread): |
---|
5 | """ |
---|
6 | Compute 2D model |
---|
7 | This calculation assumes a 2-fold symmetry of the model |
---|
8 | where points are computed for one half of the detector |
---|
9 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
10 | """ |
---|
11 | |
---|
12 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
13 | completefn = None, |
---|
14 | updatefn = None, |
---|
15 | yieldtime = 0.01, |
---|
16 | worktime = 0.01 |
---|
17 | ): |
---|
18 | CalcThread.__init__(self,completefn, |
---|
19 | updatefn, |
---|
20 | yieldtime, |
---|
21 | worktime) |
---|
22 | self.pr = pr |
---|
23 | self.nfunc = nfunc |
---|
24 | self.error_func = error_func |
---|
25 | self.starttime = 0 |
---|
26 | |
---|
27 | def compute(self): |
---|
28 | """ |
---|
29 | Perform P(r) inversion |
---|
30 | """ |
---|
31 | try: |
---|
32 | self.starttime = time.time() |
---|
33 | out, cov = self.pr.invert(self.nfunc) |
---|
34 | #out, cov = self.pr.lstsq(self.nfunc) |
---|
35 | #out, cov = self.pr.invert_optimize(self.nfunc) |
---|
36 | elapsed = time.time()-self.starttime |
---|
37 | self.complete(out=out, cov=cov, pr=self.pr, elapsed=elapsed) |
---|
38 | except KeyboardInterrupt: |
---|
39 | # Thread was interrupted, just proceed |
---|
40 | pass |
---|
41 | except: |
---|
42 | if not self.error_func==None: |
---|
43 | self.error_func("CalcPr.compute: %s" % sys.exc_value) |
---|
44 | |
---|
45 | class EstimatePr(CalcThread): |
---|
46 | """ |
---|
47 | Compute 2D model |
---|
48 | This calculation assumes a 2-fold symmetry of the model |
---|
49 | where points are computed for one half of the detector |
---|
50 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
51 | """ |
---|
52 | |
---|
53 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
54 | completefn = None, |
---|
55 | updatefn = None, |
---|
56 | yieldtime = 0.01, |
---|
57 | worktime = 0.01 |
---|
58 | ): |
---|
59 | CalcThread.__init__(self,completefn, |
---|
60 | updatefn, |
---|
61 | yieldtime, |
---|
62 | worktime) |
---|
63 | self.pr = pr |
---|
64 | self.nfunc = nfunc |
---|
65 | self.error_func = error_func |
---|
66 | self.starttime = 0 |
---|
67 | |
---|
68 | def compute(self): |
---|
69 | """ |
---|
70 | Calculates the estimate |
---|
71 | """ |
---|
72 | try: |
---|
73 | alpha, message, elapsed = self.pr.estimate_alpha(self.nfunc) |
---|
74 | self.isquit() |
---|
75 | self.complete(alpha=alpha, message=message, elapsed=elapsed) |
---|
76 | except KeyboardInterrupt: |
---|
77 | # Thread was interrupted, just proceed |
---|
78 | pass |
---|
79 | except: |
---|
80 | if not self.error_func==None: |
---|
81 | self.error_func("EstimatePr.compute: %s" % sys.exc_value) |
---|
82 | |
---|
83 | class EstimateNT(CalcThread): |
---|
84 | |
---|
85 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
86 | completefn = None, |
---|
87 | updatefn = None, |
---|
88 | yieldtime = 0.01, |
---|
89 | worktime = 0.01 |
---|
90 | ): |
---|
91 | CalcThread.__init__(self,completefn, |
---|
92 | updatefn, |
---|
93 | yieldtime, |
---|
94 | worktime) |
---|
95 | self.pr = pr |
---|
96 | self.nfunc = nfunc |
---|
97 | self.error_func = error_func |
---|
98 | self.starttime = 0 |
---|
99 | self._time_for_sleep = 0 |
---|
100 | self._sleep_delay = 1.0 |
---|
101 | |
---|
102 | |
---|
103 | def isquit(self): |
---|
104 | CalcThread.isquit(self) |
---|
105 | if time.time()>self._time_for_sleep+self._sleep_delay: |
---|
106 | time.sleep(.2) |
---|
107 | self._time_for_sleep = time.time() |
---|
108 | |
---|
109 | def compute(self): |
---|
110 | """ |
---|
111 | Calculates the estimate |
---|
112 | """ |
---|
113 | try: |
---|
114 | t_0 = time.time() |
---|
115 | self._time_for_sleep = t_0 |
---|
116 | nterms, alpha, message = self.pr.estimate_numterms(self.isquit) |
---|
117 | t_1 = time.time()-t_0 |
---|
118 | self.isquit() |
---|
119 | self.complete(nterms=nterms, alpha=alpha, message=message, elapsed=t_1) |
---|
120 | except KeyboardInterrupt: |
---|
121 | # Thread was interrupted, just proceed |
---|
122 | pass |
---|
123 | except: |
---|
124 | if not self.error_func==None: |
---|
125 | self.error_func("EstimatePr2.compute: %s" % sys.exc_value) |
---|
126 | |
---|
127 | |
---|