Skip to content

Commit 8977c7c

Browse files
committed
allow seeding with binary masks and arbitrary background comps
1 parent 52af659 commit 8977c7c

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

update_spatial_components.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@
6060
end
6161

6262
if nargin < 5 || isempty(P); P = preprocess_data(Y,1); end % etsimate noise values if not present
63-
if nargin < 4 || isempty(A_);
63+
if nargin < 4 || isempty(A_)
6464
IND = ones(d,size(C,1));
6565
else
6666
if islogical(A_) % check if search locations have been provided, otherwise estimate them
6767
IND = A_(:,1:K);
6868
if isempty(C)
6969
INDav = double(IND)/diag(sum(double(IND)));
7070
px = (sum(IND,2)>0);
71-
f = mean(Y(~px,:));
71+
[b,f] = fast_nmf(double(Y(~px,:)),[],options.nb,50);
7272
b = max(Y*f',0)/norm(f)^2;
7373
C = max(INDav'*Y - (INDav'*b)*f,0);
7474
end
@@ -116,7 +116,7 @@
116116
end
117117
f_ds = Cf_ds(end-size(f,1)+1:end,:);
118118

119-
if strcmpi(options.spatial_method,'constrained');
119+
if strcmpi(options.spatial_method,'constrained')
120120
if spatial_parallel % solve BPDN problem for each pixel
121121
Nthr = max(20*maxNumCompThreads,round(d*T/2^24));
122122
Nthr = min(Nthr,round(d/1e3));
@@ -177,7 +177,7 @@
177177
end
178178

179179
A(isnan(A))=0;
180-
A = sparse(A);
180+
A = sparse(double(A));
181181
A = threshold_components(A,options); % post-processing of components
182182

183183
fprintf('Updated spatial components \n');

update_temporal_components.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@
112112
if isempty(fin) || nargin < 5 % temporal background missing
113113
bk_pix = (sum(A,2)==0); % pixels with no active neurons
114114
if isempty(b) || nargin < 3
115-
fin = mean(Y(bk_pix,:));
116-
fin = fin/norm(fin);
117-
b = max(Y*fin',0);
115+
[b,fin] = fast_nmf(double(Y(bk_pix,:)),[],options.nb,50);
116+
% fin = mean(Y(bk_pix,:));
117+
% fin = fin/norm(fin);
118+
b = max(Y*fin',0);
118119
else
119120
fin = max(b(bk_pix,:)'*Y(bk_pix,:),0)/(b(bk_pix,:)'*b(bk_pix,:));
120121
end
@@ -124,9 +125,10 @@
124125
AY = mm_fun([A,double(b)],Y);
125126
bY = AY(size(A,2)+1:end,:);
126127
AY = AY(1:size(A,2),:);
128+
AA = sparse(double(A))'*sparse(double(A));
127129

128130
if isempty(Cin) || nargin < 4 % estimate temporal components if missing
129-
Cin = max((A'*A)\double(AY - (A'*b)*fin),0);
131+
Cin = max(AA\double(AY - (A'*b)*fin),0);
130132
ITER = max(ITER,3);
131133
end
132134

utilities/fast_nmf.m

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
H = H';
1111

1212
for iter = 1:max_iter
13+
if isempty(B)
14+
B = 1;
15+
end
1316
W = max((A*(B*H'))/(H*H'),0);
1417
H = max((W'*W)\((W'*A)*B),0);
1518
%disp(iter)
@@ -18,10 +21,14 @@
1821
function [W,H] = nnsvd(A,B,k)
1922

2023
mf = @(x,y) mat_vec(A,B,x,y);
21-
[U,S,V] = svds(mf,[size(A,1),size(B,2)],k);
24+
sz = [size(A,1), size(B,2)];
25+
if isempty(B)
26+
sz(2) = size(A,2);
27+
end
28+
[U,S,V] = svds(mf,sz,k);
2229

23-
W = zeros(size(A,1),k);
24-
H = zeros(size(B,2),k);
30+
W = zeros(sz(1),k);
31+
H = zeros(sz(2),k);
2532

2633
for j = 1:k
2734
x = U(:,j);
@@ -46,16 +53,29 @@
4653
W(:,j) = sqrt(S(j,j)*sigma)*u;
4754
H(:,j) = sqrt(S(j,j)*sigma)*v;
4855
end
49-
avg = mean(A,1)*mean(B,2);
56+
if isempty(B)
57+
avg = mean(A(:));
58+
else
59+
avg = mean(A,1)*mean(B,2);
60+
end
5061
W(W==0) = avg;
5162
H(H==0) = avg;
5263

5364
function y = mat_vec(A,B,x,method)
5465

5566
if strcmp(method,'notransp')
56-
y = A*(B*x);
67+
if isempty(B)
68+
y = A*x;
69+
else
70+
y = A*(B*x);
71+
end
5772
elseif strcmp(method,'transp')
58-
y = B'*(A'*x);
73+
if isempty(B)
74+
y = A'*x;
75+
else
76+
y = B'*(A'*x);
77+
78+
end
5979
end
6080
end
6181
end

0 commit comments

Comments
 (0)