forked from MouseLand/rastermap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_Fourier.m
160 lines (130 loc) · 3.67 KB
/
match_Fourier.m
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
mname = 'TX28';
datexp = '2018_10_02';
blk = '1';
spks = readNPY(sprintf('Z:/data/PROC/%s/%s/%s/suite2p/combined/spks.npy', mname, datexp, blk));
iscell = readNPY(sprintf('Z:/data/PROC/%s/%s/%s/suite2p/combined/iscell.npy', mname, datexp, blk));
spks = spks(logical(iscell(:,1)), :);
[NN, Nframes] = size(spks);
%%
spks = readNPY('H:/s2p_paper/subj17_spks.npy');
%%
X = readNPY('D:\Github\rastercode\imgcov.npy');
X = zscore(X, [], 2);
[U, S, V] = svdecon(X);
U = U .* diag(S)';
U = gpuArray(U(:, 1:256));
U = zscore(U, 1, 2);
%%
X = readNPY('D:/Github/data/allen-visp-alm/X.npy');
X = X(1:3:end, :);
U = gpuArray(single(X));
%%
X = gpuArray(single(spks));
X = zscore(X, [], 2);
%X = X - mean(X, 1) ;
Lblock = 128;
inds = 1:size(X,2);
iblock = ceil(inds/Lblock);
nblocks = max(iblock);
iperm = randperm(nblocks);
Ntrain = ceil(3/4 * nblocks);
itrain = ismember(iblock, iperm(1:Ntrain));
itest = ismember(iblock, iperm(1+Ntrain:nblocks));
X1 = X(:, itrain);
Xz = X(:, itest);
[U, S, V] = svdecon(X1(1:5:end, :));
U = X1 * V(:, 1:256);
U = gpuArray(U);
% U = 3.4 * U;
%%
U = 2 * zscore(U, 1, 2)/size(U,2)^.5;
ndims = 2;
niter = 4000;
eta0 = .01;
pW = 0.9;
NN = size(U,1);
ys = U(:, 1:ndims);
ys = .05 * ys./std(ys,1,1);
ys = gpuArray(single(ys));
dy = gpuArray.zeros(NN, ndims, 'single');
eta = linspace(eta0, eta0, niter);
lam = ones(NN,1);
oy = zeros(NN, ndims);
nbase = 41;
ks = zeros(nbase, nbase, 2);
ks(:,:, 1) = repmat(0:nbase-1, nbase, 1);
ks(:,:, 2) = repmat(0:nbase-1, nbase, 1)';
ks = reshape(ks, [], 2);
fxx = max(ks,[], 2) + min(ks,[], 2)/1000;
[~, isort] = sort(fxx);
ks = ks(isort, :);
alpha = 1;
nA = 1:size(ks,1);
nA = 2 * ceil(nA/2);
vscale = 1./nA.^(alpha/2);
vscale = vscale / sum(vscale.^2).^.5;
% UtU = U * U';
% UtU = UtU - diag(diag(UtU));
% err0 = mean(mean(UtU.^2));
nc = size(ks,1) * ones(niter,1);
cold = Inf;
cnew = cold;
tic
for k = 1:niter
S = gpuArray.ones(NN,nc(k), 'single');
dS = gpuArray.ones(NN,nc(k), ndims,'single');
for j = 1:ndims
S = S .* sin(pi + mod(ks(1:nc(k),j)'+1, 2) * pi/2 + ys(:,j) * floor((1+ks(1:nc(k),j)')/2));
end
for j = 1:ndims
dS(:,:,j) = S ./ sin(pi + mod(ks(1:nc(k),j)'+1, 2) * pi/2 + ys(:,j) * floor((1+ks(1:nc(k),j)')/2));
dS(:,:,j) = dS(:,:,j) .* cos(pi + mod(ks(1:nc(k),j)'+1, 2) * pi/2 + ys(:,j) * floor((1+ks(1:nc(k),j)')/2)) .* floor((1+ks(1:nc(k),j)')/2);
end
S = S .* vscale;
y2 = sum(S.^2, 2).^.5;
S = S ./y2;
if rem(k,100)==1
% ypred = S * S';
% ypred = ypred -diag(diag(ypred));
% err = ypred - UtU;
% err = err - diag(diag(err));
% cnew = mean(err(:).^2)/err0;
if cold < cnew
% eta = eta/2;
else
cold = cnew;
end
fprintf('iter %d, eta %2.4f, time %2.2f, err %2.6f \n', k, eta(k), toc, cnew)
plot(ys(:,1), ys(:,2), '.')
% xlim([-50 50])
% ylim([-50 50])
drawnow
end
% lam = mean(S .* K, 2) ./ (1e-3 + mean(W.^2, 2));
% err = (lam.*err);
err = S * (S'*S) - U * (U' * S);
dy = squeeze(mean(dS .* (err), 2));
dy = dy./sum(dy.^2,2).^.5;
oy = pW * oy + (1-pW) * dy;
ys = ys - eta(k) * oy;
end
toc
drawnow
%%
ds = reshape(ys, [NN, 1, ndims]) - reshape(ys, [1, NN, ndims]);
ds = mod(ds + pi, 2*pi) - pi;
W = exp(-sum(ds.^2, 3));
W = W - diag(diag(W));
W = gather(W);
[~, isort] = sort(W, 2, 'descend');
Xz = zscore(Xz, [], 2)/size(Xz,2)^.5;
X0 = gpuArray.zeros(size(Xz), 'single');
cb = zeros(128,1);
for j = 1:128
X0 = X0 + Xz(isort(:, j),:);
X0z = zscore(X0, [], 2)/size(X0,2)^.5;
cc = sum(Xz .* X0z, 2);
cb(j) = gather(mean(cc));
end
cb(1)
semilogx(cb)