Skip to content

Instantly share code, notes, and snippets.

@satra
Created September 13, 2011 01:46
Show Gist options
  • Select an option

  • Save satra/1212956 to your computer and use it in GitHub Desktop.

Select an option

Save satra/1212956 to your computer and use it in GitHub Desktop.
test sinking
In [1]:
cd /software/temp/nipype-tutorial/temp/
/software/temp/nipype-tutorial/temp
In [2]:
ls INBET
s1struct.nii s3struct.nii
In [3]:
import nipype.pipeline.engine as pe
from nipype.interfaces.io import DataGrabber, DataSink
from nipype.interfaces.fsl import BET
from nipype.interfaces.utility import Function
import os
In [6]:
def get_base_dir(root, name):
import os
for rootname, dirs, _ in os.walk(root):
for d in dirs:
if name == d:
return rootname
raise ValueError('%s not in any subfolder of %s'%(name, root))
In [12]:
basedir = '/software'
name = 'INBET'
wf = pe.Workflow(name='autoflow', base_dir=os.getcwd())
getroot = pe.Node(Function(input_names=['root', 'name'],
output_names=['base_dir'],
function=get_base_dir),
name = 'getdirectory')
getroot.inputs.root = basedir
getroot.inputs.name = name
source = pe.Node(DataGrabber(infields=['in_dir'],
outfields=['struct']),
name='datasource')
source.inputs.template = '%s/*struct.nii'
source.inputs.in_dir = name
better = pe.MapNode(BET(), iterfield=['in_file'], name='better')
sink = pe.Node(DataSink(parameterization=False), name='sinker')
wf.connect(getroot, 'base_dir', source, 'base_directory')
wf.connect(source, 'struct', better, 'in_file')
wf.connect(getroot, 'base_dir', sink, 'base_directory')
wf.connect(better, 'out_file', sink, 'OUTBET')
In [13]:
wf.run()
INFO:workflow:['execution', 'logging']
INFO:workflow:Running serially.
INFO:workflow:Executing node getdirectory in dir: /software/temp/nipype-tutorial/temp/autoflow/getdirectory
INFO:workflow:Collecting precomputed outputs
INFO:workflow:Executing node datasource in dir: /software/temp/nipype-tutorial/temp/autoflow/datasource
INFO:workflow:Executing node better in dir: /software/temp/nipype-tutorial/temp/autoflow/better
INFO:workflow:Executing node sinker in dir: /software/temp/nipype-tutorial/temp/autoflow/sinker
Out[13]:
<networkx.classes.digraph.DiGraph at 0x10bd23090>
In [14]:
ls INBET
s1struct.nii s3struct.nii
In [15]:
ls OUTBET
s1struct_brain.nii.gz s3struct_brain.nii.gz
@dmwelch
Copy link

dmwelch commented Sep 13, 2011

Is there a typo on line 27? '@better'

@satra
Copy link
Author

satra commented Sep 13, 2011

i just wanted to use the past tense to indicate that these inputs have been bet-ed :)

@dmwelch
Copy link

dmwelch commented Sep 16, 2011

The get_base_dir fails to get all files from a tree since it returns at the first valid instance. A file tree like so:

---->base
    |----> folder1
        |----> INBET/file1.nii.gz
    |----> INBET/file2.nii.gz

will never have file1.nii.gz processed (or file2, depending on the list order of 'dirs'). The correct way is to create a list and return it at the completion of both loops:

def get_base_dir(root, name):
    folderList = []
    for rootname, dirs, _ in os.walk( root ):                                                                    
        for d in dirs:                                                                                                     
            if name == d:                                                                                                     
                folderList.append( dirpath )
    return folderList

or create a generator from the function (if you don't care about multithreading multiple patient runs at a time) and put your workflow in a loop:

def get_base_dir(root, name):
    ...
        ...
            if name == d:                                                                                                     
                yield d

def testsink( root, name ):
    for folder in get_base_dir( root, name ):
        create_workflow( folder, name ) # Workflow with DataGrabber->BET->DataSink

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment