[SciPy-user] ODR example code needed

Barry Wark barrywark@gmail....
Wed Sep 5 12:55:30 CDT 2007


Here's a routine to fit an exponential of the form sign*(A*e^{-t/tau}
+ offset) using scipy.optimize.odr.

def fitExponentialODR(y, x0, t, sign, fitType, verbose, maxIterations):
    """
    Fit exponential of form y = sign * (A*e^(-t/tau) + offset) using
S.optimize.odr

    y: target array (must be rank 1)
    x0: initial guess of (A, tau, offset)
    t: array
    sign: +/-1
    fitType: ODR_LSQ or ODR_EXP

    @returns (A, tau, offset, fit, sd_amp, sd_tau, sd_offset)
    """

    assert(0<=fitType<=2)

    R = fitODR(exponentialDecay, t, y, x0, fitType, verbose,
maxIterations, sign)

    if(R==None):
        return None
    else:
        (output, fit) = R

    A = output.beta[0]
    tau = output.beta[1]
    offset = output.beta[2]

    sd_amp = output.sd_beta[0]
    sd_tau = output.sd_beta[1]
    sd_offset = output.sd_beta[2]

    return (A, tau, offset, fit, sd_amp, sd_tau, sd_offset)


def fitODR(fn, x, y, x0, fitType, verbose, maxIterations, *args):
    """
    fn function to fit
    x function input
    y target function output
    x0 initial parameters
    fitType One of ODR_LSQ (least squares) or ODR_EXP (explicit ODR)
    verbose If true, print verbose output on error. Else, fail
silently (return None)
    maxIterations Maximum number of iterations without convergence.
Re-run will use double

    @returns (odr.output, fit)
    """
    assert(0 <= fitType <= 2)

    model = S.odr.Model(fn, extra_args=args)
    data = S.odr.Data(x, y)
    odr = S.odr.ODR(data, model, x0, maxit=maxIterations)
    odr.set_job(fit_type=fitType)

    output = odr.run()

    if(output.info >= 4): #no convergence or ODRPACK thinks results
are "questionable"
        if(verbose):
            print 'ODRPACK unable to find solution:\n',
'\n'.join(output.stopreason)
            print 'Retrying fit...'

        odr.maxit = 2*maxIterations
        output = odr.restart()
        if(output.info >= 4):
            if(verbose):
                print 'ODRPACK still unable to find solution: \n',
'\n\t\t'.join(output.stopreason)
            return None

    return (output, fn(output.beta, x, *args))


On 9/5/07, J. K. <ryan2057@gmx.de> wrote:
> Hi,
>
> I am trying to use odr. I was able to get my programme to run on my
> data, I can see the data using pprint.
> Now the newbie problem: How do I use the data? I want need to calculate
> stuff with it, I want to save it to a file. I was trying to figure out
> the odr.Output, but I failed.
> Could someone copy and paste a working example code, so that I can work
> with that and the help included in the help?
>
> Thanks a lot,
> Jack K.
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-user mailing list