Skip to content

Instantly share code, notes, and snippets.

@jminas
Created August 2, 2016 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jminas/2ef2db7461aa379cfc95af0568dbec8c to your computer and use it in GitHub Desktop.
Save jminas/2ef2db7461aa379cfc95af0568dbec8c to your computer and use it in GitHub Desktop.
# coding: utf-8
# # Anatomical Freesurfer ROI's
# In[3]:
"""
Import modules
"""
import os # system functions
import nipype.interfaces.freesurfer as fs # freesurfer
import nipype.interfaces.io as nio # i/o routines
import nipype.interfaces.utility as util # utility
import nipype.pipeline.engine as pe # pypeline engine
"""
Define experiment specific parameters
"""
#to better access the parent folder of the experiment
experiment_dir = '/om/project/gates/wmfilt/openfmri/'
# Tell freesurfer what subjects directory to use
subjects_dir = '/mindhive/xnat/surfaces/GATES'
fs.FSCommand.set_default_subjects_dir(subjects_dir)
#dirnames for anatomical ROI pipeline
aROIOutput = 'aROI_output' #name of aROI datasink
l1contrastDir = 'l1output_02222016/model01/task001' #name of first level datasink
bbregDir = 'xfm/mean2anat' #rerun bbreg?
#list of subjectnames
subjects = ['311']
#list of contrastnumbers the pipeline should consider
contrasts = ['01'] #, '02', '03', '04']
#name of the first session from the first level pipeline
nameOfFirstSession = 'fsl'
# #######################################################################
#########################################################################
"""
Define aROI specific parameters
"""
#Specification of the regions from the original and
#the 2009 segmentation version of the FreeSurfer Color Table
# http://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/AnatomicalROI/FreeSurferColorLUT
ROIregionsorig = ['17']#, '53', '1016', '2016', '52', '13', '2008', '1008', '2029', '1029',
#'6', '8' ,'17', '53', '50', '11', '51', '12', '45', '47', '1027', '2027','1028','2028', '50', '11', '51', '12']
ROIregions2009 = ['11123'] #, '12123', '11127', '12127', '11157', '12157','11115', '11116', '11154', '11155', '12115', '12116', '12154', '12155']
#regions=( 50 11 51 12 52 13 53 17 2008 1008 2029 1029 )
# RT LT
#caudate 50 11
#putamen 51 12
#pallidum 52 13
#hippo 53 17
#IPL 2008 1008
#SPL 2029 1029
#MFG1 1014 #medialorbitofrontal
#MFG2 1003 #caudalmiddlefrontal
#IFG 1154 #ctx-lh-S_frontal_inferior
#MFG2 1155 ctx-lh-S_frontal_middle
#SFG 1156 ctx-lh-S_frontal_superior
#1027 ctx-lh-rostralmiddlefrontal 75 50 125 0
#1028 ctx-lh-superiorfrontal 20 220 160 0
#17 Left-Hippocampus 220 216 20 0
#1122 ctx-lh-G_parietal_inferior-Angular_part
#11112 ctx_lh_G_front_inf-Opercular
#11113 ctx_lh_G_front_inf-Orbital
#11114 ctx_lh_G_front_inf-Triangul
#11115 ctx_lh_G_front_middle
#11127 ctx_lh_G_parietal_sup
#11125 ctx_lh_G_pariet_inf-Angular
#11126 ctx_lh_G_pariet_inf-Suprama
#11153 ctx_lh_S_front_inf
#11154 ctx_lh_S_front_middle
#11155 ctx_lh_S_front_sup
#11157 ctx_lh_S_intrapariet_and_P_trans 143 20 220 0
#12157 ctx_rh_S_intrapariet_and_P_trans 143 20 220 0
#11128 ctx_lh_G_postcentral 20 180 140 0
#12128
#11115 ctx_lh_G_front_middle 140 100 180 0 11115 11116 11154 11155
#11116 ctx_lh_G_front_sup 180 20 140 0
#11154 ctx_lh_S_front_middle 141 20 100 0
#11155 ctx_lh_S_front_sup 61 220 100 0
#6 Left-Cerebellum-Exterior 0 148 0 0
#8 Left-Cerebellum-Cortex 230 148 34 0
#45 Right-Cerebellum-Exterior 0 148 0 0
#47 Right-Cerebellum-Cortex 230 148 34 0
#################################################################
##################################################################
"""
Define nodes
"""
#Node: IdentityInterface - to iterate over subjects and contrasts
inputnode = pe.Node(interface=util.IdentityInterface(fields=['subject_id','contrast_id']),
name='inputnode')
inputnode.iterables = [('subject_id', subjects),
('contrast_id', contrasts)]
#Node: DataGrabber - to grab the input data - copes-Data
datasource = pe.Node(interface=nio.DataGrabber(infields=['subject_id','contrast_id'],
outfields=['contrast']),
name = 'datasource')
datasource.inputs.base_directory = experiment_dir
datasource.inputs.template = '%s/%s/copes/cope%s.nii.gz'
info = dict(contrast = [[l1contrastDir, 'subject_id','contrast_id']])
datasource.inputs.template_args = info
datasource.inputs.sort_filelist = True
#------------
#Node: DataGrabber - to grab the input data - median.nii
preprocsource = pe.Node(interface=nio.DataGrabber(infields=['subject_id','contrast_id'],
outfields=['mean_id']),
name = 'preprocsource')
preprocsource.inputs.base_directory = experiment_dir
preprocsource.inputs.template = '%s/%s/mean/median.nii.gz'
info = dict(mean_id = [[l1contrastDir,'subject_id']])
preprocsource.inputs.template_args = info
preprocsource.inputs.sort_filelist = True
#run bbreg
from nipype.interfaces.freesurfer import BBRegister
bbreg = pe.Node(interface=BBRegister(),name = 'bbreg')
bbreg.inputs.init = 'header'
bbreg.inputs.contrast_type = 't2'
#Node: FreeSurferSource - to grab FreeSurfer files from the recon-all process
fssource = pe.Node(interface=nio.FreeSurferSource(),name='fssource')
fssource.inputs.subjects_dir = subjects_dir
#Node: MRIConvert - to convert files from FreeSurfer format into nifti format
MRIconversion = pe.Node(interface=fs.MRIConvert(),name='MRIconversion')
MRIconversion.inputs.out_type = 'nii'
#Node: ApplyVolTransform - to transform contrasts into anatomical space
# creates 'con_*.anat.bb.mgh' files
transformation = pe.Node(interface=fs.ApplyVolTransform(),name='transformation')
#transformation.inputs.fs_target = True
transformation.inputs.interp = 'nearest'
#Node: SegStatsorig - to extract specified regions of the original segmentation
segmentationorig = pe.Node(interface=fs.SegStats(),name='segmentationorig')
segmentationorig.inputs.segment_id = ROIregionsorig
#Node: SegStats2009 - to extract specified regions of the 2009 segmentation
segmentation2009 = pe.Node(interface=fs.SegStats(),name='segmentation2009')
segmentation2009.inputs.segment_id = ROIregions2009
#Node: Datasink - Creates a datasink node to store important outputs
datasink = pe.Node(interface=nio.DataSink(), name="datasink")
datasink.inputs.base_directory = experiment_dir
datasink.inputs.container = aROIOutput
"""
Definition of anatomical ROI workflow
"""
#Initiation of the ROI extraction workflow
aROIflow = pe.Workflow(name='aROIflow')
aROIflow.base_dir = '/om/scratch/Tue/jminas/aROI' #experiment_dir + '/workingdir_aROI'
#defines if the segmentation version orig or 2009 should be grabbed
def getSegVersion(in_file, version):
if version == 0:
return in_file[0]
else:
return in_file[1]
#Connect up all components
aROIflow.connect([(inputnode, datasource,[('subject_id','subject_id'),
('contrast_id','contrast_id')]),
(inputnode, preprocsource,[('subject_id','subject_id'),
('contrast_id','contrast_id') ]),
(inputnode, fssource,[('subject_id','subject_id')]),
(fssource, segmentationorig,[(('aparc_aseg',getSegVersion,0),
'segmentation_file')]),
(fssource, segmentation2009,[(('aparc_aseg',getSegVersion,1),
'segmentation_file')]),
(inputnode, bbreg,[('subject_id', 'subject_id')]),
(preprocsource, bbreg, [('mean_id', 'source_file')]),
(datasource, MRIconversion,[('contrast','in_file')]),
(MRIconversion, transformation,[('out_file','source_file')]),
(fssource, transformation,[(('aparc_aseg',getSegVersion,0),
'target_file')]),
(bbreg, transformation,[('out_reg_file','reg_file')]),
(transformation, segmentationorig,[('transformed_file',
'in_file')]),
(transformation, segmentation2009,[('transformed_file',
'in_file')]),
(segmentationorig, datasink,[('summary_file', 'segstatorig')]),
(segmentation2009, datasink,[('summary_file', 'segstat2009')]),
])
# In[5]:
"""
Run pipeline and generate graph
"""
#aROIflow.write_graph(graph2use='flat')
#aROIflow.run(plugin='PBS', plugin_args= {'qsub_args':'-q max30 -VX -o ../../errors -e ../../errors'})
aROIflow.run(plugin='SLURM') #, plugin_args = {'sbatch_args': '--mem=6G -c 2'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment