Skip to content

Commit 412062d

Browse files
committed
fixed bugs that were causing inaccurate results when running in patches
- spatial overlap of patches is accounted for - fixed a bug when reading Y from memory to compute YrA
1 parent 4936542 commit 412062d

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

run_CNMF_patches.m

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@
137137
end
138138

139139
cnt = 0;
140+
B = sparse(prod(sizY(1:end-1)),length(patches));
141+
MASK = zeros(sizY(1:end-1));
142+
F = zeros(length(patches),sizY(end));
140143
for i = 1:length(patches)
141144
for k = 1:K
142145
if k <= size(RESULTS(i).A,2)
@@ -152,6 +155,9 @@
152155
end
153156
end
154157
if length(sizY) == 3
158+
b_temp = sparse(sizY(1),sizY(2));
159+
b_temp(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) = reshape(RESULTS(i).b,patches{i}(2)-patches{i}(1)+1,patches{i}(4)-patches{i}(3)+1);
160+
MASK(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) = MASK(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) + 1;
155161
P.sn(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) = reshape(RESULTS(i).P.sn,patches{i}(2)-patches{i}(1)+1,patches{i}(4)-patches{i}(3)+1);
156162
P.active_pixels(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) = P.active_pixels(patches{i}(1):patches{i}(2),patches{i}(3):patches{i}(4)) + ...
157163
reshape(RESULTS(i).P.active_pixels,patches{i}(2)-patches{i}(1)+1,patches{i}(4)-patches{i}(3)+1);
@@ -167,8 +173,12 @@
167173
P.c1 = [P.c1;RESULTS(i).P.c1];
168174
P.gn = [P.gn;RESULTS(i).P.gn];
169175
P.neuron_sn = [P.neuron_sn;RESULTS(i).P.neuron_sn];
176+
B(:,i) = b_temp(:);
177+
F(i,:) = RESULTS(i).f;
170178
end
171179
A(:,cnt+1:end) = [];
180+
A = spdiags(1./MASK(:),0,prod(sizY(1:end-1)),prod(sizY(1:end-1)))*A;
181+
B = spdiags(1./MASK(:),0,prod(sizY(1:end-1)),prod(sizY(1:end-1)))*B;
172182
C = cell2mat({RESULTS(:).C}');
173183
S = cell2mat({RESULTS(:).S}');
174184
ff = find(sum(A,1)==0);
@@ -207,9 +217,11 @@
207217
end
208218
fprintf(' done. \n');
209219
%% classify components
210-
ff = classify_components(Am,Pm,options);
211-
A = Am(:,ff);
212-
C = Cm(ff,:);
220+
% ff = classify_components(Am,Pm,options);
221+
% A = Am(:,ff);
222+
% C = Cm(ff,:);
223+
A = Am;
224+
C = Cm;
213225

214226
%% update spatial components
215227
fprintf('Updating spatial components...');
@@ -226,15 +238,21 @@
226238
% cnt = cnt + fm(i)-fp(i)+1;
227239
% end
228240

229-
bsum = zeros(length(patches),1);
230-
for i = 1:length(patches)
231-
bsum(i) = sum(RESULTS(i).b);
232-
end
233-
bsum = bsum/sum(bsum);
234-
f_p = cell2mat({RESULTS(:).f}');
235-
fin = mean(spdiags(bsum,0,length(patches),length(patches))*f_p);
236-
fin = medfilt1(fin,11);
241+
% bsum = zeros(length(patches),1);
242+
% for i = 1:length(patches)
243+
% bsum(i) = sum(RESULTS(i).b);
244+
% end
245+
% bsum = bsum/sum(bsum);
246+
% f_p = cell2mat({RESULTS(:).f}');
247+
% fin = mean(spdiags(bsum,0,length(patches),length(patches))*f_p);
248+
% fin = medfilt1(fin,11);
237249

250+
fin = mean(F);
251+
for iter = 1:10
252+
bin = max(B*(F*fin')/norm(fin)^2,0);
253+
fin = max((bin'*B)*F/norm(bin)^2,0);
254+
end
255+
%%
238256
% PROCESS PATCHES
239257
options.d1 = sizY(1);
240258
options.d2 = sizY(2);

update_temporal_components.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,18 +142,19 @@
142142
mc = min(d,15); % number of constraints to be considered
143143
LD = 10*ones(mc,K);
144144
else
145+
step = 5e3;
145146
nA = sum(A.^2);
146-
AA = A'*A/spdiags(nA(:),0,length(nA),length(nA));
147+
AA = (A'*A)/spdiags(nA(:),0,length(nA),length(nA));
147148
if memmaped
148149
YA = zeros(T,length(nA));
149150
tic;
150-
for i = 1:5e3:d
151-
YA = YA + double(Y.Yr(i:min(i+1e4-1,d),:))'*A(i:min(i+1e4-1,d),:);
151+
for i = 1:step:d
152+
YA = YA + double(Y.Yr(i:min(i+step-1,d),:))'*A(i:min(i+step-1,d),:);
152153
end
153154
toc
154155
YA = YA/spdiags(nA(:),0,length(nA),length(nA));
155156
else
156-
YA = Y'*A/spdiags(nA(:),0,length(nA),length(nA));
157+
YA = (Y'*A)/spdiags(nA(:),0,length(nA),length(nA));
157158
end
158159
YrA = (YA - Cin'*AA);
159160
if strcmpi(method,'constrained_foopsi') || strcmpi(method,'MCEM_foopsi')

0 commit comments

Comments
 (0)