PEATDB/Ekin/Fitting.py
def estimateExpUncertainty(ekindata, fitdata, xuncert=0, yuncert=0,
                                 runs=10, doplots=False, callback=None, guess=True,
                                 **kwargs):
    """Chresten's method for estimating the error on each parameter due
       to the effect of the exp uncertainty, which is user provided.
       If the datapoints have x or y error values, we use that instead"""

    '''we iterate over runs and re-fit the data, each time adjusting x-y points
       then accumulate a fit val for each parameter and get mean/stdev.
       Note: errors are added +/- values'''
    
    from PEATDB.Ekin.Dataset import EkinDataset
    grad=1e-6; conv=1e-6
    for k in kwargs:
        if k=='conv':
            conv=kwargs[k]
        if k=='grad':
            grad=kwargs[k]
    if fitdata == None or len(fitdata) == 0:
        return None
    model = fitdata['model']

    F = getFitter(model=model)
    try:
        varnames = F.getVarNames()
    except:
        return None
    fitvars = {}
    for v in varnames:
        fitvars[v] = []     #group lists by variable name
    
    xd,yd,a,xerrs,yerrs = ekindata.getAll()
    estdata={}; estdata['__datatabs_fits__']={}
    for r in range(runs):
        mut_x=[];mut_y=[]
        #if the datapoint has x or y error values, we use that instead
        for i in range(len(xd)):
            if xerrs != None and xerrs[i] > 0:
                mut_x.append(xd[i] + random.uniform(-xerrs[i], xerrs[i]))
            else:
                mut_x.append(xd[i] + random.uniform(-xuncert, xuncert))
            if yerrs != None and yerrs[i] > 0:
                mut_y.append(yd[i] + random.uniform(-yerrs[i], yerrs[i]))
            else:
                mut_y.append(yd[i] + random.uniform(-yuncert,yuncert))
       
        ek = EkinDataset(xy=[mut_x,mut_y],active=a)
        fitres, X = doFit(ek, model=model, fitdata=fitdata, grad=1e-6, conv=1e-6,
                                silent=True, guess=guess, noiter=100)
        vrs = X.getVariables()

        for n in range(len(varnames)):
            fitvars[varnames[n]].append(vrs[n])
        estdata[str(r)] = ek
        estdata['__datatabs_fits__'][str(r)] = fitres

        if callback != None:
            fitstats = {}
            for v in varnames:
                fitstats[v] = numpy.mean(fitvars[v]), numpy.std(fitvars[v])
            callback(r, fitstats)

    fitstats = {}       
    for v in varnames:
        #print fitvars[v]
        err = numpy.std(fitvars[v])/2
        #print v, numpy.mean(fitvars[v]), err
        #we store the final error as +/- half the stdev.
        fitstats[v] = numpy.mean(fitvars[v]), err
    if doplots == True:           
        from PEATDB.Ekin.Web import EkinWeb
        ew = EkinWeb()
        ew.showEkinPlots(ekindata=estdata, outfile='est_exp_err.html',
                            columns=3, normalise=False, showfitvars=True,
                            imgpath='.')

    return fitstats
Chresten's method for estimating the error on each parameter due
       to the effect of the exp uncertainty, which is user provided.
       If the datapoints have x or y error values, we use that instead
Chresten's method for estimating the error on each parameter due
to the effect of the exp uncertainty, which is user provided.
If the datapoints have x or y error values, we use that instead
Chresten ' s method for estimating the error on each parameter due to the effect of the exp uncertainty which is user provided . If the datapoints have x or y error values we use that instead
def estimateExpUncertainty(ekindata, fitdata, xuncert=0, yuncert=0,
                                 runs=10, doplots=False, callback=None, guess=True,
                                 **kwargs):
    from PEATDB.Ekin.Dataset import EkinDataset
    grad=1e-6; conv=1e-6
    for k in kwargs:
        if k=='conv':
            conv=kwargs[k]
        if k=='grad':
            grad=kwargs[k]
    if fitdata == None or len(fitdata) == 0:
        return None
    model = fitdata['model']
    F = getFitter(model=model)
    try:
        varnames = F.getVarNames()
    except:
        return None
    fitvars = {}
    for v in varnames:
        fitvars[v] = []     
    xd,yd,a,xerrs,yerrs = ekindata.getAll()
    estdata={}; estdata['__datatabs_fits__']={}
    for r in range(runs):
        mut_x=[];mut_y=[]
        for i in range(len(xd)):
            if xerrs != None and xerrs[i] > 0:
                mut_x.append(xd[i] + random.uniform(-xerrs[i], xerrs[i]))
            else:
                mut_x.append(xd[i] + random.uniform(-xuncert, xuncert))
            if yerrs != None and yerrs[i] > 0:
                mut_y.append(yd[i] + random.uniform(-yerrs[i], yerrs[i]))
            else:
                mut_y.append(yd[i] + random.uniform(-yuncert,yuncert))
        ek = EkinDataset(xy=[mut_x,mut_y],active=a)
        fitres, X = doFit(ek, model=model, fitdata=fitdata, grad=1e-6, conv=1e-6,
                                silent=True, guess=guess, noiter=100)
        vrs = X.getVariables()
        for n in range(len(varnames)):
            fitvars[varnames[n]].append(vrs[n])
        estdata[str(r)] = ek
        estdata['__datatabs_fits__'][str(r)] = fitres
        if callback != None:
            fitstats = {}
            for v in varnames:
                fitstats[v] = numpy.mean(fitvars[v]), numpy.std(fitvars[v])
            callback(r, fitstats)
    fitstats = {}       
    for v in varnames:
        err = numpy.std(fitvars[v])/2
        fitstats[v] = numpy.mean(fitvars[v]), err
    if doplots == True:           
        from PEATDB.Ekin.Web import EkinWeb
        ew = EkinWeb()
        ew.showEkinPlots(ekindata=estdata, outfile='est_exp_err.html',
                            columns=3, normalise=False, showfitvars=True,
                            imgpath='.')
    return fitstats
