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