Example #1
0
def gfit1d(y, x=None, err = None, weights=None, par=None, parinfo=None,
maxiter=200, quiet=0):
    """
    Return the gaussian fit as an object.

    Parameters
    ----------
    y:   1D Numarray array
        The data to be fitted
    x:   1D Numarray array
        (optional) The x values of the y array. x and y must
        have the same shape.
    err: 1D Numarray array
        (optional) 1D array with measurement errors, must be
        the same shape as y
    weights: 1D Numarray array
        (optiional) 1D array with weights, must be the same
        shape as y
    par:  List
        (optional) Starting values for the parameters to be fitted
    parinfo: Dictionary of lists
        (optional) provides additional information for the
        parameters. For a detailed description see nmpfit.py.
        Parinfo can be used to limit parameters or keep
        some of them fixed.
    maxiter: number
        Maximum number of iterations to perform
        Default: 200
    quiet: number
        if set to 1, nmpfit does not print to the screen
        Default: 0

    Examples
    --------
    >>> x=N.arange(10,20, 0.1)
    >>> y= 10*N.e**(-(x-15)**2/4)
    >>> print gfit1d(y,x=x, maxiter=20,quiet=1).params
    [ 10.          15.           1.41421356]

    """
    if numerixenv.check_input(x) or numerixenv.check_input(y):
        raise ValueError, "Input is a NumArray array. This version of %s requires a Numpy array\n" % __name__
    
    y = y.astype(N.float)
    if weights != None:
        weights = weights.astype(N.float)
    if err != None:
        err = err.astype(N.float)
    if x == None and len(y.shape)==1 :
        x = N.arange(len(y)).astype(N.float)
    if x.shape != y.shape:
        print "input arrays X and Y must be of equal shape.\n"
        return


    fa = {'x':x, 'y':y, 'err':err, 'weights':weights}

    if par != None:
        p = par
    else:
        ysigma = y.std()
        ind = N.nonzero(y > ysigma)[0]
        if len(ind) != 0:
            xind = int(ind.mean())
            p2 = x[xind]
            p1 = y[xind]
            p3 = 1.0
        else:
            ymax = y.max()
            ymin = y.min()
            ymean= y.mean()
            if (ymax - ymean) > (abs(ymin - ymean)):
                p1 = ymax
            else: p1 = ymin
            ind = (N.nonzero(y == p1))[0]
            p2 = x.mean()
            p3 = 1.


        p = [p1, p2, p3]
    m=nmpfit.mpfit(_gauss_funct, p,parinfo = parinfo, functkw=fa,
maxiter=maxiter, quiet=quiet)
    if (m.status <=0): print 'error message = ', m.errmsg
    return m
Example #2
0
y=nn[nMaxArg-5:nMaxArg+5]
#x=bins
#y=nn
print x.shape, y.shape


def g(p,fjac = None, x = None, y=None, err=None,weights=None):
    if p[2] != 0.0: 
            Z = (x - p[1])
            model = p[0]*n.e ** (-Z**2 / (p[2] * p[2] * 2.0)) 
    else: 
            model = N.zeros(N.size(x))
    status = 0 
#    if n.sqrt(y)!=0.0:
    return [status, (y-model)]#/ (y)]
#    else:
#        return [status, (y-model)]

fa = {'x':x, 'y':y}
m=nmpfit.mpfit(g,p,functkw=fa)
p=m.params
print p
#figure()
print p[2] / abs(bins[1] - bins[0])
yy = p[0] * n.e**(-0.5*(bins-p[1])**2/p[2]**2)
l=plot(bins, yy, 'b--')
setp(l, 'linewidth', 3)
grid(1)
show()

Example #3
0
#x=bins
#y=nn
print x.shape, y.shape


def g(p, fjac=None, x=None, y=None, err=None, weights=None):
    if p[2] != 0.0:
        Z = (x - p[1])
        model = p[0] * n.e**(-Z**2 / (p[2] * p[2] * 2.0))
    else:
        model = N.zeros(N.size(x))
    status = 0
    #    if n.sqrt(y)!=0.0:
    return [status, (y - model)]  #/ (y)]


#    else:
#        return [status, (y-model)]

fa = {'x': x, 'y': y}
m = nmpfit.mpfit(g, p, functkw=fa)
p = m.params
print p
#figure()
print p[2] / abs(bins[1] - bins[0])
yy = p[0] * n.e**(-0.5 * (bins - p[1])**2 / p[2]**2)
l = plot(bins, yy, 'b--')
setp(l, 'linewidth', 3)
grid(1)
show()