Skip to content

Commit bcc9dca

Browse files
committed
UPDATE: export_toBIDS - multi-run and multi-model (partially)
1 parent 9544ef7 commit bcc9dca

File tree

1 file changed

+65
-25
lines changed

1 file changed

+65
-25
lines changed

aa_toolbox/aa_export_toBIDS.m

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function aa_export_toBIDS(varargin)
44
%
55
% This function can run standalone or be included at the end of a userscript.
66
%
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'])
88
%
99
% also uses: aap.directory_conventions.BIDSfiles
1010
%
@@ -13,7 +13,8 @@ function aa_export_toBIDS(varargin)
1313
% 1) aa_export_toBIDS('/put/results/here');
1414
% 2) aa_export_toBIDS(aap,'/put/results/here');
1515
% 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')
1718
%
1819
% notes
1920
%
@@ -23,15 +24,19 @@ function aa_export_toBIDS(varargin)
2324
% reason you would ever pass in aap, assuming you first cd to the directory
2425
% where aap_parameters.mat lives
2526
%
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
2728
% (i.e. t1) file to copy (in this example a defaced structural is used, which
2829
% is probably required if you're going to upload results to a public
2930
% repository like openfMRI. The syntax goes: stage|streamname. If you don't
3031
% supply an explict stream for t1 (or, optionally, t2), the first appearance
3132
% of t1 [t2] in the analysis tasklist will be used, which is probably the
3233
% output from dicom covert (which is not defaced).
3334
%
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:
3540
%
3641
% README - a plaintext (ASCII or UTF-8) description of the data
3742
% CHANGES- a plaintext (ASCII or UTF-8) list of version changes
@@ -49,12 +54,10 @@ function aa_export_toBIDS(varargin)
4954
%
5055
% This function is a work in progress. TODO:
5156
%
57+
% - multi-model
5258
% - multi-session
53-
% - multi-run
5459
% - covariates
5560
% - parametric
56-
% - multi-model
57-
5861

5962
if isstruct(varargin{1})
6063
aap = varargin{1};
@@ -67,6 +70,7 @@ function aa_export_toBIDS(varargin)
6770
varargin(1) = [];
6871
end
6972
args = vargParser(varargin);
73+
if ~isfield(args,'model'), args.model = 1; end % default model
7074

7175
% identify source stages present from the tasklist
7276

@@ -152,6 +156,7 @@ function aa_export_toBIDS(varargin)
152156
aas_log(aap,false,'WARNING: No header is available!')
153157
else
154158
loaded = load(fhdr); hdr = loaded.dcmhdr;
159+
if iscell(hdr), hdr = hdr{1}; end; hdr = hdr(1);
155160
json = struct(...
156161
'RepetitionTime',hdr.volumeTR,...
157162
'EchoTime',hdr.volumeTE,...
@@ -207,18 +212,35 @@ function aa_export_toBIDS(varargin)
207212

208213
aap = aas_setcurrenttask(aap,stage_func);
209214
for sess = 1:numel(aap.acq_details.sessions)
210-
aas_log(aap,false,['\tINFO: 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,['\tINFO: Exporting fMRI session: ' taskname])
217+
src_main = aas_getfiles_bystream(aap,'session',[subj sess],'epi','output');
218+
214219
% image
215-
if isempty(src)
220+
if isempty(src_main)
216221
aas_log(aap,false,'WARNING: No image is available!')
217222
continue;
218223
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+
219240
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
222244

223245
% header
224246
loaded = load(fhdr); hdr = loaded.DICOMHEADERS{1};
@@ -234,19 +256,28 @@ function aa_export_toBIDS(varargin)
234256
'PhaseEncodingDirection',[...
235257
sliceaxes{cell_index(sliceaxes(:,1),deblank(hdr.InPlanePhaseEncodingDirection)),2}{aas_get_numaris4_numval(hdr.CSAImageHeaderInfo,'PhaseEncodingDirectionPositive')+1}...
236258
],...
237-
'TaskName',aap.acq_details.sessions(sess).name ...
259+
'TaskName',taskname ...
238260
);
239261
savejson('',json,spm_file(dest,'ext','json'));
240262
end
241263

242264
% events
243265

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)
245269
aas_log(aap,false,'WARNING: No model information is available!')
246270
continue
247-
end
271+
end
248272

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);
250281
selected_model = (strcmp({models.subject},aas_getsubjname(aap,subj)) | strcmp({models.subject},'*')) & ...
251282
(strcmp({models.session},aap.acq_details.sessions(sess).name) | strcmp({models.session},'*'));
252283
if ~any(selected_model)
@@ -328,19 +359,28 @@ function aa_export_toBIDS(varargin)
328359
aap = aas_setcurrenttask(aap,stage_dwi);
329360
for sess = 1:numel(aap.acq_details.diffusion_sessions)
330361
aas_log(aap,false,['\tINFO: 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+
336364
% image
337-
if isempty(src)
365+
if isempty(src_main)
338366
aas_log(aap,false,'WARNING: No image is available!')
339367
continue;
340368
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+
341381
aas_makedir(aap,fullfile(subjpath,'dwi'));
342382
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
344384

345385
% header
346386
if isempty(fhdr)

0 commit comments

Comments
 (0)