def create_dcmconvert_pipeline(name='dcmconvert'): from nipype.pipeline.engine import Node, Workflow import nipype.interfaces.utility as util from nipype.interfaces.dcmstack import DcmStack # workflow dcmconvert = Workflow(name='dcmconvert') #inputnode inputnode = Node(util.IdentityInterface(fields=['dicoms', 'filename']), name='inputnode') # outputnode outputnode = Node(util.IdentityInterface(fields=['nifti']), name='outputnode') # conversion node converter = Node(DcmStack(embed_meta=True), name='converter') # connections dcmconvert.connect([(inputnode, converter, [('dicoms', 'dicom_files'), ('filename', 'out_format')]), (converter, outputnode, [('out_file', 'nifti')])]) return dcmconvert
def test_DcmStack_outputs(): output_map = dict(out_file=dict(), ) outputs = DcmStack.output_spec() for key, metadata in output_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(outputs.traits()[key], metakey), value
def test_DcmStack_inputs(): input_map = dict(out_format=dict(), embed_meta=dict(), out_ext=dict(usedefault=True, ), dicom_files=dict(mandatory=True, ), exclude_regexes=dict(), include_regexes=dict(), ) inputs = DcmStack.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def test_DcmStack_inputs(): input_map = dict( dicom_files=dict(mandatory=True, ), embed_meta=dict(), exclude_regexes=dict(), force_read=dict(usedefault=True, ), include_regexes=dict(), out_ext=dict(usedefault=True, ), out_format=dict(), ) inputs = DcmStack.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def create_converter_structural_pipeline(working_dir, ds_dir, name='converter_struct'): # initiate workflow converter_wf = Workflow(name=name) converter_wf.base_dir = os.path.join(working_dir, 'LeiCA_resting') # set fsl output fsl.FSLCommand.set_default_output_type('NIFTI_GZ') # inputnode inputnode = Node(util.IdentityInterface(fields=['t1w_dicom']), name='inputnode') outputnode = Node(util.IdentityInterface(fields=['t1w']), name='outputnode') niftisink = Node(nio.DataSink(), name='niftisink') niftisink.inputs.base_directory = os.path.join(ds_dir, 'raw_niftis') # convert to nifti # todo check if geometry bugs attac. use dcm2nii? converter_t1w = Node(DcmStack(embed_meta=True), name='converter_t1w') converter_t1w.plugin_args = {'submit_specs': 'request_memory = 2000'} converter_t1w.inputs.out_format = 't1w' converter_wf.connect(inputnode, 't1w_dicom', converter_t1w, 'dicom_files') # reorient to standard orientation reor_2_std = Node(fsl.Reorient2Std(), name='reor_2_std') converter_wf.connect(converter_t1w, 'out_file', reor_2_std, 'in_file') converter_wf.connect(reor_2_std, 'out_file', outputnode, 't1w') # save original niftis converter_wf.connect(reor_2_std, 'out_file', niftisink, 'sMRI') converter_wf.write_graph(dotfilename='converter_struct', graph2use='flat', format='pdf') return converter_wf
# -*- coding: utf-8 -*- """ Created on Mon Mar 9 18:20:32 2015 @author: m131199 """ import os os.chdir('/Users/m131199/Downloads/LGG-229/MRI_Hd/axial_T1/') from nipype.interfaces.dcmstack import DcmStack stacker = DcmStack() stacker.inputs.dicom_files = '/Users/m131199/Downloads/LGG-229/MRI_Hd/axial_T1/' stacker.run() #result.outputs.out_file import dcmstack from glob import glob src_dcms = glob('/Users/m131199/Downloads/LGG-229/MRI_Hd/axial_T1/*.dcm') stacks = dcmstack.parse_and_stack(src_dcms) stack = stacks.values[0] nii = stack.to_nifti() nii.to_filename('output.nii.gz') #import os #os.chdir('/Users/m131199/Downloads/LGG-229/MRI_Hd/AX_T2_OBL/') #from nipype.interfaces.freesurfer import DICOMConvert #cvt = DICOMConvert() #cvt.inputs.dicom_dir = '/Users/m131199/Downloads/LGG-229/MRI_Hd/AX_T2_OBL/' #cvt.inputs.file_mapping = [('nifti', '*.nii'), ('info', 'dicom*.txt'), ('dti', '*dti.bv*')]
wf.connect(dcm2nii_dwi, "bvecs", bvecs_rename, "in_file") bvals_rename = pe.Node(util.Rename(format_string="DTI_mx_137.bvals"), name="bvals_rename") wf.connect(dcm2nii_dwi, "bvals", bvals_rename, "in_file") ds = pe.Node(nio.DataSink(), name="dwi_datasink") ds.inputs.base_directory = '/scr/kalifornien1/data/nki_enhanced/' ds.inputs.substitutions = [('_subject_id_', '')] ds.inputs.regexp_substitutions = [('_others_rename[0-9]*/', '')] wf.connect(dwi_rename, "out_file", ds, "niftis.@dwi") wf.connect(bvals_rename, "out_file", ds, "niftis.@bvecs") wf.connect(bvecs_rename, "out_file", ds, "niftis.@bvals") for tr in ['645', '1400', '2500']: dcm2nii_others = pe.Node(DcmStack(), name="dcm2nii_%s" % tr) dcm2nii_others.inputs.embed_meta = True dcm2nii_others.plugin_args = {'submit_specs': 'request_memory = 2000'} wf.connect(datagrabber, tr, dcm2nii_others, "dicom_files") others_rename = pe.Node(util.Rename(), name="others_rename%s" % tr) others_rename.inputs.format_string = { '645': 'RfMRI_mx_645.nii.gz', '1400': 'RfMRI_mx_1400.nii.gz', '2500': 'RfMRI_std_2500.nii.gz' }[tr] wf.connect(dcm2nii_others, "out_file", others_rename, "in_file") ds = pe.Node(nio.DataSink(), name="datasink%s" % tr) ds.inputs.base_directory = '/scr/kalifornien1/data/nki_enhanced/' ds.inputs.substitutions = [('_subject_id_', '')]
def create_converter_functional_pipeline(working_dir, ds_dir, name='converter_funct'): # initiate workflow converter_wf = Workflow(name=name) converter_wf.base_dir = os.path.join(working_dir, 'LeiCA_resting') # set fsl output fsl.FSLCommand.set_default_output_type('NIFTI_GZ') # I/O NODE inputnode = Node( util.IdentityInterface(fields=['epi_dicom', 'out_format']), name='inputnode') outputnode = Node(util.IdentityInterface(fields=['epi', 'TR_ms']), name='outputnode') niftisink = Node(nio.DataSink(), name='niftisink') niftisink.inputs.base_directory = os.path.join(ds_dir, 'raw_niftis') niftisink.inputs.substitutions = [('_TR_id_', 'TR_')] # convert to nifti # todo check if geometry bugs attac. use dcm2nii? converter_epi = Node(DcmStack(embed_meta=True), name='converter_epi') converter_epi.plugin_args = {'submit_specs': 'request_memory = 2000'} def reformat_filename_fct(TR_str): return 'rsfMRI_' + TR_str reformat_filename = Node(util.Function(input_names=['TR_str'], output_names=['filename'], function=reformat_filename_fct), name='reformat_filename') converter_wf.connect(inputnode, 'out_format', reformat_filename, 'TR_str') converter_wf.connect(inputnode, 'epi_dicom', converter_epi, 'dicom_files') converter_wf.connect(reformat_filename, 'filename', converter_epi, 'out_format') # reorient to standard orientation reor_2_std = Node(fsl.Reorient2Std(), name='reor_2_std') converter_wf.connect(converter_epi, 'out_file', reor_2_std, 'in_file') converter_wf.connect(reor_2_std, 'out_file', outputnode, 'epi') # save original niftis converter_wf.connect(reor_2_std, 'out_file', niftisink, 'rsfMRI') # GET TR FROM .nii def check_TR_fct(TR): print ' ' print 'check_TR_fct checks validity of TR' print('imported TR is %s' % TR) print ' ' try: float(TR) except ValueError: isvalid_TR = 0 raise Exception( 'ERROR: TR COULD NOT AUTOMATICALLY BE EXTRACTED FROM EPI.\nEXECUTION STOPPED' ) else: isvalid_TR = 1 print 'TR is valid' if isvalid_TR: if float(TR <= 0): raise Exception( 'ERROR: TR NOT VALID (<=0).\nEXECUTION STOPPED') return float(TR) get_TR = Node(ImageInfo(), name='get_TR') converter_wf.connect(reor_2_std, 'out_file', get_TR, 'in_file') check_TR = Node(util.Function(input_names=['TR'], output_names=['TR_ms'], function=check_TR_fct), name='check_TR') converter_wf.connect(get_TR, 'TR', check_TR, 'TR') converter_wf.connect(check_TR, 'TR_ms', outputnode, 'TR_ms') converter_wf.write_graph(dotfilename=converter_wf.name, graph2use='flat', format='pdf') return converter_wf
t1s_gunzip = pe.Node(util.Function(input_names=['file'], output_names=['dir'], function=gunzip), name="t1s_gunzip") wf.connect(datagrabber, "t1s", t1s_gunzip, "file") resting_gunzip = pe.Node(util.Function(input_names=['file'], output_names=['dir'], function=gunzip), name="resting_gunzip") wf.connect(datagrabber, "resting", resting_gunzip, "file") # dcm2nii_t1s = pe.Node(DcmStack(), name="dcm2nii_t1s") # dcm2nii_t1s.inputs.embed_meta = True # wf.connect(t1s_gunzip, "dir", dcm2nii_t1s, "dicom_files") dcm2nii_resting = pe.Node(DcmStack(), name="dcm2nii_resting") dcm2nii_resting.inputs.embed_meta = True def add_asterix(s): return s + "/*" wf.connect(resting_gunzip, ("dir", add_asterix), dcm2nii_resting, "dicom_files") # recon_all = pe.Node(ReconAll(), name="recon_all") # #recon_all.inputs.subjects_dir = "/scr/adenauer1/freesurfer" # wf.connect(dcm2nii_t1s, "out_file", recon_all, "T1_files") # wf.connect(subjects_infosource, "subject_id", recon_all, "subject_id") wf.run(plugin="Linear")