def get_par(self, step, dep=[]): #--current value must be within min and max self.checklimits() #--initialize resman self.resman = RESMAN(self.nworkers) self.parman = self.resman.parman #--setups guess = self.get_guess(dep) bounds = self.get_bounds() self.set_counters() self.parman.set_new_params(guess, initial=True) #--run fit fit = least_squares(self.get_residuals, guess, bounds=bounds, method='trf', ftol=conf['ftol']) #--generate summary. It will update system with the final results self.gen_summary(step, fit.x) #--close resman self.resman.shutdown() return fit.x
def collect_exp_data(wdir): load_config('%s/input.py'%wdir) conf['bootstrap']=False istep=core.get_istep() core.mod_conf(istep) #--set conf as specified in istep resman=RESMAN(nworkers=1,parallel=False,datasets=True) obsres={} if 'idis' in conf['datasets'] : obsres['idis'] = resman.idisres if 'pidis' in conf['datasets'] : obsres['pidis'] = resman.pidisres if 'sidis' in conf['datasets'] : obsres['sidis'] = resman.sidisres if 'psidis' in conf['datasets'] : obsres['psidis'] = resman.psidisres if 'dy' in conf['datasets'] : obsres['dy'] = resman.dyres if 'sia' in conf['datasets'] : obsres['sia'] = resman.siares print('\ncollecting exp data using the setup as in %s\n'%wdir) data={} for reaction in obsres: tabs={} for idx in obsres[reaction].tabs: tabs[idx]={} tabs[idx]['value']=obsres[reaction].tabs[idx]['value'] tabs[idx]['alpha']=obsres[reaction].tabs[idx]['alpha'] data[reaction]=tabs save(data,'%s/expdata.dat'%(wdir)) #--close resman resman.shutdown()
def gen_samples(wdir,nsamples=100,nsize=10): load_config('%s/input.py'%wdir) conf['bootstrap']=False istep=core.get_istep() core.mod_conf(istep) #--set conf as specified in istep resman=RESMAN(nworkers=5,parallel=True,datasets=True) parman=resman.parman order=parman.order obsres={} if 'idis' in conf['datasets'] : obsres['idis'] = resman.idisres if 'pidis' in conf['datasets'] : obsres['pidis'] = resman.pidisres if 'sidis' in conf['datasets'] : obsres['sidis'] = resman.sidisres if 'psidis' in conf['datasets'] : obsres['psidis'] = resman.psidisres if 'dy' in conf['datasets'] : obsres['dy'] = resman.dyres if 'sia' in conf['datasets'] : obsres['sia'] = resman.siares print('\ngen ml samples using the setup as in %s\n'%wdir) checkdir('%s/mlsamples'%wdir) data=gen_dataframe(parman.order,obsres,nsize) for _ in range(nsamples): lprint('progress: %d/%d'%(_,nsamples)) par=parman.gen_flat(setup=True) res,rres,nres=resman.get_residuals(par) #--fill dataframe i=data['size'] data['params'][i]=par for reaction in obsres: for idx in data['reactions'][reaction]: prediction=copy.copy(obsres[reaction].tabs[idx]['prediction']) data['reactions'][reaction][idx][i]=prediction data['size']+=1 if data['size']==nsize: save(data,'%s/mlsamples/%s'%(wdir,id_generator(12))) data=gen_dataframe(parman.order,obsres,nsize) print #--close resman resman.shutdown()
def __init__(self, tag): load_config('data/input-%s.py' % tag) resman = RESMAN(nworkers=1, parallel=False, datasets=False) self.parman = resman.parman self.jar = np.load('data/jar-%s.npy' % tag, allow_pickle=True, encoding='latin1').item() self.parman.order = self.jar['order'] self.par = self.jar['par'] self.nrep = len(self.par)
def gen_dist_from_fitpack(wdir,dist): print('\ngenerating %s tables for benchmark using %s'%(dist,wdir)) load_config('%s/input.py'%wdir) istep=core.get_istep() core.mod_conf(istep) #--set conf as specified in istep resman=RESMAN(nworkers=1,parallel=False,datasets=False) parman=resman.parman jar=load('%s/data/jar-%d.dat'%(wdir,istep)) replicas=jar['replicas'] X,Q2=gen_grid(dist) qpd=conf[dist] nx=len(X) nQ2=len(Q2) flavs=[-5,-4,-3,-2,-1,1,2,3,4,5,21] fmap={-5:'bb',-4:'cb',-3:'sb',-2:'ub',-1:'db'} fmap.update({5:'b',4:'c',3:'s',2:'u',1:'d'}) fmap.update({21:'g'}) #--gen qpd data per replica checkdir('%s/benchmark'%wdir) cnt=0 for par in replicas: lprint('progress: %d/%d'%(cnt+1,len(replicas))) parman.set_new_params(par) data={} for _ in Q2: data[_]={} for flav in flavs: data[_][flav]=np.array([qpd.get_xF(x,_,fmap[flav]) for x in X]) save(data,'%s/benchmark/%s-%d.dat'%(wdir,dist,cnt)) cnt+=1 print
def gen_tables(wdir, dist, file_name, info, info_only=False): print('\ngenerating LHAPDF tables for %s using %s' % (dist, wdir)) load_config('%s/input.py' % wdir) istep = core.get_istep() core.mod_conf(istep) #--set conf as specified in istep resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman jar = load('%s/data/jar-%d.dat' % (wdir, istep)) replicas = jar['replicas'] #--check order consistency order = jar['order'] parman.order = order #--create output dir checkdir(wdir + '/data/') checkdir(wdir + '/data/%s/' % file_name) #--gen lhapdf_data_files if info_only == False: cnt = 0 for par in replicas: lprint('progress: %d/%d' % (cnt + 1, len(replicas))) parman.set_new_params(par) X, Q2, table = _gen_table(dist) gen_lhapdf_dat_file(X, Q2, table, wdir, file_name, cnt) cnt += 1 print #--gen_lhapdf_info_file X, Q2 = gen_grid(dist) nrep = len(replicas) gen_lhapdf_info_file(X, Q2, nrep, wdir, file_name, info)
class MAXLIKE: def __init__(self, inputfile, nworkers=2, verbose=False, msrhook=None, prior=None, seed=None): self.nworkers = nworkers self.inputfile = inputfile self.verbose = verbose self.msrhook = msrhook self.prior = prior self.seed = seed def set_counters(self): self.chi2tot = 1e1000 self.dchi2 = 0 self.t0 = time.time() self.cnt = 0 def print_status(self, res, rres, nres): #--update status parameters shifts = self.parman.shifts etime = (time.time() - self.t0) / 60 npts = res.size chi2 = np.sum(res**2) rchi2 = np.sum(rres**2) nchi2 = np.sum(nres**2) chi2tot = chi2 + rchi2 + nchi2 dchi2 = chi2tot - self.chi2tot if shifts > 2: if chi2tot < self.chi2tot: self.dchi2 = self.chi2tot - chi2tot self.chi2tot = chi2tot #--build header status = [] status.append('JAM FITTER') status.append('count = %d' % self.cnt) status.append('elapsed time(mins)=%f' % etime) status.append('shifts = %d' % shifts) status.append('npts = %d' % npts) status.append('chi2 = %f' % chi2) status.append('rchi2 = %f' % rchi2) status.append('nchi2 = %f' % nchi2) status.append('chi2tot = %f' % (chi2tot)) status.append('dchi2(iter) = %f' % self.dchi2) status.append('dchi2(local) = %f' % dchi2) #--special output for pdfs #if 'pdf' in conf['params']: # for _ in conf['pdf'].sr: # status.append('pdf %s:%f'%(_,conf['pdf'].sr[_])) # #status.append('proton uvsr = %f'%conf['pdf'].sr['uvsr']) # #status.append('proton dvsr = %f'%conf['pdf'].sr['dvsr']) # #status.append('proton msr = %f'%conf['pdf'].sr['msr']) # #if 'svsr' in conf['pdf'].sr: status.append('proton svsr = %f'%conf['pdf'].sr['svsr']) #--report from resman status.append('') status.extend(self.resman.gen_report()) #--report from parman parstatus = self.parman.gen_report() #--print into screen nstatus = len(status) nparstatus = len(parstatus) os.system('clear') for i in range(max([nstatus, nparstatus])): data = [] if i < nstatus: data.append(status[i]) else: data.append('') if i < nparstatus: data.append(parstatus[i]) else: data.append('') print '%-120s | %s' % tuple(data) return status, parstatus def get_residuals(self, par): res, rres, nres = self.resman.get_residuals(par) self.cnt += 1 if self.cnt % conf['verbose'] == 0: self.print_status(res, rres, nres) if len(rres) != 0: res = np.append(res, rres) if len(nres) != 0: res = np.append(res, nres) return res def checklimits(self): for k in conf['params']: for kk in conf['params'][k]: if conf['params'][k][kk]['fixed'] != False: continue p = conf['params'][k][kk]['value'] pmin = conf['params'][k][kk]['min'] pmax = conf['params'][k][kk]['max'] if p < pmin or p > pmax: print '%s-%s out of limits. ' % (k, kk) sys.exit() for k in conf['datasets']: for kk in conf['datasets'][k]['norm']: p = conf['datasets'][k]['norm'][kk]['value'] pmin = conf['datasets'][k]['norm'][kk]['min'] pmax = conf['datasets'][k]['norm'][kk]['max'] if p < pmin or p > pmax: print '%s-%s out of limits. ' % (k, kk) sys.exit() def get_conf(self, _conf, step): conf = copy.deepcopy(_conf) #--remove pdf/ff that is not in the step distributions = conf['params'].keys() #--pdf,ppdf,ffpion,ffkaon,... for dist in distributions: if dist in step['active distributions']: continue else: del conf['params'][dist] #--set fixed==True for passive distributions if 'passive distributions' in step: for dist in step['passive distributions']: for par in conf['params'][dist]: conf['params'][dist][par]['fixed'] = True #--set prior parameters values for passive distributions for istep in step['dep']: prior_order = self.order[istep] prior_params = self.params[istep] for i in range(len(prior_order)): _, _dist, _par = prior_order[i] if dist == _dist and par == _par: conf['params'][dist][par][ 'value'] = prior_params[i] #--another version for fixed parameters if 'fix parameters' in step: for dist in step['fix parameters']: print dist for par in step['fix parameters'][dist]: print par conf['params'][dist][par]['fixed'] = True #--set prior parameters values for passive distributions for istep in step['dep']: prior_order = self.order[istep] prior_params = self.params[istep] for i in range(len(prior_order)): _, _dist, _par = prior_order[i] if dist == _dist and par == _par: conf['params'][dist][par][ 'value'] = prior_params[i] #--remove datasets not in the step datasets = conf['datasets'].keys() #--idis,dy,.... for dataset in datasets: if dataset in step['datasets']: #--remove entry from xlsx xlsx = conf['datasets'][dataset]['xlsx'].keys() for idx in xlsx: if idx in step['datasets'][dataset]: continue else: del conf['datasets'][dataset]['xlsx'][idx] #--remove entry from norm norm = conf['datasets'][dataset]['norm'].keys() for idx in norm: if idx in step['datasets'][dataset]: continue else: del conf['datasets'][dataset]['norm'][idx] else: del conf['datasets'][dataset] return conf def get_bounds(self): order = self.parman.order bounds_min = [] bounds_max = [] for entry in order: i, k, kk = entry if i == 1: p = conf['params'][k][kk]['value'] pmin = conf['params'][k][kk]['min'] pmax = conf['params'][k][kk]['max'] if p < pmin or p > pmax: msg = '%s/%s outsize the limits %f %f %f' % (k, kk, p, pmin, pmax) raise ValueError(msg) bounds_min.append(conf['params'][k][kk]['min']) bounds_max.append(conf['params'][k][kk]['max']) elif i == 2: p = conf['datasets'][k]['norm'][kk]['value'] pmin = conf['datasets'][k]['norm'][kk]['min'] pmax = conf['datasets'][k]['norm'][kk]['max'] if p < pmin or p > pmax: msg = '%s/%s outsize the limits %f %f %f' % (k, kk, p, pmin, pmax) raise ValueError(msg) bounds_min.append(conf['datasets'][k]['norm'][kk]['min']) bounds_max.append(conf['datasets'][k]['norm'][kk]['max']) return (bounds_min, bounds_max) def get_guess(self, dep=[]): order = self.parman.order if conf['flat par']: if self.seed != None: np.random.seed(12345) guess = self.parman.gen_flat() else: guess = self.parman.par #--retrieve priors from previous steps according to dep if len(dep) > 0: for istep in dep: prior_order = self.order[istep] prior_params = self.params[istep] #--compare and match prior order to current order for i in range(len(prior_order)): for j in range(len(order)): if order[j] == prior_order[i]: guess[j] = prior_params[i] return guess def gen_summary(self, step, par): res, rres, nres = self.resman.get_residuals(par) status, parstatus = self.print_status(res, rres, nres) status.extend(parstatus) status = [l + '\n' for l in status] fname = 'step-%d.summary' % step F = open(fname, 'w') F.writelines(status) F.close() def get_par(self, step, dep=[]): #--current value must be within min and max self.checklimits() #--initialize resman self.resman = RESMAN(self.nworkers) self.parman = self.resman.parman #--setups guess = self.get_guess(dep) bounds = self.get_bounds() self.set_counters() self.parman.set_new_params(guess, initial=True) #--run fit fit = least_squares(self.get_residuals, guess, bounds=bounds, method='trf', ftol=conf['ftol']) #--generate summary. It will update system with the final results self.gen_summary(step, fit.x) #--close resman self.resman.shutdown() return fit.x def gen_output(self, istep): """ modification of the input is done using a dedicated scrip at tools/inputmod.py """ inputmod = INPUTMOD(self.inputfile) for kind in conf['params']: for par in conf['params'][kind]: value = conf['params'][kind][par]['value'] inputmod.mod_par(kind, par, 'value', value) for reaction in conf['datasets']: for idx in conf['datasets'][reaction]['norm']: value = conf['datasets'][reaction]['norm'][idx]['value'] inputmod.mod_norm(reaction, idx, 'value', value) fname = 'output-%d.py' % istep inputmod.gen_input(fname) def run(self): global conf load_config(self.inputfile) #--modify confs if 'hooks' not in conf: conf['hooks'] = {} if self.msrhook != None: conf['hooks']['msr'] = self.msrhook if self.verbose == False: conf['verbose'] = 1 else: conf['verbose'] = self.verbose #--backup conf(after mods) conf_bkp = copy.deepcopy(conf) #--decide if the input should be modified output = True if 'bootstrap' in conf and conf['bootstrap'] == True: output = False if 'steps' in conf: isteps = sorted(conf['steps']) self.order = {} self.params = {} chi2 = {} if self.prior != None: prior = load(self.prior) self.order = prior['order'] self.params = prior['params'] chi2 = prior['chi2'] prior_steps = sorted(self.order.keys()) isteps = [i for i in isteps if i not in prior_steps] for i in isteps: step = conf['steps'][i] conf.update(copy.deepcopy(conf_bkp)) conf.update(self.get_conf(conf_bkp, step)) print msg = '--step %d: ' % i msg += 'npQCD objects =' for _ in conf['params'].keys(): msg += _ + ' ' msg += 'datasets =' for _ in conf['datasets'].keys(): msg += _ + ' ' print msg par = self.get_par(i, step['dep']) self.order[i] = self.parman.order[:] self.params[i] = par[:] chi2[i] = self.resman.get_chi2() if output: self.gen_output(i) #--store the results from steps data = {'order': self.order, 'params': self.params, 'chi2': chi2} if self.prior == None: fname = '%s.msr' % id_generator(size=12) else: fname = self.prior.split('/')[-1] save(data, fname) #--run hooks if 'msr' in conf['hooks']: #--ms==multi steps cmd = conf['hooks']['msr'].replace('<<fname>>', fname) os.system(cmd) else: par = self.get_par(0) if output: self.gen_output(0) #--run remining hooks if available for _ in conf['hooks']: if _ == 'msr': continue os.system(conf['hooks'][_])
def gen_lhapdf_info_file(wdir, idxs, tar, info, dirname='JAMSIM'): #--get tables X, Q2, table, replicas = get_tables(wdir, idxs, tar) #--get flav string flavs = '' for idx in idxs: flavs += '%d, ' % idx flavs = flavs.rstrip(',') #--kinematic limits xmin = X[0] xmax = X[-1] Qmin = Q2[0]**0.5 Qmax = Q2[-1]**0.5 #--qcd params load_config('%s/input.py' % wdir) RESMAN(nworkers=1, parallel=False, datasets=False) aS = [conf['alphaS'].get_alphaS(_) for _ in Q2] mZ = conf['aux'].mZ mb = conf['aux'].mb mc = conf['aux'].mc alphaSMZ = conf['aux'].alphaSMZ #--begin lhapdf info file lines = [] lines.append('SetDesc: "<description>"') lines.append('SetIndex: <index>') lines.append('Authors: <authors>') lines.append('Reference: <reference>') lines.append('Format: lhagrid1') lines.append('DataVersion: 1') lines.append('NumMembers: 1') lines.append('Particle: <particle>') lines.append('Flavors: [%s]' % flavs) lines.append('OrderQCD: 1') lines.append('FlavorScheme: <flav scheme>') lines.append('NumFlavors: %d' % len(idxs)) lines.append('ErrorType: no error') lines.append('XMin: %0.2e' % xmin) lines.append('XMax: %0.2e' % xmax) lines.append('QMin: %0.2e' % Qmin) lines.append('QMax: %0.2e' % Qmax) lines.append('MZ: %f' % mZ) lines.append('MUp: 0.0') lines.append('MDown: 0.0') lines.append('MStrange: 0.0') lines.append('MCharm: %f' % mc) lines.append('MBottom: %f' % mb) lines.append('MTop: 180.0') lines.append('AlphaS_MZ: %f' % alphaSMZ) lines.append('AlphaS_OrderQCD: 1') lines.append('AlphaS_Type: ipol') line = 'AlphaS_Qs: [' for _ in Q2: line += ('%10.5e, ' % _**0.5).upper() line = line.rstrip(',') + ']' lines.append(line) line = 'AlphaS_Vals: [' for _ in aS: line += ('%10.5e, ' % _).upper() line = line.rstrip(',') + ']' lines.append(line) lines.append('AlphaS_Lambda4: 0') lines.append('AlphaS_Lambda5: 0') for i in range(len(lines)): for _ in info: lines[i] = lines[i].replace(_, info[_]) lines = [l + '\n' for l in lines] checkdir('%s/lhapdf/%s' % (wdir, dirname)) tab = open('%s/lhapdf/%s/%s.info' % (wdir, dirname, dirname), 'w') tab.writelines(lines) tab.close()
############################################################################ # define datasets (kinematics) to perform the simulation conf['datasets']={} conf['datasets']['sidis']={} conf['datasets']['sidis']['xlsx']={} conf['datasets']['sidis']['xlsx'][1000]='./template.xlsx' # | proton | pi+ conf['datasets']['sidis']['norm']={} for idx in conf['datasets']['sidis']['xlsx']: conf['datasets']['sidis']['norm'][idx]={'value':1,'fixed':True,'min':0,'max':1} conf['datasets']['sidis']['filters']={} ############################################################################ # run simulation resman = RESMAN() parman = conf['parman'] resman.get_residuals(parman.par) print ############################################################################ # prepare the new pseudo data set and save it as xlsx file simdata=pd.DataFrame(conf['sidis tabs'][1000]) simdata['value']=simdata['thy'] simdata['stat_u']=pd.Series(simdata['thy']*simdata['%stat']/100,index=simdata.index) simdata['syst_u']=pd.Series(simdata['thy']*simdata['%syst']/100,index=simdata.index) simdata=simdata.drop(['r-residuals','shift','alpha','residuals','yp','yh','W2','Shift','N','thy','%stat','%syst'],axis=1) print simdata[:10] writer = pd.ExcelWriter('simulation.xlsx')
def get_parameters(wdir, Q2=None): load_config('%s/input.py' % wdir) istep = core.get_istep() replicas = core.get_replicas(wdir) ## 'conf' will be modified for each replica individually later in the loop over 'replicas' ## the reason for doing this is that 'fix parameters' has to be set correctly for each replica if 'ppdf' not in conf['steps'][istep]['active distributions']: print('ppdf-proton not in active distribution') return resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman parman.order = replicas[0]['order'][ istep] ## make sure 'parman' uses the same order as all the replicas do # print parman.order ppdf = conf['ppdf'] ## setup kinematics if Q2 == None: Q2 = conf['Q20'] ## get parameters for all flavors of PDF print('\ngetting ppdf-parameters from %s at Q2 = %.2f' % (wdir, Q2)) ## check if any of the shapes are fixed shape_1 = [] shape_2 = [] shape_3 = [] for parameter in conf['params']['ppdf']: if '1' in parameter: shape_1.append(conf['params']['ppdf'][parameter]['fixed']) elif '2' in parameter: shape_2.append(conf['params']['ppdf'][parameter]['fixed']) elif '3' in parameter: shape_3.append(conf['params']['ppdf'][parameter]['fixed']) else: print('there seems to be more than three shapes') print(parameter) sys.exit('please update script %s' % __file__) fixed_shape_1 = False fixed_shape_2 = False fixed_shape_3 = False if (len(shape_1) != 0) and all([_ == True for _ in shape_1]): fixed_shape_1 = True elif len(shape_1) == 0: fixed_shape_1 = True if (len(shape_2) != 0) and all([_ == True for _ in shape_2]): fixed_shape_2 = True elif len(shape_2) == 0: fixed_shape_2 = True if (len(shape_3) != 0) and all([_ == True for _ in shape_3]): fixed_shape_3 = True elif len(shape_3) == 0: fixed_shape_3 = True parameters = {} if not fixed_shape_1: parameters[1] = {} if not fixed_shape_2: parameters[2] = {} if not fixed_shape_3: parameters[3] = {} n_replicas = len(replicas) for i in range(n_replicas): lprint('%d/%d' % (i + 1, n_replicas)) parman.order = copy.copy(replicas[i]['order'][istep]) core.mod_conf(istep, replicas[i]) parman.set_new_params(replicas[i]['params'][istep], initial=True) for flavor, value in ppdf.params.iteritems(): for j in parameters: if str(j) in flavor: flavor_key = flavor.replace(str(j), '') if flavor_key not in parameters[j]: parameters[j][flavor_key] = { 'n': [], 'a': [], 'b': [], 'c': [], 'd': [] } parameters[j][flavor_key]['n'].append(value[0]) parameters[j][flavor_key]['a'].append(value[1]) parameters[j][flavor_key]['b'].append(value[2]) parameters[j][flavor_key]['c'].append(value[3]) parameters[j][flavor_key]['d'].append(value[4]) else: pass ## remove c or d parameters if fixed in all flavors and shapes ## this is to avoid producing empty figures shapes_fixed_c = [] shapes_fixed_d = [] for shape in parameters: flavors_fixed_c = [] flavors_fixed_d = [] for flavor in parameters[shape]: cs = parameters[shape][flavor]['c'] ds = parameters[shape][flavor]['d'] if all([_ == cs[0] for _ in cs]): flavors_fixed_c.append(True) else: flavors_fixed_c.append(False) if all([_ == ds[0] for _ in ds]): flavors_fixed_d.append(True) else: flavors_fixed_d.append(False) if all(flavors_fixed_c): shapes_fixed_c.append(True) else: shapes_fixed_c.append(False) if all(flavors_fixed_d): shapes_fixed_d.append(True) else: shapes_fixed_d.append(False) if all(shapes_fixed_c): for shape in parameters: for flavor in parameters[shape]: del parameters[shape][flavor]['c'] print('parameter c is fixed for shape %s' % shape) if all(shapes_fixed_d): for shape in parameters: for flavor in parameters[shape]: del parameters[shape][flavor]['d'] print('parameter d is fixed for shape %s' % shape) print checkdir('%s/data' % wdir) if Q2 == conf['Q20']: save(parameters, '%s/data/ppdf-parameters-%d.dat' % (wdir, istep)) else: save(parameters, '%s/data/ppdf-parameters-%d-%f.dat' % (wdir, istep, Q2))
def get_moments(wdir, flavors, x_1, x_2, Q2=None): load_config('%s/input.py' % wdir) istep = core.get_istep() replicas = core.get_replicas(wdir) ## 'conf' will be modified for each replica individually later in the loop over 'replicas' ## the reason for doing this is that 'fix parameters' has to be set correctly for each replica if 'ppdf' not in conf['steps'][istep]['active distributions']: print('ppdf-proton not in active distribution') return resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman parman.order = replicas[0]['order'][istep] ## make sure 'parman' uses the same order for active distributions as all the replicas do # print parman.order ppdf = conf['ppdf'] ## setup kinematics # X = 10.0 ** np.linspace(-3, -1, 100) # X = np.append(X, np.linspace(0.1, 0.99, 100)) if Q2 == None: Q2 = conf['Q20'] print('\ngenerating momentus for polarized pdf-proton from %s at Q2 = %f' % (wdir, Q2)) ## compute moments for all replicas moments = {} statistics = {} n_replicas = len(replicas) for i in range(n_replicas): lprint('%d/%d' % (i + 1, n_replicas)) parman.order = copy.copy(replicas[i]['order'][istep]) core.mod_conf(istep, replicas[i]) parman.set_new_params(replicas[i]['params'][istep], initial=True) for flavor in flavors: if flavor not in moments: moments[flavor] = [] if flavor not in statistics: statistics[flavor] = {} if flavor == 'up': func = lambda x: (ppdf.get_xF(x, Q2, 'u') + ppdf.get_xF( x, Q2, 'ub')) / x elif flavor == 'dp': func = lambda x: (ppdf.get_xF(x, Q2, 'd') + ppdf.get_xF( x, Q2, 'sb')) / x elif flavor == 'sp': func = lambda x: (ppdf.get_xF(x, Q2, 's') + ppdf.get_xF( x, Q2, 'sb')) / x else: func = lambda x: ppdf.get_xF(x, Q2, flavor) / x moments[flavor].append(quad(func, x_1, x_2)[0]) for flavor in flavors: statistics[flavor]['mean'] = np.mean(moments[flavor]) statistics[flavor]['standard_deviation'] = np.std(moments[flavor]) print for flavor in flavors: print flavor, ': ', statistics[flavor] checkdir('%s/data' % wdir) if Q2 == conf['Q20']: save({ 'Q2': Q2, 'moments': moments, 'statistics': statistics }, '%s/data/ppdf-moments-%s-to-%s-%d.dat' % (wdir, x_1, x_2, istep)) else: save({ 'Q2': Q2, 'moments': moments, 'statistics': statistics }, '%s/data/ppdf-moments-%s-to-%s-%d-%f.dat' % (wdir, x_1, x_2, istep, Q2))
def gen_xf(wdir, flavors, Q2=None): load_config('%s/input.py' % wdir) istep = core.get_istep() replicas = core.get_replicas(wdir) ## 'conf' will be modified for each replica individually later in the loop over 'replicas' ## the reason for doing this is that 'fix parameters' has to be set correctly for each replica if 'ppdf' not in conf['steps'][istep]['active distributions']: print('ppdf-proton not in active distribution') return resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman parman.order = replicas[0]['order'][istep] ## make sure 'parman' uses the same order for active distributions as all the replicas do # print parman.order ppdf = conf['ppdf'] ## setup kinematics xs = 10.0**np.linspace(-3, -1, 100) xs = np.append(xs, np.linspace(0.1, 0.99, 100)) if Q2 == None: Q2 = conf['Q20'] print('\ngenerating polarized pdf-proton from %s at Q2 = %f' % (wdir, Q2)) ## compute xf for all replicas xfs = {} n_replicas = len(replicas) for i in range(n_replicas): lprint('%d/%d' % (i + 1, n_replicas)) ## filter #flag=False #params=replica['params'][istep] #order=replica['order'][istep] #for i in range(len(order)): # if order[i][0]!=1:continue # if order[i][1]!='ppdf':continue # #if order[i][2]=='s1 a': # # if params[i]<-0.9: flag=True #if flag: continue parman.order = copy.copy(replicas[i]['order'][istep]) core.mod_conf(istep, replicas[i]) parman.set_new_params(replicas[i]['params'][istep], initial=True) for flavor in flavors: if flavor not in xfs: xfs[flavor] = [] if flavor == 'up': func = lambda x: ppdf.get_xF(x, Q2, 'u') + ppdf.get_xF( x, Q2, 'ub') elif flavor == 'dp': func = lambda x: ppdf.get_xF(x, Q2, 'd') + ppdf.get_xF( x, Q2, 'sb') elif flavor == 'sp': func = lambda x: ppdf.get_xF(x, Q2, 's') + ppdf.get_xF( x, Q2, 'sb') else: func = lambda x: ppdf.get_xF(x, Q2, flavor) xfs[flavor].append([func(x) for x in xs]) print checkdir('%s/data' % wdir) if Q2 == conf['Q20']: save({ 'X': xs, 'Q2': Q2, 'XF': xfs }, '%s/data/ppdf-%d.dat' % (wdir, istep)) else: save({ 'X': xs, 'Q2': Q2, 'XF': xfs }, '%s/data/ppdf-%d-%f.dat' % (wdir, istep, Q2))
def gen_xf(wdir, had, flavors=['g', 'u', 'ub', 'd', 'db', 's', 'sb'], Q2=None): fflabel = 'ff%s' % had print('\ngenerating ff-%s from %s' % (had, wdir)) load_config('%s/input.py' % wdir) istep = core.get_istep() replicas = core.get_replicas(wdir) core.mod_conf(istep, replicas[0]) ## set conf as specified in istep if fflabel not in conf['steps'][istep]['active distributions']: print('ff-%s not in active distribution' % had) return resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman jar = load('%s/data/jar-%d.dat' % (wdir, istep)) ## 'jar' contains parameters and their orders replicas = jar['replicas'] ## 'jar['replicas']' is a list, in which each element is a list of parameters obeying 'jar['order']' ff = conf[fflabel] ## setup kinematics X = np.linspace(0.01, 0.99, 100) if Q2 == None: Q2 = conf['Q20'] ## compute XF for all replicas XF = {} cnt = 0 for par in replicas: cnt += 1 lprint('%d/%d' % (cnt, len(replicas))) ## filter #flag=False #params=replica['params'][istep] #order=replica['order'][istep] #for i in range(len(order)): # if order[i][0]!=1:continue # if order[i][1]!='pdf':continue # #if order[i][2]=='s1 a': # # if params[i]<-0.9: flag=True #if flag: continue parman.set_new_params(par, initial=True) #print #print conf['ffpion'].get_xF(0.5,10.0,'u') #print conf['ffkaon'].get_xF(0.5,10.0,'u') #print ff.get_xF(0.5,10.0,'u') for flavor in flavors: if flavor not in XF: XF[flavor] = [] if flavor == 'c' or flavor == 'cb' or flavor == 'c+cb': _Q2 = conf['aux'].mc2 + 1 elif flavor == 'b' or flavor == 'bb' or flavor == 'b+bb': _Q2 = conf['aux'].mb2 + 1 else: _Q2 = Q2 if flavor == 'u+ub': func = lambda x: ff.get_xF(x, _Q2, 'u') + ff.get_xF( x, _Q2, 'ub') elif flavor == 'd+db': func = lambda x: ff.get_xF(x, _Q2, 'd') + ff.get_xF( x, _Q2, 'db') elif flavor == 's+sb': func = lambda x: ff.get_xF(x, _Q2, 's') + ff.get_xF( x, _Q2, 'cb') elif flavor == 'c+cb': func = lambda x: ff.get_xF(x, _Q2, 'c') + ff.get_xF( x, _Q2, 'cb') elif flavor == 'b+bb': func = lambda x: ff.get_xF(x, _Q2, 'b') + ff.get_xF( x, _Q2, 'bb') else: func = lambda x: ff.get_xF(x, _Q2, flavor) XF[flavor].append([func(x) for x in X]) #print func(0.5) print checkdir('%s/data' % wdir) if Q2 == conf['Q20']: save({ 'X': X, 'Q2': Q2, 'XF': XF }, '%s/data/ff%s-%d.dat' % (wdir, had, istep)) else: save({ 'X': X, 'Q2': Q2, 'XF': XF }, '%s/data/ff%s-%d-%d.dat' % (wdir, had, istep, int(Q2)))
def print_grouped_parameters(working_directory, distribution_name, i_replica=1): ## print parameters from replicas and PDF class to compare load_config('%s/input.py' % working_directory) istep = core.get_istep() replicas = core.get_replicas(working_directory) core.mod_conf(istep, replicas[0]) ## set conf as specified in istep if distribution_name not in conf['steps'][istep]['active distributions']: print('%s-proton is not an active distribution' % distribution_name) return resman = RESMAN(nworkers=1, parallel=False, datasets=False) parman = resman.parman parman.order = replicas[0]['order'][ istep] ## make sure 'parman' uses the same order as all the replicas do # print parman.order distribution = conf[distribution_name] print('%s parameters in class' % distribution_name) core.mod_conf(istep, replicas[i_replica - 1]) parman.set_new_params(replicas[i_replica - 1]['params'][istep], initial=True) for name, value in distribution.params.iteritems(): if value[0] != 0.0: print '%7s: %.5e, %.5e, %.5e, %.5e, %.5e' % ( name, value[0], value[1], value[2], value[3], value[4]) print('%s parameters in replicas' % distribution_name) orders = [] unique_orders = [] values = [] for i in range(len(replicas[i_replica - 1]['order'][istep])): order = replicas[i_replica - 1]['order'][istep][i] value = replicas[i_replica - 1]['params'][istep][i] if (order[0] == 1) and (order[1] == distribution_name): orders.append(order[2]) unique_orders.append(order[2].split(' ')[0]) values.append(value) unique_orders = list(set(unique_orders)) for unique_order in unique_orders: parameters = [] all_parameters = {'N': None, 'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.0} for i in range(len(orders)): if orders[i].split(' ')[0] == unique_order: all_parameters[orders[i].split(' ')[1]] = values[i] if all_parameters == { 'N': None, 'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.0 }: continue if all_parameters['N'] == None: print '%7s: None, %.5e, %.5e, %.5e, %.5e' % ( unique_order, all_parameters['a'], all_parameters['b'], all_parameters['c'], all_parameters['d']) else: print '%7s: %.5e, %.5e, %.5e, %.5e, %.5e' % ( unique_order, all_parameters['N'], all_parameters['a'], all_parameters['b'], all_parameters['c'], all_parameters['d'])