|
1 | | -function [Ain, Cin, bin, fin, center] = initialize_components_3d(Y, K, tau, options) |
| 1 | +function [Ain, Cin, bin, fin, center] = initialize_components_2d(Y, K, tau, options) |
2 | 2 |
|
3 | 3 | % Initalize components using a greedy approach followed by hierarchical |
4 | 4 | % alternative least squares (HALS) NMF. Optional use of spatio-temporal |
|
51 | 51 | if ~isfield(options, 'tsub'), options.tsub = 1; end; tsub = options.tsub; |
52 | 52 | if tsub == 1; fprintf('No temporal downsampling is performed. Consider temporal downsampling if the recording is very long. \n'); end |
53 | 53 |
|
54 | | -ndimsY = ndims(Y)-1; |
55 | | -sY = size(Y); |
56 | | -d = sY(1:ndimsY); |
57 | | -T = sY(end); |
58 | | - |
59 | | -ds = d; |
60 | | -ds(1:2) = ceil(d(1:2)/ssub); % do not subsample along z axis |
61 | | -%d1s = ceil(d1/ssub); %size of downsampled image |
62 | | -%d2s = ceil(d2/ssub); |
| 54 | +[d1,d2,T] = size(Y); |
| 55 | +d1s = ceil(d1/ssub); %size of downsampled image |
| 56 | +d2s = ceil(d2/ssub); |
63 | 57 | Ts = floor(T/tsub); %reduced number of frames |
64 | 58 | % spatial downsampling |
65 | | -fprintf('starting resampling \n') |
66 | | -if ssub~=1; |
67 | | - if ndimsY == 2; Y_ds = imresize(Y, [ds(1), ds(2)], 'box'); end |
68 | | - if ndimsY == 3; |
69 | | - Y_ds = zeros([ds(1:2),T,ds(end)]); |
70 | | - for z = 1:ds(3) |
71 | | - Y_ds(:,:,:,z) = imresize(squeeze(Y(:,:,z,:)), [ds(1), ds(2)], 'box'); |
72 | | - end |
73 | | - Y_ds = permute(Y_ds,[1,2,4,3]); |
74 | | - end |
75 | | -else |
76 | | - Y_ds = Y; |
77 | | -end |
| 59 | +if ssub~=1, Y_ds = imresize(Y, [d1s, d2s], 'box'); else Y_ds = Y; end |
78 | 60 | % temporal downsampling |
79 | 61 | if tsub~=1 |
80 | | - if ndimsY == 2; Y_ds = squeeze(mean(reshape(Y_ds(:, :, 1:(Ts*tsub)),ds(1), ds(2), tsub, Ts), 3)); end |
81 | | - if ndimsY == 3; Y_ds = squeeze(mean(reshape(Y_ds(:, :, :, 1:(Ts*tsub)),ds(1), ds(2), ds(3), tsub, Ts), 4)); end |
| 62 | + Y_ds = squeeze(mean(reshape(Y_ds(:, :, 1:(Ts*tsub)),d1s, d2s, tsub, Ts), 3)); |
82 | 63 | end |
83 | | - |
| 64 | +options_ds = options; |
| 65 | +options_ds.d1 = d1s; |
| 66 | +options_ds.d2 = d2s; |
84 | 67 | if strcmpi(options.init_method,'greedy') |
85 | 68 | % run greedy method |
86 | 69 | fprintf('Initializing components with greedy method \n'); |
87 | | - if ndimsY == 2; [Ain, Cin, bin, fin] = greedyROI(Y_ds, K, options); end |
88 | | - if ndimsY == 3; [Ain, Cin, bin, fin] = greedyROI(Y_ds, K, options); end %, gSig, gSiz); end |
| 70 | + [Ain, Cin, bin, fin] = greedyROI2d(Y_ds, K, options_ds); |
89 | 71 | elseif strcmpi(options.init_method,'sparse_NMF') |
90 | 72 | % run sparse_NMF method |
91 | 73 | fprintf('Initializing components with sparse NMF \n'); |
92 | | - [Ain,Cin,bin,fin] = sparse_NMF_initialization(Y_ds,K,options); |
| 74 | + [Ain,Cin,bin,fin] = sparse_NMF_initialization(Y_ds,K,options_ds); |
93 | 75 | else |
94 | 76 | error('Unknown initialization method') |
95 | 77 | end |
96 | 78 |
|
97 | 79 | % refine with HALS |
98 | | -fprintf('Refining initial estimates with HALS...'); |
99 | | -%[Ain1, Cin1, bin1, fin1] = HALS_2d(Y_ds, full(Ain), Cin, bin, fin, options); |
100 | | -[Ain, Cin, bin, fin] = HALS(Y_ds, full(Ain), Cin, bin, fin, options); |
101 | | -fprintf(' done \n'); |
| 80 | +fprintf('Refining initial estimates with HALS \n'); |
| 81 | +[Ain, Cin, bin, fin] = HALS_2d(Y_ds, full(Ain), Cin, bin, fin, options); |
| 82 | + |
102 | 83 | %% upsample Ain, Cin, bin, fin |
103 | | -if ndimsY == 2; center = ssub*com(Ain,ds(1),ds(2)); else center = ssub*com(Ain,ds(1),ds(2),ds(3)); end |
104 | | -%Ain = imresize(reshape(Ain, ds(1), ds(2), sum(K)), d); |
105 | | -%Ain = imresize(reshape(full(Ain), [ds, sum(K)]), d); |
106 | | -Ain = imresize(reshape(full(Ain), [ds(1),ds(2), sum(K)*prod(ds)/ds(1)/ds(2)]),[d(1),d(2)]); %,prod(d)/d(1)/d(2)*sum(K)]); |
107 | | -Ain = sparse(reshape(Ain, prod(d), [])); |
108 | | -%bin_temp = reshape(bin, ds(1), ds(2), options.nb); |
109 | | -%bin = zeros(d(1),d(2),options.nb); |
110 | | -bin = imresize(reshape(bin,[ds(1),ds(2), options.nb*prod(ds)/ds(1)/ds(2)]),[d(1),d(2)]); |
111 | | -bin = reshape(bin,prod(d),[]); |
112 | | -% bin_temp = reshape(bin, [ds, options.nb]); |
113 | | -% bin = zeros([d,options.nb]); |
114 | | -% for i = 1:options.nb |
115 | | -% bin(:,:,i) = imresize(bin_temp(:,:,i), d); |
116 | | -% end |
117 | | -%bin = reshape(bin, [], options.nb); |
| 84 | +center = ssub*com(Ain,d1s,d2s); |
| 85 | +Ain = imresize(reshape(Ain, d1s, d2s, sum(K)), [d1, d2]); |
| 86 | +Ain = reshape(Ain, d1*d2, []); |
| 87 | +bin_temp = reshape(bin, d1s, d2s, options.nb); |
| 88 | +bin = zeros(d1,d2,options.nb); |
| 89 | +for i = 1:options.nb |
| 90 | + bin(:,:,i) = imresize(bin_temp(:,:,i), [d1, d2]); |
| 91 | +end |
| 92 | +bin = reshape(bin, [], options.nb); |
118 | 93 | Cin = imresize(Cin, [sum(K), Ts*tsub]); |
119 | 94 | fin = imresize(fin, [options.nb, Ts*tsub]); |
120 | 95 | if T ~= Ts*tsub |
|
0 commit comments