Пример #1
0
    def _smooth_spline_scikit(self,
                              data1,
                              data2,
                              xhat=None,
                              fixNonMonotonous=False):
        """Smoothing of 2D data using generalized crossvalidation

        Will return the evaluated data at the points xhat (or if xhat is empty,
        at the points of data1). Note that the result _will_ depend on xhat
        since the optimization function will try to maximize the smoothness of
        the line generated by (xhat,yhat).

        Do not call this function with a large set of (or too densily spaced)
        evaluation data. Rather use the wrap function.

        uses datasmooth from https://github.comtickel/scikit-datasmooth.git
        """
        try:
            from scikits import datasmooth as ds
        except ImportError:
            print("===================================")
            print(
                "Cannot import the module datasmooth from scikits, \nplease download it from https://github.comtickel/scikit-datasmooth.git"
            )
            print("===================================")
            import sys
            sys.exit(1)
        import operator

        x = numpy.array(data1)
        y = numpy.array(data2)
        if xhat is None:
            xhat = numpy.array(x)
        else:
            xhat = numpy.array(xhat)

        # Step 1: get the indices of the original xhat
        tmp_xhat = sorted(enumerate(xhat), key=operator.itemgetter(1))
        xhat_sorted = numpy.array([t[1] for t in tmp_xhat])
        xhat_indices = [t[0] for t in tmp_xhat]

        # fix if not monotonous increasing...
        if fixNonMonotonous:
            xhat_sorted, duplications = self.de_duplicate_array(xhat_sorted)
            xhat_sorted = numpy.array(xhat_sorted)

        # Step 2: Execute the call to smooth the data
        # throws memory error for large data -> use the wrapper
        yhat_sorted, lmbd = ds.smooth_data(x, y, xhat=xhat_sorted)

        # Step 3.1 re-insert duplicated values
        if fixNonMonotonous:
            yhat_sorted = self.re_duplicate_array(yhat_sorted, duplications)

        # Step 3: re-order the values using the original indices from before
        yhat = [None for i in range(numpy.size(xhat))]
        for i in range(numpy.size(xhat)):
            yhat[xhat_indices[i]] = yhat_sorted[i]

        return yhat
Пример #2
0
    def _smooth_spline_scikit(self, data1, data2, xhat=None, fixNonMonotonous=False):
        """Smoothing of 2D data using generalized crossvalidation

        Will return the evaluated data at the points xhat (or if xhat is empty,
        at the points of data1). Note that the result _will_ depend on xhat
        since the optimization function will try to maximize the smoothness of
        the line generated by (xhat,yhat).

        Do not call this function with a large set of (or too densily spaced)
        evaluation data. Rather use the wrap function.

        uses datasmooth from https://github.comtickel/scikit-datasmooth.git
        """
        try:
          from scikits import datasmooth as ds
        except ImportError:
            print "==================================="
            print "Cannot import the module datasmooth from scikits, \nplease download it from https://github.comtickel/scikit-datasmooth.git"
            print "==================================="
            import sys; sys.exit(1)
        import operator

        x = numpy.array(data1)
        y = numpy.array(data2)
        if xhat is None:
            xhat = numpy.array(x)
        else:
            xhat = numpy.array(xhat)

        # Step 1: get the indices of the original xhat
        tmp_xhat = sorted(enumerate(xhat), key=operator.itemgetter(1))
        xhat_sorted = numpy.array([t[1] for t in tmp_xhat])
        xhat_indices = [t[0] for t in tmp_xhat]

        # fix if not monotonous increasing... 
        if fixNonMonotonous:
            xhat_sorted,duplications = self.de_duplicate_array(xhat_sorted)
            xhat_sorted = numpy.array(xhat_sorted)

        # Step 2: Execute the call to smooth the data
        # throws memory error for large data -> use the wrapper 
        yhat_sorted,lmbd = ds.smooth_data(x,y,xhat=xhat_sorted)

        # Step 3.1 re-insert duplicated values
        if fixNonMonotonous:
            yhat_sorted = self.re_duplicate_array(yhat_sorted, duplications)

        # Step 3: re-order the values using the original indices from before
        yhat = [None for i in range(numpy.size(xhat))]
        for i in range(numpy.size(xhat)):
            yhat[ xhat_indices[i] ] = yhat_sorted[i]

        return yhat
