-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkflow1.m
281 lines (273 loc) · 8.83 KB
/
workflow1.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
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
function workflow1(G,subs,opt)
params = opt.params;
if ~isfield(opt,'medianFiltSize')
opt.medianFiltSize = 10;
end
CompsC = conncomp(G,'OutputForm','cell');
A = G.adjacency;
A_ = tril(A,-1);
S = length(CompsC);
Y = cellfun(@length,CompsC);
validC = 1:size(Y,2);
[ia,ib]=sort(Y,'descend');
algorithmkeys = {'spb','dij','bel','spa'};
algorithm = 2;
debug_level = 0;
directed = 0;
W = [];
colscomp = jet(length(validC));
colscomp = colscomp(randperm(size(colscomp,1)),:);
SX = params.sx;
SY = params.sy;
SZ = params.sz;
voxres = [SX SY SZ]/2^(params.level)/1e3; % in um
params.voxres = voxres;
runtic = tic;
Eout = [];
iter = 0;
N = max(validC);
%%
poolobj = gcp('nocreate'); % If no pool, do not create new one.
if isempty(poolobj)
poolsize = 0;
parpool(feature('numcores'))
else
poolsize = poolobj.NumWorkers
end
skipthese = zeros(1,N);
skipthese(Y<=opt.sizethreshold) = 1;
%%
initialpass = 1;
acc=0;
if ~initialpass
parfor mC=validC%1:size(Y,2)%ib(2:500)%
if Y(mC)>opt.sizethreshold %& ismember(mC,validC) %& ~any(mC==skipThese)
%%
[aa,bb,cc] = fileparts(opt.inputh5);
bb_ = strsplit(bb,'_');
pre = bb_{end-1};%['-',bb(end-6:end-4)];
swcoutname = sprintf('auto%s',pre);
fragname = sprintf('%s_cc-%04d.swc',swcoutname,mC);
swcoutfolder = fullfile(opt.outfolder,'full');
outname = fullfile(swcoutfolder,fragname);
if exist(outname,'file')
skipthese(mC) = 1;
acc=acc+1;
end
else
skipthese(mC) = 1;
end
end
end
disp('done skip')
%%
try parfor_progress(0);catch;end
parfor_progress(max(validC))
parfor mC=validC
parfor_progress
% for each cluster run reconstruction
if skipthese(mC)
continue
end
%%
iter = mC;%iter+1;
subidx = CompsC{mC};%find(Comps==mC);
subs_ = subs(subidx,:); % get back to matlab image coordinates
nidx = length(subidx);
% get lower portion to make it directed
Asub = A_(subidx,subidx); % faster
%Gsub = G.subgraph(subidx);
leafs = find(sum(Asub,2)==0);%find(sum(max(Asub,Asub'))==1,1);
[eout] = graphfuncs.buildgraph(Asub,leafs(1));
inupdate.dA = sparse(eout(:,1),eout(:,2),1,nidx,nidx);
inupdate.D = ones(nidx,1);
inupdate.R = ones(nidx,1);
inupdate.X = subs_(:,1);
inupdate.Y = subs_(:,2);
inupdate.Z = subs_(:,3);
%%
deleteThese = NaN;
while length(deleteThese)
[inupdate, deleteThese] = prunTree(inupdate,opt.lengthThr,voxres);
if opt.viz
hold on
gplot3(inupdate.dA,[inupdate.X,inupdate.Y,inupdate.Z]);
drawnow
end
end
%%
% shuffle root to one of the leafs for efficiency and not
% splitting long stretches into half
if size(inupdate.dA,1)>1
[eoutprun] = graphfuncs.buildgraph(inupdate.dA);
nidx = max(eoutprun(:));
inupdate.dA = sparse(eoutprun(:,1),eoutprun(:,2),1,nidx,nidx);
else
end
if opt.viz
hold on
gplot3(inupdate.dA,[inupdate.X,inupdate.Y,inupdate.Z],'LineWidth',3);
drawnow
end
%%
if length(inupdate.dA)<opt.sizethreshold
continue
end
%%
[inupdate] = smoothtree(inupdate,opt);
%%
% [L,list] = getBranches(inupdate.dA);
if 0
outtree = inupdate;
else
% outtree_old = downSampleTree(inupdate,opt);
outtree = sampleTree(inupdate,opt);
end
if opt.viz
cla
gplot3(inupdate.dA,[inupdate.X,inupdate.Y,inupdate.Z],'LineWidth',3);
hold on
gplot3(outtree.dA,[outtree.X,outtree.Y,outtree.Z],'--','LineWidth',3);
drawnow
end
%%
Aout = outtree.dA;
nout = size(Aout,1);
XYZout = [outtree.X,outtree.Y,outtree.Z]-1;
Rout = outtree.R;
Dout = outtree.D;
% transform location
XYZout = pix2um(params,XYZout); % center anisotropy to compansate imresize
%
At = Aout+Aout';
[DISC,PRED,CLOSE] = graphtraverse(At,1,'Method','DFS');
At(1:end,:) = At(DISC,:);
At(:,1:end) = At(:,DISC);
XYZout = XYZout(DISC,:);
Rout = Rout(DISC,:);
Dout = Dout(DISC,:);
%%
if strcmp(version('-release'),'2015b')
[dist,pred] = graphalgs(algorithmkeys{algorithm},debug_level,directed,At,1);
else
[dist,path,pred] = graphshortestpath(At,1,'DIRECTED',false);
end
swcData = [[1:nout]' Dout XYZout Rout pred(:)];
swcData(1,7) = -1;
offset = min(swcData(:,3:5),[],1);
swcData(:,3:5) = swcData(:,3:5)-ones(size(swcData,1),1)*offset;
% check quadrant
quadid = checkQuant(opt.params.outsiz,median([outtree.X,outtree.Y,outtree.Z]));
quadid = 0;
%%
%% WRITE components
if opt.writefull
%%
if quadid
swcoutfolder = fullfile(opt.outfolder,'full',num2str(quadid));
else
swcoutfolder = fullfile(opt.outfolder,'full');
end
if 1
[aa,bb,cc] = fileparts(opt.inputh5);
bb_ = strsplit(bb,'_');
pre = bb_{end-1};%['-',bb(end-6:end-4)];
% [aa,bb,cc] = fileparts(opt.inputh5);
% pre = ['-',bb(end-6:end-4)];
else
pre = '';
end
swcoutname = sprintf('auto%s',pre);
fragname = sprintf('%s_cc-%04d.swc',swcoutname,mC);
outname = fullfile(swcoutfolder,fragname);
if ~exist(fileparts(outname),'dir')
mkdir(fileparts(outname))
end
% write swc
fid = fopen(outname,'w');
mytxt = sprintf('# Generated by pw skel algorithm\n');
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# OFFSET %.6f %.6f %.6f\n',offset);
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# COLOR %f,%f,%f\n',colscomp(iter,1),colscomp(iter,2),colscomp(iter,3));
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# NAME %s\n',fragname);
fprintf(fid,'%s',mytxt);
fprintf(fid,'%d %d %f %f %f %d %d\n',swcData');
fclose(fid);
end
%% WRITE fragments
if opt.writefrag
%%
%Aout = outtree.dA;
%nout = size(Aout,1);
XYZ = [outtree.X,outtree.Y,outtree.Z]-1; % -1 one is heuristic/bug
R = outtree.R;
D = outtree.D;
[L,list] = getBranches(outtree.dA);
% write to disk as swc file
cols = jet(length(L));
cols = cols(randperm(length(L)),:);
for ii=1:length(L)
%%
out = L(ii).set;
if isempty(out)
continue
else
%out(end) = [];
if isempty(out)
continue
end
end
%%
XYZout = XYZ(out,:);
Rout = R(out);
Dout = D(out);
% transform location
XYZout = pix2um(opt.params,XYZout); % center anisotropy to compansate imresize
%XYZout = pix2um(opt,[XYZout(:,1)*opt.aniscale(1) XYZout(:,2)*opt.aniscale(2) (XYZout(:,3)-1)*opt.aniscale(3)+1]); % center anisotropy to compansate imresize
nout = size(XYZout,1);
swcData = [[1:nout]' Dout XYZout Rout [0:nout-1]'];
swcData(1,7) = -1;
offset = mean(swcData(:,[3:5]),1);
%%
% write swc file
if 1
if 1
[aa,bb,cc] = fileparts(opt.inputh5);
bb_ = strsplit(bb,'_');
pre = bb_{end-1};%['-',bb(end-6:end-4)];
else
pre = '';
end
if quadid
swcoutfolder = fullfile(opt.outfolder,'frags',num2str(quadid));
else
swcoutfolder = fullfile(opt.outfolder,'frags');
end
swcoutname = sprintf('auto%s',pre);
%swcoutname = 'myskel';
fragname = sprintf('%s_cc-%04d_branch-%04d.swc',swcoutname,mC,ii);
outname = fullfile(swcoutfolder,fragname);
if ~exist(fileparts(outname),'dir')
mkdir(fileparts(outname))
end
swcData(:,3:5) = swcData(:,3:5)-ones(size(swcData,1),1)*offset;
% write swc
fid = fopen(outname,'w');
mytxt = sprintf('# Generated by pw skel algorithm\n');
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# OFFSET %.6f %.6f %.6f\n',offset);
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# COLOR %f,%f,%f\n',cols(ii,1),cols(ii,2),cols(ii,3));
fprintf(fid,'%s',mytxt);
mytxt = sprintf('# NAME %s\n',fragname);
fprintf(fid,'%s',mytxt);
fprintf(fid,'%d %d %f %f %f %d %d\n',swcData');
fclose(fid);
end
end
end
end
parfor_progress(0)
toc(runtic)