1414
1515from nipype import config
1616config .enable_provenance ()
17- from nipype .external import six
1817
18+ from nipype .external import six
1919
2020from glob import glob
2121import os
@@ -174,8 +174,8 @@ def create_reg_workflow(name='registration'):
174174 reg .inputs .winsorize_upper_quantile = 0.995
175175 reg .inputs .args = '--float'
176176 reg .inputs .output_warped_image = 'output_warped_image.nii.gz'
177- reg .inputs .num_threads = 4
178- reg .plugin_args = {'qsub_args' : '-l nodes=1:ppn=4 ' }
177+ reg .inputs .num_threads = 2
178+ reg .plugin_args = {'qsub_args' : '-pe orte 2 ' }
179179 register .connect (stripper , 'out_file' , reg , 'moving_image' )
180180 register .connect (inputnode ,'target_image_brain' , reg ,'fixed_image' )
181181
@@ -284,12 +284,13 @@ def get_subjectinfo(subject_id, base_dir, task_id, model_id):
284284 for idx in range (n_tasks ):
285285 taskidx = np .where (taskinfo [:, 0 ] == 'task%03d' % (idx + 1 ))
286286 conds .append ([condition .replace (' ' , '_' ) for condition
287- in taskinfo [taskidx [0 ], 2 ]])
288- files = glob (os .path .join (base_dir ,
289- subject_id ,
290- 'BOLD' ,
291- 'task%03d_run*' % (idx + 1 )))
292- run_ids .insert (idx , range (1 , len (files ) + 1 ))
287+ in taskinfo [taskidx [0 ], 2 ]]) # if 'junk' not in condition])
288+ files = sorted (glob (os .path .join (base_dir ,
289+ subject_id ,
290+ 'BOLD' ,
291+ 'task%03d_run*' % (idx + 1 ))))
292+ runs = [int (val [- 3 :]) for val in files ]
293+ run_ids .insert (idx , runs )
293294 TR = np .genfromtxt (os .path .join (base_dir , 'scan_key.txt' ))[1 ]
294295 return run_ids [task_id - 1 ], conds [task_id - 1 ], TR
295296
@@ -361,25 +362,45 @@ def analyze_openfmri_dataset(data_dir, subject=None, model_id=None,
361362 """
362363 Return data components as anat, bold and behav
363364 """
364-
365- datasource = pe .Node (nio .DataGrabber (infields = ['subject_id' , 'run_id' ,
365+ contrast_file = os .path .join (data_dir , 'models' , 'model%03d' % model_id ,
366+ 'task_contrasts.txt' )
367+ has_contrast = os .path .exists (contrast_file )
368+ if has_contrast :
369+ datasource = pe .Node (nio .DataGrabber (infields = ['subject_id' , 'run_id' ,
366370 'task_id' , 'model_id' ],
367371 outfields = ['anat' , 'bold' , 'behav' ,
368372 'contrasts' ]),
369373 name = 'datasource' )
374+ else :
375+ datasource = pe .Node (nio .DataGrabber (infields = ['subject_id' , 'run_id' ,
376+ 'task_id' , 'model_id' ],
377+ outfields = ['anat' , 'bold' , 'behav' ]),
378+ name = 'datasource' )
370379 datasource .inputs .base_directory = data_dir
371380 datasource .inputs .template = '*'
372- datasource .inputs .field_template = {'anat' : '%s/anatomy/highres001.nii.gz' ,
373- 'bold' : '%s/BOLD/task%03d_r*/bold.nii.gz' ,
374- 'behav' : ('%s/model/model%03d/onsets/task%03d_'
375- 'run%03d/cond*.txt' ),
376- 'contrasts' : ('models/model%03d/'
377- 'task_contrasts.txt' )}
378- datasource .inputs .template_args = {'anat' : [['subject_id' ]],
381+
382+ if has_contrast :
383+ datasource .inputs .field_template = {'anat' : '%s/anatomy/highres001.nii.gz' ,
384+ 'bold' : '%s/BOLD/task%03d_r*/bold.nii.gz' ,
385+ 'behav' : ('%s/model/model%03d/onsets/task%03d_'
386+ 'run%03d/cond*.txt' ),
387+ 'contrasts' : ('models/model%03d/'
388+ 'task_contrasts.txt' )}
389+ datasource .inputs .template_args = {'anat' : [['subject_id' ]],
379390 'bold' : [['subject_id' , 'task_id' ]],
380391 'behav' : [['subject_id' , 'model_id' ,
381392 'task_id' , 'run_id' ]],
382393 'contrasts' : [['model_id' ]]}
394+ else :
395+ datasource .inputs .field_template = {'anat' : '%s/anatomy/highres001.nii.gz' ,
396+ 'bold' : '%s/BOLD/task%03d_r*/bold.nii.gz' ,
397+ 'behav' : ('%s/model/model%03d/onsets/task%03d_'
398+ 'run%03d/cond*.txt' )}
399+ datasource .inputs .template_args = {'anat' : [['subject_id' ]],
400+ 'bold' : [['subject_id' , 'task_id' ]],
401+ 'behav' : [['subject_id' , 'model_id' ,
402+ 'task_id' , 'run_id' ]]}
403+
383404 datasource .inputs .sort_filelist = True
384405
385406 """
@@ -412,9 +433,11 @@ def get_highpass(TR, hpcutoff):
412433
413434 def get_contrasts (contrast_file , task_id , conds ):
414435 import numpy as np
415- contrast_def = np .genfromtxt (contrast_file , dtype = object )
416- if len (contrast_def .shape ) == 1 :
417- contrast_def = contrast_def [None , :]
436+ import os
437+ contrast_def = []
438+ if os .path .exists (contrast_file ):
439+ with open (contrast_file , 'rt' ) as fp :
440+ contrast_def .extend ([np .array (row .split ()) for row in fp .readlines () if row .strip ()])
418441 contrasts = []
419442 for row in contrast_def :
420443 if row [0 ] != 'task%03d' % task_id :
@@ -448,22 +471,33 @@ def get_contrasts(contrast_file, task_id, conds):
448471 name = "modelspec" )
449472 modelspec .inputs .input_units = 'secs'
450473
451- def check_behav_list (behav ):
452- out_behav = []
474+ def check_behav_list (behav , run_id , conds ):
475+ from nipype .external import six
476+ import numpy as np
477+ num_conds = len (conds )
453478 if isinstance (behav , six .string_types ):
454479 behav = [behav ]
455- for val in behav :
456- if not isinstance (val , list ):
457- out_behav .append ([val ])
458- else :
459- out_behav .append (val )
460- return out_behav
480+ behav_array = np .array (behav ).flatten ()
481+ num_elements = behav_array .shape [0 ]
482+ return behav_array .reshape (num_elements / num_conds , num_conds ).tolist ()
483+
484+ reshape_behav = pe .Node (niu .Function (input_names = ['behav' , 'run_id' , 'conds' ],
485+ output_names = ['behav' ],
486+ function = check_behav_list ),
487+ name = 'reshape_behav' )
461488
462489 wf .connect (subjinfo , 'TR' , modelspec , 'time_repetition' )
463- wf .connect (datasource , ('behav' , check_behav_list ), modelspec , 'event_files' )
490+ wf .connect (datasource , 'behav' , reshape_behav , 'behav' )
491+ wf .connect (subjinfo , 'run_id' , reshape_behav , 'run_id' )
492+ wf .connect (subjinfo , 'conds' , reshape_behav , 'conds' )
493+ wf .connect (reshape_behav , 'behav' , modelspec , 'event_files' )
494+
464495 wf .connect (subjinfo , 'TR' , modelfit , 'inputspec.interscan_interval' )
465496 wf .connect (subjinfo , 'conds' , contrastgen , 'conds' )
466- wf .connect (datasource , 'contrasts' , contrastgen , 'contrast_file' )
497+ if has_contrast :
498+ wf .connect (datasource , 'contrasts' , contrastgen , 'contrast_file' )
499+ else :
500+ contrastgen .inputs .contrast_file = ''
467501 wf .connect (infosource , 'task_id' , contrastgen , 'task_id' )
468502 wf .connect (contrastgen , 'contrasts' , modelfit , 'inputspec.contrasts' )
469503
@@ -487,32 +521,39 @@ def check_behav_list(behav):
487521 Reorder the copes so that now it combines across runs
488522 """
489523
490- def sort_copes (files ):
491- numelements = len (files [0 ])
492- outfiles = []
493- for i in range (numelements ):
494- outfiles .insert (i , [])
495- for j , elements in enumerate (files ):
496- outfiles [i ].append (elements [i ])
497- return outfiles
498-
499- def num_copes (files ):
500- return len (files )
524+ def sort_copes (copes , varcopes , contrasts ):
525+ import numpy as np
526+ if not isinstance (copes , list ):
527+ copes = [copes ]
528+ varcopes = [varcopes ]
529+ num_copes = len (contrasts )
530+ n_runs = len (copes )
531+ all_copes = np .array (copes ).flatten ()
532+ all_varcopes = np .array (varcopes ).flatten ()
533+ outcopes = all_copes .reshape (len (all_copes )/ num_copes , num_copes ).T .tolist ()
534+ outvarcopes = all_varcopes .reshape (len (all_varcopes )/ num_copes , num_copes ).T .tolist ()
535+ return outcopes , outvarcopes , n_runs
536+
537+ cope_sorter = pe .Node (niu .Function (input_names = ['copes' , 'varcopes' ,
538+ 'contrasts' ],
539+ output_names = ['copes' , 'varcopes' ,
540+ 'n_runs' ],
541+ function = sort_copes ),
542+ name = 'cope_sorter' )
501543
502544 pickfirst = lambda x : x [0 ]
503545
546+ wf .connect (contrastgen , 'contrasts' , cope_sorter , 'contrasts' )
504547 wf .connect ([(preproc , fixed_fx , [(('outputspec.mask' , pickfirst ),
505548 'flameo.mask_file' )]),
506- (modelfit , fixed_fx , [(('outputspec.copes' , sort_copes ),
507- 'inputspec.copes' ),
508- ('outputspec.dof_file' ,
549+ (modelfit , cope_sorter , [('outputspec.copes' , 'copes' )]),
550+ (modelfit , cope_sorter , [('outputspec.varcopes' , 'varcopes' )]),
551+ (cope_sorter , fixed_fx , [('copes' , 'inputspec.copes' ),
552+ ('varcopes' , 'inputspec.varcopes' ),
553+ ('n_runs' , 'l2model.num_copes' )]),
554+ (modelfit , fixed_fx , [('outputspec.dof_file' ,
509555 'inputspec.dof_files' ),
510- (('outputspec.varcopes' ,
511- sort_copes ),
512- 'inputspec.varcopes' ),
513- (('outputspec.copes' , num_copes ),
514- 'l2model.num_copes' ),
515- ])
556+ ])
516557 ])
517558
518559 wf .connect (preproc , 'outputspec.mean' , registration , 'inputspec.mean_image' )
0 commit comments