diff --git a/nipype/workflows/smri/freesurfer/autorecon1.py b/nipype/workflows/smri/freesurfer/autorecon1.py index b77b65465c..e29a949166 100644 --- a/nipype/workflows/smri/freesurfer/autorecon1.py +++ b/nipype/workflows/smri/freesurfer/autorecon1.py @@ -1,10 +1,7 @@ -import sys -import os -import nipype from nipype.interfaces.utility import Function,IdentityInterface import nipype.pipeline.engine as pe # pypeline engine from nipype.interfaces.freesurfer import * -from .utils import copy_file, copy_files +from .utils import copy_file def checkT1s(T1_files, cw256=False): diff --git a/nipype/workflows/smri/freesurfer/autorecon2.py b/nipype/workflows/smri/freesurfer/autorecon2.py index 2fd85bce13..f09e991439 100644 --- a/nipype/workflows/smri/freesurfer/autorecon2.py +++ b/nipype/workflows/smri/freesurfer/autorecon2.py @@ -1,5 +1,3 @@ -import os -import nipype from nipype.interfaces.utility import Function, IdentityInterface, Merge import nipype.pipeline.engine as pe # pypeline engine from nipype.interfaces.freesurfer import * diff --git a/nipype/workflows/smri/freesurfer/autorecon3.py b/nipype/workflows/smri/freesurfer/autorecon3.py index ffc0eec7b9..656f81188e 100644 --- a/nipype/workflows/smri/freesurfer/autorecon3.py +++ b/nipype/workflows/smri/freesurfer/autorecon3.py @@ -1,10 +1,7 @@ -import os -import nipype -from nipype.interfaces.utility import Function, IdentityInterface, Merge +from nipype.interfaces.utility import IdentityInterface, Merge import nipype.pipeline.engine as pe # pypeline engine from nipype.interfaces.freesurfer import * from .ba_maps import create_ba_maps_wf -from .utils import createsrcsubj from nipype.interfaces.io import DataGrabber def create_AutoRecon3(name="AutoRecon3", qcache=False, plugin_args=None, diff --git a/nipype/workflows/smri/freesurfer/recode_tables/fs2abc.csv b/nipype/workflows/smri/freesurfer/recode_tables/fs2abc.csv deleted file mode 100644 index 76035d3b51..0000000000 --- a/nipype/workflows/smri/freesurfer/recode_tables/fs2abc.csv +++ /dev/null @@ -1,41 +0,0 @@ -orig_label,target_label -41,1 -2,1 -42,2 -3,2 -77,2 -251,2 -252,2 -253,2 -254,2 -255,2 -43,4 -44,4 -4,4 -5,4 -14,4 -15,4 -24,4 -58,21 -50,21 -51,21 -52,21 -26,21 -11,21 -12,21 -13,21 -49,24 -60,24 -28,24 -10,24 -63,25 -53,25 -54,25 -31,25 -17,25 -18,25 -47,11 -8,11 -46,12 -7,12 -16,30 diff --git a/nipype/workflows/smri/freesurfer/recon.py b/nipype/workflows/smri/freesurfer/recon.py index b574314f35..0f1aad8913 100644 --- a/nipype/workflows/smri/freesurfer/recon.py +++ b/nipype/workflows/smri/freesurfer/recon.py @@ -79,8 +79,7 @@ def link_masks(subjects_dir, subject_id): wf.connect(autorecon_resume, "subject_id", outputnode, "subject_id") return wf -def create_reconall_workflow(name="ReconAll", plugin_args=None, - recoding_file=None): +def create_reconall_workflow(name="ReconAll", plugin_args=None): """Creates the ReconAll workflow in nipype. Example @@ -510,15 +509,6 @@ def completemethod(datasinked_files, subject_id): (inputspec, completion, [('subject_id', 'subject_id')]), (completion, postds_outputspec, [('subject_id', 'subject_id')])]) - - #### Workflow additions go here - if recoding_file: - from utils import create_recoding_wf - recode = create_recoding_wf(recoding_file) - reconall.connect([(ar3_wf, recode, [('outputspec.aseg', 'inputspec.labelmap')]), - (recode, outputspec, [('outputspec.recodedlabelmap', 'recoded_labelmap')])]) - - return reconall diff --git a/nipype/workflows/smri/freesurfer/utils.py b/nipype/workflows/smri/freesurfer/utils.py index 7ce98a4642..74d45776d2 100644 --- a/nipype/workflows/smri/freesurfer/utils.py +++ b/nipype/workflows/smri/freesurfer/utils.py @@ -492,376 +492,3 @@ def checkenv(exitonfail=False): sys.exit(2) else: print("Warning: " + msg) - -def center_volume(in_file): - import SimpleITK as sitk - import os - img = sitk.ReadImage(in_file) - size = img.GetSize() - origin = img.GetOrigin() - new_origin = [0,0,0] - for i, xx in enumerate(origin): - new_origin[i] = float(size[i])/2 - if xx < 0: - new_origin[i] = -new_origin[i] - img.SetOrigin(new_origin) - out_file = os.path.abspath(os.path.basename(in_file)) - sitk.WriteImage(img, out_file) - return out_file - - -def recodeLabelMap(in_file, out_file, recode_file): - """This function has been adapted from BRAINSTools and serves - as a means to recode a label map based upon an input csv - file.""" - import SimpleITK as sitk - import os - import csv - import sys - - # Convert csv to RECODE_TABLE - CSVFile = open(recode_file, 'rb') - reader = csv.reader(CSVFile) - header = reader.next() - n_cols = len(header) - if n_cols == 4: - # ignore label names - label_keys = (0, 2) - elif n_cols == 2: - # no label names present - label_keys = (0, 1) - else: - # csv does not match format requirements - print("ERROR: input csv file for label recoding does meet requirements") - sys.exit() - - # read csv file - RECODE_TABLE = list() - for line in reader: - RECODE_TABLE.append((int(line[label_keys[0]]), int(line[label_keys[1]]))) - - def minimizeSizeOfImage(outlabels): - """This function will find the largest integer value in the labelmap, and - cast the image to the smallest possible integer size so that no loss of data - results.""" - measureFilt = sitk.StatisticsImageFilter() - measureFilt.Execute(outlabels) - imgMin=measureFilt.GetMinimum() - imgMax=measureFilt.GetMaximum() - if imgMax < (2**8)-1: - outlabels = sitk.Cast( outlabels, sitk.sitkUInt8 ) - elif imgMax < (2**16)-1: - outlabels = sitk.Cast( outlabels, sitk.sitkUInt16 ) - elif imgMax < (2**32)-1: - outlabels = sitk.Cast( outlabels, sitk.sitkUInt32 ) - elif imgMax < (2**64)-1: - outlabels = sitk.Cast( outlabels, sitk.sitkUInt64 ) - return outlabels - - LabelImage=sitk.Cast(sitk.ReadImage(in_file), sitk.sitkUInt32) - for (old, new) in RECODE_TABLE: - LabelImage = sitk.Cast((LabelImage == old), sitk.sitkUInt32)*(new - old)+LabelImage - LabelImage = minimizeSizeOfImage(LabelImage) - out_file = os.path.abspath(out_file) - sitk.WriteImage(LabelImage, out_file) - return out_file - - -def create_recoding_wf(in_file, out_file=None): - wf = nipype.Workflow(name="RecodeLabels") - - inputspec = nipype.pipeline.Node(nipype.IdentityInterface(['labelmap', - 'recode_file']), - name="inputspec") - inputspec.inputs.recode_file = in_file - - convert_labelmap = nipype.pipeline.Node(fs.MRIConvert(), name="ConvertLabelMap") - convert_labelmap.inputs.in_type = 'mgz' - convert_labelmap.inputs.out_type = 'nii' - convert_labelmap.inputs.out_orientation = 'RAS' - convert_labelmap.inputs.out_file = 'labelmap.nii' - wf.connect([(inputspec, convert_labelmap, [('labelmap', 'in_file')])]) - - recode = nipype.Node(nipype.Function(['in_file', - 'out_file', - 'recode_file'], - ['out_file'], - recodeLabelMap), - name = "RecodeLabelMap") - if out_file == None: - recode.inputs.out_file = 'recodedlabelmap.nii' - else: - recode.inputs.out_file = out_file - - wf.connect([(convert_labelmap, recode, [('out_file', 'in_file')]), - (inputspec, recode, [('recode_file', 'recode_file')])]) - - center_labelmap = nipype.Node(nipype.Function(['in_file'], ['out_file'], - center_volume), - name="CenterLabelMap") - - wf.connect([(recode, center_labelmap, [('out_file', 'in_file')])]) - - outputspec = nipype.Node(nipype.IdentityInterface(['recodedlabelmap']), name="outputspec") - - wf.connect([(center_labelmap, outputspec, [('out_file', 'recodedlabelmap')])]) - return wf - -def createsrcsubj(source_directory): - """ - Returns a node that acts as the datasource for a source subject such as - 'fsaverage' - """ - outfields = ['lh_BA1_exvivo', - 'lh_BA2_exvivo', - 'lh_BA3a_exvivo', - 'lh_BA3b_exvivo', - 'lh_BA4a_exvivo', - 'lh_BA4p_exvivo', - 'lh_BA6_exvivo', - 'lh_BA44_exvivo', - 'lh_BA45_exvivo', - 'lh_V1_exvivo', - 'lh_V2_exvivo', - 'lh_MT_exvivo', - 'lh_entorhinal_exvivo', - 'lh_perirhinal_exvivo', - 'lh_BA1_exvivo_thresh', - 'lh_BA2_exvivo_thresh', - 'lh_BA3a_exvivo_thresh', - 'lh_BA3b_exvivo_thresh', - 'lh_BA4a_exvivo_thresh', - 'lh_BA4p_exvivo_thresh', - 'lh_BA6_exvivo_thresh', - 'lh_BA44_exvivo_thresh', - 'lh_BA45_exvivo_thresh', - 'lh_V1_exvivo_thresh', - 'lh_V2_exvivo_thresh', - 'lh_MT_exvivo_thresh', - 'lh_entorhinal_exvivo_thresh', - 'lh_perirhinal_exvivo_thresh', - 'rh_BA1_exvivo', - 'rh_BA2_exvivo', - 'rh_BA3a_exvivo', - 'rh_BA3b_exvivo', - 'rh_BA4a_exvivo', - 'rh_BA4p_exvivo', - 'rh_BA6_exvivo', - 'rh_BA44_exvivo', - 'rh_BA45_exvivo', - 'rh_V1_exvivo', - 'rh_V2_exvivo', - 'rh_MT_exvivo', - 'rh_entorhinal_exvivo', - 'rh_perirhinal_exvivo', - 'rh_BA1_exvivo_thresh', - 'rh_BA2_exvivo_thresh', - 'rh_BA3a_exvivo_thresh', - 'rh_BA3b_exvivo_thresh', - 'rh_BA4a_exvivo_thresh', - 'rh_BA4p_exvivo_thresh', - 'rh_BA6_exvivo_thresh', - 'rh_BA44_exvivo_thresh', - 'rh_BA45_exvivo_thresh', - 'rh_V1_exvivo_thresh', - 'rh_V2_exvivo_thresh', - 'rh_MT_exvivo_thresh', - 'rh_entorhinal_exvivo_thresh', - 'rh_perirhinal_exvivo_thresh'] - datasource = pe.Node(nio.nio.DataGrabber(outfields=outfields), name="Source_Subject") - datasource.inputs.base_directory = source_directory - datasource.inputs.template = '*' - datasource.inputs.field_template = dict( - lh_BA1_exvivo='label/lh.BA1_exvivo.label', - lh_BA2_exvivo='label/lh.BA2_exvivo.label', - lh_BA3a_exvivo='label/lh.BA3a_exvivo.label', - lh_BA3b_exvivo='label/lh.BA3b_exvivo.label', - lh_BA4a_exvivo='label/lh.BA4a_exvivo.label', - lh_BA4p_exvivo='label/lh.BA4p_exvivo.label', - lh_BA6_exvivo='label/lh.BA6_exvivo.label', - lh_BA44_exvivo='label/lh.BA44_exvivo.label', - lh_BA45_exvivo='label/lh.BA45_exvivo.label', - lh_V1_exvivo='label/lh.V1_exvivo.label', - lh_V2_exvivo='label/lh.V2_exvivo.label', - lh_MT_exvivo='label/lh.MT_exvivo.label', - lh_entorhinal_exvivo='label/lh.entorhinal_exvivo.label', - lh_perirhinal_exvivo='label/lh.perirhinal_exvivo.label', - lh_BA1_exvivo_thresh='label/lh.BA1_exvivo.thresh.label', - lh_BA2_exvivo_thresh='label/lh.BA2_exvivo.thresh.label', - lh_BA3a_exvivo_thresh='label/lh.BA3a_exvivo.thresh.label', - lh_BA3b_exvivo_thresh='label/lh.BA3b_exvivo.thresh.label', - lh_BA4a_exvivo_thresh='label/lh.BA4a_exvivo.thresh.label', - lh_BA4p_exvivo_thresh='label/lh.BA4p_exvivo.thresh.label', - lh_BA6_exvivo_thresh='label/lh.BA6_exvivo.thresh.label', - lh_BA44_exvivo_thresh='label/lh.BA44_exvivo.thresh.label', - lh_BA45_exvivo_thresh='label/lh.BA45_exvivo.thresh.label', - lh_V1_exvivo_thresh='label/lh.V1_exvivo.thresh.label', - lh_V2_exvivo_thresh='label/lh.V2_exvivo.thresh.label', - lh_MT_exvivo_thresh='label/lh.MT_exvivo.thresh.label', - lh_entorhinal_exvivo_thresh='label/lh.entorhinal_exvivo.thresh.label', - lh_perirhinal_exvivo_thresh='label/lh.perirhinal_exvivo.thresh.label', - rh_BA1_exvivo='label/rh.BA1_exvivo.label', - rh_BA2_exvivo='label/rh.BA2_exvivo.label', - rh_BA3a_exvivo='label/rh.BA3a_exvivo.label', - rh_BA3b_exvivo='label/rh.BA3b_exvivo.label', - rh_BA4a_exvivo='label/rh.BA4a_exvivo.label', - rh_BA4p_exvivo='label/rh.BA4p_exvivo.label', - rh_BA6_exvivo='label/rh.BA6_exvivo.label', - rh_BA44_exvivo='label/rh.BA44_exvivo.label', - rh_BA45_exvivo='label/rh.BA45_exvivo.label', - rh_V1_exvivo='label/rh.V1_exvivo.label', - rh_V2_exvivo='label/rh.V2_exvivo.label', - rh_MT_exvivo='label/rh.MT_exvivo.label', - rh_entorhinal_exvivo='label/rh.entorhinal_exvivo.label', - rh_perirhinal_exvivo='label/rh.perirhinal_exvivo.label', - rh_BA1_exvivo_thresh='label/rh.BA1_exvivo.thresh.label', - rh_BA2_exvivo_thresh='label/rh.BA2_exvivo.thresh.label', - rh_BA3a_exvivo_thresh='label/rh.BA3a_exvivo.thresh.label', - rh_BA3b_exvivo_thresh='label/rh.BA3b_exvivo.thresh.label', - rh_BA4a_exvivo_thresh='label/rh.BA4a_exvivo.thresh.label', - rh_BA4p_exvivo_thresh='label/rh.BA4p_exvivo.thresh.label', - rh_BA6_exvivo_thresh='label/rh.BA6_exvivo.thresh.label', - rh_BA44_exvivo_thresh='label/rh.BA44_exvivo.thresh.label', - rh_BA45_exvivo_thresh='label/rh.BA45_exvivo.thresh.label', - rh_V1_exvivo_thresh='label/rh.V1_exvivo.thresh.label', - rh_V2_exvivo_thresh='label/rh.V2_exvivo.thresh.label', - rh_MT_exvivo_thresh='label/rh.MT_exvivo.thresh.label', - rh_entorhinal_exvivo_thresh='label/rh.entorhinal_exvivo.thresh.label', - rh_perirhinal_exvivo_thresh='label/rh.perirhinal_exvivo.thresh.label') - return datasource, outfields - -def source_long_files_workflow(name="Source_Longitudinal_Files"): - """Creates a workflow to source the longitudinal files from a freesurfer directory. - This should only be used when the files are not in a prexisting workflow""" - - wf = Workflow(name=name) - - inputspec = Node(IdentityInterface(fields=['subject_id', - 'subjects_dir', - 'timepoints']), - name="inputspec") - - # TODO: Create outputspec - - # grab files from the initial single session run - grab_inittp_files = pe.Node(nio.DataGrabber(), name="Grab_Initial_Files", - infields=['subject_id'], - outfileds=['inputvols', 'iscales', 'ltas']) - grab_inittp_files.inputs.template = '*' - grab_inittp_files.inputs.base_directory = config['subjects_dir'] - grab_inittp_files.inputs.field_template = dict(inputvols='%s/mri/orig/0*.mgz', - iscales='%s/mri/orig/0*-iscale.txt', - ltas='%s/mri/orig/0*.lta') - - grab_inittp_files.inputs.template_args = dict(inputvols=[['subject_id']], - iscales=[['subject_id']], - ltas=[['subject_id']]) - - wf.connect([(grab_inittp_files, outputspec, [('inputvols', 'inputspec.in_T1s'), - ('iscales', 'inputspec.iscales'), - ('ltas', 'inputspec.ltas')])]) - - merge_norms = pe.Node(Merge(len(config['timepoints'])), name="Merge_Norms") - merge_segs = pe.Node(Merge(len(config['timepoints'])), name="Merge_Segmentations") - merge_segs_noCC = pe.Node(Merge(len(config['timepoints'])), name="Merge_Segmentations_noCC") - merge_template_ltas = pe.Node(Merge(len(config['timepoints'])), name="Merge_Template_ltas") - - for i, tp in enumerate(config['timepoints']): - # datasource timepoint files - tp_data_source = pe.Node(FreeSurferSource(), name="{0}_DataSource".format(tp)) - tp_data_source.inputs.subject_id = tp - tp_data_source.inputs.subjects_dir = config['subjects_dir'] - - tp_data_grabber = pe.Node(nio.DataGrabber(), name="{0}_DataGrabber".format(tp), - infields=['tp', 'long_tempate'], - outfileds=['subj_to_template_lta', 'seg_noCC', 'seg_presurf']) - tp_data_grabber.inputs.template = '*' - tp_data_grabber.inputs.base_directory = config['subjects_dir'] - tp_data_grabber.inputs.field_template = dict( - subj_to_template_lta='%s/mri/transforms/%s_to_%s.lta', - seg_noCC='%s/mri/aseg.auto_noCCseg.mgz', - seg_presurf='%s/mri/aseg.presurf.mgz',) - - tp_data_grabber.inputs.template_args = dict( - subj_to_template_lta=[['long_template', 'tp', 'long_template']], - seg_noCC=[['tp']], - seg_presurf=[['tp']]) - - wf.connect([(tp_data_source, merge_norms, [('norm', - 'in{0}'.format(i))]), - (tp_data_grabber, merge_segs, [('seg_presurf', - 'in{0}'.format(i))]), - (tp_data_grabber, merge_segs_noCC, [('seg_noCC', - 'in{0}'.format(i))]), - (tp_data_grabber, merge_template_ltas, [('subj_to_template_lta', - 'in{0}'.format(i))])]) - - if tp == config['subject_id']: - wf.connect([(tp_data_source, outputspec, [('wm', 'inputspec.init_wm')]), - (tp_data_grabber, outputspec, [('subj_to_template_lta', - 'inputspec.subj_to_template_lta')]), - (tp_data_grabber, outputspec, [('subj_to_template_lta', - 'inputspec.subj_to_template_lta')])]) - - wf.connect([(merge_norms, outputspec, [('out', 'inputspec.alltps_norms')]), - (merge_segs, outputspec, [('out', 'inputspec.alltps_segs')]), - (merge_template_ltas, outputspec, [('out', 'inputspec.alltps_to_template_ltas')]), - (merge_segs_noCC, outputspec, [('out', 'inputspec.alltps_segs_noCC')])]) - - - - # datasource files from the template run - ds_template_files = pe.Node(FreeSurferSource(), name="Datasource_Template_Files") - ds_template_files.inputs.subject_id = config['subject_id'] - ds_template_files.inputs.subjects_dir = config['subjects_dir'] - - wf.connect([(ds_template_files, ar1_wf, [('brainmask', 'inputspec.template_brainmask')]), - (ds_template_files, outputspec, [('aseg', 'inputspec.template_aseg')])]) - - # grab files from template run - grab_template_files = pe.Node(nio.DataGrabber(), name="Grab_Template_Files", - infields=['subject_id', 'long_template'], - outfields=['template_talairach_xfm', - 'template_talairach_lta', - 'template_talairach_m3z', - 'template_label_intensities', - 'template_lh_white', - 'template_rh_white', - 'template_lh_pial', - 'template_rh_pial']) - grab_template_files.inputs.template = '*' - grab_template_files.inputs.base_directory = config['subjects_dir'] - grab_template_files.inputs.subject_id = config['subject_id'] - grab_template_files.inputs.long_template = config['long_template'] - grab_template_files.inputs.field_template = dict( - template_talairach_xfm='%s/mri/transfroms/talairach.xfm', - template_talairach_lta='%s/mri/transfroms/talairach.lta', - template_talairach_m3z='%s/mri/transfroms/talairach.m3z', - template_label_intensities='%s/mri/aseg.auto_noCCseg.label_intensities.txt', - template_lh_white='%s/surf/lh.white', - template_rh_white='%s/surf/rh.white', - template_lh_pial='%s/surf/lh.pial', - template_rh_pial='%s/surf/rh.pial') - - grab_template_files.inputs.template_args = dict( - template_talairach_xfm=[['long_template']], - template_talairach_lta=[['long_template']], - template_talairach_m3z=[['long_template']], - template_lh_white=[['long_template']], - template_rh_white=[['long_template']], - template_lh_pial=[['long_template']], - template_rh_pial=[['long_template']]) - wf.connect([(grab_template_files, outputspec, [('template_talairach_xfm', - 'inputspec.template_talairach_xfm'), - ('template_talairach_lta', - 'inputspec.template_talairach_lta'), - ('template_talairach_m3z', - 'inputspec.template_talairach_m3z'), - ('template_label_intensities', - 'inputspec.template_label_intensities'), - ('template_lh_white', 'inputspec.template_lh_white'), - ('template_rh_white', 'inputspec.template_rh_white'), - ('template_lh_pial', 'inputspec.template_lh_pial'), - ('template_rh_pial', 'inputspec.template_rh_pial')]) - ]) - return wf