def StpSzTest(params, out_pre, spec, AccuracyBoost, ParamRange, paramName, lmax, verbose=True): #StpSzs=[scale*float(stepSizes[paramName]) for scale in ScaleSet] #data=np.zeros((NumStep+1,6)) #Return list containing "StpSz_x": [parval,TT,EE,BB,TE,kk] that can be plotted #result={} #Record fiducial value #parfidval=params[paramName] for paramVal in ParamRange: if verbose: print paramName, paramVal params[paramName] = paramVal if paramName == 'theta100': params['H0'] = getHubbleCosmology(params['theta100'], params) CLS = getPowerCamb(params, spec, AccuracyBoost=AccuracyBoost) #Name gives value of the parameter and lmax np.savetxt("output/StepSize/" + paramName + "_" + str(paramVal) + "_" + out_pre + spec + "_lmax_" + str(int(lmax)) + ".csv", CLS, delimiter=",") print "--------------Done with ", paramName
def getHubblevsTheta(fparams,step): ftheta = fparams['theta100'] start = ftheta - step end = ftheta + step width = step/100. ParamRange = np.arange(start,end+width,width) data=[] for theta in ParamRange: H0 = getHubbleCosmology(theta,fparams) data.append([theta,H0]) return np.array(data)
def getHubblevsTheta(fparams, step): ftheta = fparams['theta100'] start = ftheta - step end = ftheta + step width = step / 100. ParamRange = np.arange(start, end + width, width) data = [] for theta in ParamRange: H0 = getHubbleCosmology(theta, fparams) data.append([theta, H0]) return np.array(data)
def main(argv): starttime = timer() # Read Config iniFile = "input/StepSize.ini" Config = ConfigParser.SafeConfigParser() # Reserve case when get string Config.optionxform = str Config.read(iniFile) spec = Config.get('general', 'spec') out_pre = Config.get('general', 'output_prefix') AccuracyBoost = Config.getboolean('general', 'AccuracyBoost') paramList = [] fparams = {} stepSizes = {} testSteps = {} for (key, val) in Config.items('camb'): if ',' in val: param, step, testStep = val.split(',') paramList.append(key) fparams[key] = float(param) stepSizes[key] = float(step) testSteps[key] = bool(int(testStep)) else: fparams[key] = float(val) #start,end,width = [float(x) for x in Config.get('StpSzTest','ParamRange').split(',')] if not ('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'], fparams) print paramList print fparams for paramName in paramList: if not testSteps[paramName]: continue #Make range for each parameter #---------------------------- #First test: x2 the range, x0.01 the stepsize start = fparams[paramName] - stepSizes[paramName] end = fparams[paramName] + stepSizes[paramName] width = stepSizes[paramName] / 100. ParamRange = np.arange(start, end + width, width) #---------------------------- StpSzTest(fparams, out_pre, spec, AccuracyBoost, ParamRange, paramName, fparams['lmax'], verbose=True) endtime = timer() print "Time elapsed: ", endtime - starttime
def StpSzTest(params,out_pre,spec,AccuracyBoost,ParamRange,paramName,lmax,verbose=True): #StpSzs=[scale*float(stepSizes[paramName]) for scale in ScaleSet] #data=np.zeros((NumStep+1,6)) #Return list containing "StpSz_x": [parval,TT,EE,BB,TE,kk] that can be plotted #result={} #Record fiducial value #parfidval=params[paramName] for paramVal in ParamRange: if verbose: print paramName,paramVal params[paramName]=paramVal if paramName=='theta100': params['H0'] = getHubbleCosmology(params['theta100'],params) CLS = getPowerCamb(params,spec,AccuracyBoost=AccuracyBoost) #Name gives value of the parameter and lmax np.savetxt("output/StepSize/"+paramName+"_"+str(paramVal)+"_"+out_pre+spec+"_lmax_"+str(int(lmax))+".csv",CLS,delimiter=",") print "--------------Done with ",paramName
def main(argv): starttime = timer() # Read Config iniFile = "input/StepSize.ini" Config = ConfigParser.SafeConfigParser() # Reserve case when get string Config.optionxform = str Config.read(iniFile) spec = Config.get('general','spec') out_pre = Config.get('general','output_prefix') AccuracyBoost = Config.getboolean('general','AccuracyBoost') paramList = [] fparams = {} stepSizes = {} testSteps={} for (key, val) in Config.items('camb'): if ',' in val: param, step, testStep = val.split(',') paramList.append(key) fparams[key] = float(param) stepSizes[key] = float(step) testSteps[key] = bool(int(testStep)) else: fparams[key] = float(val) #start,end,width = [float(x) for x in Config.get('StpSzTest','ParamRange').split(',')] if not('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'],fparams) print paramList print fparams for paramName in paramList: if not testSteps[paramName]: continue #Make range for each parameter #---------------------------- #First test: x2 the range, x0.01 the stepsize start = fparams[paramName] - stepSizes[paramName] end = fparams[paramName] + stepSizes[paramName] width = stepSizes[paramName]/100. ParamRange = np.arange(start,end+width,width) #---------------------------- StpSzTest(fparams,out_pre,spec,AccuracyBoost,ParamRange,paramName,fparams['lmax'],verbose=True) endtime = timer() print "Time elapsed: ",endtime-starttime
parser.add_argument("--AccuracyBoost", type=bool) parser.add_argument("--testParam", type=str) parser.add_argument("--testVal", type=float) parser.add_argument("--output", type=str) args = parser.parse_args() #Get fiducial iniFile = os.environ[ 'FISHER_DIR'] + '/input/cluster-submit/oldtestStepSize_input.ini' Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) fparams = {} for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) if not ('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'], fparams) # Assigning testVal to testParam fparams[args.testParam] = args.testVal if args.testParam == 'theta100': fparams['H0'] = getHubbleCosmology(fparams['theta100'], fparams) print "Testing ", args.testParam, "=", args.testVal CLS = getPowerCamb(fparams, args.spec, AccuracyBoost=args.AccuracyBoost) np.savetxt(args.output, CLS, delimiter=",")
#Get fiducial iniFile = os.environ[ 'FISHER_DIR'] + '/input/cluster-submit/testStepSize_input.ini' config = ConfigParser.SafeConfigParser() config.optionxform = str config.read(iniFile) fparams = {} for (key, val) in config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) if not ('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'], fparams) # Assigning testVal(stepsize) to testParam spec = args.spec AccuracyBoost = args.AccuracyBoost paramName = args.testParam h = args.testVal print "Calculating forward difference for ", paramName pparams = fparams.copy() pparams[paramName] = fparams[paramName] + 0.5 * h if paramName == 'theta100': pparams['H0'] = getHubbleCosmology(theta=pparams['theta100'], params=pparams) pCls = getPowerCamb(pparams, spec, AccuracyBoost=AccuracyBoost) print "Calculating backward difference for ", paramName
def main(argv): verbose = True # Read Config iniFile = "input/makeDefaultsBAO.ini" Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) out_pre = Config.get('general', 'output_prefix') AccuracyBoost = Config.getboolean('general', 'AccuracyBoost') paramList = [] fparams = {} stepSizes = {} for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') paramList.append(key) fparams[key] = float(param) stepSizes[key] = float(step) else: fparams[key] = float(val) zrange = [float(x) for x in Config.get('DESI', 'redshift').split(',')] sigmafk = [ float(x) / 1000. for x in Config.get('DESI', 'sigmafkx1000').split(',') ] # Uncomment if need to calculate Derivs # Save fiducials print "Calculating and saving fiducial cosmology..." if not ('H0' in fparams): fparams['H0'] = getHubbleCosmology(theta=fparams['theta100'], params=fparams) fidfk = getBAOCamb(zrange, fparams, AccuracyBoost=AccuracyBoost) np.savetxt("output/" + out_pre + "_fidfk.csv", fidfk, delimiter=",") # Calculate and save derivatives for paramName in paramList: h = stepSizes[paramName] print "Calculating forward difference for ", paramName pparams = fparams.copy() pparams[paramName] = fparams[paramName] + 0.5 * h if paramName == 'theta100': pparams['H0'] = getHubbleCosmology(theta=pparams['theta100'], params=pparams) pfk = getBAOCamb(zrange, pparams, AccuracyBoost=AccuracyBoost) print "Calculating backward difference for ", paramName mparams = fparams.copy() mparams[paramName] = fparams[paramName] - 0.5 * h if paramName == 'theta100': mparams['H0'] = getHubbleCosmology(theta=mparams['theta100'], params=mparams) mfk = getBAOCamb(zrange, mparams, AccuracyBoost=AccuracyBoost) dfk = (pfk - mfk) / h np.savetxt("output/" + out_pre + "_dfk_" + paramName + ".csv", dfk, delimiter=",") # Calculate Fisher Matrix paramCombs = itertools.combinations_with_replacement(paramList, 2) Fisher = np.zeros((len(paramList), len(paramList))) dfks = {} for paramName in paramList: dfks[paramName] = np.loadtxt("output/" + out_pre + "_dfk_" + paramName + ".csv", delimiter=",") for param1, param2 in paramCombs: if verbose: print "Parameter combination : ", param1, param2 i = paramList.index(param1) j = paramList.index(param2) Fz = 0. for k in range(0, len(zrange)): dfk1 = dfks[param1][k] dfk2 = dfks[param2][k] Fz += dfk1 * dfk2 / sigmafk[k]**2. #if verbose: print "dfk1,dfk2,sigmafk,Fz:",dfk1*dfk2/sigmafk[k]**2,Fz Fisher[i, j] = Fz Fisher[j, i] = Fz if verbose: print Fisher print np.diagonal(Fisher) np.savetxt("output/" + out_pre + "_Fisher.csv", Fisher, delimiter=",")
#Get fiducial iniFile = os.environ['FISHER_DIR']+'/input/cluster-submit/testStepSize_input.ini' config = ConfigParser.SafeConfigParser() config.optionxform = str config.read(iniFile) fparams = {} for (key, val) in config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) if not('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'],fparams) # Assigning testVal(stepsize) to testParam spec = args.spec AccuracyBoost = args.AccuracyBoost paramName = args.testParam h = args.testVal print "Calculating forward difference for ", paramName pparams = fparams.copy() pparams[paramName] = fparams[paramName] + 0.5*h if paramName=='theta100': pparams['H0'] = getHubbleCosmology(theta=pparams['theta100'],params=pparams) pCls = getPowerCamb(pparams,spec,AccuracyBoost=AccuracyBoost) print "Calculating backward difference for ", paramName mparams = fparams.copy()
def main(argv): verbose=True # Read Config iniFile = "input/makeDefaultsBAO.ini" Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) out_pre = Config.get('general','output_prefix') AccuracyBoost = Config.getboolean('general','AccuracyBoost') paramList = [] fparams = {} stepSizes = {} for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') paramList.append(key) fparams[key] = float(param) stepSizes[key] = float(step) else: fparams[key] = float(val) zrange = [float(x) for x in Config.get('DESI','redshift').split(',')] sigmafk = [float(x)/1000. for x in Config.get('DESI','sigmafkx1000').split(',')] # Uncomment if need to calculate Derivs # Save fiducials print "Calculating and saving fiducial cosmology..." if not('H0' in fparams): fparams['H0'] = getHubbleCosmology(theta=fparams['theta100'],params=fparams) fidfk = getBAOCamb(zrange,fparams,AccuracyBoost=AccuracyBoost) np.savetxt("output/"+out_pre+"_fidfk.csv",fidfk,delimiter=",") # Calculate and save derivatives for paramName in paramList: h = stepSizes[paramName] print "Calculating forward difference for ", paramName pparams = fparams.copy() pparams[paramName] = fparams[paramName] + 0.5*h if paramName=='theta100': pparams['H0'] = getHubbleCosmology(theta=pparams['theta100'],params=pparams) pfk = getBAOCamb(zrange,pparams,AccuracyBoost=AccuracyBoost) print "Calculating backward difference for ", paramName mparams = fparams.copy() mparams[paramName] = fparams[paramName] - 0.5*h if paramName=='theta100': mparams['H0'] = getHubbleCosmology(theta=mparams['theta100'],params=mparams) mfk = getBAOCamb(zrange,mparams,AccuracyBoost=AccuracyBoost) dfk = (pfk-mfk)/h np.savetxt("output/"+out_pre+"_dfk_"+paramName+".csv",dfk,delimiter=",") # Calculate Fisher Matrix paramCombs = itertools.combinations_with_replacement(paramList,2) Fisher = np.zeros((len(paramList),len(paramList))) dfks = {} for paramName in paramList: dfks[paramName] = np.loadtxt("output/"+out_pre+"_dfk_"+paramName+".csv",delimiter=",") for param1,param2 in paramCombs: if verbose: print "Parameter combination : ", param1,param2 i = paramList.index(param1) j = paramList.index(param2) Fz = 0. for k in range(0,len(zrange)): dfk1 = dfks[param1][k] dfk2 = dfks[param2][k] Fz += dfk1*dfk2/sigmafk[k]**2. #if verbose: print "dfk1,dfk2,sigmafk,Fz:",dfk1*dfk2/sigmafk[k]**2,Fz Fisher[i,j] = Fz Fisher[j,i] = Fz if verbose: print Fisher print np.diagonal(Fisher) np.savetxt("output/"+out_pre+"_Fisher.csv",Fisher,delimiter=",")
parser.add_argument("--AccuracyBoost",type=bool) parser.add_argument("--testParam",type=str) parser.add_argument("--testVal",type=float) parser.add_argument("--output",type=str) args = parser.parse_args() #Get fiducial iniFile = os.environ['FISHER_DIR']+'/input/cluster-submit/oldtestStepSize_input.ini' Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) fparams = {} for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) if not('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'],fparams) # Assigning testVal to testParam fparams[args.testParam]=args.testVal if args.testParam=='theta100': fparams['H0'] = getHubbleCosmology(fparams['theta100'],fparams) print "Testing ",args.testParam,"=",args.testVal CLS = getPowerCamb(fparams,args.spec,AccuracyBoost=args.AccuracyBoost) np.savetxt(args.output,CLS,delimiter=",")
iniFile = os.environ['FISHER_DIR']+'/input/cluster-submit/makeDerivs_input.ini' Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) fparams = {} fidscript = '' for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) fidscript += key+' = '+val+'\n' if not('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'],fparams) try: derivForm = Config.getint('general','derivForm') except: derivForm = 0 # Fid or deriv if paramName == '0': CLS = getPowerCamb(fparams,spec,AccuracyBoost=AccuracyBoost) output = args.output+'_fCls.csv' # Save fid + stepsize fidscript ='[camb]\n'+fidscript filename = args.output+'_fid.csv' with open(filename,'w') as tempFile: tempFile.write(fidscript)
def main(argv): # Read Config iniFile = "input/testmnu_makeDerivs.ini" Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) spec = Config.get("general", "spec") out_pre = Config.get("general", "output_prefix") AccuracyBoost = Config.getboolean("general", "AccuracyBoost") paramList = [] fparams = {} stepSizes = {} for (key, val) in Config.items("camb"): if "," in val: param, step = val.split(",") paramList.append(key) fparams[key] = float(param) stepSizes[key] = float(step) else: fparams[key] = float(val) # Save starting fiducials fidparams = fparams.copy() print fidparams # testList = Config.get('testmnu','testList').split(',') # spread = Config.getfloat('testmnu','spread') # testRange = np.arange(1.0-spread/2,1.0+spread/2,spread/6) testRange = {} testList = [] for (key, val) in Config.items("testmnu"): pmin, pmax, numstep = [float(x) for x in val.split(",")] testList.append(key) pstep = (pmax - pmin) / numstep testRange[key] = np.arange(pmin, pmax, pstep) # round the value to about 5 significant digits decimal = int(-(np.ceil(np.log10(pmin)) - 5)) testRange[key] = [round(x, decimal) for x in testRange[key]] print "testRange: ", testRange # avoid recalculation with central fiducial set # fidDone = 1 prefix = "" for testParam in testList: for testVal in testRange[testParam]: prefix = testParam + "_" + str(testVal) print "Testing ", testParam, " = ", testVal # Reset to starting fid, then multiply a fraction fparams = fidparams.copy() fparams[testParam] = testVal # Save fiducials print "Calculating and saving fiducial cosmology..." if not ("H0" in fparams): fparams["H0"] = getHubbleCosmology(theta=fparams["theta100"], params=fparams) fidCls = getPowerCamb(fparams, spec, AccuracyBoost=AccuracyBoost) np.savetxt("output/" + out_pre + spec + "_" + prefix + "_fCls.csv", fidCls, delimiter=",") # Calculate and save derivatives for paramName in paramList: h = stepSizes[paramName] print "Calculating forward difference for ", paramName pparams = fparams.copy() pparams[paramName] = fparams[paramName] + 0.5 * h if paramName == "theta100": pparams["H0"] = getHubbleCosmology(theta=pparams["theta100"], params=pparams) pCls = getPowerCamb(pparams, spec, AccuracyBoost=AccuracyBoost) print "Calculating backward difference for ", paramName mparams = fparams.copy() mparams[paramName] = fparams[paramName] - 0.5 * h if paramName == "theta100": mparams["H0"] = getHubbleCosmology(theta=mparams["theta100"], params=mparams) mCls = getPowerCamb(mparams, spec, AccuracyBoost=AccuracyBoost) dCls = (pCls - mCls) / h np.savetxt( "output/" + out_pre + spec + "_" + prefix + "_dCls_" + paramName + ".csv", dCls, delimiter="," )
'FISHER_DIR'] + '/input/cluster-submit/makeDerivs_input.ini' Config = ConfigParser.SafeConfigParser() Config.optionxform = str Config.read(iniFile) fparams = {} fidscript = '' for (key, val) in Config.items('camb'): if ',' in val: param, step = val.split(',') fparams[key] = float(param) else: fparams[key] = float(val) fidscript += key + ' = ' + val + '\n' if not ('H0' in fparams): fparams['H0'] = getHubbleCosmology(fparams['theta100'], fparams) try: derivForm = Config.getint('general', 'derivForm') except: derivForm = 0 # Fid or deriv if paramName == '0': CLS = getPowerCamb(fparams, spec, AccuracyBoost=AccuracyBoost) output = args.output + '_fCls.csv' # Save fid + stepsize fidscript = '[camb]\n' + fidscript filename = args.output + '_fid.csv' with open(filename, 'w') as tempFile: tempFile.write(fidscript)