Пример #3
0
def smooth( x, y, width=None, window='hanning' ):
    '''
    Smooth the input spectrum y (on wl x) with a <window> kernel
     of width ~ width (in x units)
    If width is not given, chooses an optimal smoothing width.
    <window> options: 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
    Returns the smoothed y array.
    '''
    if width == None:
        ys,l = datasmooth.smooth_data(x,y,midpointrule=True)
        print 'chose smoothing lambda of',l
        return ys
    # if given an explicit width, do it all out here
    if y.ndim != 1:
        raise ValueError, "smooth only accepts 1 dimension arrays."
    if x.size != y.size:
        raise ValueError, "Input x,y vectors must be of same size"
    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError, "Window must be one of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
    avg_width = np.abs(np.mean(x[1:]-x[:-1]))
    window_len = int(round(width/avg_width))
    if y.size < window_len:
        raise ValueError, "Input vector needs to be bigger than window size."
    if window_len<3:
        return y

    s=np.r_[y[window_len-1:0:-1],y,y[-1:-window_len:-1]]

    if window == 'flat': #moving average
        w=np.ones(window_len,'d')
    else:
        w=eval('np.'+window+'(window_len)')

    y=np.convolve(w/w.sum(),s,mode='valid')
    yout = y[(window_len/2):-(window_len/2)]
    if len(yout) < len(x):
        yout = y[(window_len/2):-(window_len/2)+1]
    elif len(yout) > len(x):
        yout = y[(window_len/2):-(window_len/2)-1]
    return yout
Пример #4
0
from scikits import datasmooth as ds

# create simulated data
npts = 50
xmin = 0
xspan = 2 * np.pi
x = xmin + xspan * np.random.rand(npts)

yt = np.sin(x)
stdev = 1e-1 * np.max(yt)
y = yt + stdev * np.random.randn(npts)

# perform the smoothing
d = 4
Nhat = 200
xmin = np.min(x)
xmax = np.max(x)
xh = np.linspace(xmin - 0.1, xmax + 0.1, Nhat)

yh, lmbd = ds.smooth_data(x, y, d, xhat=xh)

yht = np.sin(xh)

print('scaled regularization parameter =', lmbd)

cla()
plot(x, y, 'ow', xh, yh, '-b', xh, yht, '-r')
legend(['scattered', 'smoothed', 'true'], loc='best', numpoints=1)
show()
draw()
from scikits import datasmooth as ds

# create simulated data
npts = 50
xmin = 0
xspan = 2*np.pi
x = xmin + xspan*np.random.rand(npts)

yt = np.sin(x)
stdev = 1e-1*np.max(yt)
y = yt + stdev*np.random.randn(npts)

# perform the smoothing
d = 4
Nhat = 200
xmin = np.min(x)
xmax = np.max(x)
xh = np.linspace(xmin-0.1,xmax+0.1,Nhat)

yh,lmbd = ds.smooth_data(x,y,d,xhat=xh)

yht = np.sin(xh)

print 'scaled regularization parameter =', lmbd

cla()
plot(x,y,'ow',xh,yh,'-b',xh,yht,'-r')
legend(['scattered','smoothed','true'],loc='best',numpoints=1)
show()
draw()
Пример #6
0
from __future__ import print_function

import numpy as np
from matplotlib.pyplot import *
from scikits import datasmooth as ds

# create simulated data
npts = 100
xmin = 0
xspan = 2*np.pi
x = np.linspace(xmin,xmin+xspan,npts)
# give variability to x if desired
x = x + xspan/npts*(np.random.rand(npts)-0.5)

yt = np.sin(x)
stdev = 1e-1*np.max(yt)
y = yt + stdev*np.random.randn(npts)

# perform the smoothing
d = 4
yh,lmbd = ds.smooth_data(x,y,d,stdev=stdev,midpointrule=True)

print('scaled regularization parameter =', lmbd)
print('standard deviation =', np.std(y-yh,ddof=1))

cla()
plot(x,y,'ow',x,yh,'-b',x,yt,'-r')
legend(['data','smoothed','true'], numpoints=1, loc='best')
show()
draw()