source: sasview/sansview/perspectives/fitting/gui_thread.py @ 52611f5

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 52611f5 was 52611f5, checked in by Gervaise Alina <gervyh@…>, 15 years ago

singular point handler for compute chisqr

  • Property mode set to 100644
File size: 5.6 KB
Line 
1
2
3import time
4import numpy
5import copy
6import math
7import sys
8import wx
9
10from calcthread import CalcThread
11
12class CalcChisqr1D(CalcThread):
13    """
14       Compute chisqr
15    """
16    def __init__(self, x, y,dy, model,
17                 smearer=None,
18                 qmin=None,
19                 qmax=None,
20                 completefn = None,
21                 updatefn   = None,
22                 yieldtime  = 0.01,
23                 worktime   = 0.01
24                 ):
25        CalcThread.__init__(self,completefn,
26                 updatefn,
27                 yieldtime,
28                 worktime)
29        self.smearer =smearer
30        self.y = numpy.array(y)
31        self.x = numpy.array(x)
32        self.dy= copy.deepcopy(dy)
33        self.model = model
34        self.qmin = qmin
35        self.qmax = qmax
36        self.smearer = smearer
37        self.starttime = 0 
38       
39    def isquit(self):
40        """
41             @raise KeyboardInterrupt: when the thread is interrupted
42        """
43        try:
44            CalcThread.isquit(self)
45        except KeyboardInterrupt:
46            raise KeyboardInterrupt   
47       
48       
49    def compute(self):
50        """
51            Compute the data given a model function
52        """
53        self.starttime = time.time()
54       
55        x,y = [numpy.asarray(v) for v in (self.x,self.y)]
56        if self.dy==None or self.dy==[]:
57            self.dy= numpy.zeros(len(self.y))
58        self.dy[self.dy==0]=1
59       
60        if self.qmin==None:
61            if min (self.x) ==0.0 and self.x[0]==0 and not numpy.isfinite(self.y[0]):
62                self.qmin = min(self.x[sel.x!=0])
63            else:
64                self.qmin= min(self.x)
65       
66        if self.qmax==None:
67            self.qmax= max(self.x)
68           
69        fx = numpy.zeros(len(self.x)) 
70       
71        output= None
72        res=[]
73        try: 
74           
75            for i_x in range(len(self.x)):
76               
77                # Check whether we need to bail out
78                self.isquit() 
79               
80                fx[i_x]=self.model.run(self.x[i_x])
81               
82            if self.smearer!=None:
83                fx= self.smearer(fx)
84               
85            for i_y in range(len(fx)):
86                # Check whether we need to bail out
87                self.isquit()   
88               
89                temp=(self.y[i_y] - fx[i_y])/self.dy[i_y]
90                res.append(temp*temp)
91            #sum of residuals
92            sum=0
93            for item in res:
94                # Check whether we need to bail out
95                self.isquit() 
96                if numpy.isfinite(item):
97                    sum +=item
98            output = sum/ len(res)
99            elapsed = time.time()-self.starttime
100            self.complete(output= output,  elapsed=elapsed)
101           
102        except KeyboardInterrupt:
103            # Thread was interrupted, just proceed and re-raise.
104            # Real code should not print, but this is an example...
105            raise
106        except:
107            raise
108       
109class CalcChisqr2D(CalcThread):
110    """
111       Compute chisqr
112    """
113   
114    def __init__(self, x_bins, y_bins,data,err_data, model,
115                 qmin,
116                 qmax,
117                 completefn = None,
118                 updatefn   = None,
119                 yieldtime  = 0.01,
120                 worktime   = 0.01
121                 ):
122        CalcThread.__init__(self,completefn,
123                 updatefn,
124                 yieldtime,
125                 worktime)
126     
127        self.y_bins = y_bins
128        self.x_bins = x_bins
129        self.data= data
130        self.err_data= copy.deepcopy(err_data)
131        self.model = model
132        self.qmin = qmin
133        self.qmax = qmax
134     
135        self.starttime = 0 
136       
137    def isquit(self):
138        """
139             @raise KeyboardInterrupt: when the thread is interrupted
140        """
141        try:
142            CalcThread.isquit(self)
143        except KeyboardInterrupt:
144            raise KeyboardInterrupt   
145       
146       
147    def compute(self):
148        """
149            Compute the data given a model function
150        """
151        self.starttime = time.time()
152        if self.err_data==None or self.err_data==[]:
153            self.err_data= numpy.zeros(len(self.x_bins),len(self.y_bins))
154           
155        self.err_data[self.err_data==0]=1
156           
157        output= None
158        res=[]
159        try:
160         
161            for i in range(len(self.x_bins)):
162                # Check whether we need to bail out
163                self.isquit()   
164                for j in range(len(self.y_bins)):
165                    #Check the range containing data between self.qmin_x and self.qmax_x
166                    value =  math.pow(self.x_bins[i],2)+ math.pow(self.y_bins[j],2)
167                    if value >= math.pow(self.qmin,2) and value <= math.pow(self.qmax,2):
168                       
169                        temp = [self.x_bins[i],self.y_bins[j]]
170                        error= self.err_data[j][i]
171                        chisqrji = (self.data[j][i]- self.model.runXY(temp ))/error
172                        #Vector containing residuals
173                        res.append( math.pow(chisqrji,2) )
174 
175            sum=0
176            for item in res:
177                # Check whether we need to bail out
178                self.isquit() 
179                if numpy.isfinite(item):
180                    sum +=item
181            output = sum/ len(res)
182            elapsed = time.time()-self.starttime
183            self.complete(output= output,  elapsed=elapsed)
184           
185        except KeyboardInterrupt:
186            # Thread was interrupted, just proceed and re-raise.
187            # Real code should not print, but this is an example...
188            raise
Note: See TracBrowser for help on using the repository browser.