@@ -4,7 +4,7 @@ function aa_export_toBIDS(varargin)
4
4
%
5
5
% This function can run standalone or be included at the end of a userscript.
6
6
%
7
- % usage: aa_export_toBIDS([aap,] dest[, 'anatt1', <stagetag[|streamname]>][, 'anatt2', <stagetag[|streamname]>])
7
+ % usage: aa_export_toBIDS([aap,] dest[, 'anatt1', <stagetag[|streamname]>][, 'anatt2', <stagetag[|streamname]>][, 'model', <stageindex>|'session'] )
8
8
%
9
9
% also uses: aap.directory_conventions.BIDSfiles
10
10
%
@@ -13,7 +13,8 @@ function aa_export_toBIDS(varargin)
13
13
% 1) aa_export_toBIDS('/put/results/here');
14
14
% 2) aa_export_toBIDS(aap,'/put/results/here');
15
15
% 3) aa_export_toBIDS('put/results/here','anatt1','aamod_freesurfer_deface_00001|defaced_structural')
16
- % 4) aa_export_toBIDS(aap,'put/results/here','anatt1','aamod_freesurfer_deface_00001|defaced_structural')
16
+ % 4) aa_export_toBIDS('put/results/here','model',1)
17
+ % 5) aa_export_toBIDS('put/results/here','model','session')
17
18
%
18
19
% notes
19
20
%
@@ -23,15 +24,19 @@ function aa_export_toBIDS(varargin)
23
24
% reason you would ever pass in aap, assuming you first cd to the directory
24
25
% where aap_parameters.mat lives
25
26
%
26
- % b) example usage #3 and #4 shows how to override the default structural
27
+ % b) example usage #3 shows how to override the default structural
27
28
% (i.e. t1) file to copy (in this example a defaced structural is used, which
28
29
% is probably required if you're going to upload results to a public
29
30
% repository like openfMRI. The syntax goes: stage|streamname. If you don't
30
31
% supply an explict stream for t1 (or, optionally, t2), the first appearance
31
32
% of t1 [t2] in the analysis tasklist will be used, which is probably the
32
33
% output from dicom covert (which is not defaced).
33
34
%
34
- % c) BIDS requires three files to appear in the top level directory:
35
+ % c) eample usage #4 and #5 show how to select model to extract events. aa
36
+ % uses the first model by default. A number here can specify a certain
37
+ % model, while 'session' tells aa that every session has its own specific model.
38
+ %
39
+ % d) BIDS requires three files to appear in the top level directory:
35
40
%
36
41
% README - a plaintext (ASCII or UTF-8) description of the data
37
42
% CHANGES- a plaintext (ASCII or UTF-8) list of version changes
@@ -49,12 +54,10 @@ function aa_export_toBIDS(varargin)
49
54
%
50
55
% This function is a work in progress. TODO:
51
56
%
57
+ % - multi-model
52
58
% - multi-session
53
- % - multi-run
54
59
% - covariates
55
60
% - parametric
56
- % - multi-model
57
-
58
61
59
62
if isstruct(varargin{1 })
60
63
aap = varargin{1 };
@@ -67,6 +70,7 @@ function aa_export_toBIDS(varargin)
67
70
varargin(1 ) = [];
68
71
end
69
72
args = vargParser(varargin );
73
+ if ~isfield(args ,' model' ), args.model = 1 ; end % default model
70
74
71
75
% identify source stages present from the tasklist
72
76
@@ -152,6 +156,7 @@ function aa_export_toBIDS(varargin)
152
156
aas_log(aap ,false ,' WARNING: No header is available!' )
153
157
else
154
158
loaded = load(fhdr ); hdr = loaded .dcmhdr ;
159
+ if iscell(hdr ), hdr = hdr{1 }; end ; hdr = hdr(1 );
155
160
json = struct(...
156
161
' RepetitionTime' ,hdr .volumeTR ,...
157
162
' EchoTime' ,hdr .volumeTE ,...
@@ -207,18 +212,35 @@ function aa_export_toBIDS(varargin)
207
212
208
213
aap = aas_setcurrenttask(aap ,stage_func );
209
214
for sess = 1 : numel(aap .acq_details .sessions )
210
- aas_log( aap , false ,[ ' \t INFO: Exporting fMRI session: ' aas_getsessname(aap ,sess )])
211
- src = aas_getfiles_bystream (aap ,' session' ,[ subj sess ], ' epi ' , ' output ' );
212
- fhdr = aas_getfiles_bystream(aap ,' session' ,[subj sess ],' epi_dicom_header ' ,' output' );
213
-
215
+ taskname = aas_getsessname(aap ,sess );
216
+ aas_log (aap ,false ,[ ' \t INFO: Exporting fMRI session: ' taskname ])
217
+ src_main = aas_getfiles_bystream(aap ,' session' ,[subj sess ],' epi ' ,' output' );
218
+
214
219
% image
215
- if isempty(src )
220
+ if isempty(src_main )
216
221
aas_log(aap ,false ,' WARNING: No image is available!' )
217
222
continue ;
218
223
end
224
+
225
+ if any(strcmp(aas_getstreams(aap ,' output' ),' dummyscans' ))
226
+ src = spm_file(tempname ,' ext' ,' nii' );
227
+ src_dummy = aas_getfiles_bystream(aap ,' session' ,[subj sess ],' dummyscans' ,' output' );
228
+ spm_file_merge(char(src_dummy ,src_main ),src );
229
+ else
230
+ src = src_main ;
231
+ end
232
+ fhdr = aas_getfiles_bystream(aap ,' session' ,[subj sess ],' epi_dicom_header' ,' output' );
233
+
234
+ isRun = regexp(taskname ,' [_-]?[rR]un[0-9]*$' );
235
+ if isRun
236
+ taskname = taskname(1 : isRun - 1 );
237
+ runNo = str2double(regexp(aap .acq_details .sessions(sess ).name(isRun : end ),' [0-9]*' ,' match' ));
238
+ end
239
+
219
240
aas_makedir(aap ,fullfile(subjpath ,' func' ));
220
- dest = fullfile(subjpath ,' func' ,sprintf(' %s _task-%s _bold.nii' ,suboutname ,valueValidate(aap .acq_details .sessions(sess ).name)));
221
- copyfile(src ,dest ); gzip(dest ); delete(dest );
241
+ dest = fullfile(subjpath ,' func' ,sprintf(' %s _task-%s _bold.nii' ,suboutname ,valueValidate(taskname )));
242
+ if isRun , dest = strrep(dest ,' bold.nii' ,sprintf(' run-%d _bold.nii' ,runNo )); end
243
+ copyfile(src ,dest ); gzip(dest ); delete(dest ); if exist(' src_dummy' ,' var' ), delete(src ); clear src_dummy ; end
222
244
223
245
% header
224
246
loaded = load(fhdr ); hdr = loaded.DICOMHEADERS{1 };
@@ -234,19 +256,28 @@ function aa_export_toBIDS(varargin)
234
256
' PhaseEncodingDirection' ,[...
235
257
sliceaxes{cell_index(sliceaxes(: ,1 ),deblank(hdr .InPlanePhaseEncodingDirection )),2 }{aas_get_numaris4_numval(hdr .CSAImageHeaderInfo ,' PhaseEncodingDirectionPositive' )+1 }...
236
258
],...
237
- ' TaskName' ,aap . acq_details .sessions( sess ).name ...
259
+ ' TaskName' ,taskname ...
238
260
);
239
261
savejson(' ' ,json ,spm_file(dest ,' ext' ,' json' ));
240
262
end
241
263
242
264
% events
243
265
244
- if ~isfield(aap .tasksettings ,' aamod_firstlevel_model' )
266
+ stageModels = aap .tasklist .main .module(strcmp({aap .tasklist .main .module .name },' aamod_firstlevel_model' ));
267
+
268
+ if isempty(stageModels )
245
269
aas_log(aap ,false ,' WARNING: No model information is available!' )
246
270
continue
247
- end
271
+ end
248
272
249
- models = aap .tasksettings .aamod_firstlevel_model(1 ).model(2 : end );
273
+ switch args .model
274
+ case ' session'
275
+ stageindex = strcmp(arrayfun(@(x ) x .extraparameters .aap .acq_details .selected_sessions , stageModels ,' UniformOutput' ,false ),aas_getsessname(aap ,sess ));
276
+ otherwise
277
+ stageindex = args .model ;
278
+ end
279
+
280
+ models = aap .tasksettings .aamod_firstlevel_model(stageindex ).model(2 : end );
250
281
selected_model = (strcmp({models .subject },aas_getsubjname(aap ,subj )) | strcmp({models .subject },' *' )) & ...
251
282
(strcmp({models .session },aap .acq_details .sessions(sess ).name) | strcmp({models .session },' *' ));
252
283
if ~any(selected_model )
@@ -328,19 +359,28 @@ function aa_export_toBIDS(varargin)
328
359
aap = aas_setcurrenttask(aap ,stage_dwi );
329
360
for sess = 1 : numel(aap .acq_details .diffusion_sessions )
330
361
aas_log(aap ,false ,[' \t INFO: Exporting DWI session: ' aas_getsessname(aap ,sess )])
331
- src = aas_getfiles_bystream(aap ,' diffusion_session' ,[subj sess ],' diffusion_data' ,' output' );
332
- aap.options.verbose = - 1 ;
333
- fhdr = aas_getfiles_bystream(aap ,' diffusion_session' ,[subj sess ],' diffusion_dicom_header' ,' output' );
334
- aap.options.verbose = 2 ;
335
-
362
+ src_main = aas_getfiles_bystream(aap ,' diffusion_session' ,[subj sess ],' diffusion_data' ,' output' );
363
+
336
364
% image
337
- if isempty(src )
365
+ if isempty(src_main )
338
366
aas_log(aap ,false ,' WARNING: No image is available!' )
339
367
continue ;
340
368
end
369
+
370
+ if any(strcmp(aas_getstreams(aap ,' output' ),' dummyscans' ))
371
+ src = spm_file(tempname ,' ext' ,' nii' );
372
+ src_dummy = aas_getfiles_bystream(aap ,' session' ,[subj sess ],' dummyscans' ,' output' );
373
+ spm_file_merge(char(src_dummy ,src_main ),src );
374
+ else
375
+ src = src_main ;
376
+ end
377
+ aap.options.verbose = - 1 ;
378
+ fhdr = aas_getfiles_bystream(aap ,' diffusion_session' ,[subj sess ],' diffusion_dicom_header' ,' output' );
379
+ aap.options.verbose = 2 ;
380
+
341
381
aas_makedir(aap ,fullfile(subjpath ,' dwi' ));
342
382
dest = fullfile(subjpath ,' dwi' ,sprintf(' %s _dwi.nii' ,suboutname ));
343
- copyfile(src ,dest ); gzip(dest ); delete(dest );
383
+ copyfile(src ,dest ); gzip(dest ); delete(dest ); if exist( ' src_dummy ' , ' var ' ), delete( src ); clear src_dummy ; end
344
384
345
385
% header
346
386
if isempty(fhdr )
0 commit comments