-
Notifications
You must be signed in to change notification settings - Fork 14
/
aqua_cmd_batch2.m
304 lines (265 loc) · 8.82 KB
/
aqua_cmd_batch2.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
%% setup
% -- preset 1: original Lck. 2: Jen Lck. 5: GluSnFR
%
%
% Read Me:
% 'p0' is the folder containing tifs you want to deal with.
% Suggest sort the files in order, so that you can set the parameters
% conviniently. AQuA/cfg/parameters_for_batch is the parameters excel.
% The script will read the parameters from that excel to deal with data.
% How many files you have, how many parameter settings should be in that excel.
close all
clearvars
startup; % initialize
load('random_Seed.mat');
rng(s);
p0 = 'D:\'; %% tif folder
%% For cell boundary and landmark
p_cell = ''; % cell boundary folder path, if you have
p_landmark = ''; % landmark folder path, if you have
files = dir(fullfile(p0,'*.tif'));
filesCell = dir(fullfile(p_cell,'*.mat'));
filesLandMark = dir(fullfile(p_landmark,'*.mat'));
%%
for x = 1:size(files,1)
f0 = files(x).name; % file name
bd = containers.Map;
bd('None') = [];
% load mask
try
f_cell = [p_cell,filesCell(x).name];
cell_region = load(f_cell);
bd('cell') = cell_region.bd0;
catch
disp(['No cell boundary found for ',f0]);
end
try
f_landMark = [p_landmark,filesLandMark(x).name];
landmark = load(f_landMark);
bd('landmk') = landmark.bd0;
catch
disp(['No landmark found for ',f0]);
end
%% Note: Setting the parameters should be consistent with your target file
opts = util.parseParam_for_batch(x,0);
[datOrg,opts] = burst.prep1(p0,f0,[],opts); % read data
[folder, name, ext] = fileparts(strcat(p0,'\',f0));
%% detection
sz = opts.sz;
evtSpatialMask = ones(sz(1),sz(2));
if bd.isKey('cell')
bd0 = bd('cell');
evtSpatialMask = zeros(sz(1),sz(2));
for ii=1:numel(bd0)
idx = bd0{ii}{2};
spaMsk0 = zeros(sz(1),sz(2));
spaMsk0(idx) = 1;
evtSpatialMask(spaMsk0>0) = 1;
end
end
[dat,dF,~,lmLoc,opts,dL] = burst.actTop(datOrg,opts,evtSpatialMask); % foreground and seed detection
[svLst,~,riseX] = burst.spTop(dat,dF,lmLoc,evtSpatialMask,opts); % super voxel detection
[riseLst,datR,evtLst,seLst] = burst.evtTop(dat,dF,svLst,riseX,opts,[],bd); % events
[ftsLst,dffMat] = fea.getFeatureQuick(dat,evtLst,opts);
% filter by significance level
mskx = ftsLst.curve.dffMaxZ>opts.zThr;
dffMatFilterZ = dffMat(mskx,:);
evtLstFilterZ = evtLst(mskx);
tBeginFilterZ = ftsLst.curve.tBegin(mskx);
riseLstFilterZ = riseLst(mskx);
% merging (glutamate)
if opts.ignoreMerge==0
evtLstMerge = burst.mergeEvt(evtLstFilterZ,dffMatFilterZ,tBeginFilterZ,opts,bd);
else
evtLstMerge = evtLstFilterZ;
end
% reconstruction (glutamate)
if opts.extendSV==0 || opts.ignoreMerge==0 || opts.extendEvtRe>0
[riseLstE,datRE,evtLstE] = burst.evtTopEx(dat,dF,evtLstMerge,opts);
else
riseLstE = riseLstFilterZ; datRE = datR; evtLstE = evtLstFilterZ;
end
% feature extraction
[ftsLstE,dffMatE,dMatE] = fea.getFeaturesTop(datOrg,evtLstE,opts);
ftsLstE = fea.getFeaturesPropTop(dat,datRE,evtLstE,ftsLstE,opts);
% update network features
sz = size(datOrg);
evtx1 = evtLstE;
ftsLstE.networkAll = [];
ftsLstE.network = [];
if bd.isKey('cell')
bd0 = bd('cell');
evtSpatialMask = zeros(sz(1),sz(2));
regLst = cell(numel(bd0),1);
for ii=1:numel(bd0)
pix00 = bd0{ii}{2};
regLst{ii} = pix00;
evtSpatialMask(pix00) = 1;
end
else
regLst = [];
evtSpatialMask = ones(sz(1),sz(2));
end
if bd.isKey('landmk')
bd1 = bd('landmk');
lmkLst = cell(numel(bd1),1);
for ii=1:numel(bd1)
lmkLst{ii} = bd1{ii}{2};
end
else
lmkLst = [];
end
try
if ~isempty(regLst) || ~isempty(lmkLst)
fprintf('Updating region and landmark features ...\n')
ftsLstE.region = fea.getDistRegionBorderMIMO(evtLstE,datR,regLst,lmkLst,opts.spatialRes,opts.minShow1);
if bd.isKey('cell')
bd0 = bd('cell');
for i = 1:numel(regLst)
cname{i} = bd0{i}{4};
if(strcmp(cname{i},'None'))
cname{i} = num2str(i);
end
end
ftsLstE.region.cell.name = cname;
end
if bd.isKey('landmk')
bd0 = bd('landmk');
for i = 1:numel(lmkLst)
lname{i} = bd0{i}{4};
if(strcmp(lname{i},'None'))
lname{i} = num2str(i);
end
end
ftsLstE.region.landMark.name = lname;
end
end
catch
end
try
ftsLstE.networkAll = fea.getEvtNetworkFeatures(evtLstE,sz); % all filtered events
ftsLstE.network = fea.getEvtNetworkFeatures(evtx1,sz); % events inside cells only
catch
end
%% export table
fts = ftsLstE;
tb = readtable('userFeatures.csv','Delimiter',',');
if(isempty(ftsLstE.basic))
nEvt = 0;
else
nEvt = numel(ftsLstE.basic.area);
end
nFt = numel(tb.Name);
ftsTb = nan(nFt,nEvt);
ftsName = cell(nFt,1);
ftsCnt = 1;
dixx = ftsLstE.notes.propDirectionOrder;
lmkLst = [];
for ii=1:nFt
cmdSel0 = tb.Script{ii};
ftsName0 = tb.Name{ii};
% if find landmark or direction
if ~isempty(strfind(cmdSel0,'xxLmk')) %#ok<STREMP>
for xxLmk=1:numel(lmkLst)
try
eval([cmdSel0,';']);
catch
fprintf('Feature "%s" not used\n',ftsName0)
x = nan(nEvt,1);
end
ftsTb(ftsCnt,:) = reshape(x,1,[]);
ftsName1 = [ftsName0,' - landmark ',num2str(xxLmk)];
ftsName{ftsCnt} = ftsName1;
ftsCnt = ftsCnt + 1;
end
elseif ~isempty(strfind(cmdSel0,'xxDi')) %#ok<STREMP>
for xxDi=1:4
try
eval([cmdSel0,';']);
ftsTb(ftsCnt,:) = reshape(x,1,[]);
catch
fprintf('Feature "%s" not used\n',ftsName0)
ftsTb(ftsCnt,:) = nan;
end
ftsName1 = [ftsName0,' - ',dixx{xxDi}];
ftsName{ftsCnt} = ftsName1;
ftsCnt = ftsCnt + 1;
end
else
try
eval([cmdSel0,';']);
ftsTb(ftsCnt,:) = reshape(x,1,[]);
catch
fprintf('Feature "%s" not used\n',ftsName0)
ftsTb(ftsCnt,:) = nan;
end
ftsName{ftsCnt} = ftsName0;
ftsCnt = ftsCnt + 1;
end
end
featureTable = table(ftsTb,'RowNames',ftsName);
path0 = [p0,name,'\'];
if ~exist(path0,'dir') && ~isempty(path0)
mkdir(path0);
end
ftb = [path0,name,'_FeatureTable.xlsx'];
writetable(featureTable,ftb,'WriteVariableNames',0,'WriteRowNames',1);
%% export movie
datL = zeros(opts.sz);
for i = 1:numel(evtLstE)
datL(evtLstE{i}) = i;
end
ov1 = zeros(opts.sz(1),opts.sz(2),3,opts.sz(3));
% re-scale movie
c0 = zeros(nEvt,3);
for nn=1:nEvt
x = rand(1,3);
while (x(1)>0.8 && x(2)>0.8 && x(3)>0.8) || sum(x)<1
x = rand(1,3);
end
x = x/max(x);
c0(nn,:) = x;
end
for tt=1:opts.sz(3)
if mod(tt,100)==0
fprintf('Frame %d\n',tt);
end
dat0 = datOrg(:,:,tt);
if opts.usePG==1
dat0 = dat0.^2;
end
datx = cat(3,dat0,dat0,dat0);
datxCol = datx;
[H,W,~] = size(datx);
reCon = double(datRE(:,:,tt))/255;
rPlane = zeros(H,W);
gPlane = rPlane;
bPlane = rPlane;
map = datL(:,:,tt);
rPlane(map>0) = c0(map(map>0),1);
gPlane(map>0) = c0(map(map>0),2);
bPlane(map>0) = c0(map(map>0),3);
datxCol(:,:,1) = rPlane.*reCon + datxCol(:,:,1);
datxCol(:,:,2) = gPlane.*reCon + datxCol(:,:,2);
datxCol(:,:,3) = bPlane.*reCon + datxCol(:,:,3);
ov1(:,:,:,tt) = datxCol;
end
fmov = [path0,name,'_Movie.tif'];
io.writeTiffSeq(fmov,ov1,8);
%% export to GUI
res = fea.gatherRes(datOrg,opts,evtLstE,ftsLstE,dffMatE,dMatE,riseLstE,datRE);
% aqua_gui(res);
% visualize the results in each step
if 0
ov1 = plt.regionMapWithData(arLst,datOrg,0.5); zzshow(ov1);
ov1 = plt.regionMapWithData(svLst,datOrg,0.5); zzshow(ov1);
ov1 = plt.regionMapWithData(seLst,datOrg,0.5,datR); zzshow(ov1);
ov1 = plt.regionMapWithData(evtLst,datOrg,0.5,datR); zzshow(ov1);
ov1 = plt.regionMapWithData(evtLstFilterZ,datOrg,0.5,datR); zzshow(ov1);
ov1 = plt.regionMapWithData(evtLstMerge,datOrg,0.5,datR); zzshow(ov1);
[ov1,lblMapS] = plt.regionMapWithData(evtLstE,datOrg,0.5,datRE); zzshow(ov1);
end
%% save output
res.bd = bd;
save([path0,name,'_AQuA.mat'], 'res');
end