Directory as an output from a DataGrabber Node

--------------------- NiPype datagrabber like functionality for directories ------

I think I’m probably using the wrong module to do this… but I am trying to write a simple script to iterate through my raw DICOM data and convert it into NIFTI. I had modified some of the old code I had written before, but in this case I want to match a directory name, not return a file. I now realize HeudiConv (thanks Satra) would likely work better for my specific use case, but I’d still like to know how I would do this in NiPype… for my use case I need to find the matching DIRECTORY for a pattern, not a matching file…

Basically I have a list of directories

BaseSubjPath = ‘/some/where/data/lives’
DirsWithSubjectData = [Subject1, Subject2, Subject3, Subject4]

For now I am only going to convert the T1 Image, but ideally I’ll add in additional outFields from my DataGrabber node to grab the DTI data, T2 images, etc…

DCM2NII_wf = pe.Workflow(‘stoutDCM2NII_wf’) ## Initialize the workflow
DCM2NII_wf.base_dir = NiPypeOutputDir ## Target to dump the results of the workflow

SessionId_InfoSrc = pe.Node(util.IdentityInterface(fields=[‘imageSessionName’]),name=‘imageSession_Id_InfoSrc’)
SessionId_InfoSrc.iterables = (‘imageSessionName’, DirsWithSubjectData)

Create a datasource… this basically helps me find the individual image files and data sets for an image session

#a single image directory likely consists of DTI data, T2 images, T1 images, etc, etc
datasource = pe.Node(interface=nio.DataGrabber(infields=[‘imageSessionName’], outfields=[‘t1Mprage_dir’]), name=‘datasource’)
datasource.inputs.base_directory = StoutRawData
datasource.inputs.template = ‘%s/t1_mprage_sag_*’
datasource.inputs.sort_filelist = True
datasource.inputs.template_args = dict( t1Mprage_dir=[[‘imageSessionName’]])

Eventually I’ll add in templates for the DTI data, Functional data, etc…

Now create a node for the dicom converter!

dcmConvert = pe.Node(interface=Dcm2nii(),name=‘dcmConvert’)
dcmConvert.inputs.gzip_output = False
dcmConvert.inputs.reorient = False
dcmConvert.inputs.reorient_and_crop = False

DCM2NII_wf.connect(imageSession_Id_InfoSrc, ‘imageSessionName’, datasource, ‘imageSessionName’)
DCM2NII_wf.connect(datasource,‘t1Mprage_dir’, dcmConvert, ‘source_dir’ )

So the work flow as written failed because the datagrabber was/is trying to return files and not a directory…

As a hack, I just created a list of all the t1 directories and bypassed the datagrabber node, and connected it directly to the DCM Converter. However this isn’t particularly elegant, and I’d like to know the NiPyponic method to achieve this so I can make this clean.

T1ImageInputDirectories = glob( oj(RawData,’/t1_mp’))

@dagutman - i just tested this and it appears to return a directory

dg = DataGrabber(infields=['prefix'], outfields=['directory'])
dg.inputs.field_template = dict(directory='miniconda3/envs/%s')
dg.inputs.prefix = 'dev*'
dg.inputs.template = '*'
res =

results in

In [35]:
Out[35]: '/home/satra/miniconda3/envs/dev3pype'

however, at present it simply checks to make sure that the path exists, without caring about it being a file or directory.