source: sasview/sansview/perspectives/fitting/invariant.py @ 70d975c

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

working on invariant

  • Property mode set to 100644
File size: 4.2 KB
Line 
1"""
2    This module is responsible to compute invariant related computation.
3    @author: Gervaise B. Alina/UTK
4"""
5import numpy
6class InvariantCalculator(object):
7    """
8        Compute invariant
9    """
10    def __init__(self):
11        """
12            Initialize variables
13        """
14        self.x =[]
15        self.y =[]
16        self.dxl = None
17       
18    def setData(self, x=[], y=[], dxl=None):
19        """
20            set the data
21        """
22        tempx = numpy.array(x)
23        tempy = numpy.array(y)
24        #Make sure data is order before computing
25        ind = numpy.lexsort((tempy,tempx))
26        self.x = tempx[ind]
27        self.y = tempy[ind]
28        if dxl !=None:
29            tempdxl= numpy.array(dxl)
30            self.dxl= tempdxl[ind]
31        else:
32            self.dxl=dxl
33     
34   
35    def computeInvariant(self):
36        """
37            Compute invariant
38        """
39        if len(self.x)<=1 or len(self.y)<=1 or len(self.x)!=len(self.y):
40            msg=  "Length x and y must be equal"
41            msg +=" and greater than 1; got x=%s, y=%s"%(len(self.x),len(self.y))
42            raise ValueError,msg
43        elif len(self.x)==1 and len(self.y)==1:
44            return 0
45   
46        else:
47            n= len(self.x)-1
48            #compute the first delta
49            dx0= self.x[1]- self.x[0]
50            #compute the last delta
51            dxn= self.x[n]- self.x[n-1]
52            sum = 0
53            sum += self.x[0]* self.x[0]* self.y[0]*dx0
54            sum += self.x[n]* self.x[n]* self.y[n]*dxn
55            if len(self.x)==2:
56                return sum
57            else:
58                #iterate between for element different from the first and the last
59                for i in xrange(1, n-1):
60                    dxi = (self.x[i+1] - self.x[i-1])/2
61                    sum += self.x[i]*self.x[i]* self.y[i]* dxi
62                return sum
63           
64               
65    def computeSmearInvariant(self):
66        """
67            Compute invariant with smearing info
68        """
69        if self.dxl ==None:
70            msg = "Cannot compute Smear invariant dxl "
71            msg +="must be a list, got dx= %s"%str(self.dxl)
72            raise ValueError,msg
73
74        if len(self.x)<=1 or len(self.y)<=1 or len(self.x)!=len(self.y)\
75                or len(self.x)!= len(self.dxl):
76            msg=  "Length x and y must be equal"
77            msg +=" and greater than 1; got x=%s, y=%s"%(len(self.x),len(self.y))
78            raise ValueError,msg
79        else:
80            n= len(self.x)-1
81            #compute the first delta
82            dx0= self.x[1]- self.x[0]
83            #compute the last delta
84            dxn= self.x[n]- self.x[n-1]
85            sum = 0
86            sum += self.x[0]* self.dxl[0]* self.y[0]*dx0
87            sum += self.x[n]* self.dxl[n]* self.y[n]*dxn
88            if len(self.x)==2:
89                return sum
90            else:
91                #iterate between for element different from the first and the last
92                for i in xrange(1, n-1):
93                    dxi = (self.x[i+1] - self.x[i-1])/2
94                    sum += self.x[i]*self.dxl[i]* self.y[i]* dxi
95                return sum
96           
97if __name__=="__main__":
98    # test the module
99    x=[1,2,3,4,10]
100    y=[2,3,4,5,6]
101   
102    I= InvariantCalculator()
103    I.setData(x=x, y=y)
104    invariant = I.computeInvariant()
105    print "1-Invariant : ", invariant
106   
107    x=[0,1]
108    y=[0,2]
109    I.setData(x=x, y=y)
110    invariant = I.computeInvariant()
111    print "2-Invariant : ", invariant
112   
113    x=[1,3,4,10,2]
114    y=[2,4,5,6,3]
115    I.setData(x=x, y=y)
116    invariant = I.computeInvariant()
117    print "3-Invariant : ", invariant
118   
119    # compute invariant with smear information
120    from sans.guiframe.dataFitting import Data1D
121    data1= Data1D(x=x,y=y )
122    data1.dxl =[0.1,0.1,0.1,0.1,0.1]
123   
124    I.setData(x= data1.x, y= data1.y, dxl=data1.dxl)
125    invariant = I.computeSmearInvariant()
126    print "4-Smear Invariant:", invariant
127    I.setData(x= data1.x, y= data1.y)
128    try:
129        invariant = I.computeSmearInvariant()
130    except:
131        print "5-Smear Invariant error for dxl=None"
Note: See TracBrowser for help on using the repository browser.