-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cluster3DMC.m
236 lines (187 loc) · 6.46 KB
/
Cluster3DMC.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
function Cluster3DMC(directory,loadStart,numToLoad,rect,basename,useRed,useDepths,parallelDepths)
% This code will motion correct and do pixel CI for all of the image stacks
% located in the specified directory
%
% Directory - is the path to all of the files to be motion corrected
% loadStart - Which file to begin the loading on (set to 1 for all
% files)
% numToLoad - Number of files to Motion Correct
% rect - (optional) rectangle to exclude edges from motion
% correction. defaults to [1 1 511 511]
% basename - (optional) basename that all Motion Corrected Files must
% match
% useRed - (optional) align using red channel.
% useDepths - (optional) array of depths to be processed this run.
% defaults to all.
% parallelDepths - (optional) different form of parallelization that
% seperates each depth. In practice doesn't save much
% time as the load steps are the slow ones.
%if dir is selected, make sure you provide a direcotry
if exist(directory)~=7
errordlg('Please provide directory')
end;
if nargin<4
rect=[1 1 511 511];
basename ='';
parallelDepths =0;
useRed =0;
useDepths=0;
elseif nargin<5
basename ='';
parallelDepths =0;
useRed =0;
useDepths=0;
elseif nargin<6
useRed =0;
parallelDepths=0;
useDepths=0;
elseif nargin <7
useDepths=0;
parallelDepths=0;
elseif nargin <8
parallelDepths=0;
end
if isempty(basename) || ~ischar(basename)
basename='';
end
verboseFlag = 0;
%% get all files in dir
fprintf('Identifying Files... \n');
k=dir(directory);k(1:2)=[];
i=1;
for n=1:(numel(k))
fn=k(n).name;
if regexp(fn,regexptranslate('wildcard',[basename '*.tif']))
filenames{i}=fullfile(directory, k(n).name);
i=i+1;
end;
end;
fprintf([ num2str(numel(filenames)) ' Files Detected... \n']);
%% Extract MetaData
s = imfinfo(filenames{1});
header = s(1).Software;
MD = parseSI5Header(header);
%% set rectangle
try
nDepths = MD.hFastZ.numFramesPerVolume;
if isempty(nDepths)
nDepths = 1;
end
%if useDepths~=0;
% nDepths=numel(useDepths);
%end
catch
nDepths = 1;
end
if useDepths == 0
useDepths = 1:nDepths;
end
nDepths=numel(useDepths);
fprintf([num2str(nDepths) ' depths requested: [' num2str(useDepths) ']\n']);
if ~exist('rect');
rect=[1 1 511 511];
rect=repmat(rect,[nDepths 1]);
disp('no rectangle, using whole image')
elseif size(rect,2) ~=4
rect=[1 1 511 511];
rect=repmat(rect,[nDepths 1]);
disp('rect did not have 4 elements, using whole image')
elseif size(rect,1) ~= nDepths
rect=repmat(rect(1,:),[nDepths 1]);
disp('rect only included for one depth, appying to all')
end;
for n=1:nDepths
wiTemp=rect(n,1):rect(n,1)+rect(n,3);
hiTemp=rect(n,2):rect(n,2)+rect(n,4);
%check Range Errors
wiTemp(wiTemp>=511)=[];
wiTemp(wiTemp<=1)=[];
hiTemp(hiTemp>=511)=[];
hiTemp(hiTemp<=1)=[];
wi{n}=wiTemp;
hi{n}=hiTemp;
end
%% check and see if .align file exists already in dir
% if not, run MC and possibly CI
nn = loadStart:(numToLoad+loadStart-1);
f=filenames(nn);
k=f{1}; k(length(k)-6:length(k))=[];
outputBase=[k num2str(loadStart) '-' num2str(numToLoad+loadStart-1)];
useTheseFiles=filenames(nn)';
% parallelDepths =0;
if parallelDepths
nc = feature('numcores'); %number of cores
availCores = nc -nDepths; %each depth takes a core to launch
minCore = floor(availCores/nDepths);
jobCores = ones(nDepths,1)*minCore;
remCore = rem(availCores,nDepths);
for i=1:remCore %distribute unused cores across jobs
jobCores(i)=jobCores(i)+1;
end
jobCores=max(jobCores,1);
disp([num2str(availCores) ' cores available. divided as: ' num2str(jobCores')]);
for depth=useDepths
disp(['Launching Batch: ' num2str(depth)]);
j(depth) = batch(['sbxAlignOneDepth(outputBase,useTheseFiles,hi,wi,MD,' num2str(depth) ',useRed)'],...
'AttachedFiles', 'sbxAlignOneDepth.m',...
'Pool',jobCores(depth));
end
%wait for jobs to end
for i=1:numel(j)
jTime = tic;
disp(['Waiting for Job ' num2str(i) ' (others might still be running)']);
wait(j(i));
disp(['Depth ' num2str(i) ' took ' num2str(toc(jTime)) 's additional']);
if verboseFlag
disp(['Diary for Depth ' num2str(i) ': ']);
diary(j(i));
end
end
delete(j);
clear j;
for depth=useDepths
OPB=[outputBase '_depth_' num2str(i)];
fprintf('Saving Files\n');
save([OPB '.align'],'outputBase','-append');
save([OPB '.align'],'useTheseFiles','-append');
save([OPB '.align'],'loadStart','-append');
save([OPB '.align'],'numToLoad','-append');
save([OPB '.align'],'hi','wi','-append');
try
save([OPB '.align'],'rect','-append');
end
end;
disp('jobs completed');
else
poolTime = tic;
nc = feature('numcores'); %number of cores
disp([num2str(nc) ' cores available.']);
% try
% parpool(nc);
% catch
% fprintf('Could not launch the desired number of cores switching to default\n')
% parpool
% end
fprintf('Launching pool with default number of cores\n')
parpool
fprintf(['Launching parpool took ' num2str(toc(poolTime)) 's\n']);
for depth=useDepths
%thisDepth=useDepths(depth);
dTime=tic;
disp(['Starting Depth: ' num2str(depth)]);
sbxAlignOneDepth(outputBase,useTheseFiles,hi,wi,MD,depth,useRed)
disp(['Depth ' num2str(depth) ' took ' num2str(toc(dTime)) 's']);
OPB=[outputBase '_depth_' num2str(depth)];
fprintf('Saving Files\n');
save([OPB '.align'],'outputBase','-append');
save([OPB '.align'],'useTheseFiles','-append');
save([OPB '.align'],'loadStart','-append');
save([OPB '.align'],'numToLoad','-append');
save([OPB '.align'],'hi','wi','-append');
try
save([OPB '.align'],'rect','-append');
end
end
disp('job completed');
end
end