[41340860] | 1 | |
---|
| 2 | |
---|
| 3 | import time |
---|
[2b63df0] | 4 | import numpy |
---|
| 5 | import copy |
---|
| 6 | import math |
---|
[41340860] | 7 | import sys |
---|
| 8 | import wx |
---|
| 9 | |
---|
[e733619] | 10 | from data_util.calcthread import CalcThread |
---|
[077809c] | 11 | from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly |
---|
| 12 | |
---|
[41340860] | 13 | |
---|
[2b63df0] | 14 | class CalcChisqr1D(CalcThread): |
---|
[41340860] | 15 | """ |
---|
[2b63df0] | 16 | Compute chisqr |
---|
[41340860] | 17 | """ |
---|
[077809c] | 18 | def __init__(self, data1d, model, |
---|
[2b63df0] | 19 | smearer=None, |
---|
| 20 | qmin=None, |
---|
| 21 | qmax=None, |
---|
[41340860] | 22 | completefn = None, |
---|
| 23 | updatefn = None, |
---|
| 24 | yieldtime = 0.01, |
---|
| 25 | worktime = 0.01 |
---|
| 26 | ): |
---|
| 27 | CalcThread.__init__(self,completefn, |
---|
| 28 | updatefn, |
---|
| 29 | yieldtime, |
---|
| 30 | worktime) |
---|
[077809c] | 31 | |
---|
| 32 | if model ==None or data1d ==None: |
---|
| 33 | raise ValueError, "Need data and model to compute chisqr" |
---|
| 34 | |
---|
| 35 | if data1d.__class__.__name__ !="Data1D": |
---|
| 36 | msg= str(data1d.__class__.__name__) |
---|
| 37 | raise ValueError, "need Data1D to compute chisqr. Current class %s"%msg |
---|
| 38 | |
---|
| 39 | self.fitdata= FitData1D(data1d, smearer=smearer) |
---|
| 40 | self.fitdata.setFitRange(qmin=qmin,qmax=qmax) |
---|
[2b63df0] | 41 | self.model = model |
---|
[077809c] | 42 | |
---|
[41340860] | 43 | self.starttime = 0 |
---|
| 44 | |
---|
[2b63df0] | 45 | def isquit(self): |
---|
| 46 | """ |
---|
| 47 | @raise KeyboardInterrupt: when the thread is interrupted |
---|
| 48 | """ |
---|
| 49 | try: |
---|
| 50 | CalcThread.isquit(self) |
---|
| 51 | except KeyboardInterrupt: |
---|
| 52 | raise KeyboardInterrupt |
---|
| 53 | |
---|
| 54 | |
---|
[41340860] | 55 | def compute(self): |
---|
| 56 | """ |
---|
| 57 | Compute the data given a model function |
---|
| 58 | """ |
---|
[2b63df0] | 59 | self.starttime = time.time() |
---|
[52611f5] | 60 | |
---|
[2b63df0] | 61 | output= None |
---|
| 62 | res=[] |
---|
[077809c] | 63 | try: |
---|
[785c8233] | 64 | res = self.fitdata.residuals(self.model.evalDistribution) |
---|
[2b63df0] | 65 | sum=0 |
---|
| 66 | for item in res: |
---|
| 67 | # Check whether we need to bail out |
---|
| 68 | self.isquit() |
---|
| 69 | if numpy.isfinite(item): |
---|
[077809c] | 70 | sum +=item*item |
---|
| 71 | if len(res)>0: |
---|
| 72 | output = sum/ len(res) |
---|
| 73 | |
---|
[2b63df0] | 74 | elapsed = time.time()-self.starttime |
---|
| 75 | self.complete(output= output, elapsed=elapsed) |
---|
| 76 | |
---|
| 77 | except KeyboardInterrupt: |
---|
| 78 | # Thread was interrupted, just proceed and re-raise. |
---|
| 79 | # Real code should not print, but this is an example... |
---|
[8d78399] | 80 | raise |
---|
[bfe4644] | 81 | |
---|
[2b63df0] | 82 | |
---|
| 83 | class CalcChisqr2D(CalcThread): |
---|
| 84 | """ |
---|
| 85 | Compute chisqr |
---|
| 86 | """ |
---|
| 87 | |
---|
[077809c] | 88 | def __init__(self,data2d, model, |
---|
[2b63df0] | 89 | qmin, |
---|
| 90 | qmax, |
---|
| 91 | completefn = None, |
---|
| 92 | updatefn = None, |
---|
| 93 | yieldtime = 0.01, |
---|
| 94 | worktime = 0.01 |
---|
| 95 | ): |
---|
| 96 | CalcThread.__init__(self,completefn, |
---|
| 97 | updatefn, |
---|
| 98 | yieldtime, |
---|
| 99 | worktime) |
---|
[077809c] | 100 | |
---|
| 101 | if model ==None or data2d ==None: |
---|
| 102 | raise ValueError, "Need data and model to compute chisqr" |
---|
| 103 | |
---|
| 104 | if data2d.__class__.__name__ !="Data2D": |
---|
| 105 | msg= str(data2d.__class__.__name__) |
---|
| 106 | raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg |
---|
| 107 | |
---|
| 108 | self.fitdata = FitData2D(data2d) |
---|
| 109 | self.fitdata.setFitRange(qmin=qmin,qmax=qmax) |
---|
| 110 | |
---|
[2b63df0] | 111 | self.model = model |
---|
| 112 | |
---|
| 113 | self.starttime = 0 |
---|
| 114 | |
---|
| 115 | def isquit(self): |
---|
| 116 | """ |
---|
| 117 | @raise KeyboardInterrupt: when the thread is interrupted |
---|
| 118 | """ |
---|
| 119 | try: |
---|
| 120 | CalcThread.isquit(self) |
---|
| 121 | except KeyboardInterrupt: |
---|
| 122 | raise KeyboardInterrupt |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | def compute(self): |
---|
| 126 | """ |
---|
| 127 | Compute the data given a model function |
---|
| 128 | """ |
---|
| 129 | self.starttime = time.time() |
---|
[077809c] | 130 | |
---|
[2b63df0] | 131 | output= None |
---|
| 132 | res=[] |
---|
| 133 | try: |
---|
[785c8233] | 134 | res = self.fitdata.residuals(self.model.evalDistribution) |
---|
[2b63df0] | 135 | sum=0 |
---|
| 136 | for item in res: |
---|
| 137 | # Check whether we need to bail out |
---|
| 138 | self.isquit() |
---|
| 139 | if numpy.isfinite(item): |
---|
[077809c] | 140 | sum +=item*item |
---|
| 141 | if len(res)>0: |
---|
| 142 | output = sum/ len(res) |
---|
| 143 | |
---|
[2b63df0] | 144 | elapsed = time.time()-self.starttime |
---|
| 145 | self.complete(output= output, elapsed=elapsed) |
---|
| 146 | |
---|
| 147 | except KeyboardInterrupt: |
---|
| 148 | # Thread was interrupted, just proceed and re-raise. |
---|
| 149 | # Real code should not print, but this is an example... |
---|
[8d78399] | 150 | raise |
---|
[bfe4644] | 151 | |
---|