-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_motion_correct_queued.m
132 lines (122 loc) · 3.92 KB
/
batch_motion_correct_queued.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
function batch_motion_correct_queued(fn_list,target,save_path,align_ch,save_ch, n_sum, n_sum_align,opt,to_queue,ffn_ROI)
if(isempty(fn_list))
warning('No files selected.')
return;
end
if(ischar(fn_list))
if(isdir(fn_list))
[~, fn_list] = fastdir(fn_list,common_regexp('tiff_ext'));
else
fn_list = {fn_list};
end
end
if(nargin<2)
target = [];
end
if(nargin<3||isempty(save_path))
warning('Save path is not set! Doing nothing.')
return;
end
if(nargin<4 || isempty(align_ch))
align_ch = -1; % last channel
end
if(nargin<5 || isempty(save_ch))
save_ch = 1;
end
if(nargin<6||isempty(n_sum))
n_sum = 50;
end
if(nargin<7||isempty(n_sum_align))
n_sum_align = inf;
end
if(nargin<8||isempty(opt))
opt='';
end
if(nargin<9||isempty(to_queue))
to_queue = any(1==strfind(mfilename('fullpath'),'/usr/local/lab/People/Aki/ForLabMembers/'));
end
if(nargin<10||isempty(ffn_ROI))
ffn_ROI = [];
end
L = Logger;
[~,fn_root,~] = fileparts(fn_list{1});
target_save_path=fullfile(save_path,'target');
target_fn = fullfile(target_save_path,[fn_root '_AVG.tif']);
if(~exists_file(target_fn))
if(isempty(target))
L.newline('Making target from %s', fn_list{1});
target = make_template_from_file(fn_list{1},align_ch,[],false);
end
target=parse_image_input(target,align_ch);
if(~exists_file(save_path))
L.newline('Make save dir');
mkdir(save_path);
end
if(~exists_file(target_save_path))
mkdir(target_save_path);
end
write_tiff(target_fn,int16(target));
end
queued_job_ids = {};
processed = false;
L.newline('Queueing jobs');
if(to_queue)
for i = 1:length(fn_list)
fn = fn_list{i};
if(strcmp(opt,'f')||strcmp(opt,'force')||~motion_correct(fn,[],save_path,align_ch,save_ch, n_sum, n_sum_align))
queued_job_ids{end+1}=queue_job('motion_correct',{fn,target_fn,save_path,align_ch,save_ch, n_sum, n_sum_align});
processed = true;
else
disp('skipping because already motion corrected.');
end
end
else
to_process=false(length(fn_list),1);
for i = 1:length(fn_list)
fn = fn_list{i};
if(strcmp(opt,'f')||strcmp(opt,'force')||~motion_correct(fn,[],save_path,align_ch,save_ch, n_sum, n_sum_align))
to_process(i)=true;
else
disp('skipping because already motion corrected.');
end
end
if(any(to_process))
fn_list_to_compute=fn_list(to_process);
if exist('gcp')
if isempty(gcp('nocreate'))
parpool(2);
end
else
try
matlabpool 2
catch e
disp(e);
end
end
parfor i=1:length(fn_list_to_compute)
% for i=1:length(fn_list_to_compute)
fn = fn_list_to_compute{i};
motion_correct(fn,target_fn,save_path,align_ch,save_ch, n_sum, n_sum_align,[],[],ffn_ROI);
end
end
processed = any(to_process);
end
if(processed || ~make_summed_tiff(save_path,'-s'))
if(to_queue)
queue_job('make_summed_tiff',{save_path},queued_job_ids);
else
make_summed_tiff(save_path);
end
end
if(~isempty(ffn_ROI)&&(processed||~exist(fullfile(save_path,'roi_values.mat'),'file')))
try
make_roi_values(save_path);
catch e
disp(e);
end
end
L.newline('done');
end
function b = exists_file(fn)
b = java.io.File(fn).exists()>0;
end