-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
40 lines (36 loc) · 1.86 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
def Space_Subsample(dim,s=2):
space_sampled_indices = []
for z in range(0,dim[2],s):
for y in range(0,dim[1],s):
for x in range(0,dim[0],s):
index = (((z) * dim[1] + y) * dim[0] + x)
space_sampled_indices.append(index)
space_sampled_indices = np.asarray(space_sampled_indices)
return space_sampled_indices
def get_mgrid(sidelen, dim=2, s=1,t=0):
'''Generates a flattened grid of (x,y,...) coordinates in a range of -1 to 1.'''
if isinstance(sidelen, int):
sidelen = dim * (sidelen,)
if dim == 2:
pixel_coords = np.stack(np.mgrid[:sidelen[1]:s, :sidelen[0]:s], axis=-1)[None, ...].astype(np.float32)
pixel_coords[..., 0] = pixel_coords[..., 0] / (sidelen[1] - 1)
pixel_coords[..., 1] = pixel_coords[..., 1] / (sidelen[0] - 1)
elif dim == 3:
pixel_coords = np.stack(np.mgrid[:sidelen[2]:s, :sidelen[1]:s, :sidelen[0]:s], axis=-1)[None, ...].astype(np.float32)
pixel_coords[..., 0] = pixel_coords[..., 0] / (sidelen[2] - 1)
pixel_coords[..., 1] = pixel_coords[..., 1] / (sidelen[1] - 1)
pixel_coords[..., 2] = pixel_coords[..., 2] / (sidelen[0] - 1)
elif dim == 4:
pixel_coords = np.stack(np.mgrid[:sidelen[0]:(t+1), :sidelen[3]:s, :sidelen[2]:s, :sidelen[1]:s], axis=-1)[None, ...].astype(np.float32)
pixel_coords[..., 0] = pixel_coords[..., 0] / max(sidelen[0] - 1, 1)
pixel_coords[..., 1] = pixel_coords[..., 1] / (sidelen[3] - 1)
pixel_coords[..., 2] = pixel_coords[..., 2] / (sidelen[2] - 1)
pixel_coords[..., 3] = pixel_coords[..., 3] / (sidelen[1] - 1)
else:
raise NotImplementedError('Not implemented for dim=%d' % dim)
pixel_coords -= 0.5
pixel_coords *= 2.
# print(pixel_coords.shape)
pixel_coords = np.reshape(pixel_coords,(-1,dim))
return pixel_coords