[5c09f6]: / data / simulated / generate_twod_data.py

Download this file

189 lines (160 with data), 5.5 kB

  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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import sys
sys.path.append("../..")
sys.path.append("../../data")
from warps import apply_gp_warp
from gpsa.util import rbf_kernel_numpy as rbf_covariance
from gpsa import polar_warp
from scipy.stats import multivariate_normal as mvnpy
def generate_twod_data(
n_views,
n_outputs,
grid_size,
n_latent_gps=None,
kernel_variance=0.1,
kernel_lengthscale=5,
noise_variance=0.0,
fixed_view_idx=None,
):
kernel = rbf_covariance
kernel_params_true = [np.log(1.0), np.log(1.0)]
xlimits = [0, 10]
ylimits = [0, 10]
x1s = np.linspace(*xlimits, num=grid_size)
x2s = np.linspace(*ylimits, num=grid_size)
X1, X2 = np.meshgrid(x1s, x2s)
X_orig_single = np.vstack([X1.ravel(), X2.ravel()]).T
X_orig = np.concatenate([X_orig_single.copy(), X_orig_single.copy()], axis=0)
n_samples_per_view = X_orig.shape[0] // 2
n_samples_list = [n_samples_per_view] * n_views
cumulative_sums = np.cumsum(n_samples_list)
cumulative_sums = np.insert(cumulative_sums, 0, 0)
view_idx = np.array(
[
np.arange(cumulative_sums[ii], cumulative_sums[ii + 1])
for ii in range(n_views)
]
)
n = np.sum(n_samples_list)
K_XX = kernel(X_orig_single, X_orig_single, kernel_params_true)
nY = n_outputs if n_latent_gps is None else n_latent_gps
Y_orig = np.vstack(
[
mvnpy.rvs(
mean=np.zeros(X_orig_single.shape[0]),
cov=K_XX + 0.001 * np.eye(K_XX.shape[0]),
)
for _ in range(nY)
]
).T
if n_latent_gps is not None:
W_mat = np.random.normal(size=(n_latent_gps, n_outputs))
Y_orig = Y_orig @ W_mat
Y = np.concatenate([Y_orig] * n_views, axis=0)
X = X_orig.copy()
# X[n_samples_per_view:] = X[n_samples_per_view:] @ (
# np.eye(2) + np.random.normal(0, 0.01, size=(2, 2))
# )
# X[:n_samples_per_view] = X[:n_samples_per_view] @ (
# np.eye(2) + np.random.normal(0, 0.01, size=(2, 2))
# )
X, Y, n_samples_list, view_idx = apply_gp_warp(
X_orig_single[:n_samples_per_view],
Y_orig[:n_samples_per_view],
n_views=2,
kernel_variance=kernel_variance,
kernel_lengthscale=kernel_lengthscale,
noise_variance=noise_variance,
)
if fixed_view_idx is not None:
X[view_idx[fixed_view_idx]] = X_orig_single
return X, Y, n_samples_list, view_idx
def generate_twod_data_partial_overlap(
n_views,
n_outputs,
grid_size,
n_latent_gps=None,
kernel_variance=0.1,
kernel_lengthscale=5,
noise_variance=0.0,
):
kernel = rbf_covariance
kernel_params_true = [np.log(1.0), np.log(1.0)]
xlimits = [-5, 5]
ylimits = [-5, 5]
x1s = np.linspace(*xlimits, num=grid_size)
x2s = np.linspace(*ylimits, num=grid_size)
X1, X2 = np.meshgrid(x1s, x2s)
X_orig_single = np.vstack([X1.ravel(), X2.ravel()]).T
## Only keep the center square of points
X_orig_single_partial = X_orig_single.copy()
keep_idx = np.logical_and(np.abs(X_orig_single_partial[:, 0]) < 2.5, np.abs(X_orig_single_partial[:, 1]) < 2.5)
X_orig_single_partial = X_orig_single_partial[keep_idx]
X_orig = np.concatenate([X_orig_single.copy(), X_orig_single_partial.copy()], axis=0)
n_samples_per_view = X_orig.shape[0] // 2
n_samples_list = [n_samples_per_view] * n_views
cumulative_sums = np.cumsum(n_samples_list)
cumulative_sums = np.insert(cumulative_sums, 0, 0)
view_idx = np.array(
[
np.arange(cumulative_sums[ii], cumulative_sums[ii + 1])
for ii in range(n_views)
]
)
n = np.sum(n_samples_list)
K_XX = kernel(X_orig_single, X_orig_single, kernel_params_true)
nY = n_outputs if n_latent_gps is None else n_latent_gps
Y_orig = np.vstack(
[
mvnpy.rvs(
mean=np.zeros(X_orig_single.shape[0]),
cov=K_XX + 0.001 * np.eye(K_XX.shape[0]),
)
for _ in range(nY)
]
).T
if n_latent_gps is not None:
W_mat = np.random.normal(size=(n_latent_gps, n_outputs))
Y_orig = Y_orig @ W_mat
# Y = np.concatenate([Y_orig] * n_views, axis=0)
# Y = np.concatenate(
# [
# Y_orig,
# Y_orig[keep_idx],
# ]
# )
# X = X_orig.copy()
# X[n_samples_per_view:] = X[n_samples_per_view:] @ (
# np.eye(2) + np.random.normal(0, 0.01, size=(2, 2))
# )
# X[:n_samples_per_view] = X[:n_samples_per_view] @ (
# np.eye(2) + np.random.normal(0, 0.01, size=(2, 2))
# )
X, Y, n_samples_list, view_idx = apply_gp_warp(
X_orig_single[:grid_size**2],
Y_orig[:grid_size**2],
n_views=2,
kernel_variance=kernel_variance,
kernel_lengthscale=kernel_lengthscale,
noise_variance=noise_variance,
)
X = np.concatenate(
[
X[:grid_size**2],
X[grid_size**2:][keep_idx],
]
)
Y = np.concatenate(
[
Y[:grid_size**2],
Y[grid_size**2:][keep_idx],
]
)
view_idx = view_idx.tolist()
view_idx[1] = np.where(keep_idx == True)[0]
n_samples_list[1] = keep_idx.sum()
# import ipdb; ipdb.set_trace()
return X, Y, n_samples_list, view_idx, keep_idx