def range(min_val, max_val, bin_size): """range function for the binning of the histogram""" # checks if min_val > max_val: min_val, max_val = max_val, min_val # calculate start and stop bins min_bin = int(sp.floor_divide(min_val, bin_size)) max_bin = int(sp.floor_divide(max_val, bin_size) + 1) # return return sp.arange(min_bin, max_bin) * bin_size
def GenerateProjectionMat2(x,y,cpx,cpy,gridsize): tempcpx = cpx.reshape((-1,)) tempcpy = cpy.reshape((-1,)) positionx=sp.floor_divide(tempcpx+2,4./(gridsize-1)) positiony=sp.floor_divide(tempcpy+2,4./(gridsize-1)) mat = scipy.sparse.lil_matrix((tempcpx.size,gridsize**2)) gridscalar = (gridsize-1)**2/16. for idx in range(0,tempcpx.size): px = int(positionx[idx]) py = int(positiony[idx]) mat[idx,py*gridsize+px] = gridscalar*(tempcpx[idx]-x[0,px])*(tempcpy[idx]-y[py,0]) mat[idx,py*gridsize+px+1] = gridscalar*(x[0,px+1]-tempcpx[idx])*(tempcpy[idx]-y[py,0]) mat[idx,(py+1)*gridsize+px] = gridscalar*(tempcpx[idx]-x[0,px])*(y[py+1,0]-tempcpy[idx]) mat[idx,(py+1)*gridsize+px+1] = gridscalar*(x[0,px+1]-tempcpx[idx])*(y[py+1,0]-tempcpy[idx]) return mat
def GenerateProjectionMat(x,y,cpx,cpy,gridsize): positionx=sp.floor_divide(cpx+2,4./(gridsize-1)) positiony=sp.floor_divide(cpy+2,4./(gridsize-1)) mat = scipy.sparse.lil_matrix((gridsize**2,gridsize**2)) gridscalar = (gridsize-1)**2/16. for idxy in range(0,gridsize): tempidxy = idxy*gridsize for idxx in range(0,gridsize): px = int(positionx[idxy,idxx]) py = int(positiony[idxy,idxx]) mat[tempidxy+idxx,py*gridsize+px] = gridscalar*(cpx[idxy,idxx]-x[0,px])*(cpy[idxy,idxx]-y[py,0]) mat[tempidxy+idxx,py*gridsize+px+1] = gridscalar*(x[0,px+1]-cpx[idxy,idxx])*(cpy[idxy,idxx]-y[py,0]) mat[tempidxy+idxx,(py+1)*gridsize+px] = gridscalar*(cpx[idxy,idxx]-x[0,px])*(y[py+1,0]-cpy[idxy,idxx]) mat[tempidxy+idxx,(py+1)*gridsize+px+1] = gridscalar*(x[0,px+1]-cpx[idxy,idxx])*(y[py+1,0]-cpy[idxy,idxx]) return mat
def SetProjMatPETSC(cpCorArray,ProjMat,DA,vg): m = DA.getSizes()[0] dx = 4./m AO = DA.getAO() psnx = sp.int32(sp.floor_divide(cpCorArray[:,0]+2.,dx)) psny = sp.int32(sp.floor_divide(cpCorArray[:,1]+2.,dx)) idxbl = psnx+psny*m idxbr = psnx+1+psny*m idxul = psnx+(psny+1)*m idxur = psnx+1+(psny+1)*m idxbl = AO.app2petsc(idxbl) idxbr = AO.app2petsc(idxbr) idxul = AO.app2petsc(idxul) idxur = AO.app2petsc(idxur) start,end = vg.getOwnershipRange() modx = sp.mod(cpCorArray[:,0]+2.,dx) mody = sp.mod(cpCorArray[:,1]+2.,dx) dx2 = dx**2 for i in sp.arange(end-start): ProjMat[i+start,idxbl[i]] = (dx-modx[i])*(dx-mody[i])/dx2 ProjMat[i+start,idxbr[i]] = modx[i]*(dx-mody[i])/dx2 ProjMat[i+start,idxul[i]] = (dx-modx[i])*mody[i]/dx2 ProjMat[i+start,idxur[i]] = modx[i]*mody[i]/dx2 return
def fastMonteCarlo(self, mig, kw): dic_init = self.dic_init ### Get parameters try: if sp.remainder(len(dic_init['fastMonteCarlo']), 2) != 0: raise nb_fMC = int(dic_init['fastMonteCarlo'][0]) seed_fMC = int(dic_init['fastMonteCarlo'][1]) sp.random.seed(seed=seed_fMC) nb_expected_values = sp.floor_divide( len(dic_init['fastMonteCarlo']) - 2, 2) for i in range(nb_expected_values): key = dic_init['fastMonteCarlo'][2 * (i + 1)] val = dic_init['fastMonteCarlo'][2 * (i + 1) + 1] if len(key) > 4 and key[:4] == 'fix_': kw[key] = bool(val) kw['error_' + key[4:]] = 0. elif len(key) > 5 and key[:5] == 'free_': kw[key] = bool(val) else: val = float(val) mig.values[key] = val kw[key] = val except Exception as error: print( ' ERROR::picca/py/picca/fitter/Chi2.py:: error in fast Monte-Carlo = ', error) print(' Exit') sys.exit(0) ### Get bes fit if not self.auto is None: rp = self.auto.rp rt = self.auto.rt z = self.auto.z bestFit_auto = self.cosmo.valueAuto(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pauto }) ### Metals met = None if not self.met is None: met = self.met.valueAuto(mig.values) bestFit_auto += met bestFit_auto = sp.dot(self.auto.dm, bestFit_auto) ### Broadband bb = None if not self.bb is None: p, b = self.bb.value(self.auto.da - bestFit_auto[self.auto.cuts]) bb = self.bb(rt, rp, p) if self.dic_init['distort_bb_auto']: bb = sp.dot(self.auto.dm, bb) bestFit_auto += bb if not self.cross is None: rp = self.cross.rp rt = self.cross.rt z = self.cross.z bestFit_cross = self.cosmo.valueCross(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pcross }) ### Metals met = None if not self.met is None: met = self.met.valueCross(mig.values) bestFit_cross += met bestFit_cross = sp.dot(self.cross.dm, bestFit_cross) ### Broadband bb = None if not self.bb_cross is None: p, b = self.bb_cross.value( self.cross.da - bestFit_cross[self.cross.cuts], mig.values['drp']) bb = self.bb_cross(rt, rp, mig.values['drp'], p) if self.dic_init['distort_bb_cross']: bb = sp.dot(self.cross.dm, bb) bestFit_cross += bb if not self.autoQSO is None: rp = self.autoQSO.rp rt = self.autoQSO.rt z = self.autoQSO.z bestFit_autoQSO = self.cosmo.valueAutoQSO(rp, rt, z, { p: mig.values[p] for p in self.cosmo.pglob + self.cosmo.pautoQSO }) bestFit_autoQSO = sp.dot(self.autoQSO.dm, bestFit_autoQSO) ### File to save into output_name = dic_init['output_prefix'] output_name += "save.pars.fastMonteCarlo" f = open(output_name, "w", 0) f.write("#\n#seed = {}\n#\n".format(seed_fMC)) for p in mig.parameters: f.write("{} ".format(p)) f.write("chi2 ") for p in mig.parameters: f.write("{} ".format("err_" + p)) f.write("err_chi2\n") ### Get realisation fastMonteCarlo for i in range(nb_fMC): print(' fastMonteCarlo: ', i, ' over ', nb_fMC) sys.stdout.flush() ### Get realisation fastMonteCarlo if not dic_init['data_auto'] is None: self.auto.get_realisation_fastMonteCarlo(bestFit=bestFit_auto) if not dic_init['data_cross'] is None: self.cross.get_realisation_fastMonteCarlo( bestFit=bestFit_cross) if not dic_init['data_autoQSO'] is None: self.autoQSO.get_realisation_fastMonteCarlo( bestFit=bestFit_autoQSO) ### Fit mig_fMC = iminuit.Minuit(self, forced_parameters=self.pname, errordef=1, print_level=0, **kw) try: mig_fMC.migrad() chi2_result = mig_fMC.get_fmin().fval except: chi2_result = sp.nan ### Save for p in mig_fMC.parameters: f.write("{} ".format(mig_fMC.values[p])) f.write("{} ".format(chi2_result)) for p in mig_fMC.parameters: f.write("{} ".format(mig_fMC.errors[p])) f.write("{}\n".format(0.)) f.close() return