Exemple #1
0
def mpfitpeaks(x, y, N, error=None):

    pars = [1, np.min(y), 0]
    parinfo = [ {"value": N, "fixed": 1, "name": "Number of Peaks",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": 1.6, "fixed": 0, "name": "Sigma",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": pars[1], "fixed": 0, "name": "Offset",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": pars[2], "fixed": 0, "name": "Slope",
                    "limited": [0, 0], "limits": [0, 0]}]
    
    for i in range(N):
        v = {"value": np.max(y)/2., "fixed": 0, "name": "Peak Value(%i)" % i,
                "limited": [1, 0], "limits": [0, 0]}
        pars.append(np.max(y))
        parinfo.append(v)

        v = {"value": x[np.argmax(y)], "fixed": 0, "name": "Centroid(%i)" % i}
        pars.append(x[np.argmax(y)])
        parinfo.append(v)

    fa = {"x": x, "y": y}
    if error is not None: fa["error"] = error
    return mpfit.mpfit(multi_gaussian_residuals, parinfo=parinfo, functkw=fa,
            quiet=1)
Exemple #2
0
def do_fit_wavelengths(pixels, lambdas, alphaguess, 
        sinbetaguess, gammaguess, deltaguess, band, pixel_y, error=None):

    ''' THIS HSOULD BE REMOVED'''

    bmap = {"Y": 6, "J": 5, "H": 4, "K": 3}
    order = bmap[band]

    
    parinfo = [
            {'fixed': 1, 'value': order, 'parname': 'order', 
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 1, 'value': pixel_y, 'parname': 'Y',
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 0, 'value': alphaguess, 'parname': 'alpha', 'step': 1e-5,
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 0, 'value': sinbetaguess, 'parname': 'sinbeta', 
                'step': 1e-5, 'limited': [0,0], 'limits': [30,50]},
            {'fixed': 0, 'value': gammaguess, 'parname': 'gamma','step': 1e-15,
                'limited': [1,1], 'limits': [0,20e-13]},
            {'fixed': 0, 'value': deltaguess, 'parname': 'delta', 'step': 1e-1,
                'limited': [1,1], 'limits': [0,2048]},
            ]

    fa = {"x": pixels, "y": lambdas}
    if error is not None:
        fa["error"] = error

    lsf = mpfit.mpfit(wavelength_residuals, parinfo=parinfo, functkw=fa, 
            quiet=1, maxiter=20)

    return lsf
Exemple #3
0
def do_fit_wavelengths(pixels, lambdas, alphaguess, 
        sinbetaguess, gammaguess, deltaguess, band, pixel_y, error=None):

    ''' THIS HSOULD BE REMOVED'''

    bmap = {"Y": 6, "J": 5, "H": 4, "K": 3}
    order = bmap[band]

    
    parinfo = [
            {'fixed': 1, 'value': order, 'parname': 'order', 
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 1, 'value': pixel_y, 'parname': 'Y',
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 0, 'value': alphaguess, 'parname': 'alpha', 'step': 1e-5,
                'limited': [0,0], 'limits': [0,0]},
            {'fixed': 0, 'value': sinbetaguess, 'parname': 'sinbeta', 
                'step': 1e-5, 'limited': [0,0], 'limits': [30,50]},
            {'fixed': 0, 'value': gammaguess, 'parname': 'gamma','step': 1e-15,
                'limited': [1,1], 'limits': [0,20e-13]},
            {'fixed': 0, 'value': deltaguess, 'parname': 'delta', 'step': 1e-1,
                'limited': [1,1], 'limits': [0,2048]},
            ]

    fa = {"x": pixels, "y": lambdas}
    if error is not None:
        fa["error"] = error

    lsf = mpfit.mpfit(wavelength_residuals, parinfo=parinfo, functkw=fa, 
            quiet=1, maxiter=20)

    return lsf
