-
Notifications
You must be signed in to change notification settings - Fork 0
/
mm_arg.m
464 lines (371 loc) · 14.2 KB
/
mm_arg.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
function varargout = mm_arg(typeAnal, argfile)
% Get arguments for MLM/SVD analysis.
% Format varargout = mm_arg(typeAnal, argfile)
% nbsub = Number of subject or space domain.
% Pimg = Beta images for MLM, Data images for SVD.
% Res = estimated residual variance image.
% Mask = analysis mask image indicating which voxels were
% included in the analysis
% gcsd = global result directory.
% cwd = current working directory (dir contening the model)
% csd = current saving directory for each space domain.
% fname = Eigen image basename.
% Xc = Contrast
% gsf = global scaling factor.
% Filter = Sparse temporal smoothing matrix.
% leig = Eigen Images to save.
% paramsAnal = Parameters for the anlysis :
%================================================================================
%- Copyright (C) 1997-2002 CEA
%- This software and supporting documentation were developed by
%- CEA/DSV/SHFJ/UNAF
%- 4 place du General Leclerc
%- 91401 Orsay cedex
%- France
%================================================================================
switch typeAnal
case 'MLM'
[nbsub, Pimg, Res, Mask, Yimg, gcsd, cwd, csd, xC, fname, gsf, Filter, leig, paramsAnal,W] = ...
sf_arg_mlm(argfile);
varargout = {nbsub, Pimg, Res, Mask, Yimg,gcsd, cwd, csd, xC, fname, gsf, Filter, ...
leig, paramsAnal,W};
case 'SVD'
[nbsub, Pimg, Res, Mask, gcsd, cwd, csd, xC, Filter, paramsAnal, fname, leig, gsf,W] = ...
sf_arg_svd(argfile);
varargout = {nbsub, Pimg, Res, Mask, gcsd, cwd, csd, xC, Filter, paramsAnal, ...
fname, leig, gsf,W};
otherwise
error('unknown type of analysis')
end
%=========================================================================================================
function [nbsub, Pimg, Res, Mask, Yimg ,gcsd, cwd, csd, xC, fname, gsf, Filter, leig, paramsAnal,W] = ...
sf_arg_mlm(argfile)
%- if the argfile is not given then the interactive mode is use.
% argfile format is : one line per subject whith 3 variables cwd, csd and d delimited by a semicolon ";".
%
% input argument for the multivariate analysis.
% FORMAT [nbsub, Pimg, Res,Mask] = mm_arg(argfile)
%
% nbsub : number of subject
% Pimg : images filename, input data for the mlm computation
% cwd, csd : SPM.mat directory, directory for saving results.
% argfile : parameter file
% cwd, csd : working and saving directory.
% Xc : contrast.
% paramsAnal : on what is the eigen value decomposition done :
% - divideByRessd = 1 : divide by the residual standard deviation
% - temporalFilter = 1 : apply the temporal filter to data first
% fname : generic name for the eigen images
% leig : liste of number for which the corresponding eigenimages will be constructed
%=========================================================================================================
if isempty(argfile) %- INTERACTIVE MODE
%- Set the number of subjects and gcsd
%--------------------------------------------------------------------
gcsd = ui_arg(mm_load_arg('god'),'god');
nbsub = ui_arg(mm_load_arg('nbsub'),'nbsub');
%- Set working directory for each subject and names for eigenimages
%--------------------------------------------------------------------
for sub=1:nbsub
input = sprintf('for space domain %d ',sub);
arg = mm_load_arg('mod');
arg.mod.input.prompt = ['model ' input];
cwd{sub} = spm_str_manip(ui_arg(arg,'mod'),'H');
if nbsub > 1
arg = mm_load_arg('iod');
arg.iod.input.prompt = ['dir ' input];
csd{sub} = ui_arg(arg,'iod');
else
csd{1} = gcsd;
end
arg = mm_load_arg('neig');arg.neig.input.prompt=[arg.neig.input.prompt input];
fname{sub} = ui_arg(arg,'neig');
arg = mm_load_arg('DefImages');arg.DefImages.input.prompt=['Images ' input];
defImg=ui_arg(arg,'DefImages');
if defImg
load(fullfile(cwd{sub},'SPM.mat'));
Yimg{sub}=SPM.xY.P;
else
arg=mm_load_arg('Image');
arg.image.input.prompt=['Images ' input] ;
Yimg{sub} = ui_arg(arg,'Image');
end
end
% csd = cwd; %- by default working and saving directory are the same.
%- Load the design Matrix, (same for all subjects) and the temporal filter
%--------------------------------------------------------------------
Pmat = fullfile(cwd{1},'SPM.mat');
load(Pmat);
xX=SPM.xX
Filter = SPM.xX.K;
W =SPM.xX.W;
%- Load the contrast
%--------------------------------------------------------------------
%Pcon = ui_arg(mm_load_arg('Cm'),'Cm'); %- get name of xCon matrix
%load(Pcon,'xCon');
arg = mm_load_arg('Ci'); %- Ci : a specific contrast
arg.Ci.input.SPM=SPM;
arg.Ci.input.xCon=SPM.xCon;
arg = ui_arg(arg,'Ci');
[Ic,xCon] = deal(arg{:});
xC = xCon(Ic);
%SPM.xCon=xCon;
%try, save(cwd{1},'SPM'); catch, fprintf('can not save xCon : check permissions\n'); end;
clear arg;
elseif isstruct(argfile) %- SPM BATCH MODE
job = argfile;
gcsd = job.god{1};
nbsub = numel(job.domain);
%- Working directories and eigen image names
for sub=1:nbsub
cwd{sub} = spm_str_manip(job.domain(sub).mod{1},'H'); %#ok<AGROW>
if nbsub > 1
csd{sub} = job.domain(sub).iod{1}; %#ok<AGROW>
else
csd{1} = gcsd;
end
fname{sub} = job.domain(sub).neig;
defImg=isfield(job.domain(sub).DefImages, 'UseDefImage');
if defImg
load(fullfile(cwd{sub},'SPM.mat'));
Yimg{sub}=SPM.xY.P; %#ok<AGROW>
else
Yimg{sub} = job.domain(sub).DefImages.Image; %#ok<AGROW>
end
end
%- Design matrix and temporal filter
Pmat = fullfile(cwd{1},'SPM.mat');
load(Pmat);
xX = SPM.xX; %#ok<NASGU>
Filter = SPM.xX.K;
W = SPM.xX.W;
%- Contrast
Ic = job.Ci;xCon = SPM.xCon;
%[Ic,xCon] = deal([job.Ci(1), SPM.xCon]);
xC = xCon(Ic);
%- leig
leig = job.leig;
else %- BATCH MODE
[ggcsd cwd csd d fname] = textread(argfile,'%s %s %s %d %s','commentstyle','matlab');
gcsd=ggcsd{1};
nbsub = size(cwd,1);
load(fullfile(cwd{1},'xCon.mat'),'xCon');
load(fullfile(cwd{1},'SPM.mat'),'xX');
Filter=xX.K;
d = d(1,1);
if isempty(d), d=1; end
xC = xCon(d);
%------ SOME DEFAULTS VALES - TO BE PUT IN A DEFAULT FILE ?
leig = 1:5; % defaults that should
end
%- Set parameters : filtering and divide by std deviation
%--------------------------------------------------------------------
paramsAnal.divideByRessd = 1;
paramsAnal.temporalFilter = 1;
paramsAnal.resContSp = 0;
%- set the parameters: Pimg, Res and Mask filenames.
%--------------------------------------------------------------------
for sub = 1:nbsub
load(fullfile(cwd{sub},'SPM.mat'));
Vbeta=SPM.Vbeta;
%Pimg{sub} = [repmat([cwd{sub}, filesep],length(Vbeta),1),char(Vbeta.fname)];
Pimg{sub} = char(Vbeta.fname);
Mask{sub} = fullfile(cwd{sub}, 'mask.nii');
VResMS =SPM.VResMS
%Res{sub} = fullfile(cwd{sub},char(VResMS.fname));
Res{sub} = char(VResMS.fname);
gsf{sub} = SPM.xGX.gSF;
end
if isempty(argfile) %- NOT IN BATCH MODE
%- Set number of eigenimages
%--------------------------------------------------------------------
arg = mm_load_arg('leig');
arg.leig.input.def=['[1:' num2str(min(5,size(Pimg{1},1))) ']'];
leig = ui_arg(arg,'leig');
end
clear Vbeta,VResMS;
%=========================================================================================================
function [nbsub, Pimg, Res, Mask, gcsd, cwd, csd, xC, Filter, paramsAnal, fname, leig, gsf,W] = ...
sf_arg_svd(argfile);
%
% paramsAnal : on what is the eigen value decomposition done :
% - divideByRessd = 1 : divide by the residual standard deviation
% - temporalFilter = 1 : apply the temporal filter to data first
% fname : generic name for the eigen images
% leig : liste of number for which the corresponding
% eigenimages will be constructed
%
%=========================================================================================================
paramsAnal = struct(...
'divideByRessd', 1, ...
'temporalFilter', 1, ...
'resContSp', 0 ...
);
if isempty(argfile) %- INTERACTIVE MODE
%- Set the number of subjects and gcsd
%--------------------------------------------------------------------
gcsd = ui_arg(mm_load_arg('god'),'god');
nbsub = ui_arg(mm_load_arg('nbsub'),'nbsub');
%- Set input and output directories and names for eigenimages - get gsf
%--------------------------------------------------------------------
for sub=1:nbsub
input=sprintf('for space domain %d ',sub);
arg=mm_load_arg('mod');
arg.mod.input.prompt=['model ' input];
cwd{sub} = spm_str_manip(ui_arg(arg,'mod'),'H');
load(fullfile(cwd{sub},'SPM.mat'));
if nbsub > 1
arg=mm_load_arg('iod');arg.iod.input.prompt = ['result directory ' input];
csd{sub} = ui_arg(arg,'iod');
else
csd{1} = gcsd;
end
arg = mm_load_arg('DefImages');arg.DefImages.input.prompt=['Images ' input];
defImg=ui_arg(arg,'DefImages');
if defImg
Pimg{sub}=SPM.xY.P;
else
arg=mm_load_arg('Image');
arg.image.input.prompt=['Images ' input];
Pimg{sub} = ui_arg(arg,'Image');
end
arg = mm_load_arg('neig'); arg.neig.input.prompt=[arg.neig.input.prompt input];
fname{sub} = ui_arg(arg,'neig');
gsf{sub} = SPM.xGX.gSF;
Mask{sub} = fullfile(cwd{sub},'mask.img');
Res{sub} = fullfile(cwd{sub},SPM.VResMS.fname);
end
%- Load design matrix of the first subject
%--------------------------------------------------------------------
load(fullfile(cwd{1},'SPM.mat'));
xX = SPM.xX;
Filter = SPM.xX.K;
W = SPM.xX.W;
%- Load the contrast
%--------------------------------------------------------------------
arg=mm_load_arg('Ci');
arg.Ci.input.SPM=SPM;
arg.Ci.input.xCon=SPM.xCon;
arg = ui_arg(arg,'Ci');
[Ic,SPM.xCon] = deal(arg{:});
xC = SPM.xCon(Ic);
SPM.xCon=SPM.xCon;
% try, fprintf('can not save contrast'); save(cwd{1},'SPM'); catch, fprintf('can not save xCon : check permissions\n'); end;
%clear arg SPM
%- Get images + Mask + ResMS per subject
%- %--------------------------------------------------------------------
%- for sub=1:nbsub
%-
%- Mask{sub} = fullfile(cwd{sub},'mask.img');
%- load(fullfile(cwd{sub},'SPM.mat'),'VResMS');
%- Res{sub} = fullfile(cwd{sub},char(VResMS));
%- end
%-
%- Set number of eigenimages
%--------------------------------------------------------------------
arg = mm_load_arg('leig');
arg.leig.input.def=['[1:' num2str(min(5,size(Pimg{1},1))) ']'];
leig = ui_arg(arg,'leig');
%- Set parameters : filtering and divide by std deviation
%--------------------------------------------------------------------
paramsAnal.resContSp = ui_arg(mm_load_arg('Pres'),'Pres');
paramsAnal.divideByRessd = ui_arg(mm_load_arg('dvres'),'dvres');
paramsAnal.temporalFilter = ui_arg(mm_load_arg('filter'),'filter');
elseif isstruct(argfile) %- SPM BATCH MODE
job = argfile;
gcsd = job.god{1};
nbsub = numel(job.domain);
%- Working directories and eigen image names
for sub=1:nbsub
cwd{sub} = spm_str_manip(job.domain(sub).mod{1},'H'); %#ok<AGROW>
if nbsub > 1
csd{sub} = job.domain(sub).iod{1}; %#ok<AGROW>
else
csd{1} = gcsd;
end
fname{sub} = job.domain(sub).neig;
defImg=isfield(job.domain(sub).DefImages, 'UseDefImage');
if defImg
load(fullfile(cwd{sub},'SPM.mat'));
Pimg{sub}=SPM.xY.P %#ok<AGROW>
else
Pimg{sub} = job.domain(sub).DefImages.Image; %#ok<AGROW>
end
gsf{sub} = SPM.xGX.gSF;
Mask{sub} = fullfile(cwd{sub},'mask.img');
Res{sub} = fullfile(cwd{sub},SPM.VResMS.fname);
end
%- Design matrix and temporal filter
Pmat = fullfile(cwd{1},'SPM.mat');
load(Pmat);
xX = SPM.xX; %#ok<NASGU>
Filter = SPM.xX.K;
W = SPM.xX.W;
%- Contrast
[Ic,xCon] = deal([job.Ci{1}, SPM.xCon]);
xC = xCon(Ic);
%- leig
leig = job.leig;
%- Filtering and divide by std deviation
paramsAnal.resContSp = job.typeanal.svdanal.pres;
paramsAnal.divideByRessd = job.typeanal.svdanal.dvres;
paramsAnal.temporalFilter = job.typeanal.svdanal.filter;
else %- BATCH MODE
[gcsdr cwd csd d fname ImgDir ImgName paramsAnal.temporalFilter paramsAnal.divideByRessd paramsAnal.resContSp ] ...
= textread(argfile,'%s %s %s %d %s %s %s %d %d %d','commentstyle','matlab');
nbsub = size(cwd,1);
gcsd=gcsdr{1}
load(fullfile(cwd{1},'SPM.mat'),'xX');
Filter = xX.K;
load(fullfile(cwd{1},'xCon.mat'),'xCon');
d = d(1,1);
if isempty(d), d=1; end
xC = xCon(d);
clear xX xCon
for sub=1:nbsub
[DIn Dsz] =sf_strsplit(ImgName{sub},';');
for i=1:Dsz
pimg{i}=fullfile(ImgDir{sub},spm_select('List',ImgDir{sub},str_clean(DIn{i})));
end
Pimg{sub} = cat(1,pimg{:});
Mask{sub} = fullfile(cwd{sub},'mask.img');
load(fullfile(cwd{sub},'SPM.mat'),'VResMS');
Res{sub} = fullfile(cwd{sub},char(VResMS));
load(fullfile(cwd{sub},'SPMcfg.mat'), 'xGX');
gsf{sub} = xGX.gSF;
clear xGX;
end
leig = 1:5;
end
function [res,n]=sf_strsplit(str,fs)
%- Split the string str into cell array elements a{1}, a{2},
%- a{n}, and return n. The separation will be
%- done with the expression fs
%-
if length(fs) >1
disp('error')
return;
end
id=find(str==fs);
if isempty(id)
n=1;
res{n}=str;
return
end
sz=length(str);
cur=1;
n=1;
for i=1:length(id)
tmp=str(cur:id(i)-1);
if length(tmp)
res{n}=str(cur:id(i)-1);
n=n+1;
end
cur=id(i)+1;
if (id(i)==sz)
break;
end
end
if id(length(id))<sz
res{n}=str(id(length(id))+1:sz);
else n=n-1;
end