from nipype import SelectFiles, Node

# Create SelectFiles node
templates={'func': '{subject}/{session}/func/{subject}_{session}_task-fingerfootlips_bold.nii.gz'}
sf = Node(SelectFiles(templates),
          name='selectfiles')
sf.inputs.base_directory = '/data/ds000114'
sf.inputs.subject = 'sub-01'
sf.inputs.session = 'ses-test'

"""
Explanation: Data Output
Similarly important to data input is data output. Using a data output module allows you to restructure and rename computed output and to spatial differentiate relevant output files from the temporary computed intermediate files in the working directory. Nipype provides the following modules to handle data stream output:
DataSink
JSONFileSink
MySQLSink
SQLiteSink
XNATSink

This tutorial covers only DataSink. For the rest, see the section interfaces.io on the official homepage.
Preparation
Before we can use DataSink we first need to run a workflow. For this purpose, let's create a very short preprocessing workflow that realigns and smooths one functional image of one subject.
First, let's create a SelectFiles node. For an explanation about this step, see the Data Input tutorial.
End of explanation
"""


from nipype.interfaces.fsl import MCFLIRT, IsotropicSmooth

# Create Motion Correction Node
mcflirt = Node(MCFLIRT(mean_vol=True,
                       save_plots=True),
               name='mcflirt')

# Create Smoothing node
smooth = Node(IsotropicSmooth(fwhm=4),
              name='smooth')

"""
Explanation: Second, let's create the motion correction and smoothing node. For an explanation about this step, see the Nodes and Interfaces tutorial.
End of explanation
"""


from nipype import Workflow
from os.path import abspath

# Create a preprocessing workflow
wf = Workflow(name="preprocWF")
wf.base_dir = '/output/working_dir'

# Connect the three nodes to each other
wf.connect([(sf, mcflirt, [("func", "in_file")]),
            (mcflirt, smooth, [("out_file", "in_file")])])

"""
Explanation: Third, let's create the workflow that will contain those three nodes. For an explanation about this step, see the Workflow tutorial.
End of explanation
"""


wf.run()

"""
Explanation: Now that everything is set up, let's run the preprocessing workflow.
End of explanation
"""


! tree /output/working_dir/preprocWF

"""
Explanation: After the execution of the workflow we have all the data hidden in the working directory 'working_dir'. Let's take a closer look at the content of this folder:
End of explanation
"""


from nipype.interfaces.io import DataSink

# Create DataSink object
sinker = Node(DataSink(), name='sinker')

# Name of the output folder
sinker.inputs.base_directory = '/output/working_dir/preprocWF_output'

# Connect DataSink with the relevant nodes
wf.connect([(smooth, sinker, [('out_file', 'in_file')]),
            (mcflirt, sinker, [('mean_img', 'mean_img'),
                               ('par_file', 'par_file')]),
            ])
wf.run()

"""
Explanation: As we can see, there is way too much content that we might not really care about. To relocate and rename all the files that are relevant for you, you can use DataSink?
DataSink
DataSink is Nipype's standard output module to restructure your output files. It allows you to relocate and rename files that you deem relevant.
Based on the preprocessing pipeline above, let's say we want to keep the smoothed functional images as well as the motion correction paramters. To do this, we first need to create the DataSink object.
End of explanation
"""


! tree /output/working_dir/preprocWF_output

"""
Explanation: Let's take a look at the output folder:
End of explanation
"""


wf.connect([(smooth, sinker, [('out_file', 'preproc.@in_file')]),
            (mcflirt, sinker, [('mean_img', 'preproc.@mean_img'),
                               ('par_file', 'preproc.@par_file')]),
            ])
wf.run()

"""
Explanation: This looks nice. It is what we asked it to do. But having a specific output folder for each individual output file might be suboptimal. So let's change the code above to save the output in one folder, which we will call 'preproc'.
For this we can use the same code as above. We only have to change the connection part:
End of explanation
"""


! tree /output/working_dir/preprocWF_output

"""
Explanation: Let's take a look at the new output folder structure:
End of explanation
"""


# Define substitution strings
substitutions = [('_task-fingerfootlips', ''),
                 ("_ses-test", ""),
                 ('_bold_mcf', ''),
                 ('.nii.gz_mean_reg', '_mean'),
                 ('.nii.gz.par', '.par')]

# Feed the substitution strings to the DataSink node
sinker.inputs.substitutions = substitutions

# Run the workflow again with the substitutions in place
wf.run()

"""
Explanation: This is already much better. But what if you want to rename the output files to represent something a bit readable. For this DataSink has the substitution input field.
For example, let's assume we want to get rid of the string 'task-fingerfootlips' and 'bold_mcf' and that we want to rename the mean file, as well as adapt the file ending of the motion parameter file:
End of explanation
"""


! tree /output/working_dir/preprocWF_output

"""
Explanation: Now, let's take a final look at the output folder:
End of explanation
"""