Exemple #4
0
def mpfitpeaks(x, y, N, error=None):

    pars = [1, np.min(y), 0]
    parinfo = [ {"value": N, "fixed": 1, "name": "Number of Peaks",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": 1.6, "fixed": 0, "name": "Sigma",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": pars[1], "fixed": 0, "name": "Offset",
                    "limited": [0, 0], "limits": [0, 0]},
                {"value": pars[2], "fixed": 0, "name": "Slope",
                    "limited": [0, 0], "limits": [0, 0]}]
    
    for i in range(N):
        v = {"value": np.max(y)/2., "fixed": 0, "name": "Peak Value(%i)" % i,
                "limited": [1, 0], "limits": [0, 0]}
        pars.append(np.max(y))
        parinfo.append(v)

        v = {"value": x[np.argmax(y)], "fixed": 0, "name": "Centroid(%i)" % i}
        pars.append(x[np.argmax(y)])
        parinfo.append(v)

    fa = {"x": x, "y": y}
    if error is not None: fa["error"] = error
    return mpfit.mpfit(multi_gaussian_residuals, parinfo=parinfo, functkw=fa,
            quiet=1)
Exemple #5
0
def mpfit_do(residual_fun, # function returned from mpfit_residuals() above
        x, # input x
        y, # input y = f(x)
        parinfo, # initial parameter guess
        error=None,
        maxiter=20):

    #TODO : Document parinfo part

    fa = {"x": x, "y": y}
    if error is not None:
        fa["error"] = error

    lsf = mpfit.mpfit(residual_fun, parinfo=parinfo, functkw=fa, 
            quiet=1, maxiter=maxiter)

    return lsf
Exemple #6
0
def mpfit_do(residual_fun, # function returned from mpfit_residuals() above
        x, # input x
        y, # input y = f(x)
        parinfo, # initial parameter guess
        error=None,
        maxiter=20):

    #TODO : Document parinfo part

    fa = {"x": x, "y": y}
    if error is not None:
        fa["error"] = error

    lsf = mpfit.mpfit(residual_fun, parinfo=parinfo, functkw=fa, 
            quiet=1, maxiter=maxiter)

    return lsf
Exemple #7
0
def mpfitpeak(x, y, error=None):
    
    parinfo = [{"value": np.max(y), "fixed": 0, "name": "Peak Value",
                    'step': 10},
                {"value": x[np.argmax(y)], "fixed": 0, "name": "Centroid",
                    'step': .1},
                {"value": 1.1, "fixed": 0, "name": "Sigma",
                    'step': .1},
                {"value": np.min(y), "fixed": 0, "name": "Offset",
                    'step': 10},
                {"value": 0, "fixed": 0, "name": "Slope",
                    'step': 1e-5}]

    fa = {"x": x, "y": y}
    if error is not None: fa["error"] = error


    return mpfit.mpfit(gaussian_residuals, parinfo=parinfo, functkw=fa, quiet=1)
Exemple #8
0
def mpfitpeak(x, y, error=None):
    
    parinfo = [{"value": np.max(y), "fixed": 0, "name": "Peak Value",
                    'step': 10},
                {"value": x[np.argmax(y)], "fixed": 0, "name": "Centroid",
                    'step': .1},
                {"value": 1.1, "fixed": 0, "name": "Sigma",
                    'step': .1},
                {"value": np.min(y), "fixed": 0, "name": "Offset",
                    'step': 10},
                {"value": 0, "fixed": 0, "name": "Slope",
                    'step': 1e-5}]

    fa = {"x": x, "y": y}
    if error is not None: fa["error"] = error


    return mpfit.mpfit(gaussian_residuals, parinfo=parinfo, functkw=fa, quiet=1)
def run_fit(m, m_err, X, M, M_err, c, c_err):
    p0 = [25.0, -0.05]

    ##########
    # MPFIT
    ##########
    print '\n\n'
    print '##########'
    print '# MPFIT'
    print '##########'
    from pytools.nmpfit import mpfit
    import copy
    nparams = len(p0)
    infoTemplate = {'parname':'', 'value':0, 'step':0.0, 'fixed':0, 
                       'limits':[0., 0.], 'limited':[0, 0]}
    parinfo = []
    for ii in range(nparams):
        parinfo.append(copy.deepcopy(infoTemplate))
    
    # Zeropoint
    parinfo[0]['parname'] = 'Zeropoint'
    parinfo[0]['value'] = p0[0]
    parinfo[0]['limited'] = [1, 1]
    parinfo[0]['limits'] = [20.0, 30.0]
    
    # Airmass Coefficient
    parinfo[1]['parname'] = 'Airmass Coefficient'
    parinfo[1]['value'] = p0[1]
    parinfo[1]['step'] = 0.001
    parinfo[1]['limited'] = [1, 1]
    parinfo[1]['limits'] = [-1, 1]

    # Color Term
    if nparams > 2:
        parinfo[2]['parname'] = 'Color Term'
        parinfo[2]['value'] = p0[2]
        parinfo[2]['limited'] = [1, 1]
        parinfo[2]['limits'] = [-0.1, 0.1]
        parinfo[2]['step'] = 0.001

    # Color-Airmass Term
    if nparams > 3:
        parinfo[3]['parname'] = 'Airmass/Color Term'
        parinfo[3]['value'] = p0[3]
        parinfo[3]['limited'] = [1, 1]
        parinfo[3]['limits'] = [-0.1, 0.1]


    args = {'m': m, 'merr': m_err, 'X': X, 'M': M, 'Merr': M_err, 
            'C': c, 'Cerr': c_err}

    output = mpfit(mpfit_func_fit, p0, functkw=args, parinfo=parinfo,
                   xtol=1e-15, gtol=1e-16, ftol=1e-16)
    print 'Fit Status:'
    print '  %d %s' % (output.status, output.errmsg)
    print
    print "Fitted parameters at minimum:" 
    for ii in range(len(output.params)):
        print "%2d  %-20s  %12f +/- %10f" % \
            (ii, parinfo[ii]['parname'], output.params[ii], output.perror[ii]) 
    print
        
    p = output.params
    p_err = output.perror

