splat/model.py
def _modelFitEMCEE_summary(sampler,spec,file,**kwargs):
    '''
    for now just plotting best model
    would like to do draws from posterior instead
    '''

# extract best fit values
    base_samples = sampler.chain
    nwalkers = base_samples.shape[0]
    nsamples = base_samples.shape[1]
    nparameters = base_samples.shape[2]
    samples = base_samples[:, int(kwargs['burn_fraction']*nsamples):, :].reshape((-1, nparameters))

    f = open(file,'w')
    f.write('EMCEE fitting analysis of spectrum of {} using the models of {}'.format(spec.name,kwargs['model']))
    f.write('\nFitting performed on {}'.format(time.strftime("%Y %h %d %I:%M:%S")))
    f.write('\n\nMCMC paramaters:')
    f.write('\n\tNumber of walkers = {}'.format(nwalkers))
    f.write('\n\tNumber of samples = {}'.format(nsamples))
    f.write('\n\tNumber of fit parameters = {}'.format(nparameters))
    f.write('\n\tBurn-in fraction = {}'.format(kwargs['burn_fraction']))

    f.write('\n\nBest fit parameters')
    for i,v in enumerate(['teff','logg','z'][0:nparameters]):
        fit = numpy.percentile(samples[:,i], [16, 50, 84])
        f.write('\n\t{} = {}+{}-{} {}'.format(SPECTRAL_MODEL_PARAMETERS[v]['title'],fit[1],fit[2]-fit[1],fit[1]-fit[0],SPECTRAL_MODEL_PARAMETERS[v]['unit'].to_string()))

    mkwargs = copy.deepcopy(kwargs)
    for i,l in enumerate(['teff','logg','z'][:samples.shape[-1]]):
        mkwargs[l] = numpy.median(samples[:,i])
    mdl = loadModel(**mkwargs)
    stat,scl = compareSpectra(spec,mdl,**kwargs)

# copmute DOF
    try:
        dof = spec.dof
    except:
        dof = len(spec.wave)
    if len(kwargs.get('mask',[])) > 0:
        dof = dof*(numpy.sum(1.-kwargs['mask']))/len(kwargs['mask'])
    dof = dof-nparameters-1

    f.write('\n\nResidual chi^2 = {:.0f} for {:.0f} degrees of freedom'.format(stat,dof))
    f.write('\nProbability that model matches data = {:.4f}'.format(stats.chi2.sf(stat,dof)))
    f.write('\nSource/model scale factor = {:.2f} implying a radius of {:.3f} solar radii at 10 pc\n'.format(scl,(scl**0.5*10.*u.pc).to(u.Rsun).value))

    f.write('\n\nFitting completed in {:.1f} seconds = {:.2f} hours'.format(kwargs['total_time'],kwargs['total_time']/3600.))
    f.write('\nResults may be found in the files {}*'.format(kwargs['filebase']))
    f.close()
    return

    for now just plotting best model
    would like to do draws from posterior instead
    
for now just plotting best model
would like to do draws from posterior instead
for now just plotting best model would like to do draws from posterior instead
def _modelFitEMCEE_summary(sampler,spec,file,**kwargs):
    base_samples = sampler.chain
    nwalkers = base_samples.shape[0]
    nsamples = base_samples.shape[1]
    nparameters = base_samples.shape[2]
    samples = base_samples[:, int(kwargs['burn_fraction']*nsamples):, :].reshape((-1, nparameters))
    f = open(file,'w')
    f.write('EMCEE fitting analysis of spectrum of {} using the models of {}'.format(spec.name,kwargs['model']))
    f.write('\nFitting performed on {}'.format(time.strftime("%Y %h %d %I:%M:%S")))
    f.write('\n\nMCMC paramaters:')
    f.write('\n\tNumber of walkers = {}'.format(nwalkers))
    f.write('\n\tNumber of samples = {}'.format(nsamples))
    f.write('\n\tNumber of fit parameters = {}'.format(nparameters))
    f.write('\n\tBurn-in fraction = {}'.format(kwargs['burn_fraction']))
    f.write('\n\nBest fit parameters')
    for i,v in enumerate(['teff','logg','z'][0:nparameters]):
        fit = numpy.percentile(samples[:,i], [16, 50, 84])
        f.write('\n\t{} = {}+{}-{} {}'.format(SPECTRAL_MODEL_PARAMETERS[v]['title'],fit[1],fit[2]-fit[1],fit[1]-fit[0],SPECTRAL_MODEL_PARAMETERS[v]['unit'].to_string()))
    mkwargs = copy.deepcopy(kwargs)
    for i,l in enumerate(['teff','logg','z'][:samples.shape[-1]]):
        mkwargs[l] = numpy.median(samples[:,i])
    mdl = loadModel(**mkwargs)
    stat,scl = compareSpectra(spec,mdl,**kwargs)
    try:
        dof = spec.dof
    except:
        dof = len(spec.wave)
    if len(kwargs.get('mask',[])) > 0:
        dof = dof*(numpy.sum(1.-kwargs['mask']))/len(kwargs['mask'])
    dof = dof-nparameters-1
    f.write('\n\nResidual chi^2 = {:.0f} for {:.0f} degrees of freedom'.format(stat,dof))
    f.write('\nProbability that model matches data = {:.4f}'.format(stats.chi2.sf(stat,dof)))
    f.write('\nSource/model scale factor = {:.2f} implying a radius of {:.3f} solar radii at 10 pc\n'.format(scl,(scl**0.5*10.*u.pc).to(u.Rsun).value))
    f.write('\n\nFitting completed in {:.1f} seconds = {:.2f} hours'.format(kwargs['total_time'],kwargs['total_time']/3600.))
    f.write('\nResults may be found in the files {}*'.format(kwargs['filebase']))
    f.close()
    return
