[f3d51f6] | 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 | elapsed = time.time()-self.starttime |
---|
| 44 | self.complete(out=out, cov=cov, pr=self.pr, elapsed=elapsed) |
---|
| 45 | except: |
---|
| 46 | if not self.error_func==None: |
---|
| 47 | self.error_func("CalcPr.compute: %s" % sys.exc_value) |
---|
| 48 | |
---|
| 49 | class EstimatePr(CalcThread): |
---|
| 50 | """ |
---|
| 51 | Compute 2D model |
---|
| 52 | This calculation assumes a 2-fold symmetry of the model |
---|
| 53 | where points are computed for one half of the detector |
---|
| 54 | and I(qx, qy) = I(-qx, -qy) is assumed. |
---|
| 55 | """ |
---|
| 56 | |
---|
| 57 | def __init__(self, pr, nfunc=5, error_func=None, |
---|
| 58 | completefn = None, |
---|
| 59 | updatefn = None, |
---|
| 60 | yieldtime = 0.01, |
---|
| 61 | worktime = 0.01 |
---|
| 62 | ): |
---|
| 63 | CalcThread.__init__(self,completefn, |
---|
| 64 | updatefn, |
---|
| 65 | yieldtime, |
---|
| 66 | worktime) |
---|
| 67 | self.pr = pr |
---|
| 68 | self.nfunc = nfunc |
---|
| 69 | self.error_func = error_func |
---|
| 70 | self.starttime = 0 |
---|
| 71 | |
---|
| 72 | def isquit(self): |
---|
| 73 | try: |
---|
| 74 | CalcThread.isquit(self) |
---|
| 75 | except KeyboardInterrupt: |
---|
| 76 | printEVT("P(r) calc interrupted") |
---|
[32dffae4] | 77 | raise KeyboardInterrupt |
---|
[f3d51f6] | 78 | |
---|
| 79 | def compute(self): |
---|
| 80 | import time |
---|
[32dffae4] | 81 | try: |
---|
[f3d51f6] | 82 | self.starttime = time.time() |
---|
| 83 | # If the current alpha is zero, try |
---|
| 84 | # another value |
---|
| 85 | if self.pr.alpha<=0: |
---|
| 86 | self.pr.alpha = 0.0001 |
---|
| 87 | |
---|
[32dffae4] | 88 | # Perform inversion to find the largest alpha |
---|
[f3d51f6] | 89 | out, cov = self.pr.lstsq(self.nfunc) |
---|
| 90 | elapsed = time.time()-self.starttime |
---|
[32dffae4] | 91 | initial_alpha = self.pr.alpha |
---|
| 92 | initial_peaks = self.pr.get_peaks(out) |
---|
| 93 | |
---|
| 94 | # Try the inversion with the estimated alpha |
---|
| 95 | self.pr.alpha = self.pr.suggested_alpha |
---|
| 96 | out, cov = self.pr.lstsq(self.nfunc) |
---|
[f3d51f6] | 97 | |
---|
[32dffae4] | 98 | npeaks = self.pr.get_peaks(out) |
---|
| 99 | # if more than one peak to start with |
---|
| 100 | # just return the estimate |
---|
| 101 | if npeaks>1: |
---|
| 102 | message = "Your P(r) is not smooth, please check your inversion parameters" |
---|
| 103 | self.complete(alpha=self.pr.suggested_alpha, message=message, elapsed=elapsed) |
---|
| 104 | else: |
---|
| 105 | |
---|
| 106 | # Look at smaller values |
---|
| 107 | # We assume that for the suggested alpha, we have 1 peak |
---|
| 108 | # if not, send a message to change parameters |
---|
| 109 | alpha = self.pr.suggested_alpha |
---|
| 110 | best_alpha = self.pr.suggested_alpha |
---|
| 111 | found = False |
---|
| 112 | for i in range(10): |
---|
| 113 | self.pr.alpha = (0.33)**(i+1)*alpha |
---|
| 114 | out, cov = self.pr.lstsq(self.nfunc) |
---|
| 115 | #osc = self.pr.oscillations(out) |
---|
| 116 | #print self.pr.alpha, osc |
---|
| 117 | |
---|
| 118 | peaks = self.pr.get_peaks(out) |
---|
| 119 | print self.pr.alpha, peaks |
---|
| 120 | if peaks>1: |
---|
| 121 | found = True |
---|
| 122 | break |
---|
| 123 | best_alpha = self.pr.alpha |
---|
| 124 | |
---|
| 125 | # If we didn't find a turning point for alpha and |
---|
| 126 | # the initial alpha already had only one peak, |
---|
| 127 | # just return that |
---|
| 128 | if not found and initial_peaks==1 and initial_alpha<best_alpha: |
---|
| 129 | best_alpha = initial_alpha |
---|
| 130 | |
---|
| 131 | # Check whether the size makes sense |
---|
| 132 | message=None |
---|
| 133 | |
---|
| 134 | if not found: |
---|
| 135 | message = "None" |
---|
| 136 | elif best_alpha>=0.5*self.pr.suggested_alpha: |
---|
| 137 | # best alpha is too big, return a |
---|
| 138 | # reasonable value |
---|
| 139 | message = "The estimated alpha for your system is too large. " |
---|
| 140 | message += "Try increasing your maximum distance." |
---|
| 141 | |
---|
| 142 | self.complete(alpha=best_alpha, message=None, elapsed=elapsed) |
---|
[f3d51f6] | 143 | |
---|
| 144 | except: |
---|
| 145 | if not self.error_func==None: |
---|
| 146 | printEVT("EstimatePr.compute: %s" % sys.exc_value) |
---|
| 147 | |
---|
| 148 | |
---|