source: sasview/sansview/perspectives/fitting/gui_thread.py @ 15ff344

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 15ff344 was 15ff344, checked in by Jae Cho <jhjcho@…>, 15 years ago

fixed more about dy

  • Property mode set to 100644
File size: 5.4 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        x,y = [numpy.asarray(v) for v in (self.x,self.y)]
55        if self.dy==None or self.dy==[]:
56            self.dy= numpy.zeros(len(self.y))
57        self.dy[self.dy==0]=1
58        if self.qmin==None:
59            self.qmin= min(self.x)
60       
61        if self.qmax==None:
62            self.qmax= max(self.x)
63           
64        fx = numpy.zeros(len(self.x)) 
65       
66        output= None
67        res=[]
68        try: 
69           
70            for i_x in range(len(self.x)):
71               
72                # Check whether we need to bail out
73                self.isquit()   
74                fx[i_x]=self.model.run(self.x[i_x])
75               
76            if self.smearer!=None:
77                fx= self.smearer(fx)
78            for i_y in range(len(fx)):
79                # Check whether we need to bail out
80                self.isquit()   
81                temp=(self.y[i_y] - fx[i_y])/self.dy[i_y]
82                res.append(temp*temp)
83           
84           
85            #sum of residuals
86            sum=0
87            for item in res:
88                # Check whether we need to bail out
89                self.isquit() 
90                if numpy.isfinite(item):
91                    sum +=item
92            output = sum/ len(res)
93            elapsed = time.time()-self.starttime
94            self.complete(output= output,  elapsed=elapsed)
95           
96        except KeyboardInterrupt:
97            # Thread was interrupted, just proceed and re-raise.
98            # Real code should not print, but this is an example...
99            raise
100        except:
101            raise
102       
103class CalcChisqr2D(CalcThread):
104    """
105       Compute chisqr
106    """
107   
108    def __init__(self, x_bins, y_bins,data,err_data, model,
109                 qmin,
110                 qmax,
111                 completefn = None,
112                 updatefn   = None,
113                 yieldtime  = 0.01,
114                 worktime   = 0.01
115                 ):
116        CalcThread.__init__(self,completefn,
117                 updatefn,
118                 yieldtime,
119                 worktime)
120     
121        self.y_bins = y_bins
122        self.x_bins = x_bins
123        self.data= data
124        self.err_data= copy.deepcopy(err_data)
125        self.model = model
126        self.qmin = qmin
127        self.qmax = qmax
128     
129        self.starttime = 0 
130       
131    def isquit(self):
132        """
133             @raise KeyboardInterrupt: when the thread is interrupted
134        """
135        try:
136            CalcThread.isquit(self)
137        except KeyboardInterrupt:
138            raise KeyboardInterrupt   
139       
140       
141    def compute(self):
142        """
143            Compute the data given a model function
144        """
145        self.starttime = time.time()
146        if self.err_data==None or self.err_data==[]:
147            self.err_data= numpy.zeros(len(self.x_bins),len(self.y_bins))
148           
149        self.err_data[self.err_data==0]=1
150           
151        output= None
152        res=[]
153        try:
154         
155            for i in range(len(self.x_bins)):
156                # Check whether we need to bail out
157                self.isquit()   
158                for j in range(len(self.y_bins)):
159                    #Check the range containing data between self.qmin_x and self.qmax_x
160                    value =  math.pow(self.x_bins[i],2)+ math.pow(self.y_bins[j],2)
161                    if value >= math.pow(self.qmin,2) and value <= math.pow(self.qmax,2):
162                       
163                        temp = [self.x_bins[i],self.y_bins[j]]
164                        error= self.err_data[j][i]
165                        chisqrji = (self.data[j][i]- self.model.runXY(temp ))/error
166                        #Vector containing residuals
167                        res.append( math.pow(chisqrji,2) )
168 
169            sum=0
170            for item in res:
171                # Check whether we need to bail out
172                self.isquit() 
173                if numpy.isfinite(item):
174                    sum +=item
175            output = sum/ len(res)
176            elapsed = time.time()-self.starttime
177            self.complete(output= output,  elapsed=elapsed)
178           
179        except KeyboardInterrupt:
180            # Thread was interrupted, just proceed and re-raise.
181            # Real code should not print, but this is an example...
182            raise
Note: See TracBrowser for help on using the repository browser.