def make_fcn(filename):
    """ Return the fit-test function corresponding to file ``filename``.  """
    # collect lines and parse first several lines for fit info
    with open(filename, 'r') as ifile:
        lines = ifile.readlines()
    name = filename.split('.')[0]
    _certified_values = re.compile('.*Certified Values\s*\(lines\s*([0-9]*)\s*to\s*([0-9]*)\)')
    _data = re.compile('.*Data\s*\(lines\s*([0-9]*)\s*to\s*([0-9]*)\)')
    _model = re.compile('^Model:')
    values = None
    data = None
    fcn = None
    for i,line in enumerate(lines):
        m = _certified_values.match(line)
        if m is not None:
            values = (int(m.group(1)), int(m.group(2)))
        m = _data.match(line)
        if m is not None:
            data = (int(m.group(1)), int(m.group(2)))
        m = _model.match(line)
        if m is not None:
            fcn = ""
            for j in range(i+3,values[0] - 1):
                s = lines[j].strip()
                if s == '':
                    break
                fcn += s
        if None not in [values, data, fcn]:
            break
    # get rid of + e
    fcn = fcn[:-4]
    # fix [] brackets
    s = fcn.split('[')
    if len(s) > 1:
        _bracket = re.compile('(.*)\](.*)')
        nfcn = s[0]
        for si in s[1:]:
            m = _bracket.match(si)
            if m is not None:
                si = m.group(1) + ')' + m.group(2)
                si = '(' + m.group(1) + ')' + m.group(2)
            nfcn += si
        fcn = nfcn
    # parameter names
    nparam = values[1] - values[0] + 1 - 5
    pnames = ','.join(['b' + str(i+1) for i in range(nparam)])
    p = []
    p0 = []
    prior = []
    for i in range(values[0] - 1, values[0] - 1 + nparam):
        s = lines[i].split()
        p.append(gv.gvar(s[-2] + ' +- ' + s[-1]))
        if USE_2ND_STARTING_VALUE:
            p0.append(float(s[3]))
        else:
            p0.append(float(s[2]))
        prior.append(gv.gvar('0 +- ' + str(200 * p[-1].mean)))
    p = np.array(p)
    p0 = np.array(p0)
    prior = list(gv.fmt(prior))
    nistp = answers.get(name, str(p))
    # error on y
    s = lines[values[1] - 3].split()
    yerr = '0 +- ' + s[-1]
    # collect x, y
    x = []
    y = []
    for i in range(data[0] - 1, data[1]):
        s = lines[i].split()
        if len(s) == 2:
            x.append(float(s[-1]))
            y.append(float(s[-2]))
        elif len(s) > 2:
            y.append(float(s[0]))
            x.append([float(si) for si in s[1:]])
    x = np.array(x)
    if len(x.shape) > 1:
        xnames = ','.join(['x' + str(i+1) for i in range(x.shape[1])])
        x = x.T
    else:
        xnames = 'x'
    y = np.array(y)
    if len(nistp.split('\n')) > 1:
        nistp = '\\n'.join(nistp.split('\n'))
    if name == 'nelson':
        fcn = fcn.replace('log(y)', 'y')
        yname = 'log(y)'
    else:
        yname = 'y'
    if name == 'mgh10' and not USE_2ND_STARTING_VALUE:
        p0 = gv.sdev(gv.gvar(prior)) / 100.
    template = \
"""
def {name}():
    print(20 * '=', '{name}')
    x = np.{x}
    y = np.{y}
    y = {yname} + gv.gvar(len(y) * ['{yerr}'])
    def fcn(x, b):
        {xnames} = x
        {pnames} = b
        {fcn}
        return y
    prior = gv.gvar({prior})
    p0 = np.{p0}
    fit = lsqfit.nonlinear_fit(
        prior=prior, data=(x,y), fcn=fcn, p0=p0, tol=1e-10,
        )
    print(fit)
    assert str(fit.p) == '{nistp}'
"""
    return template.format(
        name=name,
        x=repr(x),
        y=repr(y),
        p0=repr(p0),
        yerr=yerr,
        fcn=fcn,
        prior=repr(prior),
        nistp=nistp,
        pnames=pnames,
        xnames=xnames,
        yname=yname,
        )
