- Timestamp:
- Feb 26, 2019, 8:27:25 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2342 r2370 154 154 # multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string 155 155 # multi_index_vec: Function to provide the coordinates of multiples repetitions of a value inside a vector 156 # Nloops_1D: Function to compress a series of multiples loops into a single vector 156 157 # Nomasked: Function to bring back a given array wthout the masked values reducing a given dimension 157 158 # Nstr: Function to transform a number to string, but making sure it preserves … … 15169 15170 return iszero 15170 15171 15172 def Nloops_1D(loops): 15173 """ Function to compress a series of multiples loops into a single vector 15174 loops: list with the length of the N-loops 15175 >>> Nloops_1D([3,5,2]) 15176 [[0 0 0] 15177 [0 0 1] 15178 [0 1 0] 15179 [0 1 1] 15180 [0 2 0] 15181 [0 2 1] 15182 [0 3 0] 15183 [0 3 1] 15184 [0 4 0] 15185 [0 4 1] 15186 [1 0 0] 15187 [1 0 1] 15188 [1 1 0] 15189 [1 1 1] 15190 [1 2 0] 15191 [1 2 1] 15192 [1 3 0] 15193 [1 3 1] 15194 [1 4 0] 15195 [1 4 1] 15196 [2 0 0] 15197 [2 0 1] 15198 [2 1 0] 15199 [2 1 1] 15200 [2 2 0] 15201 [2 2 1] 15202 [2 3 0] 15203 [2 3 1] 15204 [2 4 0] 15205 [2 4 1]] 15206 """ 15207 fname = 'Nloops_1D' 15208 15209 Nloops = len(loops) 15210 Tloops = np.prod(np.array(loops)) 15211 loops1D = np.zeros((Tloops, Nloops), dtype=int) 15212 prevl = np.zeros((Nloops), dtype=int) 15213 loops1D[0,...] = prevl[:] 15214 for il in range(1,Tloops): 15215 prevl[Nloops-1] = prevl[Nloops-1] + 1 15216 if prevl[Nloops-1] > loops[Nloops-1]-1: 15217 prevl[Nloops-1] = 0 15218 for ii in range(Nloops-2,-1,-1): 15219 prevl[ii] = prevl[ii] + 1 15220 if prevl[ii] <= loops[ii]-1: 15221 break 15222 else: 15223 prevl[ii] = 0 15224 loops1D[il,...] = prevl[:] 15225 15226 return loops1D 15227 15228 def range_slicing(dimns, dimvs, intdims): 15229 """ Function to provide a list of slices for a series of dimensions giving 15230 intervals for a series of dimensions in order to avoid memory problems 15231 dimns: list with the names of dimensions 15232 dimvs: list with the lengths of dimensions (same order) 15233 intdims: dictionary with the name of dimensions with the length of intervals 15234 >>> range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7}) 15235 """ 15236 fname = 'range_slicing' 15237 15238 Ndims = len(dimns) 15239 15240 # Constructing dictionary with dimn and dimvs (ensuring same order !) 15241 dims = {} 15242 for iid in range(Ndims): 15243 dims[dimns[iid]] = dimvs[iid] 15244 15245 # Getting individual intervals 15246 intslices = {} 15247 Lslcdim = {} 15248 islcdim = {} 15249 doneallslices = {} 15250 for dimn in intdims.keys(): 15251 slicev = range(0,dims[dimn],intdims[dimn]) 15252 if np.mod(dims[dimn],intdims[dimn]) != 0: slicev.append(dims[dimn]) 15253 15254 intslices[dimn] = slicev 15255 Lslcdim[dimn] = len(slicev)-1 15256 islcdim[dimn] = 0 15257 doneallslices[dimn] = False 15258 print 'intervals ', dimn, ':', intslices[dimn] 15259 15260 origslicesize = {} 15261 Tslicesize = 1 15262 for dimn in dimns: 15263 idim = index_vec(dimns, dimn) 15264 origslicesize[dimn] = dims[dimn] 15265 if searchInlist(intdims.keys(), dimn): 15266 Tslicesize = Tslicesize*Lslcdim[dimn] 15267 lastintdimn = dimn 15268 15269 print ' ' + fname + ': Total number of slices to provide:', Tslicesize 15270 15271 slices = [] 15272 for il in range(Tslicesize): 15273 advanced = False 15274 print 'il:', il 15275 islice = [] 15276 for idim in range(Ndims): 15277 dimn = dimns[idim] 15278 if intdims.has_key(dimn): 15279 # getting dimension slice status 15280 Lslc = Lslcdim[dimn] 15281 slcsd = intslices[dimn] 15282 dimdone = doneallslices[dimn] 15283 islc = islcdim[dimn] 15284 15285 # Which is the status of later dimensions 15286 if idim < Ndims-1: 15287 allintdns = True 15288 for iid in range(idim+1,Ndims): 15289 if intdims.has_key(dimns[iid]): 15290 # Up to the dimension without all the intervals done 15291 if not doneallslices[dimns[iid]]: 15292 allintdns = False 15293 break 15294 else: 15295 allintdns = False 15296 15297 if allintdns: 15298 if not advanced: 15299 if islc+1 <= Lslc-1: 15300 print 'Advancing dim !!', dimn, islc, islc+1, '.', Lslc, slcsd 15301 islc = islc + 1 15302 advanced = True 15303 else: 15304 doneallslices[dimn] = True 15305 # Zeroeing later dims 15306 for iid in range(idim+1,Ndims): 15307 if intdims.has_key(dimns[iid]): 15308 islcdim[dimns[iid]] = 0 15309 doneallslices[dimns[iid]] = False 15310 islice.append(slice(slcsd[islc], slcsd[islc+1])) 15311 islcdim[dimn] = islc 15312 else: 15313 islice.append(slice(0,origslicesize[dimn])) 15314 15315 print il, ':', islcdim, doneallslices 15316 15317 slices.append(islice) 15318 if not advanced: 15319 islc = islcdim[lastintdimn] 15320 islc = islc + 1 15321 islcdim[lastintdimn] = islc 15322 if islc > Lslc-1: 15323 doneallslices[lastintdimn] = islc 15324 15325 return slices 15326 15327 slcs = range_slicing(['z', 't', 'y', 'x'], [2, 3, 10, 20], {'x': 5, 'y': 7}) 15328 Nslcs = len(slcs) 15329 for i in range(Nslcs): 15330 print i, ':', slcs[i] 15331 15171 15332 #quit() 15172 15333
Note: See TracChangeset
for help on using the changeset viewer.