This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_rings_master_OAR.m
472 lines (297 loc) · 13.9 KB
/
stack_rings_master_OAR.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
% macro to select the optimal parameters for the ring correction before
% using the compiled parallelized version
% argument: number of the slice for the test
% origin Paul Tafforeau ESRF 2009
function stack_rings_master_OAR(test_slice,varargin)
%% THE PARAMETERS
median_size = 200 ; % maximum size of the rings that will be corrected
number_of_pass = 3 ; % set to the desired number of pass of the whole filter
structure_removal_level = 0.07 ; % threshold value to remove the samples structures before the final rings calculation, best 0.1, set to 0 to desactivate
blur_angle = 70 ; % minimum angular length of the rings
fusion_angle = 70 ;% angular value for the fusion of the two picture parts in case of 180 degrees correction
strong_rings = 5 ; % give the filter size for direct removal of full circles
rotate_slices = 0 ; % rotation factor in case of reconstructions with angular offset for the 180 degres algorithm.2
double_polar_corr = 1 ; % set to 1 in case of highly structured sample creating more rings than before the correction if nothing else works
%% for parallelisation
slices_per_job = 10 ; % number of slices processed by each job for automatic determination of the number of jobs
start_now = true ; % put true to start the processing directly after parameters acceptation of false to create only the cmd file
first = 1 ;
last = 0 ; % set to 0 to process up to the last slice
walltime = '10:00:00'
previous_result_overwrite = 0 ; % 0, skipping files already in the result directory, 1 reprocessing everything
%% expert settings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
high_contrast_cut = 000 ; % indicate the maximum deviation for contrast around the median of the slice. set to 0 to desactivate
amedian = 0000 ; % median level of the slice, to be manually selected in case of high contrast exclusion needed
%% resources parameters
gpu_cpu = 'all_core_BE';
warning ('off')
if test_slice>0
test=1;
else
test=0;
end
warning ('off')
secure_process = 1 ; % automatic stop and restart in case of problem
waiting_time = 120 ; % how long (in minutes) the system will wait before restarting the job submission
number_of_trials = 1 ; % in case of blocked jobs, how many times will be the system resubmitted before exiting of the loop
%% using the whole range for contrast determination during the correction
%define original data type
djp2=dir('*.jp2');
dtif=dir('*.tif');
njp2=size(djp2,1);
ntif=size(dtif,1);
if njp2>ntif
file_type='jp2';
NOF=njp2;
d=djp2;
else
file_type='tif'
NOF=ntif
d=dtif;
end
disp(sprintf('I found %1.0i slices to process in this directory',NOF));
fname={d.name};
scan_dir=cleandirectoryname(pwd)
switch file_type
case 'jp2'
pos2=findstr(scan_dir,'jpeg2000-');
compression_factor=str2num(scan_dir(pos2+9:pos2+10));
if isempty (compression_factor)
pos2=findstr(scan_dir,'jp2-');
compression_factor=str2num(scan_dir(pos2+4:pos2+5));
end
if isempty (compression_factor)
disp('it was not possible to find the compression factor, I use 10 by default')
compression_factor=10;
else
fprintf('saving type compression factor: %1.0f\n',compression_factor);
end
if compression_factor<1
disp('error in compression_factor, I force it to 10');
compression_factor=10;
end
if compression_factor>20
disp('error in compression_factor, I force it to 10');
compression_factor=10;
end
otherwise
compression_factor=0;
end
%testing image class
testa=imread(fname{1});
class(testa)
switch class (testa)
case 'uint16'
amin=0
amax=65635
case 'single'
amin=min2(testa);
amax=max2(testa);
case 'double'
amin=min2(testa);
amax=max2(testa);
case 'uint8'
amin=0
amax=255
end
%%
if test_slice>0
test=1;
imname=sprintf(fname{test_slice});
im=imread(imname);
remove_rings_OAR_dev(im,median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,test,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut);
toc
start_processing=inputwdefault('do you want to start processing of multiple slices using parallel version ? ', 'n');
if (start_processing == 'n')|(start_processing == 'no');
return
end
end
%% determination of the number of slices to process
if last==0
last=NOF;
end
directory=cleandirectoryname(pwd);
number_of_jobs=ceil((last-first+1)/slices_per_job)
if previous_result_overwrite==1
restart_flag=0
else
restart_flag=1
end
param_string=sprintf('%1.0i %1.0i %4.4f %1.0f %1.0f %1.0f %1.2f %1.2f %1.2f %1.0f %1.0f %1.0f %s %1.0f %1.0f',median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut,file_type,compression_factor,restart_flag);
do_OAR_id19_2017('stack_rings_slave_OAR_dev',first,last,number_of_jobs,directory,start_now,gpu_cpu,walltime,param_string);
%% waiting loop for the rings correction
resultdir=[directory 'RC_'];
mkdir (resultdir)
disp ('the directory for the result files already existed or has been created')
%cd (resultdir)
waiting=1;
prec_treated=0;
disp('the survey of the number of files will be updated every minute, do not take into account the first measurement')
if secure_process
non_running_time=0;
job_restart=0;
end
while waiting==1
switch file_type
case 'jp2'
d=dir([resultdir '/*.jp2']);
case 'tif'
d=dir([resultdir '/*.tif']);
end
number_of_files=size(d,1);
if number_of_files<last
waiting=1;
pause(60)
treated=number_of_files-prec_treated;
remaining_time=((last-number_of_files)/treated)*60;
if secure_process
if treated==0
non_running_time=non_running_time+1;
disp('it seems that nothing new happened during the last minute, I am waiting before restarting the submission')
else
non_running_time=0;
end
if non_running_time==waiting_time
job_restart=job_restart+1;
if job_restart>number_of_trials
disp('it seems impossible to do the processing, I stop the waiting loop, please investigate the problem more in details')
return
end
disp('some of the jobs are not starting, I resubmit all of them');
restart_flag=1
param_string=sprintf('%1.0i %1.0i %4.4f %1.0f %1.0f %1.0f %1.2f %1.2f %1.2f %1.0f %1.0f %1.0f %s %1.0f %1.0f',median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut,file_type,compression_factor,restart_flag);
do_OAR_id19_2017('stack_rings_slave_OAR_dev',first,last,number_of_jobs,directory,start_now,gpu_cpu,walltime,param_string);
cd (resultdir)
non_running_time=0;
end
end
if remaining_time>3600
remaining_time=remaining_time/3600;
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.1f hours ',treated,number_of_files, last,remaining_time));
else
if remaining_time<60
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.0f seconds ',treated,number_of_files,last,remaining_time));
else
remaining_time=remaining_time/60;
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.1f minutes ',treated,number_of_files,last,remaining_time));
end
end
prec_treated=number_of_files;
else
waiting=0;
break
% ! oardel_myjobs
end
end
%%
disp('checking integrity of the reconstructed files, to be done in a clever way with Vincent')
[ProcessDone,FileError]=stack_checker(resultdir,file_type,number_of_files)
if FileError>0
restart_flag=1
param_string=sprintf('%1.0i %1.0i %4.4f %1.0f %1.0f %1.0f %1.2f %1.2f %1.2f %1.0f %1.0f %1.0f %s %1.0f %1.0f',median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut,file_type,compression_factor,restart_flag);
do_OAR_id19_2017('stack_rings_slave_OAR_dev',first,last,number_of_jobs,directory,start_now,gpu_cpu,walltime,param_string);
%% waiting loop for the rings correction
resultdir=[directory 'RC_'];
mkdir (resultdir)
disp ('the directory for the result files already existed or has been created')
%cd (resultdir)
waiting=1;
prec_treated=0;
disp('the survey of the number of files will be updated every minute, do not take into account the first measurement')
if secure_process
non_running_time=0;
job_restart=0;
end
while waiting==1
switch file_type
case 'jp2'
d=dir([resultdir '/*.jp2']);
case 'tif'
d=dir([resultdir '/*.tif']);
end
number_of_files=size(d,1);
if number_of_files<last
waiting=1;
pause(60)
treated=number_of_files-prec_treated;
remaining_time=((last-number_of_files)/treated)*60;
if secure_process
if treated==0
non_running_time=non_running_time+1;
disp('it seems that nothing new happened during the last minute, I am waiting before restarting the submission')
else
non_running_time=0;
end
if non_running_time==waiting_time
job_restart=job_restart+1;
if job_restart>number_of_trials
disp('it seems impossible to do the processing, I stop the waiting loop, please investigate the problem more in details')
return
end
disp('some of the jobs are not starting, I resubmit all of them');
restart_flag=1
param_string=sprintf('%1.0i %1.0i %4.4f %1.0f %1.0f %1.0f %1.2f %1.2f %1.2f %1.0f %1.0f %1.0f %s %1.0f %1.0f',median_size,number_of_pass,structure_removal_level,blur_angle,fusion_angle,rotate_slices,amin,amax,amedian,strong_rings,double_polar_corr,high_contrast_cut,file_type,compression_factor,restart_flag);
do_OAR_id19_2017('stack_rings_slave_OAR_dev',first,last,number_of_jobs,directory,start_now,gpu_cpu,walltime,param_string);
cd (resultdir)
non_running_time=0;
end
end
if remaining_time>3600
remaining_time=remaining_time/3600;
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.1f hours ',treated,number_of_files, last,remaining_time));
else
if remaining_time<60
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.0f seconds ',treated,number_of_files,last,remaining_time));
else
remaining_time=remaining_time/60;
disp(sprintf('%1.0f files have been treated in one minute, a total of %1.0f files on %1.0f have been treated, it should finish in about %1.1f minutes ',treated,number_of_files,last,remaining_time));
end
end
prec_treated=number_of_files;
else
waiting=0;
break
% ! oardel_myjobs
end
end
end
disp('checking integrity of the reconstructed files, in case of problem, go for manual correction')
[ProcessDone,FileError]=stack_checker(resultdir,file_type,number_of_files)
disp('the rings correction of the whole stack of slices is finished')
%% adding reconstruction parameters to the reconstruction_log.info file
% writting info file
info=dir('*info');
infoname={info.name};
number_of_info=size(info,1);
fprintf('found %1.0f info files\n',number_of_info);
file=[];
hd=[];
for ii=1:number_of_info
info_to_read=infoname{ii};
fprintf('reading %s\n',info_to_read);
fp=fopen(info_to_read,'r');
if fp ~= -1
hd=fscanf(fp,'%c');
else
fprintf('cannot read %s\n',info_to_read);
hd=[];
end
file=[file sprintf('\nprevious info from pyhst\n------------------------\n')];
file=[file hd];
end
fprintf('adding current program parameters\n')
file =[file sprintf('\rings_dev\n------------------------\n')];
file =[file sprintf('median_size=%1.0f\n ',median_size)];
file =[file sprintf('number_of_pass=%1.0f\n ',number_of_pass)];
file =[file sprintf('structure_removal_level=%1.2f\n ',structure_removal_level)];
file =[file sprintf('blur_angle=%1.0f\n ',blur_angle)];
file =[file sprintf('fusion_angle=%1.0f\n ',fusion_angle)];
file =[file sprintf('strong_rings=%1.0f\n ',strong_rings)];
file =[file sprintf('rotate_slices=%1.0f\n ',rotate_slices)];
file =[file sprintf('double_polar_corr=%1.0f\n ',double_polar_corr)];
logname=sprintf('%s/reconstruction_log.info',resultdir);
fid=fopen(logname,'a+');
fwrite(fid,file,'uchar');
fclose(fid);
%%
end