def makeStartFromExplore(ListOfChains,StablePerc,SampleParamFile,OutputParamFile): """ Reads a list of single parameter step-exoploration chains and prints a startparam file based on this data. This routine also prints whether a chain has stabilized the correct single-parameter acceptance rate. """ ListFiles = open(ListOfChains,'r') ListFiles = ListFiles.readlines() ModelParams = ReadStartParams(SampleParamFile) step = {} for file in ListFiles: data = readMCMC(file.strip('\n')) if data['acr'][-1] > 0.44-StablePerc and data['acr'][-1] < 0.44+StablePerc: for par in data.keys(): if not isNonParam(par): medfrac = np.median(data['frac'][-1000:]) print par+' has stabilized, acr = '+format(data['acr'][-1],'.2f')+' frac = '+str(medfrac) step[par] = {'frac':medfrac} else: for par in data.keys(): if not isNonParam(par): print par+' has NOT stabilized, acr = '+format(data['acr'][-1],'.2f') for par in step.keys(): for key in ModelParams.keys(): if key == par: ModelParams[par]['step'] = ModelParams[par]['step']*step[par]['frac'] ModelParams[par]['open'] = True PrintModelParams(ModelParams,OutputParamFile)
def printDerivedParFile_MTQ_2011(ParFile,OutParFile): """ print the derived parameters in the format of the parameter file """ ModelParams = ReadStartParams(ParFile) derived = {} Period = computePeriod(ModelParams) derived['Period'] = {'value':Period,'printformat':'.9f','open':True,'step':0.0} RefFilt = ModelParams['RefFilt']['printformat'] # get Filter and Transit time tags Tags = getTags(ModelParams) # compute parameters used to compute lightcurve using the reference filter Dref, v1ref, v2ref = MTQ_2011.MTQ_FilterParams(RefFilt,Tags,ModelParams) u1ref, u2ref = LDC_v2u(v1ref,v2ref) tT = ModelParams['tT']['value'] tG = ModelParams['tG']['value'] TransitRef = MTQ_2011.MTQ_getDerivedParams(Dref,tT,tG,u1ref,u2ref,Period) #print TransitRef derived['inc'] = {'value':TransitRef['inc']*180e0/np.pi,'printformat':'.4f','open':True,'step':0.0} derived['b'] = {'value':TransitRef['b'],'printformat':'.6f','open':True,'step':0.0} derived['aRs'] = {'value':TransitRef['aRs'],'printformat':'.6f','open':True,'step':0.0} derived['velRs'] = {'value':TransitRef['velRs'],'printformat':'.6f','open':True,'step':0.0} derived['rho_star'] = {'value':TransitRef['rho_star'],'printformat':'.6f','open':True,'step':0.0} derived['RpRs'+'.'+ModelParams['RefFilt']['printformat']] = \ {'value':TransitRef['RpRs'],'printformat':'.9f','open':True,'step':0.0} for key in ModelParams.keys(): if key.startswith('T0'): split_TT = map(str,key.split('.')) transit_tag = split_TT[1].strip() # compute D, v1, v2 and then u1 and u2 for a given transit tag D, v1, v2 = MTQ_2011.MTQ_FilterParams(transit_tag,Tags,ModelParams) u1, u2 = LDC_v2u(v1,v2) RpRs = computeRpRs(u1,u2,tT,tG,D) filterD = filterMatchD(transit_tag,Tags,ModelParams) #print filterD derived['RpRs'+'.'+filterD] = {'value':RpRs,'printformat':'.9f','open':True,'step':0.0} PrintModelParams(derived,OutParFile)
def MinuitPar2Err(ParFile,ErrFile): """ Convert a Minuit par file to an error file """ ModelParams = ReadStartParams(ParFile) OutFile = open(ErrFile,'w') header = '# parname | value | lower | upper | useSingle ?' print >> OutFile, header for par in ModelParams.keys(): if par.lower() != 'reffilt': if ModelParams[par]['open']: ValString = format(ModelParams[par]['value'],ModelParams[par]['printformat']) ErrString = format(ModelParams[par]['step'],ModelParams[par]['printformat']) print >> OutFile, par+'|'+ValString+'|'+ErrString+'|'+ErrString+'| True ' else: ValString = format(ModelParams[par]['value'],ModelParams[par]['printformat']) ErrString = 'nan' print >> OutFile, par+'|'+ValString+'|'+ErrString+'|'+ErrString+'| False ' OutFile.close()
def printErrors(MCMCfile,BestfitFile,OutputFile): """ Gets data out from the MCMC data file and the BESTFIT parameters file and prints uncertainties """ mcmcData = readMCMC(MCMCfile) BestFitParams = ReadStartParams(BestfitFile) erf15 = ((1e0 - scipy.special.erf(1e0/np.sqrt(2e0)))/2e0) erf84 = (1e0 - erf15) OutFileObject = open(OutputFile,'w') print >> OutFileObject, '# Parameter | low15 | upp84 | use Single ?' for param in BestFitParams.keys(): if BestFitParams[param]['open']: sort_list = sorted(mcmcData[param]) Npoints = len(mcmcData[param]) id84 = long(round(erf84*(Npoints-1))) id15 = long(round(erf15*(Npoints-1))) low15 = abs(BestFitParams[param]['value']-sort_list[id15]) upp84 = abs(BestFitParams[param]['value']-sort_list[id84]) if low15 >= upp84: greater = low15 lower = upp84 else: greater = upp84 lower = low15 useSingle = False if 1.1*lower >= 0.9*greater: useSingle = True print >> OutFileObject, param+'|'+\ format(BestFitParams[param]['value'],BestFitParams[param]['printformat'])+'|'+\ format(low15,BestFitParams[param]['printformat'])+'|'+\ format(upp84,BestFitParams[param]['printformat'])+'|'+\ str(useSingle)+'|:' else: if param.lower() != 'reffilt': print >> OutFileObject, param+'|'+\ format(BestFitParams[param]['value'],BestFitParams[param]['printformat'])+'|'+\ format(float('nan'),BestFitParams[param]['printformat'])+'|'+\ format(float('nan'),BestFitParams[param]['printformat'])+'|'+\ str(True)+'|:' OutFileObject.close()
def printDerived_MTQ_2011(STARTFILE,MCMCfile,DerivedFile): """ print a file with Derived parameters from the MCMC ensemble. """ hdrKeys = readMCMChdr(MCMCfile) ModelParams = ReadStartParams(STARTFILE) mcmcFile = open(MCMCfile,'r') mcmcFile = mcmcFile.readlines() OutFileObject = open(DerivedFile,'w') for line in mcmcFile: derived = {} if not line.startswith('#'): data_line = ReadMCMCline(line,hdrKeys) print 'step/line = ',format(long(data_line['istep']),'n') for key in ModelParams.keys(): if ModelParams[key]['open']: ModelParams[key]['value'] = data_line[key] Period = computePeriod(ModelParams) derived['Period'] = {'value':Period,'printformat':'.9f'} RefFilt = ModelParams['RefFilt']['printformat'] # get Filter and Transit time tags Tags = getTags(ModelParams) # compute parameters used to compute lightcurve using the reference filter Dref, v1ref, v2ref = MTQ_2011.MTQ_FilterParams(RefFilt,Tags,ModelParams) u1ref, u2ref = LDC_v2u(v1ref,v2ref) tT = ModelParams['tT']['value'] tG = ModelParams['tG']['value'] TransitRef = MTQ_2011.MTQ_getDerivedParams(Dref,tT,tG,u1ref,u2ref,Period) #print TransitRef derived['inc'] = {'value':TransitRef['inc']*180e0/np.pi,'printformat':'.4f'} derived['b'] = {'value':TransitRef['b'],'printformat':'.6f'} derived['aRs'] = {'value':TransitRef['aRs'],'printformat':'.6f'} derived['velRs'] = {'value':TransitRef['velRs'],'printformat':'.6f'} derived['rho_star'] = {'value':TransitRef['rho_star'],'printformat':'.6f'} derived['RpRs'+'.'+ModelParams['RefFilt']['printformat']] = \ {'value':TransitRef['RpRs'],'printformat':'.9f'} for key in ModelParams.keys(): if key.startswith('T0'): split_TT = map(str,key.split('.')) transit_tag = split_TT[1].strip() # compute D, v1, v2 and then u1 and u2 for a given transit tag D, v1, v2 = MTQ_2011.MTQ_FilterParams(transit_tag,Tags,ModelParams) u1, u2 = LDC_v2u(v1,v2) RpRs = computeRpRs(u1,u2,tT,tG,D) filterD = filterMatchD(transit_tag,Tags,ModelParams) #print filterD derived['RpRs'+'.'+filterD] = {'value':RpRs,'printformat':'.9f'} DerivedLine = '' keylist = derived.keys() Nkeys = len(keylist) if data_line['istep'] == 0: keyorder = {} for i in range(len(keylist)): keyorder[i] = keylist[i] for i in range(Nkeys): if i == 0: DerivedLine =\ str(format(derived[keylist[i]]['value'],\ derived[keylist[i]]['printformat'])) else: DerivedLine =\ DerivedLine+'|'+str(format(derived[keylist[i]]['value'],\ derived[keylist[i]]['printformat'])) else: for i in range(Nkeys): if i == 0: DerivedLine =\ str(format(derived[keylist[i]]['value'],\ derived[keylist[i]]['printformat'])) else: DerivedLine = DerivedLine+'|'+str(format(derived[keylist[i]]['value'],\ derived[keylist[i]]['printformat'])) DerivedLine = DerivedLine+'|'+str(format(data_line['istep'],'.0f'))+'|:' print >> OutFileObject, DerivedLine keyline = '' for i in range(Nkeys): if i == 0: keyline = '#'+keylist[i] else: keyline = keyline+'|'+keylist[i] keyline = keyline+'|istep|:' OutFileObject.close() #print >> OutFileObject, keyline opencopy = open(DerivedFile,'r') opencopy = opencopy.readlines() os.system('rm -v %s' % (DerivedFile)) rearranged = open(DerivedFile,'w') print >> rearranged, keyline for iline in range(len(opencopy)): print >> rearranged, opencopy[iline].strip('\n') rearranged.close()