source: sasview/sansview/perspectives/fitting/gui_thread.py @ 2b63df0

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 2b63df0 was 2b63df0, checked in by Gervaise Alina <gervyh@…>, 15 years ago

add thread for selected compute chisqrt

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