~ubuntu-branches/ubuntu/vivid/pymca/vivid-proposed

« back to all changes in this revision

Viewing changes to PyMca/SimpleMath.py

  • Committer: Package Import Robot
  • Author(s): Picca Frédéric-Emmanuel
  • Date: 2014-10-01 21:38:53 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20141001213853-b6b199r0ty8smcxd
Tags: 4.7.4+dfsg-1
* Imported Upstream version 4.7.4+dfsg
* debian/patches
  - 0003-forwaded-upstream-allow-to-build-with-the-system-qhu.patch
    (an equivalent patch was applyed by upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
                              right=yInterPrime[-1])
68
68
        return x, result
69
69
 
70
 
    def average(self,xdata0,ydata0):
71
 
        #check if all the x axis are identical (no interpolation needed)
72
 
        allthesamex=1
73
 
        x0=xdata0[0]
74
 
        for xaxis in xdata0:
75
 
            if len(x0) == len(xaxis):
76
 
                if numpy.alltrue(x0==xaxis):
 
70
    def average(self, xarr, yarr, x=None):
 
71
        """
 
72
        :param xarr : List containing x values in 1-D numpy arrays
 
73
        :param yarr : List containing y Values in 1-D numpy arrays
 
74
        :param x: x values of the final average spectrum (or None)
 
75
        :return: Average spectrum. In case of invalid input (None, None) tuple is returned.
 
76
 
 
77
        From the spectra given in xarr & yarr, the method determines the overlap in
 
78
        the x-range. For spectra with unequal x-ranges, the method interpolates all
 
79
        spectra on the values given in x if provided or the first curve and averages them.
 
80
        """
 
81
        if (len(xarr) != len(yarr)) or\
 
82
           (len(xarr) == 0) or (len(yarr) == 0):
 
83
            if DEBUG:
 
84
                print('specAverage -- invalid input!')
 
85
                print('Array lengths do not match or are 0')
 
86
            return None, None 
 
87
 
 
88
        same = True
 
89
        if x == None:
 
90
            SUPPLIED = False
 
91
            x0 = xarr[0]
 
92
        else:
 
93
            SUPPLIED = True
 
94
            x0 = x
 
95
        for x in xarr:
 
96
            if len(x0) == len(x):
 
97
                if numpy.all(x0 == x):
77
98
                    pass
78
99
                else:
79
 
                    allthesamex=0
 
100
                    same = False
80
101
                    break
81
102
            else:
82
 
                allthesamex=0
83
 
                break
84
 
 
85
 
        if allthesamex:
86
 
            xdata=[]
87
 
            ydata=[]
88
 
            i=0
89
 
            for x0 in xdata0:
90
 
                x=numpy.array(x0)
91
 
                xdata.append(x)
92
 
                ydata.append(numpy.array(ydata0[i]))
93
 
                i=i+1
94
 
                
95
 
            finalx=numpy.array(x0)
96
 
            finalx=xdata0[0]
97
 
            finaly=numpy.zeros(finalx.shape ,numpy.float)
98
 
            i = 0
99
 
            for x0 in xdata0:
100
 
                finaly += ydata[i]
101
 
                i=i+1
102
 
        else:
103
 
            #sort the data
104
 
            xdata=[]
105
 
            ydata=[]
106
 
            i=0
107
 
            for x0 in xdata0:
108
 
                x=numpy.array(x0)
109
 
                i1=numpy.argsort(x)
110
 
                xdata.append(numpy.take(x,i1))
111
 
                ydata.append(numpy.take(numpy.array(ydata0[i]),i1))
112
 
                i=i+1         
113
 
            
114
 
            #get the max and the min x axis
115
 
            xmin=xdata[0][0]
116
 
            xmax=xdata[0][-1]
117
 
            for x in xdata:
118
 
                if xmin < x[0]:
119
 
                    xmin=x[0]
120
 
                if xmax > x[-1]:
121
 
                    xmax=x[-1]
122
 
            #take the data in between
123
 
            x=[]
124
 
            y=[]
125
 
            i=0
126
 
            minimumLength = len(xdata[0])
127
 
            for x0 in xdata:
128
 
                i1=numpy.nonzero((x0>=xmin) & (x0<=xmax))[0]
