-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_utils.py
75 lines (52 loc) · 1.8 KB
/
grid_utils.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import numpy as np
import itertools
import tensorflow as tf
def linear_interpolate(X, U, kernel):
"""
performs linear kernel interpolation
Note: this assumes a regular (equispaced) grid for U
Args:
X (): observed points
U (): grid locations
kernel (): kernel function
Returns:
"""
return 0
def find_nn(X, U, k):
"""
Finds the k nearest neigbhors in U for each point in X
Args:
X (): observed points
U (): grid locations
N (): number of neighbors desired
Returns:
"""
distance = tf.reduce_sum(tf.square(tf.subtract(U, tf.expand_dims(X, 1))),
axis=2)
top_k_vals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)
return top_k_vals, top_k_indices
def fill_grid(X, y):
"""
Fills a partial grid with "imaginary" observations
Args:
X (np.array): data that lies on a partial grid
Returns:
X_grid: full grid X (including real and imagined points)
y_full: full grid y (with zeros corresponding to imagined points)
obs_idx: indices of observed points
imag_idx: indices of imagined points
"""
D = X.shape[1]
x_dims = [np.unique(X[:, d]) for d in range(D)]
X_grid = np.array(list(itertools.product(*x_dims)))
d_indices = [{k: v for k, v in zip(x_dims[d], range(x_dims[d].shape[0]))}
for d in range(D)]
grid_part = np.ones([x_d.shape[0] for x_d in x_dims])*-1
for i in range(X.shape[0]):
idx = tuple([d_indices[d][X[i, d]] for d in range(D)])
grid_part[idx] = 1
obs_idx = np.where(grid_part.flatten() > -1)[0]
imag_idx = np.where(grid_part.flatten() == -1)[0]
y_full = np.zeros(X_grid.shape[0])
y_full[obs_idx] = y
return X_grid, y_full, obs_idx, imag_idx