def estimateExpUncertainty ( ekindata , fitdata , xuncert = 0 , yuncert = 0 , runs = 10 , doplots = False , callback = None , guess = True , ** kwargs ) : '''we iterate over runs and re-fit the data, each time adjusting x-y points
       then accumulate a fit val for each parameter and get mean/stdev.
       Note: errors are added +/- values''' from PEATDB . Ekin . Dataset import EkinDataset grad = 1e-6 ; conv = 1e-6 for k in kwargs : if k == 'conv' : conv = kwargs [ k ] if k == 'grad' : grad = kwargs [ k ] if fitdata == None or len ( fitdata ) == 0 : return None model = fitdata [ 'model' ] F = getFitter ( model = model ) try : varnames = F . getVarNames ( ) except : return None fitvars = { } for v in varnames : fitvars [ v ] = [ ] xd , yd , a , xerrs , yerrs = ekindata . getAll ( ) estdata = { } ; estdata [ '__datatabs_fits__' ] = { } for r in range ( runs ) : mut_x = [ ] ; mut_y = [ ] for i in range ( len ( xd ) ) : if xerrs != None and xerrs [ i ] > 0 : mut_x . append ( xd [ i ] + random . uniform ( - xerrs [ i ] , xerrs [ i ] ) ) else : mut_x . append ( xd [ i ] + random . uniform ( - xuncert , xuncert ) ) if yerrs != None and yerrs [ i ] > 0 : mut_y . append ( yd [ i ] + random . uniform ( - yerrs [ i ] , yerrs [ i ] ) ) else : mut_y . append ( yd [ i ] + random . uniform ( - yuncert , yuncert ) ) ek = EkinDataset ( xy = [ mut_x , mut_y ] , active = a ) fitres , X = doFit ( ek , model = model , fitdata = fitdata , grad = 1e-6 , conv = 1e-6 , silent = True , guess = guess , noiter = 100 ) vrs = X . getVariables ( ) for n in range ( len ( varnames ) ) : fitvars [ varnames [ n ] ] . append ( vrs [ n ] ) estdata [ str ( r ) ] = ek estdata [ '__datatabs_fits__' ] [ str ( r ) ] = fitres if callback != None : fitstats = { } for v in varnames : fitstats [ v ] = numpy . mean ( fitvars [ v ] ) , numpy . std ( fitvars [ v ] ) callback ( r , fitstats ) fitstats = { } for v in varnames : err = numpy . std ( fitvars [ v ] ) / 2 fitstats [ v ] = numpy . mean ( fitvars [ v ] ) , err if doplots == True : from PEATDB . Ekin . Web import EkinWeb ew = EkinWeb ( ) ew . showEkinPlots ( ekindata = estdata , outfile = 'est_exp_err.html' , columns = 3 , normalise = False , showfitvars = True , imgpath = '.' ) return fitstats
Chresten's method for estimating the error on each parameter due
to the effect of the exp uncertainty, which is user provided.
Chresten ' s method for estimating the error on each parameter due to the effect of the exp uncertainty which is user provided .
"""Chresten's method for estimating the error on each parameter due
       to the effect of the exp uncertainty, which is user provided.
       If the datapoints have x or y error values, we use that instead""" '''we iterate over runs and re-fit the data, each time adjusting x-y points
       then accumulate a fit val for each parameter and get mean/stdev.
       Note: errors are added +/- values''' #group lists by variable name #if the datapoint has x or y error values, we use that instead #print fitvars[v] #print v, numpy.mean(fitvars[v]), err #we store the final error as +/- half the stdev.
ekindata
fitdata
xuncert
yuncert
runs
doplots
callback
guess