129
 
                x.append(numpy.take(x0,i1))
130
 
                y.append(numpy.take(numpy.array(ydata[i]),i1))
131
 
                if len(x0) < minimumLength:
132
 
                    minimumLength = len(x0)
133
 
                i=i+1
134
 
 
135
 
            if minimumLength < 2:
136
 
                raise ValueError("Not enough points to take a meaningfull average")
137
 
            #take as x axis the first
138
 
            finalx=x[0]
139
 
            for i in range(len(x)):
140
 
                if x[i][0] > finalx[0]:
141
 
                    finalx = x[i] 
142
 
            finaly=numpy.zeros(finalx.shape, numpy.float)
143
 
            j=-1
144
 
            allthesamex=0
145
 
            for p in range(len(finalx)):
146
 
              point=finalx[p] 
147
 
              i=0
148
 
              j=j+1
149
 
              try:            
150
 
                for x0 in x:
151
 
                    if allthesamex:
152
 
                        finaly[p]+=y[i][p]
153
 
                    else:
154
 
                        i1=max(numpy.nonzero(x0<=point)[0])
155
 
                        i2=min(numpy.nonzero(x0>=point)[0])
156
 
                        if i1 >= i2:
157
 
                            #take the point as it is
158
 
                            finaly[p]+=y[i][i1]
159
 
                        else:
160
 
                            #interpolation
161
 
                            A=(x0[i2]-point)/(x0[i2]-x0[i1])
162
 
                            B=1.-A
163
 
                            finaly[p]+=A*y[i][i1]+B*y[i][i2]
164
 
                    i=i+1
165
 
              except:
166
 
                break
167
 
        if allthesamex:
168
 
              finalx=finalx[0:]
169
 
              finaly=finaly[0:]/len(xdata0)      
170
 
        else:
171
 
              finalx=finalx[0:j]
172
 
              finaly=finaly[0:j]/len(xdata0)
173
 
     
174
 
        return finalx,finaly
 
103
                same = False
 
104
                break
 
105
 
 
106
        xsort = []
 
107
        ysort = []
 
108
        for (x,y) in zip(xarr, yarr):
 
109
            if numpy.all(numpy.diff(x) > 0.):
 
110
                # All values sorted
 
111
                xsort.append(x)
 
112
                ysort.append(y)
 
113
            else:
 
114
                # Sort values
 
115
                mask = numpy.argsort(x)
 
116
                xsort.append(x.take(mask))
 
117
                ysort.append(y.take(mask))
 
118
 
 
119
        if SUPPLIED:
 
120
            xmin0 = x0.min()
 
121
            xmax0 = x0.max()
 
122
        else:
 
123
            xmin0 = xsort[0][0]
 
124
            xmax0 = xsort[0][-1]
 
125
        if (not same) or (not SUPPLIED):
 
126
            # Determine global xmin0 & xmax0
 
127
            for x in xsort:
 
128
                xmin = x.min()
 
129
                xmax = x.max()
 
130
                if xmin > xmin0:
 
131
                    xmin0 = xmin
 
132
                if xmax < xmax0:
 
133
                    xmax0 = xmax
 
134
            if xmax <= xmin:
 
135
                if DEBUG:
 
136
                    print('specAverage -- ')
 
137
                    print('No overlap between spectra!')
 
138
                return numpy.array([]), numpy.array([])
 
139
 
 
140
        # Clip xRange to maximal overlap in spectra
 
141
        mask = numpy.nonzero((x0 >= xmin0) & 
 
142
                             (x0 <= xmax0))[0]
 
143
        xnew = numpy.take(x0, mask)
 
144
        ynew = numpy.zeros(len(xnew))
 
145
 
 
146
        # Perform average
 
147
        for (x, y) in zip(xsort, ysort):
 
148
            if same:
 
149
                ynew += y  
 
150
            else:
 
151
                yinter = numpy.interp(xnew, x, y)
 
152
                ynew   += numpy.asarray(yinter)
 
153
        num = len(yarr)
 
154
        ynew /= num
 
155
        return xnew, ynew
175
156
 
176
157
    def smooth(self, *var, **kw):
177
158
        """