#     ##########
#     # SCIPY.OPTIMIZE.LEASTSQ
#     ##########
#     print '\n\n'
#     print '##########'
#     print '# SCIPY.OPTIMIZE.LEASTSQ'
#     print '##########'
#     weights = 1.0 / np.sqrt(m_err**2 + M_err**2)
#     output = optimize.leastsq(func_fit, p0, full_output=1,
#                               args=(m, m_err, X, 
#                                     M, M_err, c, c_err))#,
# #                              diag=weights)

#     p = output[0]
#     cov = output[1]
#     info = output[2]
#     mesg = output[3]
#     success = output[4]

#     chisq = np.sum(info["fvec"]*info["fvec"])
#     dof = len(m) - len(p)

#     print 'Converged with chi squared ', chisq
#     print 'degrees of freedom, dof ', dof
#     print 'RMS of residuals (i.e. sqrt(chisq/dof))', math.sqrt(chisq/dof)
#     print 'Reduced chisq (i.e. variance of residuals)', chisq/dof
#     print ''

#     # Uncertainties in parameters
#     # uncertainties are calculated as per gnuplot, "fixing" the result 
#     # for non unit values of the reduced chisq. 
#     # values at min match gnuplot 
#     print "Fitted parameters at minimum, with 68% C.I.:" 
#     for i, pmin in enumerate(p): 
#         print "%2d %12f +/- %10f" % \
#             (i, pmin, math.sqrt(cov[i,i]**2)) 
#     print 

#     print "Correlation matrix" 
#     # correlation matrix close to gnuplot 
#     print "               ", 
#     for i in range(len(p)): print "p[%d]      " % (i), 
#     print 
#     for i in range(len(p)): 
#         print "   p[%d]   " % i, 
#         for j in range(i+1): 
#             print "%10f" % (cov[i,j]/math.sqrt(cov[i,i]*cov[j,j]),), 
#         print 

#     print 'Results: ', success, mesg
#     print '           Zeropoint = %7.3f' % p[0]
#     print ' Airmass Coefficient = %9.5f' % p[1]
# #     print '          Color Term = %9.5f' % p[2]
# #     print '  Airmass Color Term = %9.5f' % p[3]



    # Get residuals
    res, res_err = func_residuals(p, m, m_err, X, M, M_err, c, c_err)

    # Get the predicted instrumental magnitude
    m_pred, m_pre_err = func_calc_mag_obs_predicted(p, 
                                                    M, M_err, 
                                                    X, c, c_err)

    return p, p_err, res, m_pred
