def corr_v(M, t, indices, avg=1, axes=['Ux', 'Ux'], p=1, average=False): C = [] C_norm = [] X, Y = chose_axe(M, t, axes) if average: Xmoy, Xstd = statP.average(X) # Xmoy, Xstd: median and std of X Ymoy, Ystd = statP.average(Y) else: Xmoy = 0 Xstd = 0 Ymoy = 0 Ystd = 0 for i, j in indices.keys(): # print(indices[i,j]) k, l = indices[i, j] Sp = (X[i, j] - Xmoy)**p * ( Y[k, l] - Ymoy)**p # remove the average in space ? -> remove by default C.append(Sp) # for k,l in indices[(i,j)]]) Sp_norm = (X[i, j] - Xmoy)**(2 * p) C_norm.append(Sp_norm) # substract the mean flow ? it shouldn't change the result so much, as there is no strong mean flow # -> to be checked # how to compute the mean flow : at which scale ? box size ? local average ? # Cmoy,Cstd=average(C) Cf = statP.average(C)[0] / statP.average(C_norm)[0] return Cf
def corr_d(M, frame, indices=None, dlist=None, axes=['Ux', 'Ux'], p=1, average=False): """ Compute the correlation function in time at a given instant INPUT ----- M : Mdata object frame : int frame index indices : dict of 2 elements tuple (for both keys and values) pairs of coordinates that defines the distance of computation. Default value is None : compute the pair of indices directly dlist : list distances between points. defaut is None (goes with indices) axes : 2 element string list field names to be used p : int order of the correlation function OUTPUT ----- dlist : list of distances beetween points C : correlation function (un-normalized) """ if indices is None: dlist, indices = get_indices(M) # else: # print('dlist ?') C = [] X, Y = access.chose_axe(M, frame, axes) if average: Xmoy, Xstd = statP.average(X) Ymoy, Ystd = statP.average(Y) else: Xmoy = 0 Xstd = 0 Ymoy = 0 Ystd = 0 C = [[] for i in indices] for m, ind in enumerate(indices): for i, j in ind.keys(): k, l = ind[i, j] Sp = (X[i, j] - Xmoy) ** p * (Y[k, l] - Ymoy) ** p # remove the average in space ? -> remove by default C[m].append(Sp) # for k,l in indices[(i,j)]]) C[m], std = statP.average(C[m]) return dlist, C
def structure_function(M, t, indices, axes=['E', 'E'], p=2): """ Compute structure functions from a Mdata set INPUT ----- M : Mdata object t : int time index indices : list of tuple list of pair of indices, used as the sample of Mdata to compute two points quantities axe : string. Possible values are : 'E', 'xx', 'yy', 'xy' to be implemented : 'l' : longitudinal, 't' : transverse OUTPUT ----- C : list of float list of evaluated structure functions on the positions specified by the list indices """ X, Y = chose_axe(M, t, axes) C = [] for i, j in indices.keys(): for k, l in zip(indices[(i, j)][0], indices[(i, j)][1]): C.append(X[i, j]**p - Y[k, l]**p) # for k,l in indices[(i,j)]]) # print(len(C)) Cmoy, Cstd = statP.average(C) return C
def compute(M, t, indices, avg=1, axes=['U', 'U'], p=1, average=False): C = [] C_norm = [] X, Y = chose_axe(M, t, axes) if average: Xmoy, Xstd = statP.average(X) Ymoy, Ystd = statP.average(Y) else: Xmoy = 0 Xstd = 0 Ymoy = 0 Ystd = 0 for i, j in indices.keys(): # print(indices[i,j]) k, l = indices[i, j] vec_t = [k - i, l - j] # Xl = project(X[i,j,...],vec_t,'l') # Yl = project(Y[k,l,...],vec_t,'l') # Xt = project(X[i,j,...],vec_t,'t') # Yt = project(Y[k,l,...],vec_t,'t') Sp = (X[i, j, ...] - Xmoy)**p * ( Y[k, l, ...] - Ymoy)**p # remove the average in space ? -> remove by default C.append(Sp) # for k,l in indices[(i,j)]]) Sp_norm = ((X[i, j, ...] + Y[k, l, ...] - Xmoy - Ymoy) / 2)**(2 * p) C_norm.append(Sp_norm) # substract the mean flow ? it shouldn't change the result so much, as there is no strong mean flow # -> to be checked # how to compute the mean flow : at which scale ? box size ? local average ? # Cmoy,Cstd=average(C) Cf = statP.average(C)[0] / statP.average(C_norm)[0] return Cf
def corr_v_tn(M, t, indices, avg=1, p=1): """ Compute the correlation coefficient at time t for a collection of pair indices INPUT M : Mdata object t : time indice indices : dictionnary containing pair of indices """ C = [] x = M.x y = M.y Ux = M.Ux[..., t] Uy = M.Uy[..., t] Xmoy, Xstd = statP.average(Ux) Ymoy, Ystd = statP.average(Uy) for i, j in indices.keys(): k, l = indices[i, j] # tangent vector u = (x[k, l] - x[i, j], y[k, l] - y[i, j]) theta, r = Smath.cart2pol(u) Sp = (X[i, j] - Xmoy)**p * (Y[k, l] - Ymoy)**p # remove the average in space ? # for k,l in zip(indices[(i,j)][0],indices[(i,j)][1]): C.append(Sp) # for k,l in indices[(i,j)]]) # substract the mean flow ? it shouldn't change the result so much # but it does change ! # at which scale ? box size ? local average ? # # Cmoy,Cstd=average(C) return statP.average(C)
def corr_d_stat(M, N, indices=None, axes=['Ux', 'Ux'], p=1, average=False): """ Compute the correlation function from an ensemble N of time """ # select N times to compute the if indices is None: dlist, indices = get_indices(M) nx, ny, nt = M.shape() frames = range(0, nt, nt / N) Ctot = [[] for i in frames] for i, frame in enumerate(frames): d, Ctot[i] = corr_d(M, frame, indices=indices, dlist=dlist, axes=axes, p=p, average=average) Ctot = np.asarray(Ctot) Ctot, Cstd = statP.average(Ctot, axis=0) return dlist, Ctot
def corr_v_d(Mlist, t, axes=['Ux', 'Ux'], N=100, Dt=1, p=1, display=False, save=False, label='^', fignum=1): """ compute the correlation function in space at a given time. Average over space (ie along d-1 dimensions of Mlist[axes[0]]),and eventually over time INPUT ------ M : Mdata object must have attributes : t, fields ('Ux','Uy', ...) must have method shape() t : int time index axes : 2 elements string list attributes of M to be used for the correlation function N : int Number of frames before and after time t. p : int. default value is 1 power of the fields C_p(Dt) = U1**p * U2**p / <U1**2p > display : bool. default value is false """ # Ct=[] # how to average ?? -> need a mean on several realization ? or just substract the average value (in space) ? M = Mlist[0] tlist = M.t # print("Compute mean values") tmin = max(0, t - N) tmax = min(len(tlist) - 1, t + N) # print(tmin) # print(tmax) t_c = np.asarray(M.t)[np.arange(tmin, tmax)] # -M.t[t] # print(np.mean(np.diff(t_c))) # max number of time step # print("Compute mean values 2") # Compute the average flow Xm = [] Ym = [] for M in Mlist: Xref, Yref = chose_axe(M, t, axes) Xm.append(Xref) Ym.append(Yref) Xm = np.asarray(Xm) Ym = np.asarray(Ym) # print(Xm.shape) Xmoy_ref, Xstd_ref = statP.average(Xref, axis=()) # idea : compute the flow average over several realizations (ensemble avg) Ct = [] for tc in range(tmin, tmax): Sp = [] Xt_moy = [] XDt_moy = [] for M in Mlist: Xref, Yref = chose_axe(M, t, axes) Xt_m, Xstd_ref = statP.average(Xref) Xt_moy.append(Xt_m) X, Y = chose_axe(M, tc, axes) XDt_m, Xstd = statP.average(X) XDt_moy.append(XDt_m) Xt_ref = statP.average(Xt_moy) XDt_ref = statP.average(XDt_moy) for M in Mlist: Xref, Yref = chose_axe(M, t, axes) # Xmoy_ref,Xstd_ref=statP.average(Xref) X, Y = chose_axe(M, tc, axes) # Xmoy,Xstd=statP.average(X) Sp.append((X - XDt_ref)**p * (Xref - XDt_ref)**p) # print(np.shape(Sp)) Ct.append(statP.average(Sp)[0]) # print(Ct) C0 = Ct[t - tmin] C_norm = np.asarray(Ct) / C0 indices = np.argsort(t_c) t_c = t_c[indices] - M.t[t] C_norm = C_norm[indices] # plot distribution of Sp values ? figs = {} if display or save: field = axes[0] title = str(axes[0]) + ', ' + str(axes[1]) graphes.graph(t_c, C_norm, fignum=fignum, label=label) figs.update(graphes.legende('$t (s)$', '$C$', title)) if save: name = 'fx_' + str(int(np.floor(M.fx * 1000))) + 'm_t_' + str( int(np.floor(M.t[t] * 1000))) + 'm_' graphes.save_figs(figs, prefix='./Stat_avg/Time_correlation/Serie/' + field + '/' + name, dpi=300, display=True, frmt='png') return t_c, C_norm