forked from MouseLightPipeline/skeletonize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit_bsub_jobs_for_skeletonization_from_paths.m
202 lines (184 loc) · 10.6 KB
/
submit_bsub_jobs_for_skeletonization_from_paths.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
function submit_bsub_jobs_for_skeletonization_from_paths(output_folder_path, whole_brain_p_map_h5_file_path, sizethreshold, probThr, fullh, ...
do_actually_submit, ...
maximum_running_slot_count)
% output_folder_path
% whole_brain_p_map_h5_file_path
% sizethreshold
% probThr
% fullh
core_count_for_each_bjob = 8;
p_map_dataset_path = '/prob0' ;
[full_stack_shape_ijk, has_ladenness_info, laden_octree_chunk_bounding_boxes_ijk0, h5_chunk_shape_ijk] = ...
h5parser_new(whole_brain_p_map_h5_file_path, p_map_dataset_path) ;
% get a multiple of h5_chunk_shape_ijk that is around 1000^3
analysis_chunk_shape_ijk = round(1000./h5_chunk_shape_ijk).*h5_chunk_shape_ijk;
% to get %10 overlap overhead use multiple of 10
fullh_for_create_ovelap_box = h5_chunk_shape_ijk ; % add 1 to make it odd (heuristic)
[~,whole_brain_p_map_h5_base_name,~] = fileparts(whole_brain_p_map_h5_file_path) ;
if ~exist(output_folder_path, 'dir') ,
mkdir(output_folder_path) ;
end
unix(sprintf('umask g+rxw %s',output_folder_path)) ;
unix(sprintf('chmod g+rwx %s',output_folder_path)) ;
% s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
%
% %find number of random characters to choose from
% numRands = length(s);
% sLength = 10;
% Construct a set of overlapped chunks for analysis
padded_analysis_chunk_bounding_boxes_ijk1 = createOverlapBox(full_stack_shape_ijk, analysis_chunk_shape_ijk, fullh_for_create_ovelap_box) ;
analysis_chunk_count = size(padded_analysis_chunk_bounding_boxes_ijk1, 1) ;
fprintf('Count of analysis chunks: %d\n', analysis_chunk_count) ;
% Figure out which of the analysis chunks overlap with the laden octree
% chunks, erring on the side of caution.
if has_ladenness_info ,
BBoxes = laden_octree_chunk_bounding_boxes_ijk0(:,[1 4 2 5 3 6])+1 ;
X = BBoxes(:,1:2);
Y = BBoxes(:,3:4);
Z = BBoxes(:,5:6);
laden_octree_chunk_bounding_box_corners_ijk1 = unique([X(:),Y(:),Z(:)],'rows') ;
padded_analysis_chunk_bounding_box_corners_ijk1 = ...
[ padded_analysis_chunk_bounding_boxes_ijk1(:,1:2:end) ; ...
padded_analysis_chunk_bounding_boxes_ijk1(:,2:2:end) ] ;
is_padded_analysis_chunk_bounding_box_corner_in_laden_hull = ...
inhull(padded_analysis_chunk_bounding_box_corners_ijk1, ...
laden_octree_chunk_bounding_box_corners_ijk1) ;
is_analysis_chunk_laden = any(reshape(is_padded_analysis_chunk_bounding_box_corner_in_laden_hull, [analysis_chunk_count 2]),2) ;
else
is_analysis_chunk_laden = true(analysis_chunk_count, 1) ;
end
laden_analysis_chunk_count = sum(is_analysis_chunk_laden) ;
fprintf('Count of laden analysis chunks: %d\n', laden_analysis_chunk_count) ;
% Figure out which ones have already been analysed
is_analysis_chunk_already_skeletonized = false(analysis_chunk_count, 1) ;
output_text_file_template_path = fullfile(output_folder_path, '*.txt') ;
extant_output_text_file_names = simple_dir(output_text_file_template_path) ;
for ii = 1:length(extant_output_text_file_names) ,
extant_output_text_file_name = extant_output_text_file_names{ii} ;
bbox_index = bounding_box_index_from_file_name(extant_output_text_file_name) ;
is_analysis_chunk_already_skeletonized(bbox_index) = true ;
end
fprintf('Count of already-skeletonized analysis chunks: %d\n', sum(is_analysis_chunk_already_skeletonized)) ;
% Determine which chunks need to be skeletonized
does_analysis_chunk_need_to_be_run = is_analysis_chunk_laden & ~is_analysis_chunk_already_skeletonized ;
fprintf('Count of analysis chunks to be run: %d\n', sum(does_analysis_chunk_need_to_be_run)) ;
% Submit the jobs
time_limit_in_seconds = 10*60 ;
bsub_options = sprintf('-P mouselight -We %s', num2str(time_limit_in_seconds/60)) ;
bqueue = bqueue_type(do_actually_submit, bsub_options, core_count_for_each_bjob, maximum_running_slot_count) ;
jobs_submitted_count = 0 ;
batch_function = @cluster_skelh5 ;
for analysis_chunk_index = 1:analysis_chunk_count ,
if ~does_analysis_chunk_need_to_be_run(analysis_chunk_index) , % skip
continue
end
padded_analysis_chunk_bounding_box_ijk1 = padded_analysis_chunk_bounding_boxes_ijk1(analysis_chunk_index, :) ;
output_file_name = sprintf('%s_idx-%05d_stxyzendxyz-%d_%d_%d_%d_%d_%d.txt', ...
whole_brain_p_map_h5_base_name, ...
analysis_chunk_index, ...
padded_analysis_chunk_bounding_box_ijk1(1:2:end), ...
padded_analysis_chunk_bounding_box_ijk1(2:2:end)) ;
output_file_path = fullfile(output_folder_path, output_file_name) ;
bqueue.enqueue(batch_function, ...
whole_brain_p_map_h5_file_path, ...
p_map_dataset_path, ...
padded_analysis_chunk_bounding_box_ijk1, ...
output_file_path, ...
sizethreshold, ...
probThr, ...
fullh) ;
% random_string_for_job_name = s( ceil(rand(1,sLength)*numRands) ) ;
% job_name = sprintf('skel-%05d-%s', analysis_chunk_index, random_string_for_job_name) ;
% matlab_command_template = ...
% 'try; modpath; cluster_skelh5(''%s'', ''%s'', %s, ''%s'', %.18g, %.18g, %.18g); catch err; fprintf(2, ''%%s\\n'', err.getReport()); quit(1); end; quit(0);' ;
% matlab_command = sprintf(matlab_command_template, ...
% whole_brain_p_map_h5_file_path, ...
% p_map_dataset_path, ...
% mat2str(padded_analysis_chunk_bounding_box_ijk1), ...
% output_file_path, ...
% sizethreshold, ...
% probThr, ...
% fullh) ;
% matlab_command_line_as_tokens = { '/misc/local/matlab-2018b/bin/matlab', '-nodisplay', '-r', matlab_command } ;
% stdout_file_name = sprintf('%s.out.txt', job_name) ;
% stderr_file_name = sprintf('%s.out.txt', job_name) ;
% bsub_command_line_as_tokens = { 'bsub', ...
% '-P', 'mouselight', ...
% '-n', num2str(core_count_for_each_bjob), ...
% '-We', num2str(time_limit_in_seconds/60), ...
% '-J', job_name, ...
% '-oo', stdout_file_name, ...
% '-eo', stderr_file_name, ...
% matlab_command_line_as_tokens } ;
% bsub_command_line_as_string = bash_command_line_from_token_list(bsub_command_line_as_tokens) ;
% fprintf('%s\n', bsub_command_line_as_string) ;
% return_code = system(bsub_command_line_as_string) ; % submit the job!
% if return_code ~= 0 ,
% fprintf('There was a problem with the submission of the job to create file %s\n', output_file_path) ;
% end
jobs_submitted_count = jobs_submitted_count + 1 ;
%maybe_fprint_dot(false, jobs_submitted_count) ;
end
%maybe_fprint_dot(true, jobs_submitted_count) ; % fprintf() final newline
%fclose(fid) ;
%unix(sprintf('chmod +x %s',bsub_script_file_path));
fprintf('%d jobs ''submitted''\n', jobs_submitted_count) ;
job_statuses = bqueue.run() ;
if all(job_statuses==1) ,
fprintf('All %d %s() batch jobs completed without errors.\n', bqueue.queue_length(), func2str(batch_function)) ;
else
fprintf('All %d %s() batch jobs exited, but some had errors.\n', bqueue.queue_length(), func2str(batch_function)) ;
had_error = (job_statuses==-1) ;
job_ids = bqueue.job_ids ;
bad_job_ids = job_ids(had_error) ;
fprintf('Job ids with errors: %s\n', mat2str(bad_job_ids)) ;
end
% Check that the number of output files matches laden_analysis_chunk_count
is_analysis_chunk_already_skeletonized = false(analysis_chunk_count, 1) ;
output_text_file_template_path = fullfile(output_folder_path, '*.txt') ;
extant_output_text_file_names = simple_dir(output_text_file_template_path) ;
for ii = 1:length(extant_output_text_file_names) ,
extant_output_text_file_name = extant_output_text_file_names{ii} ;
bbox_index = bounding_box_index_from_file_name(extant_output_text_file_name) ;
is_analysis_chunk_already_skeletonized(bbox_index) = true ;
end
already_analyzed_laden_analysis_chunk_count = sum(is_analysis_chunk_already_skeletonized) ;
if already_analyzed_laden_analysis_chunk_count == laden_analysis_chunk_count ,
fprintf('\nSuccess!\n') ;
fprintf('The count of analyzed analysis chunks is %d, which matches the laden analysis chunk count.\n', already_analyzed_laden_analysis_chunk_count) ;
else
fprintf('\nFailure!\n') ;
fprintf('The count of analyzed analysis chunks is %d, but the laden analysis chunk count is %d.\n', ...
already_analyzed_laden_analysis_chunk_count, laden_analysis_chunk_count) ;
end
end
% function maybe_fprint_dot(is_done, count)
% if is_done ,
% % print a newline, but only if with didn't just print a newline for
% % the final iteration
% if ~mod(count, 100) ,
% fprintf('\n') ;
% end
% else
% fprintf('.') ;
% if mod(count, 100) ,
% fprintf('\n') ;
% end
% end
% end
% function result = determine_bsub_script_file_path(output_folder_path)
% did_determine_result = false ;
% max_round_count = 20 ;
% for round = 1:max_round_count ,
% putative_bsub_script_file_name = sprintf('submit_skeletonization_jobs_round_%02d.sh', round) ;
% putative_bsub_script_file_path = fullfile(output_folder_path, putative_bsub_script_file_name) ;
% if ~exist(putative_bsub_script_file_path, 'file') ,
% did_determine_result = true ;
% result = putative_bsub_script_file_path ;
% break
% end
% end
% if ~did_determine_result ,
% error('You''ve already done %d rounds of skeletonization. Maybe something is wrong?', max_round_count) ;
% end
% end