<s> from numpy import linspace , zeros , sin , exp , random , sqrt , pi , sign <EOL> from lmfit import Parameters , Parameter , Minimizer <EOL> from lmfit . lineshapes import gaussian , lorentzian , pvoigt <EOL> from lmfit . printfuncs import report_fit <EOL> import sys <EOL> WITHPLOT = True <EOL> for arg in sys . argv : <EOL> if '<STR_LIT>' in arg : <EOL> WITHPLOT = False <EOL> if WITHPLOT : <EOL> try : <EOL> import matplotlib <EOL> import pylab <EOL> except ImportError : <EOL> WITHPLOT = False <EOL> def test_constraints ( with_plot = True ) : <EOL> with_plot = with_plot and WITHPLOT <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> yl = lorentzian ( 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 + yl + 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> data = ( gaussian ( x , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) + <EOL> lorentzian ( x , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT> ) + <EOL> random . normal ( scale = <NUM_LIT> , size = n ) + <EOL> x * <NUM_LIT:0.5> ) <EOL> if with_plot : <EOL> pylab . plot ( x , data , '<STR_LIT>' ) <EOL> pfit = Parameters ( ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:10> ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:9> ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:1> ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:20> ) <EOL> pfit . add ( name = '<STR_LIT>' , expr = '<STR_LIT>' ) <EOL> pfit . add ( name = '<STR_LIT>' , expr = '<STR_LIT>' ) <EOL> pfit . add ( name = '<STR_LIT>' , expr = '<STR_LIT>' ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:0.0> ) <EOL> pfit . add ( name = '<STR_LIT>' , value = <NUM_LIT:0.0> ) <EOL> sigma = <NUM_LIT> <EOL> myfit = Minimizer ( residual , pfit , <EOL> fcn_args = ( x , ) , fcn_kws = { '<STR_LIT>' : sigma , '<STR_LIT:data>' : data } , <EOL> scale_covar = True ) <EOL> myfit . prepare_fit ( ) <EOL> init = residual ( myfit . params , x ) <EOL> result = myfit . leastsq ( ) <EOL> print ( '<STR_LIT>' , result . nfev ) <EOL> print ( result . chisqr , result . redchi , result . nfree ) <EOL> report_fit ( result . params , min_correl = <NUM_LIT> ) <EOL> fit = residual ( result . params , x ) <EOL> if with_plot : <EOL> pylab . plot ( x , fit , '<STR_LIT>' ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == <NUM_LIT> + result . params [ '<STR_LIT>' ] . value ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == result . params [ '<STR_LIT>' ] . value - result . params [ '<STR_LIT>' ] . value ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == <NUM_LIT:2> * result . params [ '<STR_LIT>' ] . value ) <EOL> myfit . params [ '<STR_LIT>' ] . expr = '<STR_LIT>' <EOL> result = myfit . leastsq ( ) <EOL> report_fit ( result . params , min_correl = <NUM_LIT> ) <EOL> fit2 = residual ( result . params , x ) <EOL> if with_plot : <EOL> pylab . plot ( x , fit2 , '<STR_LIT:k>' ) <EOL> pylab . show ( ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == <NUM_LIT> + result . params [ '<STR_LIT>' ] . value ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == result . params [ '<STR_LIT>' ] . value - result . params [ '<STR_LIT>' ] . value ) <EOL> assert ( result . params [ '<STR_LIT>' ] . value == <NUM_LIT> * result . params [ '<STR_LIT>' ] . value ) <EOL> test_constraints ( ) </s>
