<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from . parameter import Parameters <EOL> import re <EOL> def alphanumeric_sort ( s , _nsre = re . compile ( '<STR_LIT>' ) ) : <EOL> return [ int ( text ) if text . isdigit ( ) else text . lower ( ) <EOL> for text in re . split ( _nsre , s ) ] <EOL> def getfloat_attr ( obj , attr , fmt = '<STR_LIT>' ) : <EOL> "<STR_LIT>" <EOL> val = getattr ( obj , attr , None ) <EOL> if val is None : <EOL> return '<STR_LIT>' <EOL> if isinstance ( val , int ) : <EOL> return '<STR_LIT>' % val <EOL> if isinstance ( val , float ) : <EOL> return fmt % val <EOL> else : <EOL> return repr ( val ) <EOL> def gformat ( val , length = <NUM_LIT:11> ) : <EOL> """<STR_LIT>""" <EOL> length = max ( length , <NUM_LIT:7> ) <EOL> fmt = '<STR_LIT>' % ( length - <NUM_LIT:6> ) <EOL> if isinstance ( val , int ) : <EOL> out = ( '<STR_LIT>' % ( length - <NUM_LIT:2> ) ) . format ( val ) <EOL> if len ( out ) > length : <EOL> out = fmt . format ( val ) <EOL> else : <EOL> out = fmt . format ( val ) <EOL> if len ( out ) < length : <EOL> if '<STR_LIT:e>' in out : <EOL> ie = out . find ( '<STR_LIT:e>' ) <EOL> if '<STR_LIT:.>' not in out [ : ie ] : <EOL> out = out [ : ie ] + '<STR_LIT:.>' + out [ ie : ] <EOL> out = out . replace ( '<STR_LIT:e>' , '<STR_LIT:0>' * ( length - len ( out ) ) + '<STR_LIT:e>' ) <EOL> else : <EOL> fmt = '<STR_LIT>' % ( length - <NUM_LIT:1> ) <EOL> out = fmt . format ( val ) [ : length ] <EOL> if len ( out ) < length : <EOL> pad = '<STR_LIT:0>' if '<STR_LIT:.>' in out else '<STR_LIT:U+0020>' <EOL> out += pad * ( length - len ( out ) ) <EOL> return out <EOL> CORREL_HEAD = '<STR_LIT>' <EOL> def fit_report ( inpars , modelpars = None , show_correl = True , min_correl = <NUM_LIT:0.1> , <EOL> sort_pars = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( inpars , Parameters ) : <EOL> result , params = None , inpars <EOL> if hasattr ( inpars , '<STR_LIT>' ) : <EOL> result = inpars <EOL> params = inpars . params <EOL> if sort_pars : <EOL> if callable ( sort_pars ) : <EOL> key = sort_pars <EOL> else : <EOL> key = alphanumeric_sort <EOL> parnames = sorted ( params , key = key ) <EOL> else : <EOL> parnames = list ( params . keys ( ) ) <EOL> buff = [ ] <EOL> add = buff . append <EOL> if result is not None : <EOL> add ( "<STR_LIT>" ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> add ( "<STR_LIT>" % getfloat_attr ( result , '<STR_LIT>' ) ) <EOL> namelen = max ( [ len ( n ) for n in parnames ] ) <EOL> add ( "<STR_LIT>" ) <EOL> for name in parnames : <EOL> par = params [ name ] <EOL> space = '<STR_LIT:U+0020>' * ( namelen + <NUM_LIT:1> - len ( name ) ) <EOL> nout = "<STR_LIT>" % ( name , space ) <EOL> inval = '<STR_LIT>' <EOL> if par . init_value is not None : <EOL> inval = '<STR_LIT>' % par . init_value <EOL> if modelpars is not None and name in modelpars : <EOL> inval = '<STR_LIT>' % ( inval , modelpars [ name ] . value ) <EOL> try : <EOL> sval = gformat ( par . value ) <EOL> except ( TypeError , ValueError ) : <EOL> sval = '<STR_LIT>' <EOL> if par . stderr is not None : <EOL> serr = gformat ( par . stderr , length = <NUM_LIT:9> ) <EOL> try : <EOL> spercent = '<STR_LIT>' . format ( abs ( par . stderr / par . value ) ) <EOL> except ZeroDivisionError : <EOL> spercent = '<STR_LIT>' <EOL> sval = '<STR_LIT>' % ( sval , serr , spercent ) <EOL> if par . vary : <EOL> add ( "<STR_LIT>" % ( nout , sval , inval ) ) <EOL> elif par . expr is not None : <EOL> add ( "<STR_LIT>" % ( nout , sval , par . expr ) ) <EOL> else : <EOL> add ( "<STR_LIT>" % ( nout , par . value ) ) <EOL> if show_correl : <EOL> add ( CORREL_HEAD % min_correl ) <EOL> correls = { } <EOL> for i , name in enumerate ( parnames ) : <EOL> par = params [ name ] <EOL> if not par . vary : <EOL> continue <EOL> if hasattr ( par , '<STR_LIT>' ) and par . correl is not None : <EOL> for name2 in parnames [ i + <NUM_LIT:1> : ] : <EOL> if ( name != name2 and name2 in par . correl and <EOL> abs ( par . correl [ name2 ] ) > min_correl ) : <EOL> correls [ "<STR_LIT>" % ( name , name2 ) ] = par . correl [ name2 ] <EOL> sort_correl = sorted ( correls . items ( ) , key = lambda it : abs ( it [ <NUM_LIT:1> ] ) ) <EOL> sort_correl . reverse ( ) <EOL> for name , val in sort_correl : <EOL> lspace = max ( <NUM_LIT:1> , <NUM_LIT> - len ( name ) ) <EOL> add ( '<STR_LIT>' % ( name , ( '<STR_LIT:U+0020>' * <NUM_LIT:30> ) [ : lspace ] , val ) ) <EOL> return '<STR_LIT:\n>' . join ( buff ) <EOL> def report_errors ( params , ** kws ) : <EOL> """<STR_LIT>""" <EOL> print ( fit_report ( params , ** kws ) ) <EOL> def report_fit ( params , ** kws ) : <EOL> """<STR_LIT>""" <EOL> print ( fit_report ( params , ** kws ) ) <EOL> def ci_report ( ci , with_offset = True , ndigits = <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> maxlen = max ( [ len ( i ) for i in ci ] ) <EOL> buff = [ ] <EOL> add = buff . append <EOL> def convp ( x ) : <EOL> if abs ( x [ <NUM_LIT:0> ] ) < <NUM_LIT> : <EOL> return "<STR_LIT>" <EOL> return "<STR_LIT>" % ( x [ <NUM_LIT:0> ] * <NUM_LIT:100> ) <EOL> title_shown = False <EOL> fmt_best = fmt_diff = "<STR_LIT>" % ndigits <EOL> if with_offset : <EOL> fmt_diff = "<STR_LIT>" % ndigits <EOL> for name , row in ci . items ( ) : <EOL> if not title_shown : <EOL> add ( "<STR_LIT>" . join ( [ '<STR_LIT>' . rjust ( maxlen + <NUM_LIT:1> ) ] + [ i . rjust ( ndigits + <NUM_LIT:5> ) <EOL> for i in map ( convp , row ) ] ) ) <EOL> title_shown = True <EOL> thisrow = [ "<STR_LIT>" % name . ljust ( maxlen ) ] <EOL> offset = <NUM_LIT:0.0> <EOL> if with_offset : <EOL> for cval , val in row : <EOL> if abs ( cval ) < <NUM_LIT> : <EOL> offset = val <EOL> for cval , val in row : <EOL> if cval < <NUM_LIT> : <EOL> sval = fmt_best . format ( val ) <EOL> else : <EOL> sval = fmt_diff . format ( val - offset ) <EOL> thisrow . append ( sval . rjust ( ndigits + <NUM_LIT:5> ) ) <EOL> add ( "<STR_LIT>" . join ( thisrow ) ) <EOL> return '<STR_LIT:\n>' . join ( buff ) <EOL> def report_ci ( ci ) : <EOL> """<STR_LIT>""" <EOL> print ( ci_report ( ci ) ) </s>
