-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsim_process.py
More file actions
302 lines (196 loc) · 8.48 KB
/
Copy pathsim_process.py
File metadata and controls
302 lines (196 loc) · 8.48 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import matplotlib
matplotlib.use('agg')
import numpy as np
import sympy
from scipy.stats import gaussian_kde,norm
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import time
from tqdm import tqdm
def sim_process_multi(mu,sigma,**params):
def dW(delta_t,n_vars,flight=False):
"""Sample a random number at each call."""
if flight:
return np.random.levy(loc = np.zeros((n_vars,1)), scale = np.sqrt(delta_t))
return np.random.normal(loc = np.zeros((n_vars,1)), scale = np.sqrt(delta_t))
N = params.setdefault('n_points', 100)
t_init = params.setdefault('t0', 0)
t_end = params.setdefault('tn', 5)
x_init = params.setdefault('x_init', [0,0])
n_vars = len(x_init)
dt = (t_end - t_init) / N
ts= np.arange(t_init, t_end, dt)
xs = np.zeros( (N,n_vars) )
xs[0,:] = np.array(x_init)
for i in range(1, ts.size):
t = (i-1) * dt
x = np.array( xs[i-1, :] )
xs[i,:] = x + np.array(mu(t,*x)) * dt + np.squeeze(np.array(sigma(t,*x)).reshape(-1,n_vars) @ dW(dt, n_vars))
return xs, ts
def gen_movie_given_c(xt, ts, imgs, w=64, r=16, nc=3, bg_frames=None, **params):
'''
Generates a movie given an SDE, xt and time stamp, ts
xt : N x 2 vector denoting the position of the object at N times
ts : N x 1 vector denoting the time
w : width of the image
r : radius of the ball
nc : number of channels
returns the movie as a numpy array
'''
if imgs is not None:
return gen_movie_mnist_given(xt, ts, imgs, **params)
# generate the ball
gX, gY = np.meshgrid(np.linspace(-1,1,16), np.linspace(-1,1,16))
ball = (gX **2 + gY **2 < 1)
cvar = np.random.rand(3)
cvar = np.array([1,0.5,0])
# setup the frames
if bg_frames is None:
x_frames = np.zeros((len(ts), nc, w, w))
else:
x_frames = bg_frames.copy()
# generate the movie
for idx in range(ts.size):
idx1 = (xt[idx, :] * (w - r)).astype(int)
x_frames[idx, 2, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball*cvar[0]
x_frames[idx, 1, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball*cvar[1]
x_frames[idx, 0, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball*cvar[2]
x_frames[x_frames > 255] = 255
return x_frames
def gen_movie_given(xt, ts, imgs, w=64, r=16, nc=3, bg_frames=None, **params):
'''
Generates a movie given an SDE, xt and time stamp, ts
xt : N x 2 vector denoting the position of the object at N times
ts : N x 1 vector denoting the time
w : width of the image
r : radius of the ball
nc : number of channels
returns the movie as a numpy array
'''
if imgs is not None:
return gen_movie_mnist_given(xt, ts, imgs, **params)
# generate the ball
gX, gY = np.meshgrid(np.linspace(-1,1,16), np.linspace(-1,1,16))
ball = (gX **2 + gY **2 < 1)
# setup the frames
if bg_frames is None:
x_frames = np.zeros((len(ts), nc, w, w))
else:
x_frames = bg_frames.copy()
# generate the movie
for idx in range(ts.size):
idx1 = (xt[idx, :] * (w - r)).astype(int)
x_frames[idx, 1, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball
x_frames[idx, 0, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball
x_frames[x_frames > 255] = 255
return x_frames
def gen_movie_mnist_given(xt, ts, imgs, w=64, r=16, nc=3, **params):
# Get two images from mnist
# Put them in the appropriate location from the SDE
# Move them each frame
assert xt.shape[1] == 2*len(imgs), 'xt should be 2 times the number of digits'
x_frames = np.zeros((len(ts), nc, w, w))
img1 = imgs[0]
img2 = imgs[1]
for idx in range(ts.size):
idx1 = (xt[idx, :2] * (w - r)).astype(int)
idx2 = (xt[idx, 2:] * (w - r)).astype(int)
x_frames[idx, 0, idx1[0]:idx1[0] + img1.shape[0], idx1[1]:idx1[1] + img1.shape[1]] += img1
x_frames[idx, 0, idx2[0]:idx2[0] + img2.shape[0], idx2[1]:idx2[1] + img2.shape[1]] += img2
x_frames[x_frames > 255] = 255
return x_frames
def gen_movie_fmnist_given(xt, ts, imgs, nc=3, **params):
import ot
reg = 0.004
basis = np.eye(len(imgs))
imgs_np = np.zeros((len(imgs), imgs[0].shape[0], imgs[0].shape[1]))
x_frames = np.zeros((len(ts), nc, imgs[0].shape[0], imgs[0].shape[1]))
for idx, img in enumerate(imgs):
imgs_np[idx,:,:] = img / img.sum()
for idx in tqdm(range(ts.size)):
x = xt[idx,0]
weights = (1 - x) * basis[0,:] + x * basis[1,:]
frame = ot.bregman.convolutional_barycenter2d(imgs_np, reg, weights)
x_frames[idx, :, :, :] = frame / frame.max()
return x_frames
def gen_movie(mu, sigma, w=64, r=16, nc=3, **params):
xt_orig, ts = sim_process_multi(mu, sigma, **params)
gX, gY= np.meshgrid(np.linspace(-1,1,16), np.linspace(-1,1,16))
ball = (gX **2 + gY **2 < 1)
xt = ( xt_orig - xt_orig.min()) / (xt_orig.max() - xt_orig.min())
scale = (xt_orig.max() - xt_orig.min())
#gX, gY = np.meshgrid(np.linspace(-1,1,w), np.linspace(-1,1,w))
x_frames = np.zeros((len(ts), nc, w, w))
for idx in range(ts.size):
#x_frames[idx, :2, :, :] = ( (gX - xt[idx,0])**2 + (gY - xt[idx,1])**2 < r**2 )
#x_frames[idx, :, :, 1] = np.abs(gX - xt[idx,0]) + np.abs(gY - xt[idx,1]) < r*2
idx1 = (xt[idx, :] * (w - r)).astype(int)
x_frames[idx, 1, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball
x_frames[idx, 0, idx1[0]:idx1[0] + ball.shape[0], idx1[1]:idx1[1] + ball.shape[1]] += ball
#x_frames[x_frames > 255] = 255
#print(idx)
return x_frames, ts, xt, xt_orig
def gen_movie_mnist(mu, sigma, imgs, w=64, r=16, nc=3, **params):
# Get two images from mnist
# Put them in the appropriate location from the SDE
# Move them each frame
xt, ts = sim_process_multi(mu, sigma, **params)
assert xt.shape[1] == 2*len(imgs), 'xt should be 2 times the number of digits'
xt = ( xt - xt.min()) / (xt.max() - xt.min())
x_frames = np.zeros((len(ts), nc, w, w))
img1 = imgs[0]
img2 = imgs[1]
for idx in range(ts.size):
idx1 = (xt[idx, :2] * (w - r)).astype(int)
idx2 = (xt[idx, 2:] * (w - r)).astype(int)
x_frames[idx, 0, idx1[0]:idx1[0] + img1.shape[0], idx1[1]:idx1[1] + img1.shape[1]] += img1
x_frames[idx, 0, idx2[0]:idx2[0] + img2.shape[0], idx2[1]:idx2[1] + img2.shape[1]] += img2
x_frames[x_frames > 255] = 255
return x_frames, ts, xt
def gen_movie_fmnist(mu, sigma, imgs, nc=3, **params):
import ot
reg = 0.004
xt_orig, ts = sim_process_multi(mu, sigma, **params)
xt = ( xt_orig - xt_orig.min()) / (xt_orig.max() - xt_orig.min())
basis = np.eye(len(imgs))
imgs_np = np.zeros((len(imgs), imgs[0].shape[0], imgs[0].shape[1]))
x_frames = np.zeros((len(ts)+1, nc, imgs[0].shape[0], imgs[0].shape[1]))
for idx, img in enumerate(imgs):
imgs_np[idx,:,:] = img / img.sum()
plot_example_wass(imgs_np)
for idx in range(ts.size):
x = xt[idx,0]
#y = xt[idx,1]
weights = (1 - x) * basis[0,:] + x * basis[1,:]
frame = ot.bregman.convolutional_barycenter2d(imgs_np, reg, weights)
x_frames[idx, :, :, :] = frame / frame.max()
return x_frames, ts, xt[:,0].reshape(-1,1), xt_orig[:,0]
def plot_example_wass(imgs,n_examples=5):
import ot
reg = 0.004
basis = np.eye(imgs.shape[0])
plt.subplot(1, n_examples + 1, 1)
plt.imshow(imgs[0], cmap='gray')
plt.axis('off')
for i in range(n_examples):
plt.subplot(1, n_examples + 1, i + 2)
tx = float(i) / (n_examples - 1)
weights = (1 - tx) * basis[0,:] + tx * basis[1,:]
frame = ot.bregman.convolutional_barycenter2d(imgs, reg, weights)
plt.imshow(frame, cmap='gray')
plt.axis('off')
plt.subplot(1, n_examples + 1, n_examples + 1)
plt.imshow(imgs[1], cmap='gray')
plt.axis('off')
plt.savefig('wass_example.png')
plt.close('all')
print('output example')
if __name__ == '__main__':
fcn_mu = '[0,0]'
fcn_sigma = '[[5,0],[0,5]]'
mu_s = sympy.sympify(fcn_mu)
sigma_s = sympy.sympify(fcn_sigma)
x = sympy.symbols([x for x in ['t','x','y']])
mu = sympy.lambdify(x,mu_s)
sigma = sympy.lambdify(x,sigma_s)
gen_movie(mu, sigma)