def lens_map(self,map,use_Pool = 0): """ Lens the input map according to the displacement fields dx dy. 'map' typically would be (8192 * 8192) np array, or the path to the array on disk. Does this by splitting the job in chunks (of typically (256 * 256), as specified by the LD_res parameters) allowing a buffer size to ensure the junctions are properly performed. Set use_Pool to a power of two to use explicit threading via the multiprocessing module. 'use_Pool' ** 2 is the number of threads. On laptop and Darwin use_Pool = 16 has the best performances. It use_Pool is set, then 'map' must be the path to the map to lens. """ if use_Pool > 0 : if not isinstance(map,str) : assert self.has_lib_dir(),"Specify lib. dir. if you want to use Pool." np.save(self.lib_dir + '/temp_maptolens.npy',map) if not self.is_dxdy_ondisk() : assert self.has_lib_dir(),"Specify lib. dir. if you want to use Pool." print "lens_map::writing displacements on disk :" self.write_npy( self.lib_dir + '/temp_displ' + str(pbs.rank)) # this turns dx and dy to the paths path_to_map = map if isinstance(map,str) else self.lib_dir + '/temp_maptolens.npy' return lens_Pool.get_lens_Pooled(self.mk_args(path_to_map,self.dx,self.dy),root_Nthreads = use_Pool) assert map.shape == self.shape,map.shape s = self.chk_shape idc0,idc1 = np.indices(s) # Two (256 * 256) maps dx_gu = np.empty(s) # will dx displ. in grid units of each chunk (typ. (256 * 256) ) dy_gu = np.empty(s) # will dy displ. in grid units of each chunk (typ. (256 * 256) ) map_chk = np.empty(s) # Will be map chunk # (typ. (256 * 256) ) lensed_map = np.empty(self.shape) spliter_lib = map_spliter.periodicmap_spliter() # library to split periodic maps. for i,N in utils.enumerate_progress(xrange(self.N_chks),label = 'ffs_displacement::lensing map'): # doing chunk N sLDs,sHDs = spliter_lib.get_slices_chk_N(N,self.LD_res,self.HD_res,self.buffers) for sLD,sHD in zip(sLDs,sHDs): # Displacements chunk in grid units, and map chunk to displace. dx_gu[sLD] = self.get_dx()[sHD] / self.rmin[1] dy_gu[sLD] = self.get_dy()[sHD] / self.rmin[0] map_chk[sLD] = map[sHD] lx = (idc1 + dx_gu).flatten() # No need to enforce periodicity here. ly = (idc0 + dy_gu).flatten() # No need to enforce periodicity here. sLDs,sHDs = spliter_lib.get_slices_chk_N(N,self.LD_res,self.HD_res,self.buffers,inverse = True) lensed_map[sHDs[0]] = interpolate.RectBivariateSpline(np.arange(s[0]), np.arange(s[1]), map_chk, kx=self.k, ky=self.k).ev(ly, lx).reshape(self.chk_shape)[sLDs[0]] return lensed_map
unl_sim = par.lib_cmb_unl.get_sim(0) path_to_unlsim = './tests/unlsim.npy' np.save(path_to_unlsim,unl_sim) displ_sim = par.lib_displ.get_sim(0) path_to_dx = './tests/dx.npy' path_to_dy = './tests/dy.npy' displ = par.lib_displ.get_sim(0) np.save(path_to_dx,displ.dx) np.save(path_to_dy,displ.dy) args = displ.mk_args(path_to_unlsim,path_to_dx,path_to_dy) print "** setup OK, args : ",args for rootN in [16] : t0 = time.time() len_sim = lens_Pool.get_lens_Pooled(args,root_Nthreads=rootN) print '*** Ex. time of lens_map Pool, map,dx,dy on disk, in sec, rootN ',rootN,np.round(time.time() - t0,2) np.save('./tests/lensim_rN'+str(rootN)+'.npy',len_sim) del len_sim t0 = time.time() len_sim = displ.lens_map(np.load(path_to_unlsim)) print '*** Ex. time of lens_map in sec. :',np.round(time.time() - t0,2) print "*** Checking outputs are identical :" if not np.allclose(len_sim,np.load('./tests/lensim_rN16.npy')): print 'somethings wrong' np.save('./tests/lensim_usual.py',len_sim) elif not np.all(len_sim == np.load('./tests/lensim_rN16.npy')): print 'Outputs all close but not identical' else : print "Outputs perfectly identical"