Example #1
0
    def apply_along_axis(self, func1d, axis=-1, ylabel=None, yunit=None):
        """Apply a function to 1-D slices along the given axis.

        Execute `func1d(a, x)` where `func1d` operates on 1-D arrays and `a`
        is a 1-D slice of `w` along `axis`
        """
        axis = self.getaxis(axis)

        newy = np.apply_along_axis(func1d, axis, self._y, self.x[axis])

        if newy.shape == self.shape:
            return Waveform(self.x, newy, xlabels=self.xlabels, 
                            xunits=self.xunits,
                            ylabel=ylabel, yunit=yunit)
        else:
            if len(newy.shape) == 0:
                return newy
            elif newy.shape == tuple(remove_index(self.shape, axis)):
                return Waveform(remove_index(self.x, axis), newy, 
                                xlabels=remove_index(self.xlabels, axis),
                                xunits=remove_index(self.xunits, axis),
                                ylabel=ylabel, yunit=yunit)
            else:
                print newy.shape, tuple(remove_index(self.shape, axis))
                raise ValueError('Shape mismatch')
Example #2
0
    def apply_along_axis(self, func1d, axis=-1, ylabel=None, yunit=None):
        """Apply a function to 1-D slices along the given axis.

        Execute `func1d(a, x)` where `func1d` operates on 1-D arrays and `a`
        is a 1-D slice of `w` along `axis`
        """
        axis = self.getaxis(axis)

        newy = np.apply_along_axis(func1d, axis, self._y, self.x[axis])

        if newy.shape == self.shape:
            return Waveform(self.x,
                            newy,
                            xlabels=self.xlabels,
                            xunits=self.xunits,
                            ylabel=ylabel,
                            yunit=yunit)
        else:
            if len(newy.shape) == 0:
                return newy
            elif newy.shape == tuple(remove_index(self.shape, axis)):
                return Waveform(remove_index(self.x, axis),
                                newy,
                                xlabels=remove_index(self.xlabels, axis),
                                xunits=remove_index(self.xunits, axis),
                                ylabel=ylabel,
                                yunit=yunit)
            else:
                raise ValueError('Shape mismatch')
Example #3
0
    def value(self, x, axis = -1):
        """Returns and interpolated at the given x-value
        
        *x* can be a number or a waveform where the number of dimensions of x is
        is one less than the waveform it is operating on
           
        
        Examples:

        1-d waveform

        >>> w1=Waveform(array([1,2,3]),array([3,5,6]))
        >>> w1.value(1.5)
        4.0

        2-d waveform
        >>> w2=Waveform([[1,2],[2,3,4]], array([[3,5,6], [4,6,7]]))
        >>> w2.value(2.5)
        Waveform(array([1, 2]), array([ 4.,  5.]))
        
        `x` is a waveform
        >>> w2=Waveform([[1,2],[2,3,4]], array([[3,5,6], [4,6,7]]))
        >>> w2.value(Waveform([[1, 2]], array([2.5, 3.5])))
        Waveform(array([1, 2]), array([ 4. ,  6.5]))

        """
        axis = self.getaxis(axis)
        def findvalue(y):
            if len(self._xlist[axis]) == 1 and axis == -1:
                return y[-1]
            res = sp.interpolate.interp1d(self._xlist[axis], y)(x)
            try:
                return np.asscalar(res)
            except TypeError:
                return res

        def findvalue_mdimindex(y, i):
            xindex = list(i)
            del xindex[axis]
            xindex = tuple(xindex)
            res = sp.interpolate.interp1d(self._xlist[axis], y)(x._y[xindex])
            try:
                return np.asscalar(res)
            except TypeError:
                return res
            

        if iswave(x):
            newyshape = list(self._y.shape)
            del newyshape[axis]
            newyshape = tuple(newyshape)
            newy = apply_along_axis_with_idx(findvalue_mdimindex,
                                             axis,
                                             self._y).reshape(newyshape)
            return reducedim(self, newy, axis=axis)

        outw = applyfunc_and_reducedim(findvalue, self, axis = axis)
        if outw and not isscalar(outw):
            outw.ylabel = self.ylabel
            outw.yunit = self.yunit
            outw.xunits = remove_index(self.xunits, axis)
            outw.xlabels = remove_index(self.xlabels, axis)

        return outw