<s> import sys <EOL> from numpy import linspace , zeros , sin , exp , random , sqrt , pi , sign <EOL> from scipy . optimize import leastsq <EOL> from lmfit import Parameters , Minimizer , report_fit <EOL> from lmfit . lineshapes import gaussian <EOL> try : <EOL> import pylab <EOL> HASPYLAB = True <EOL> except ImportError : <EOL> HASPYLAB = False <EOL> HASPYLAB = False <EOL> def residual ( pars , x , sigma = None , data = None ) : <EOL> yg = gaussian ( x , pars [ '<STR_LIT>' ] . value , <EOL> pars [ '<STR_LIT>' ] . value , pars [ '<STR_LIT>' ] . value ) <EOL> slope = pars [ '<STR_LIT>' ] . value <EOL> offset = pars [ '<STR_LIT>' ] . value <EOL> model = yg + offset + x * slope <EOL> if data is None : <EOL> return model <EOL> if sigma is None : <EOL> return ( model - data ) <EOL> return ( model - data ) / sigma <EOL> n = <NUM_LIT> <EOL> xmin = <NUM_LIT:0.> <EOL> xmax = <NUM_LIT> <EOL> x = linspace ( xmin , xmax , n ) <EOL> p_true = Parameters ( ) <EOL> p_true . add ( '<STR_LIT>' , value = <NUM_LIT> ) <EOL> p_true . add ( '<STR_LIT>' , value = <NUM_LIT> ) <EOL> p_true . add ( '<STR_LIT>' , value = <NUM_LIT> ) <EOL> p_true . add ( '<STR_LIT>' , value = - <NUM_LIT> ) <EOL> p_true . add ( '<STR_LIT>' , value = <NUM_LIT> ) <EOL> data = ( gaussian ( x , p_true [ '<STR_LIT>' ] . value , p_true [ '<STR_LIT>' ] . value , <EOL> p_true [ '<STR_LIT>' ] . value ) + <EOL> random . normal ( scale = <NUM_LIT> , size = n ) + <EOL> x * p_true [ '<STR_LIT>' ] . value + p_true [ '<STR_LIT>' ] . value ) <EOL> if HASPYLAB : <EOL> pylab . plot ( x , data , '<STR_LIT>' ) <EOL> p_fit = Parameters ( ) <EOL> p_fit . add ( '<STR_LIT>' , value = <NUM_LIT> ) <EOL> p_fit . add ( '<STR_LIT>' , value = <NUM_LIT:9> ) <EOL> p_fit . add ( '<STR_LIT>' , value = <NUM_LIT:1> ) <EOL> p_fit . add ( '<STR_LIT>' , value = <NUM_LIT:0.0> ) <EOL> p_fit . add ( '<STR_LIT>' , value = <NUM_LIT:0.0> ) <EOL> myfit = Minimizer ( residual , p_fit , <EOL> fcn_args = ( x , ) , <EOL> fcn_kws = { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT:data>' : data } ) <EOL> myfit . prepare_fit ( ) <EOL> for scale_covar in ( True , False ) : <EOL> myfit . scale_covar = scale_covar <EOL> print '<STR_LIT>' , myfit . scale_covar , '<STR_LIT>' <EOL> for sigma in ( <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> ) : <EOL> myfit . userkws [ '<STR_LIT>' ] = sigma <EOL> p_fit [ '<STR_LIT>' ] . value = <NUM_LIT:10> <EOL> p_fit [ '<STR_LIT>' ] . value = <NUM_LIT:9> <EOL> p_fit [ '<STR_LIT>' ] . value = <NUM_LIT:1> <EOL> p_fit [ '<STR_LIT>' ] . value = <NUM_LIT:0.0> <EOL> p_fit [ '<STR_LIT>' ] . value = <NUM_LIT:0.0> <EOL> out = myfit . leastsq ( ) <EOL> print '<STR_LIT>' , sigma <EOL> print '<STR_LIT>' , out . chisqr <EOL> print '<STR_LIT>' , out . redchi <EOL> report_fit ( out . params , modelpars = p_true , show_correl = False ) <EOL> print '<STR_LIT>' </s>
