nipy / nipype

Workflows and interfaces for neuroimaging packages
https://nipype.readthedocs.org/en/latest/
Other
745 stars 529 forks source link

FSL motion correction output directory #2504

Closed PkuClosed closed 6 years ago

PkuClosed commented 6 years ago

Summary

I am able to run the FSL head motion correction pipeline, however, I am wondering how I can specify the output directory

Actual behavior

Here is a minimal example:

from nipype.workflows.dmri.fsl.artifacts import hmc_pipeline

dwiname = '/home/HDD1/chuyangye/PD/ST0/converted/SE8/SE8_NODDI-90_20180318074608_6.nii.gz'
bevcsname = '/home/HDD1/chuyangye/PD/ST0/converted/SE8/SE8_NODDI-90_20180318074608_6.bvec'
bvalsname = '/home/HDD1/chuyangye/PD/ST0/converted/SE8/SE8_NODDI-90_20180318074608_6_corrected.bval'
output_mask_name = '/home/HDD1/chuyangye/PD/ST0/converted/SE8/SE8_NODDI-90_20180318074608_6_brain_mask.nii.gz'

hmc = hmc_pipeline()
hmc.inputs.inputnode.in_file = dwiname
hmc.inputs.inputnode.in_bvec = bvecsname
hmc.inputs.inputnode.in_bval = bvalsname
hmc.inputs.inputnode.in_mask = output_mask_name
res_hmc = hmc.run()

I am able to find the output files at /tmp/tmpVToGVC/motion_correct/, for example, /tmp/tmpVToGVC/motion_correct/DWICoregistration/B0Equalize/hmc_ref_corrected_enh.nii.gz. But is there a way I can designate the output directory.

Platform details:

{'nibabel_version': '2.2.1', 'sys_executable': '/Users/pkuclosed/anaconda2/bin/python', 'networkx_version': '2.0', 'numpy_version': '1.13.3', 'sys_platform': 'darwin', 'sys_version': '2.7.14 |Anaconda custom (64-bit)| (default, Oct 5 2017, 02:28:52) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]', 'commit_source': 'installation', 'commit_hash': 'bded0d02', 'pkg_path': '/Users/pkuclosed/anaconda2/lib/python2.7/site-packages/nipype', 'nipype_version': '1.0.1', 'traits_version': '4.6.0', 'scipy_version': '0.19.1'} 1.0.1

Thank you in advance, Chuyang

mgxd commented 6 years ago

@PkuClosed you could either: 1) Assign the the hmc_pipeline's working directory

hmc = hmc_pipeline(base_dir='/outputdir')

2) Make a DataSink node and connect it to hmc_pipeline.outputnode's outputs https://github.com/nipy/nipype/blob/045b28ef9056fac1107bc4f0707859d043f3bfd1/nipype/workflows/dmri/fsl/artifacts.py#L459-L461

PkuClosed commented 6 years ago

Hi @mgxd ,

Thanks for the reply. However, when I tried your first solution, I got the error: hmc_pipeline() got an unexpected keyword argument 'base_dir’

I will take a look at your second solution. But since what I want is simply setting an output directory, I would appreciate it if there is something as simple as solution one.

Chuyang

effigies commented 6 years ago

The simplest approach would be to add hmc.base_dir = '/outputdir' to your setup.

PkuClosed commented 6 years ago

Hi @effigies ,

Thanks a lot. This works. But can I ask an additional question? After I run this, I can see the output at the designated directory. However, the elements in hmc.outputs.outputnode, i.e., out_file, out_bvec, out_xfms, are all None. I thought these were automatically determined. Did I understand this in a wrong way? I would like to feed out_xfms into the next eddy current correction pipeline, so I need to specify it.

Best, Chuyang

effigies commented 6 years ago

The hmc object is a workflow that can be run. When it is run, it is used to construct an execution graph, whose nodes will actually store results, and this will be in the res_hmc object. One component of constructing the execution graph is to remove IdentityInterfaces such as outputnode, so you're not going to find that. If I have an IdentityInterface node B with input from A and output to C, nipype will connect A directly to C in the execution graph.

You can still use this while connecting nodes to workflows, but if you're attempting to debug by looking at the outputs, you're going to run across this issue.

So what you might do is:

meta_wf = pe.Workflow(name='meta_wf', base_dir='/outputdir')
hmc = hmc_pipeline()
# Set up hmc as you like it
eddy = eddy_pipeline()  # Assuming that eddy_pipeline returns a workflow
meta_wf.connect([(hmc, eddy, [('outputnode.out_xfms', 'inputnode.in_xfms')])])

If you're connecting the outputs of a workflow to a Node, you might do:

meta_wf = pe.Workflow(name='meta_wf', base_dir='/outputdir')
hmc = hmc_pipeline()
# Set up hmc as you like it
eddy = pe.Node(Eddy(), name='eddy')  # Assuming that eddy has an input called 'in_xfms'
meta_wf.connect([(hmc, eddy, [('outputnode.out_xfms', 'in_xfms')])])

You can then run hmc and eddy by running meta_wf.run(). hmc and eddy base directories will be subdirectories of the meta_wf base directory, so there's no point in setting them manually.

Does this make the approach a little clearer?

PkuClosed commented 6 years ago

Hi @effigies

Thank you for the explanation and suggestion.

Chuyang

mgxd commented 6 years ago

closing for now - @PkuClosed feel free to reopen if needed