Skip to content

Commit 6437bf3

Browse files
author
David Ellis
committed
FIX: Removes unnecessary methods from smri freesurfer workflow utils.
1 parent 6b0fbaa commit 6437bf3

File tree

3 files changed

+1
-261
lines changed

3 files changed

+1
-261
lines changed

nipype/workflows/smri/freesurfer/recode_tables/fs2abc.csv

Lines changed: 0 additions & 41 deletions
This file was deleted.

nipype/workflows/smri/freesurfer/recon.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ def link_masks(subjects_dir, subject_id):
7979
wf.connect(autorecon_resume, "subject_id", outputnode, "subject_id")
8080
return wf
8181

82-
def create_reconall_workflow(name="ReconAll", plugin_args=None,
83-
recoding_file=None):
82+
def create_reconall_workflow(name="ReconAll", plugin_args=None):
8483
"""Creates the ReconAll workflow in nipype.
8584
8685
Example
@@ -510,15 +509,6 @@ def completemethod(datasinked_files, subject_id):
510509
(inputspec, completion, [('subject_id', 'subject_id')]),
511510
(completion, postds_outputspec, [('subject_id', 'subject_id')])])
512511

513-
514-
#### Workflow additions go here
515-
if recoding_file:
516-
from utils import create_recoding_wf
517-
recode = create_recoding_wf(recoding_file)
518-
reconall.connect([(ar3_wf, recode, [('outputspec.aseg', 'inputspec.labelmap')]),
519-
(recode, outputspec, [('outputspec.recodedlabelmap', 'recoded_labelmap')])])
520-
521-
522512
return reconall
523513

524514

nipype/workflows/smri/freesurfer/utils.py

Lines changed: 0 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -493,79 +493,6 @@ def checkenv(exitonfail=False):
493493
else:
494494
print("Warning: " + msg)
495495

496-
def center_volume(in_file):
497-
import SimpleITK as sitk
498-
import os
499-
img = sitk.ReadImage(in_file)
500-
size = img.GetSize()
501-
origin = img.GetOrigin()
502-
new_origin = [0,0,0]
503-
for i, xx in enumerate(origin):
504-
new_origin[i] = float(size[i])/2
505-
if xx < 0:
506-
new_origin[i] = -new_origin[i]
507-
img.SetOrigin(new_origin)
508-
out_file = os.path.abspath(os.path.basename(in_file))
509-
sitk.WriteImage(img, out_file)
510-
return out_file
511-
512-
513-
def recodeLabelMap(in_file, out_file, recode_file):
514-
"""This function has been adapted from BRAINSTools and serves
515-
as a means to recode a label map based upon an input csv
516-
file."""
517-
import SimpleITK as sitk
518-
import os
519-
import csv
520-
import sys
521-
522-
# Convert csv to RECODE_TABLE
523-
CSVFile = open(recode_file, 'rb')
524-
reader = csv.reader(CSVFile)
525-
header = reader.next()
526-
n_cols = len(header)
527-
if n_cols == 4:
528-
# ignore label names
529-
label_keys = (0, 2)
530-
elif n_cols == 2:
531-
# no label names present
532-
label_keys = (0, 1)
533-
else:
534-
# csv does not match format requirements
535-
print("ERROR: input csv file for label recoding does meet requirements")
536-
sys.exit()
537-
538-
# read csv file
539-
RECODE_TABLE = list()
540-
for line in reader:
541-
RECODE_TABLE.append((int(line[label_keys[0]]), int(line[label_keys[1]])))
542-
543-
def minimizeSizeOfImage(outlabels):
544-
"""This function will find the largest integer value in the labelmap, and
545-
cast the image to the smallest possible integer size so that no loss of data
546-
results."""
547-
measureFilt = sitk.StatisticsImageFilter()
548-
measureFilt.Execute(outlabels)
549-
imgMin=measureFilt.GetMinimum()
550-
imgMax=measureFilt.GetMaximum()
551-
if imgMax < (2**8)-1:
552-
outlabels = sitk.Cast( outlabels, sitk.sitkUInt8 )
553-
elif imgMax < (2**16)-1:
554-
outlabels = sitk.Cast( outlabels, sitk.sitkUInt16 )
555-
elif imgMax < (2**32)-1:
556-
outlabels = sitk.Cast( outlabels, sitk.sitkUInt32 )
557-
elif imgMax < (2**64)-1:
558-
outlabels = sitk.Cast( outlabels, sitk.sitkUInt64 )
559-
return outlabels
560-
561-
LabelImage=sitk.Cast(sitk.ReadImage(in_file), sitk.sitkUInt32)
562-
for (old, new) in RECODE_TABLE:
563-
LabelImage = sitk.Cast((LabelImage == old), sitk.sitkUInt32)*(new - old)+LabelImage
564-
LabelImage = minimizeSizeOfImage(LabelImage)
565-
out_file = os.path.abspath(out_file)
566-
sitk.WriteImage(LabelImage, out_file)
567-
return out_file
568-
569496

570497
def create_recoding_wf(in_file, out_file=None):
571498
wf = nipype.Workflow(name="RecodeLabels")
@@ -729,139 +656,3 @@ def createsrcsubj(source_directory):
729656
rh_entorhinal_exvivo_thresh='label/rh.entorhinal_exvivo.thresh.label',
730657
rh_perirhinal_exvivo_thresh='label/rh.perirhinal_exvivo.thresh.label')
731658
return datasource, outfields
732-
733-
def source_long_files_workflow(name="Source_Longitudinal_Files"):
734-
"""Creates a workflow to source the longitudinal files from a freesurfer directory.
735-
This should only be used when the files are not in a prexisting workflow"""
736-
737-
wf = Workflow(name=name)
738-
739-
inputspec = Node(IdentityInterface(fields=['subject_id',
740-
'subjects_dir',
741-
'timepoints']),
742-
name="inputspec")
743-
744-
# TODO: Create outputspec
745-
746-
# grab files from the initial single session run
747-
grab_inittp_files = pe.Node(nio.DataGrabber(), name="Grab_Initial_Files",
748-
infields=['subject_id'],
749-
outfileds=['inputvols', 'iscales', 'ltas'])
750-
grab_inittp_files.inputs.template = '*'
751-
grab_inittp_files.inputs.base_directory = config['subjects_dir']
752-
grab_inittp_files.inputs.field_template = dict(inputvols='%s/mri/orig/0*.mgz',
753-
iscales='%s/mri/orig/0*-iscale.txt',
754-
ltas='%s/mri/orig/0*.lta')
755-
756-
grab_inittp_files.inputs.template_args = dict(inputvols=[['subject_id']],
757-
iscales=[['subject_id']],
758-
ltas=[['subject_id']])
759-
760-
wf.connect([(grab_inittp_files, outputspec, [('inputvols', 'inputspec.in_T1s'),
761-
('iscales', 'inputspec.iscales'),
762-
('ltas', 'inputspec.ltas')])])
763-
764-
merge_norms = pe.Node(Merge(len(config['timepoints'])), name="Merge_Norms")
765-
merge_segs = pe.Node(Merge(len(config['timepoints'])), name="Merge_Segmentations")
766-
merge_segs_noCC = pe.Node(Merge(len(config['timepoints'])), name="Merge_Segmentations_noCC")
767-
merge_template_ltas = pe.Node(Merge(len(config['timepoints'])), name="Merge_Template_ltas")
768-
769-
for i, tp in enumerate(config['timepoints']):
770-
# datasource timepoint files
771-
tp_data_source = pe.Node(FreeSurferSource(), name="{0}_DataSource".format(tp))
772-
tp_data_source.inputs.subject_id = tp
773-
tp_data_source.inputs.subjects_dir = config['subjects_dir']
774-
775-
tp_data_grabber = pe.Node(nio.DataGrabber(), name="{0}_DataGrabber".format(tp),
776-
infields=['tp', 'long_tempate'],
777-
outfileds=['subj_to_template_lta', 'seg_noCC', 'seg_presurf'])
778-
tp_data_grabber.inputs.template = '*'
779-
tp_data_grabber.inputs.base_directory = config['subjects_dir']
780-
tp_data_grabber.inputs.field_template = dict(
781-
subj_to_template_lta='%s/mri/transforms/%s_to_%s.lta',
782-
seg_noCC='%s/mri/aseg.auto_noCCseg.mgz',
783-
seg_presurf='%s/mri/aseg.presurf.mgz',)
784-
785-
tp_data_grabber.inputs.template_args = dict(
786-
subj_to_template_lta=[['long_template', 'tp', 'long_template']],
787-
seg_noCC=[['tp']],
788-
seg_presurf=[['tp']])
789-
790-
wf.connect([(tp_data_source, merge_norms, [('norm',
791-
'in{0}'.format(i))]),
792-
(tp_data_grabber, merge_segs, [('seg_presurf',
793-
'in{0}'.format(i))]),
794-
(tp_data_grabber, merge_segs_noCC, [('seg_noCC',
795-
'in{0}'.format(i))]),
796-
(tp_data_grabber, merge_template_ltas, [('subj_to_template_lta',
797-
'in{0}'.format(i))])])
798-
799-
if tp == config['subject_id']:
800-
wf.connect([(tp_data_source, outputspec, [('wm', 'inputspec.init_wm')]),
801-
(tp_data_grabber, outputspec, [('subj_to_template_lta',
802-
'inputspec.subj_to_template_lta')]),
803-
(tp_data_grabber, outputspec, [('subj_to_template_lta',
804-
'inputspec.subj_to_template_lta')])])
805-
806-
wf.connect([(merge_norms, outputspec, [('out', 'inputspec.alltps_norms')]),
807-
(merge_segs, outputspec, [('out', 'inputspec.alltps_segs')]),
808-
(merge_template_ltas, outputspec, [('out', 'inputspec.alltps_to_template_ltas')]),
809-
(merge_segs_noCC, outputspec, [('out', 'inputspec.alltps_segs_noCC')])])
810-
811-
812-
813-
# datasource files from the template run
814-
ds_template_files = pe.Node(FreeSurferSource(), name="Datasource_Template_Files")
815-
ds_template_files.inputs.subject_id = config['subject_id']
816-
ds_template_files.inputs.subjects_dir = config['subjects_dir']
817-
818-
wf.connect([(ds_template_files, ar1_wf, [('brainmask', 'inputspec.template_brainmask')]),
819-
(ds_template_files, outputspec, [('aseg', 'inputspec.template_aseg')])])
820-
821-
# grab files from template run
822-
grab_template_files = pe.Node(nio.DataGrabber(), name="Grab_Template_Files",
823-
infields=['subject_id', 'long_template'],
824-
outfields=['template_talairach_xfm',
825-
'template_talairach_lta',
826-
'template_talairach_m3z',
827-
'template_label_intensities',
828-
'template_lh_white',
829-
'template_rh_white',
830-
'template_lh_pial',
831-
'template_rh_pial'])
832-
grab_template_files.inputs.template = '*'
833-
grab_template_files.inputs.base_directory = config['subjects_dir']
834-
grab_template_files.inputs.subject_id = config['subject_id']
835-
grab_template_files.inputs.long_template = config['long_template']
836-
grab_template_files.inputs.field_template = dict(
837-
template_talairach_xfm='%s/mri/transfroms/talairach.xfm',
838-
template_talairach_lta='%s/mri/transfroms/talairach.lta',
839-
template_talairach_m3z='%s/mri/transfroms/talairach.m3z',
840-
template_label_intensities='%s/mri/aseg.auto_noCCseg.label_intensities.txt',
841-
template_lh_white='%s/surf/lh.white',
842-
template_rh_white='%s/surf/rh.white',
843-
template_lh_pial='%s/surf/lh.pial',
844-
template_rh_pial='%s/surf/rh.pial')
845-
846-
grab_template_files.inputs.template_args = dict(
847-
template_talairach_xfm=[['long_template']],
848-
template_talairach_lta=[['long_template']],
849-
template_talairach_m3z=[['long_template']],
850-
template_lh_white=[['long_template']],
851-
template_rh_white=[['long_template']],
852-
template_lh_pial=[['long_template']],
853-
template_rh_pial=[['long_template']])
854-
wf.connect([(grab_template_files, outputspec, [('template_talairach_xfm',
855-
'inputspec.template_talairach_xfm'),
856-
('template_talairach_lta',
857-
'inputspec.template_talairach_lta'),
858-
('template_talairach_m3z',
859-
'inputspec.template_talairach_m3z'),
860-
('template_label_intensities',
861-
'inputspec.template_label_intensities'),
862-
('template_lh_white', 'inputspec.template_lh_white'),
863-
('template_rh_white', 'inputspec.template_rh_white'),
864-
('template_lh_pial', 'inputspec.template_lh_pial'),
865-
('template_rh_pial', 'inputspec.template_rh_pial')])
866-
])
867-
return wf

0 commit comments

Comments
 (0)