-
Notifications
You must be signed in to change notification settings - Fork 1
/
LazPCA.jl
320 lines (281 loc) · 7.82 KB
/
LazPCA.jl
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
##############################################################################
#
# Copyright (c) 2018
# Ka Ho Yuen, Ka Wai Ho, Yue Hu, Junda Chen and Alex Lazarian
# All Rights Reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
##############################################################################
"""
module LazPCA
PCA Method for VGT
Include:
Reference: Brunt & Heyer 2002b, Heyer et al.2008, Hu et. al 2018a
Author: Ka Ho Yuen, Yue Hu, Dora Ho, Junda Chen
Changelog:
- Mike Initiate module LazPCA
- Ka Ho review the code and compare with PCA_v10.jl
"""
module LazPCA
# using PyCall
using LsqFit
using FFTW # fft, ifft
using Statistics # var, std
using LinearAlgebra
using LazType,LazThermal
export compute_PCA,ACF,linspace_float,isotropy_index,compute_PCA_2002
# Below are debug line
export pvslice,covar_pvslice,line,find_char_scale,Gaus,find_char_scale_with_gaussian
export find_char_scale_matrix,covar_matrixdot,twopix_approx,covar_matrixcorr,covar_pvcorr
export eig,PCA_channel_dot_product
e=exp(1);
# """@internal"""
function linspace(a,b,c)
# KH : Construct a 1d linspace
# TODO: linspce is called LinRange in Julia v1.0
width=mod(b-a,c);
return [x for i in a:width:b]
end
function twopix_approx(x1,x2,y1,y2,y)
# y-y1 = (y2-y1)/(x2-x1)*(x-x1)
x=(y-y1)/(y2-y1)*(x2-x1)+x1;
return x
end
function project_x( A ::Cube )
return sum(A, dims=1)[1, :, :]
end
function project_y( A ::Cube )
return sum(A, dims=2)[:, 1, :]
end
function pvslice(p::Cube,thickness::Number)
# KH: In my cube the thickness is 1/nx
# nx,ny,nv=size(p);
if (thickness<=0)
thickness=1/nx;
end
Wx = project_x( p ) / thickness
Wy = project_y( p ) / thickness
# Wx = reshape(sum(p,dims=2), nx, nv) / thickness;
# Wy = reshape(sum(p,dims=1), ny, nv) / thickness;
return Wx,Wy
end
function covar_pvslice(W::Mat)
nx,nv=size(W);
C=zeros(nv,nv);
for j in 1:nv, i in 1:nv
C[i,j]=sum(W[:,i].*W[:,j])/nx;
end
return C
end
function covar_pvcorr(W::Mat)
nx,nv=size(W);
C=zeros(nv,nv);
for j in 1:nv, i in 1:nv
C[i,j]=sum(W[:,i].*W[:,j])./nx.-sum(W[:,i])*sum(W[:,j])/(nx.^2);
end
return C
end
function covar_matrixdot(p::Cube)
nx,ny,nv=size(p);
C=zeros(nv,nv);
for j in 1:nv, i in 1:nv
C[i,j]=sum((p[:,:,i].*p[:,:,j])[:])/nx/ny;
end
return C
end
function covar_matrixcorr(p::Cube)
nx,ny,nv=size(p);
C=zeros(nv,nv);
for j in 1:nv, i in 1:nv
C[i,j]=sum((p[:,:,i].*p[:,:,j])[:])/nx/ny-sum(p[:,:,i])*sum(p[:,:,j])/(nx*ny)^2
end
return C
end
function ACF(A::Mat)
# This is the normalized autocorrelation function
# notice: CF R(x) = <A(r)A(r+x)> = real(ifft(abs(fft(A)).^2))[end:-1:1,end:-1:1];
# ACF=CF/var(A)
return real(ifft(abs.(fft(A)).^2))[end:-1:1,end:-1:1]/var(A);
end
function ACF(A::Vec)
# This is the normalized autocorrelation function
# notice: CF R(x) = <A(r)A(r+x)> = real(ifft(abs(fft(A)).^2))[end:-1:1,end:-1:1];
# ACF=CF/var(A)
return real(ifft(abs.(fft(A)).^2))/var(A);
end
function line(x,p)
return p[1].+p[2].*x
end
function Gaus(x,p)
return p[1].*exp.(.-(x.-p[2]).^2.0.*p[3]);
end
function find_char_scale(A::Vec,Scale::Vec)
Ax=ACF(A);
Ax=Ax.-mean(Ax);
Ax=Ax./Ax[1];
# Abusing the findmix function
# Definition of delta v and L
# Ax(delta v) = 1/e
pos=0;
for i in 1:length(Scale)
if (Ax[i]>1/e)
pos=twopix_approx(Scale[i],Scale[i+1],Ax[i],Ax[i+1],1/e)
else
break;
end
end
#if (pos < div(nx,2))
return pos
#else
#return Scale[nx+1-pos];
#end
end
function find_char_scale_matrix(A::Mat,Scale::Vec)
Ax=ACF(A);
Ax=Ax.-mean(Ax);
Ax=Ax./Ax[1,1];
# Abusing the findmix function
# Definition of delta v and L
# Ax(delta v) = 1/e
nx,ny=size(Ax);
posx=0;posy=0;
for i in 1:nx
if (Ax[i,1]>1/e)
posx=i;
else
break;
end
end
for i in 1:ny
if (Ax[1,i]>1/e)
posy=i;
else
break;
end
end
#if (pos < div(nx,2))
return Scale[posx],Scale[posy];
#else
#return Scale[nx+1-pos];
#end
end
function find_char_scale_with_gaussian(A::Vec,Scale::Vec)
Ax=fftshift(ACF(A));
nx=size(Ax)[1];
fit=curve_fit(Gaus,Scale,Ax,[rand(3)...]);
return 1/fit.param[3]
end
function linspace_float(a,b,number)
A=linspace(a,b,number)
nx=size(A)[1];
Ax=zeros(nx);
for i in 1:nx
Ax[i]=Float64(A[i]);
end
return Ax
end
function eig(A::Mat)
eigv=eigvecs(A);
eiglambda=eigvals(A);
return eiglambda,eigv
end
function compute_PCA(p::Cube,pscale::Vec,sample_number::Number)
nx,ny,nv=size(p);
xscale=lspace(0,10,nx);
Wx,Wy=pvslice(p,1/nx);
Cx=covar_pvcorr(Wx);
Cy=covar_pvcorr(Wy);
Cx_eig_value,Cx_eig_vectors=eig(Cx);
Cy_eig_value,Cy_eig_vectors=eig(Cy);
# Typically C?_eig_value only has ~10 dominant part
Ix=Wx*Cx_eig_vectors;
Iy=Wy*Cy_eig_vectors;
# Abusing the sort and sortperm
# 1st indice for eig_vector (2nd indice for IxIy) is the sort-perm indice
# From big eigenvalue to small
px=sortperm(Cx_eig_value,rev=true)[1:sample_number];
py=sortperm(Cy_eig_value,rev=true)[1:sample_number];
# With px,py we now know the biggest contributions, denoted by px[1...] and py[1...]
delta_vx=zeros(sample_number);
delta_vy=zeros(sample_number);
tau_x=zeros(sample_number);
tau_y=zeros(sample_number);
pscalex=pscale[pscale.>0];
for i in 1:sample_number
delta_vx[i]=abs(find_char_scale(reshape(Cx_eig_vectors[:,px[i]],nv),pscalex));
delta_vy[i]=abs(find_char_scale(reshape(Cy_eig_vectors[:,py[i]],nv),pscalex));
tau_x[i]=find_char_scale(Ix[:,px[i]],xscale);
tau_y[i]=find_char_scale(Iy[:,py[i]],xscale);
end
fitx=curve_fit(line,log.(tau_x),log.(delta_vx),[rand(2)...]);
fity=curve_fit(line,log.(tau_y),log.(delta_vy),[rand(2)...]);
ax=fitx.param[2];
ay=fity.param[2];
return ax,ay,tau_x,tau_y,delta_vx,delta_vy
end
function PCA_channel_dot_product(p::Cube)
## KH: Simply produce the first few channels of PCA-channels
nx,ny,nv=size(p);
xscale=lspace(0,10,nx);
C=covar_matrixcorr(p);
# TODO: [#eigs] Add support to older version
Cv,Ce=eig(C);
# Typically C?_eig_value only has ~10 dominant part
pp=zeros(nx,ny,nv);
for i in 1:nv,j in 1:nv
pp[:,:,i]+=p[:,:,j]*Ce[j,i]
end
# The
return pp,Cv
end
# TODO: Add support for old version
#==
function eig(A::Mat)
return eigvals(A), eigvecs(A)
end
==#
function compute_PCA_2002(p::Cube,pscale::Vec,sample_number::Number)
nx,ny,nv=size(p);
xscale=linspace_float(0,10,nx);
C=covar_matrixcorr(p);
# TODO: [#eigs] Add support to older version
Cv,Ce=eig(C);
pp=sortperm(Cv,rev=true)[1:sample_number];
dv=zeros(sample_number);
dx=zeros(sample_number);
dy=zeros(sample_number);
pscalex=pscale[pscale.>0]
for i in 1:sample_number
I=zeros(nx,ny);
for kk in 1:nv, jj in 1:ny, ii in 1:nx
I[ii,jj]+=p[ii,jj,kk].*Ce[kk,pp[i]];
end
dv[i]=abs(find_char_scale(reshape(Ce[:,pp[i]],nv),pscalex));
dx[i],dy[i]=find_char_scale_matrix(I,xscale);
end
dl=sqrt(dx.^2+dy.^2);
fit=curve_fit(line,log(dl),log(dv),[rand(2)...]);
a=fit.param[2];
return a,dl,dv
end
function isotropy_index(ax,ay)
if (ax*ay>0)
return 1-abs(ax-ay)/sqrt(ax*ay)
end
return NaN
end
function isotropy_average(ax,ay)
return (ax-ay)/(ax+ay)
end
end # End Module LazPCA