def run_fit(m, m_err, X, M, M_err, c, c_err):
    p0 = [25.0, -0.05]

    ##########
    # MPFIT
    ##########
    print '\n\n'
    print '##########'
    print '# MPFIT'
    print '##########'
    from pytools.nmpfit import mpfit
    import copy
    nparams = len(p0)
    infoTemplate = {
        'parname': '',
        'value': 0,
        'step': 0.0,
        'fixed': 0,
        'limits': [0., 0.],
        'limited': [0, 0]
    }
    parinfo = []
    for ii in range(nparams):
        parinfo.append(copy.deepcopy(infoTemplate))

    # Zeropoint
    parinfo[0]['parname'] = 'Zeropoint'
    parinfo[0]['value'] = p0[0]
    parinfo[0]['limited'] = [1, 1]
    parinfo[0]['limits'] = [20.0, 30.0]

    # Airmass Coefficient
    parinfo[1]['parname'] = 'Airmass Coefficient'
    parinfo[1]['value'] = p0[1]
    parinfo[1]['step'] = 0.001
    parinfo[1]['limited'] = [1, 1]
    parinfo[1]['limits'] = [-1, 1]

    # Color Term
    if nparams > 2:
        parinfo[2]['parname'] = 'Color Term'
        parinfo[2]['value'] = p0[2]
        parinfo[2]['limited'] = [1, 1]
        parinfo[2]['limits'] = [-0.1, 0.1]
        parinfo[2]['step'] = 0.001

    # Color-Airmass Term
    if nparams > 3:
        parinfo[3]['parname'] = 'Airmass/Color Term'
        parinfo[3]['value'] = p0[3]
        parinfo[3]['limited'] = [1, 1]
        parinfo[3]['limits'] = [-0.1, 0.1]

    args = {
        'm': m,
        'merr': m_err,
        'X': X,
        'M': M,
        'Merr': M_err,
        'C': c,
        'Cerr': c_err
    }

    output = mpfit(mpfit_func_fit,
                   p0,
                   functkw=args,
                   parinfo=parinfo,
                   xtol=1e-15,
                   gtol=1e-16,
                   ftol=1e-16)
    print 'Fit Status:'
    print '  %d %s' % (output.status, output.errmsg)
    print
    print "Fitted parameters at minimum:"
    for ii in range(len(output.params)):
        print "%2d  %-20s  %12f +/- %10f" % \
            (ii, parinfo[ii]['parname'], output.params[ii], output.perror[ii])
    print

    p = output.params
    p_err = output.perror

    #     ##########
    #     # SCIPY.OPTIMIZE.LEASTSQ
    #     ##########
    #     print '\n\n'
    #     print '##########'
    #     print '# SCIPY.OPTIMIZE.LEASTSQ'
    #     print '##########'
    #     weights = 1.0 / np.sqrt(m_err**2 + M_err**2)
    #     output = optimize.leastsq(func_fit, p0, full_output=1,
    #                               args=(m, m_err, X,
    #                                     M, M_err, c, c_err))#,
    # #                              diag=weights)

    #     p = output[0]
    #     cov = output[1]
    #     info = output[2]
    #     mesg = output[3]
    #     success = output[4]

    #     chisq = np.sum(info["fvec"]*info["fvec"])
    #     dof = len(m) - len(p)

    #     print 'Converged with chi squared ', chisq
    #     print 'degrees of freedom, dof ', dof
    #     print 'RMS of residuals (i.e. sqrt(chisq/dof))', math.sqrt(chisq/dof)
    #     print 'Reduced chisq (i.e. variance of residuals)', chisq/dof
    #     print ''

    #     # Uncertainties in parameters
    #     # uncertainties are calculated as per gnuplot, "fixing" the result
    #     # for non unit values of the reduced chisq.
    #     # values at min match gnuplot
    #     print "Fitted parameters at minimum, with 68% C.I.:"
    #     for i, pmin in enumerate(p):
    #         print "%2d %12f +/- %10f" % \
    #             (i, pmin, math.sqrt(cov[i,i]**2))
    #     print

    #     print "Correlation matrix"
    #     # correlation matrix close to gnuplot
    #     print "               ",
    #     for i in range(len(p)): print "p[%d]      " % (i),
    #     print
    #     for i in range(len(p)):
    #         print "   p[%d]   " % i,
    #         for j in range(i+1):
    #             print "%10f" % (cov[i,j]/math.sqrt(cov[i,i]*cov[j,j]),),
    #         print

    #     print 'Results: ', success, mesg
    #     print '           Zeropoint = %7.3f' % p[0]
    #     print ' Airmass Coefficient = %9.5f' % p[1]
    # #     print '          Color Term = %9.5f' % p[2]
    # #     print '  Airmass Color Term = %9.5f' % p[3]

    # Get residuals
    res, res_err = func_residuals(p, m, m_err, X, M, M_err, c, c_err)

    # Get the predicted instrumental magnitude
    m_pred, m_pre_err = func_calc_mag_obs_predicted(p, M, M_err, X, c, c_err)

    return p, p_err, res, m_pred