def _modelFitEMCEE_summary ( sampler , spec , file , ** kwargs ) : base_samples = sampler . chain nwalkers = base_samples . shape [ 0 ] nsamples = base_samples . shape [ 1 ] nparameters = base_samples . shape [ 2 ] samples = base_samples [ : , int ( kwargs [ 'burn_fraction' ] * nsamples ) : , : ] . reshape ( ( - 1 , nparameters ) ) f = open ( file , 'w' ) f . write ( 'EMCEE fitting analysis of spectrum of {} using the models of {}' . format ( spec . name , kwargs [ 'model' ] ) ) f . write ( '\nFitting performed on {}' . format ( time . strftime ( "%Y %h %d %I:%M:%S" ) ) ) f . write ( '\n\nMCMC paramaters:' ) f . write ( '\n\tNumber of walkers = {}' . format ( nwalkers ) ) f . write ( '\n\tNumber of samples = {}' . format ( nsamples ) ) f . write ( '\n\tNumber of fit parameters = {}' . format ( nparameters ) ) f . write ( '\n\tBurn-in fraction = {}' . format ( kwargs [ 'burn_fraction' ] ) ) f . write ( '\n\nBest fit parameters' ) for i , v in enumerate ( [ 'teff' , 'logg' , 'z' ] [ 0 : nparameters ] ) : fit = numpy . percentile ( samples [ : , i ] , [ 16 , 50 , 84 ] ) f . write ( '\n\t{} = {}+{}-{} {}' . format ( SPECTRAL_MODEL_PARAMETERS [ v ] [ 'title' ] , fit [ 1 ] , fit [ 2 ] - fit [ 1 ] , fit [ 1 ] - fit [ 0 ] , SPECTRAL_MODEL_PARAMETERS [ v ] [ 'unit' ] . to_string ( ) ) ) mkwargs = copy . deepcopy ( kwargs ) for i , l in enumerate ( [ 'teff' , 'logg' , 'z' ] [ : samples . shape [ - 1 ] ] ) : mkwargs [ l ] = numpy . median ( samples [ : , i ] ) mdl = loadModel ( ** mkwargs ) stat , scl = compareSpectra ( spec , mdl , ** kwargs ) try : dof = spec . dof except : dof = len ( spec . wave ) if len ( kwargs . get ( 'mask' , [ ] ) ) > 0 : dof = dof * ( numpy . sum ( 1. - kwargs [ 'mask' ] ) ) / len ( kwargs [ 'mask' ] ) dof = dof - nparameters - 1 f . write ( '\n\nResidual chi^2 = {:.0f} for {:.0f} degrees of freedom' . format ( stat , dof ) ) f . write ( '\nProbability that model matches data = {:.4f}' . format ( stats . chi2 . sf ( stat , dof ) ) ) f . write ( '\nSource/model scale factor = {:.2f} implying a radius of {:.3f} solar radii at 10 pc\n' . format ( scl , ( scl ** 0.5 * 10. * u . pc ) . to ( u . Rsun ) . value ) ) f . write ( '\n\nFitting completed in {:.1f} seconds = {:.2f} hours' . format ( kwargs [ 'total_time' ] , kwargs [ 'total_time' ] / 3600. ) ) f . write ( '\nResults may be found in the files {}*' . format ( kwargs [ 'filebase' ] ) ) f . close ( ) return
for now just plotting best model
would like to do draws from posterior instead
for now just plotting best model would like to do draws from posterior instead
'''
    for now just plotting best model
    would like to do draws from posterior instead
    ''' # extract best fit values # copmute DOF
sampler
spec
file