Giter Site home page Giter Site logo

fitit's Introduction

fitit.py

An interface to scipy's optimize.curve_fit function. fitit Replaces passing parameters as arrays with passing a Params object to allow such things as easily disabling a fit parameter.

Install

pip install fitit

Usage

import fitit

# Params class stores the fit variables and allows attribute access
p0_peak = fitit.Params('A k f0_opt y0')

# function to fit, accepts Params as second argument
def peak(f, p):
    df = f - p.f0_opt
    return p.A / (p.k**2 / 4. + df**2.) + p.y0


# create a copy of p0_peak and set initial fit parameters
p0 = fitit.Params(like=p0_peak)
p0.f0_opt = 195047
p0.k = 60
p0.A = 4e10 * p0.k**2 / 4.
p0.y0 = 0

# do the fit, returns another Params object
popt = fitit.fit(peak, fopt, Pzz, p0, sigma=1)


figure()

plot(fopt, Pzz, 'o')

plot(fopt, peak(fopt, p0))
plot(fopt, peak(fopt, popt))

Holding parameters constant

To hold parameters constant, and not pass the parameter to the least squares algorithm, prefix the parameter with a star:

p0 = fitit.Params('A k f0_opt *y0')

Bounded fit

Upper and lower bounds on the fit can also be specified:

# inital guess parameters
p0 = fitit.Params('A k f0_opt *y0')
p0.f0_opt = 195047
p0.k = 60
p0.A = 4e10 * p0.k**2 / 4.
p0.y0 = 0

# lower limits
p_lower = fitit.Params(like=p0)
p_lower.f0_opt = p0.f0_opt - 100
p_lower.k = 0
p_lower.A = 0

# upper limits
p_upper = fitit.Params(like=p0)
p_upper.f0_opt = p0.f0_opt + 100
p_upper.k = 100
p_upper.A = np.inf

# do the fit, returns another Params object
popt = fitit.fit(peak, fopt, Pzz, p0, sigma=1, bounds=[p_lower, p_upper])

fitit's People

Contributors

cdoolin avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.