-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
418 lines (312 loc) · 11.2 KB
/
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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
"""
Created on Sat Mar 21 11:06:52 2020
@author: ron
"""
# Imports:
from flowtracks.io import Scene
# from flowtracks.io import save_particles_table
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# ===============================================
# Loading trajectory lists
# ===============================================
def get_traj_list(file_path):
'''
this function loads h5 files and returns a list of trajectories.
file_path is either the path to a single h5 file, or a list of links
to multiple h5 files.
'''
if type(file_path) == str:
file_path = [file_path]
t = []
for i in file_path:
s = Scene(i)
for traj in s.iter_trajectories():
t.append(traj)
return t
# ===============================================
# Visualizations
# ===============================================
def plot_3D_quiver(traj_list, v_max, subtract_mean = False, FPS = 500.0,
size_factor = 2.0, aspect='equal'):
'''
will return a 3D plot of floating quivers that stand for
the Lagrangin velocity samples.
traj_list - list of trajectories
v_max - maximum velocity for normalizing colors
subtract_mean - if true will determine a mean velocity over all samples
and remove this from each trajectory. will remove also
this mean component of the displacement.
'''
fig = plt.figure()
ax = fig.gca(projection='3d')
# estimate arrow lengths:
i = 0
while len(traj_list[i]) < 5 and i < len(traj_list):
i += 1
L = np.mean(np.linalg.norm(np.gradient(traj_list[i].pos())[0], axis=1))
if subtract_mean:
VV = get_mean_velocity(traj_list)
else:
VV = np.zeros((3,))
t0 = traj_list[0].time()[0]
for tr in traj_list:
if tr.time()[0] < t0:
t0 = tr.time()[0]
cmap = matplotlib.cm.get_cmap('viridis')
for tr in traj_list:
tm = tr.time() - t0
x,y,z = tr.pos()[:,0], tr.pos()[:, 2], tr.pos()[:, 1]
x,y,z = x - tm*VV[0]/FPS, y - tm*VV[2]/FPS, z - tm*VV[1]/FPS
u,v,w = tr.velocity()[:, 0], tr.velocity()[:, 2], tr.velocity()[:, 1]
u, v, w = u - VV[0], v - VV[2], w - VV[1]
#ax.plot(x,y,z,lw=1,color='k')
V = 1.0*np.linalg.norm(tr.velocity(), axis=1)/v_max
V = V * (V <= 1) + (V > 1)
c = cmap(V)
ax.quiver(x, y, z, u, v, w, length=L*size_factor,
arrow_length_ratio = .5, colors = c)
ax.set_xlabel(r'$x$ [m]')
ax.set_ylabel(r'$y$ [m]')
ax.set_zlabel(r'$z$ [m]')
# ax.set_aspect(aspect)
return fig, ax
def plot_traj_xy(traj_list,min_len=5, shape='o-', lw=0.5):
fig, ax = plt.subplots()
for i in traj_list:
if len(i) > min_len:
r = i.pos()
ax.plot(r[:,0] , r[:,1], shape, ms=1, lw=lw)
ax.set_aspect('equal')
ax.set_xlabel(r'$x$ [m]')
ax.set_ylabel(r'$y$ [m]')
return fig, ax
def plot_traj_xz(traj_list,min_len=5, shape='o-', lw=1):
fig, ax = plt.subplots()
for i in traj_list:
if len(i) > min_len:
r = i.pos()
ax.plot(r[:,0] , r[:,2], shape, ms=1, lw=lw)
ax.set_aspect('equal')
ax.set_xlabel(r'$x$ [m]')
ax.set_ylabel(r'$z$ [m]')
return fig, ax
def plot_traj_yz(traj_list,min_len=5, shape='o-', lw=1):
fig, ax = plt.subplots()
for i in traj_list:
if len(i) > min_len:
r = i.pos()
ax.plot(r[:,1] , r[:,2], shape, ms=1, lw=lw)
ax.set_xlabel(r'$y$ [m]')
ax.set_ylabel(r'$z$ [m]')
ax.set_aspect('equal')
return fig, ax
# ============================================
# Statistics
# ============================================
def get_mean_velocity(traj_list):
'''
will return a numpy array representing the mean value
of velocity components of all trajectories in the list.
Inputs:
traj_list - list of trajectories
Returns:
mean vel - (vx,vy,vz)
'''
# vx,vy,vz = [],[],[]
# for tr in traj_list:
# v = tr.velocity()
# for i in range(v.shape[0]):
# vx.append(v[i,0])
# vy.append(v[i,1])
# vz.append(v[i,2])
# V = np.array([np.mean(vx), np.mean(vy), np.mean(vz)])
# return V
return np.mean([np.mean(tr.velocity(), axis=0) for tr in traj_list],
axis=0)
def get_vel_p_moment(traj_list, p):
'''
will calculate the central p'th moment of velocities
returns -
mp - < (vx - Vx)^p >, < (vy - Vy)^p >, < (vz - Vz)^p >
'''
V = get_mean_velocity(traj_list)
# vx,vy,vz = [],[],[]
# for tr in traj_list:
# v = tr.velocity() - V
# for i in range(v.shape[0]):
# vx.append(v[i,0])
# vy.append(v[i,1])
# vz.append(v[i,2])
# mp = [np.mean(np.array(vv)**p) for vv in [vx,vy,vz]]
# return np.array(mp)
return np.mean([np.mean((tr.velocity()-V)**p, axis=0) for tr in traj_list],
axis=0)
def plot_vel_pdfs(traj_list, fit_gaussian=True, bins=100, bin_range=None):
'''
will generate a pdf of trajectory vecolicties and if specified
by (fit_gaussian = True) will fit a gaussian to the data
'''
vx,vy,vz = [],[],[]
M = -1.0
for i in traj_list:
v = i.velocity()
for j in range(v.shape[0]):
vx.append(v[j, 0])
vy.append(v[j, 1])
vz.append(v[j, 2])
if np.amax(np.abs(v)) > M:
M = np.amax(np.abs(v))
if bin_range==None:
bin_range=(-M,M)
fig, ax = plt.subplots()
c = ['b','r','g']
shp = ['o','d','v']
lbl = [r'$v_x$',r'$v_y$',r'$v_z$']
for e,i in enumerate([vx, vy, vz]):
h = np.histogram(i, bins=bins, density=True, range=bin_range)
x,y = 0.5*(h[1][:-1] + h[1][1:]), h[0]
m,s = np.mean(i), np.std(i)
xx = np.arange(-M,M,2.0*M/500)
ax.plot(x,y,c[e]+shp[e]+'-',lw=0.4,
label = lbl[e]+r' $\mu = %.3f$ $\sigma = $%0.3f'%(m,s))
if fit_gaussian:
ax.plot(xx, gaussian(xx, m, s), c[e], lw = 1.2)
ax.legend()
ax.set_xlabel(r'$v_i$')
ax.set_ylabel(r'P($v_i$)')
return fig, ax
# =======================================================
# Lagrangian 2nd order Structure function
# =======================================================
def plot_Dii(traj_list, FPS = 1.0, axis = 0, xlabel=None, ylabel=None):
'''
will plot and return the matplotlib axis object for the 2nd order
Lagrangian structures function:
D_ii = < (v_i(t + x) - v_i(t))^2 >
here i is the axis for the function calculation
'''
D = []
for i in traj_list:
v = i.velocity()[:,axis]
D.append([0])
for j in range(len(i) - 1):
D[-1].append( np.mean( (v[:-(j+1)] - v[(j+1):])**2 ) )
D_ii = average_lists(D)
time = np.arange(len(D_ii))/FPS
fig,ax = plt.subplots()
ax.plot(np.arange(len(D_ii))/FPS, D_ii,'-o')
if xlabel==None: ax.set_xlabel(r'$\tau$')
else: ax.set_xlabel(xlabel)
if ylabel==None: ax.set_ylabel(r'$D_{ii}(\tau)$')
else: ax.set_ylabel(ylabel)
return fig, ax, time, D_ii
# ====================================================
# Lagrangian Autocorrelations
# ====================================================
def plot_velocity_autocorrelation(traj_list, FPS = 1.0, axis = 0,
xlabel=None, ylabel=None):
'''
will calculate the Lagrangian autocorrelation function of a
velocity component ('axis' component), will plot it and finally
return the results.
'''
v_lst = []
for traj in traj_list:
v_lst.append(traj.velocity()[:,axis])
rho_ii, N, S = list_corelation(v_lst)
time = np.arange(len(rho_ii)) / FPS
fig, ax = plt.subplots()
ax.plot(time, rho_ii)
if xlabel==None: ax.set_xlabel(r'$\tau$')
else: ax.set_xlabel(xlabel)
if ylabel==None: ax.set_ylabel(r'$\rho_{v,ii}(\tau)$')
else: ax.set_ylabel(ylabel)
return fig, ax, rho_ii, time
def plot_acceleration_autocorrelation(traj_list, FPS = 1.0, axis = 0,
xlabel=None, ylabel=None):
'''
will calculate the Lagrangian autocorrelation function of a
velocity component ('axis' component), will plot it and finally
return the results.
'''
a_lst = []
for traj in traj_list:
a_lst.append(traj.accel()[:,axis])
rho_ii, N, S = list_corelation(a_lst)
time = np.arange(len(rho_ii)) / FPS
fig, ax = plt.subplots()
ax.plot(time, rho_ii)
if xlabel==None: ax.set_xlabel(r'$\tau$')
else: ax.set_xlabel(xlabel)
if ylabel==None: ax.set_ylabel(r'$\rho_{a,ii}(\tau)$')
else: ax.set_ylabel(ylabel)
return fig, ax, rho_ii, time
# ===================================================
# General Utilities
# ===================================================
def gaussian(x,m,s):
return 1.0/np.sqrt(2*np.pi)/s * np.exp(-0.5 * ((x-m)/s)**2)
def average_lists(lsts, get_N = False):
'''
retruns an index wise average of
all the lists in lsts, and the indexed number of averaged values
'''
lsts = sorted( lsts, key=len )
N = len( lsts[-1] )
indexes = range(N)
averaged_lsts = []
N_lsts = []
for i in indexes:
while len(lsts[0])<i+1:
lsts.pop(0)
temp = 0
temp_N = 0
for j in range(len(lsts)):
temp += lsts[j][i]
temp_N += 1
averaged_lsts.append(temp*1.0/len(lsts))
N_lsts.append(temp_N)
if get_N:
return averaged_lsts, N_lsts
else:
return averaged_lsts
def list_corelation(arr_list):
'''
returns the array of correlation for a list of arrays as a function of
time lag:
< (arr(t+x) - <arr(t+x)> )*( arr(t) - <arr(t)> ) >
R = ===============================================================
sqrt( < (arr(t+x) - <arr(t+x)>)^2 > < (arr(t) - <arr(t)>)^2 > )
( where <> is average over samples and x is a time (index) lag)
returns -
R - array of correlation coefficients
S - array of standard deviations for R as a funciton of time
N - array of number of elements used at each time
'''
N = max( [len(i) for i in arr_list] )
r = [ [ [],[] ] for i in range(N)]
for arr in arr_list:
for val in arr:
r[0][0].append(val)
r[0][1].append(val)
for i in range(1,len(arr)):
for val in arr[:-i]:
r[i][0].append(val)
for val in arr[i:]:
r[i][1].append(val)
R,S,N = [],[],[]
for i in r:
if len(i[1]) <= 1:
R.append(0)
S.append(0)
N.append(1)
else:
r1 = np.array(i[0]) - np.mean(i[0])
r2 = np.array(i[1]) - np.mean(i[1])
R.append( np.mean(r1*r2) / np.sqrt(np.mean(r1**2) * np.mean(r2**2) ) )
S.append( np.std(r1*r2) / np.sqrt(np.mean(r1**2) * np.mean(r2**2) ) )
N.append(len(r1))
return np.array(R), np.array(S), np.array(N)