def make_fcn ( filename ) : with open ( filename , 'r' ) as ifile : lines = ifile . readlines ( ) name = filename . split ( '.' ) [ 0 ] _certified_values = re . compile ( '.*Certified Values\s*\(lines\s*([0-9]*)\s*to\s*([0-9]*)\)' ) _data = re . compile ( '.*Data\s*\(lines\s*([0-9]*)\s*to\s*([0-9]*)\)' ) _model = re . compile ( '^Model:' ) values = None data = None fcn = None for i , line in enumerate ( lines ) : m = _certified_values . match ( line ) if m is not None : values = ( int ( m . group ( 1 ) ) , int ( m . group ( 2 ) ) ) m = _data . match ( line ) if m is not None : data = ( int ( m . group ( 1 ) ) , int ( m . group ( 2 ) ) ) m = _model . match ( line ) if m is not None : fcn = "" for j in range ( i + 3 , values [ 0 ] - 1 ) : s = lines [ j ] . strip ( ) if s == '' : break fcn += s if None not in [ values , data , fcn ] : break fcn = fcn [ : - 4 ] s = fcn . split ( '[' ) if len ( s ) > 1 : _bracket = re . compile ( '(.*)\](.*)' ) nfcn = s [ 0 ] for si in s [ 1 : ] : m = _bracket . match ( si ) if m is not None : si = m . group ( 1 ) + ')' + m . group ( 2 ) si = '(' + m . group ( 1 ) + ')' + m . group ( 2 ) nfcn += si fcn = nfcn nparam = values [ 1 ] - values [ 0 ] + 1 - 5 pnames = ',' . join ( [ 'b' + str ( i + 1 ) for i in range ( nparam ) ] ) p = [ ] p0 = [ ] prior = [ ] for i in range ( values [ 0 ] - 1 , values [ 0 ] - 1 + nparam ) : s = lines [ i ] . split ( ) p . append ( gv . gvar ( s [ - 2 ] + ' +- ' + s [ - 1 ] ) ) if USE_2ND_STARTING_VALUE : p0 . append ( float ( s [ 3 ] ) ) else : p0 . append ( float ( s [ 2 ] ) ) prior . append ( gv . gvar ( '0 +- ' + str ( 200 * p [ - 1 ] . mean ) ) ) p = np . array ( p ) p0 = np . array ( p0 ) prior = list ( gv . fmt ( prior ) ) nistp = answers . get ( name , str ( p ) ) s = lines [ values [ 1 ] - 3 ] . split ( ) yerr = '0 +- ' + s [ - 1 ] x = [ ] y = [ ] for i in range ( data [ 0 ] - 1 , data [ 1 ] ) : s = lines [ i ] . split ( ) if len ( s ) == 2 : x . append ( float ( s [ - 1 ] ) ) y . append ( float ( s [ - 2 ] ) ) elif len ( s ) > 2 : y . append ( float ( s [ 0 ] ) ) x . append ( [ float ( si ) for si in s [ 1 : ] ] ) x = np . array ( x ) if len ( x . shape ) > 1 : xnames = ',' . join ( [ 'x' + str ( i + 1 ) for i in range ( x . shape [ 1 ] ) ] ) x = x . T else : xnames = 'x' y = np . array ( y ) if len ( nistp . split ( '\n' ) ) > 1 : nistp = '\\n' . join ( nistp . split ( '\n' ) ) if name == 'nelson' : fcn = fcn . replace ( 'log(y)' , 'y' ) yname = 'log(y)' else : yname = 'y' if name == 'mgh10' and not USE_2ND_STARTING_VALUE : p0 = gv . sdev ( gv . gvar ( prior ) ) / 100. template = return template . format ( name = name , x = repr ( x ) , y = repr ( y ) , p0 = repr ( p0 ) , yerr = yerr , fcn = fcn , prior = repr ( prior ) , nistp = nistp , pnames = pnames , xnames = xnames , yname = yname , )
Return the fit-test function corresponding to file ``filename``.