<jupyter_start><jupyter_text>01 Using Nipype to load fMRI data===================== Date: Feb 6 2018 Author: Farahana We will use new Python Package called Nipype to deal with fMRI data. There are many different ways to get the data using this package. We will use *SelectFiles* for this step.<jupyter_code>from nipype import SelectFiles, Node<jupyter_output>/home/farahana/anaconda3/lib/python3.5/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters<jupyter_text>There are few informations we have to know before hand, such as;1. the directory2. the string3. how to import/design of new importAs example, we are using ds052 data from OpenfMRI.1. Directory: **/home/farahana/Documents/dataset/ds052/**   2. String;    *anat*: **/ds052/sub-01/anat/sub-01_run-1_T1w.nii.gz**        *func*: **/ds052/sub-01/func/sub-01_task-reversalweatherprediction_run-1_bold.nii.gz**         3. How to import:    ~~~  /ds052/sub-{subject_id}/anat/sub-{subject_id}_run-{ses_no}_T1w.nii.gz/ds052/sub-{subject_id}/func/sub-{subject_id}_task-{task_name}_run-{ses_no}_bold.nii.gz~~~  One subject calls<jupyter_code># The template string
templates_1s = { 'anat' : 'sub-{subject_id}/anat/sub-{subject_id}_run-{ses_no}_T1w.nii.gz',
              'func' : 'sub-{subject_id}/func/sub-{subject_id}_task-{task_name}_run-{ses_no}_bold.nii.gz'}
# How to address and import using SelectFiles node
sf_1s = Node(SelectFiles(templates_1s),
          name='selectfiles')
# Location of the dataset folder
sf_1s.inputs.base_directory = '/home/farahana/Documents/dataset/ds052'<jupyter_output><empty_output><jupyter_text>> Feed {}-based placeholder strings with values~~~pythonsf.inputs.subject_id = '01'-'14'sf.inputs.ses_no = "1","2"sf.inputs.task_name = 'reversalweatherprediction','weatherprediction'~~~<jupyter_code># We will feed the {}-based placeholder strings with values
sf_1s.inputs.subject_id = '01'
sf_1s.inputs.ses_no = "1"
sf_1s.inputs.task_name = 'reversalweatherprediction'<jupyter_output><empty_output><jupyter_text>Then, we can view the output:<jupyter_code>sf_1s.run().outputs<jupyter_output>180211-15:41:10,80 workflow INFO:
	 Executing node selectfiles in dir: /tmp/tmp7gh93_6v/selectfiles
180211-15:41:10,83 workflow INFO:
	 Running node "selectfiles" ("nipype.interfaces.io.SelectFiles").<jupyter_text>Multiple subjects calls<jupyter_code># The template string
templates = { 'anat' : 'sub-*/anat/sub-*_run-{ses_no}_T1w.nii.gz',
              'func' : 'sub-*/func/sub-*_task-{task_name}_run-{ses_no}_bold.nii.gz'}
# How to address and import using SelectFiles node
sf = Node(SelectFiles(templates),
          name='selectfiles')
sf.inputs.base_directory = '/home/farahana/Documents/dataset/ds052'
# We will feed the {}-based placeholder strings with values
sf.inputs.ses_no = "1"
sf.inputs.task_name = 'reversalweatherprediction'
sf.run().outputs<jupyter_output>180211-15:41:12,984 workflow INFO:
	 Executing node selectfiles in dir: /tmp/tmpjmxoas5c/selectfiles
180211-15:41:12,987 workflow INFO:
	 Running node "selectfiles" ("nipype.interfaces.io.SelectFiles").<jupyter_text>Experiment ParametersWe will specify the important parameters here such as the output and working directories, smoothing widths and other pre-processing workflows params.<jupyter_code>experiment_dir = '/output'
output_dir = 'datasink'
working_dir = 'workingdir'

# Smoothing widths to apply
fwhm = [4, 8]

# Isometric resample of functional images to voxel size (in mm)
iso_size = 4<jupyter_output><empty_output><jupyter_text>Let us try the example of preprocessing workflow shown in nipype website: [nipype_tutorial](https://miykael.github.io/nipype_tutorial/notebooks/example_preprocessing.html)  Interfaces<jupyter_code>%matplotlib inline
from nilearn.plotting import plot_img, plot_anat, plot_stat_map

# Import BET from the FSL interface
from nipype.interfaces.fsl import BET<jupyter_output><empty_output><jupyter_text>Create Node<jupyter_code>#---- Form 1
skullstrip = BET(in_file="/home/farahana/Documents/dataset/ds052/sub-01/anat/sub-01_run-2_T1w.nii.gz",
                out_file = "output/node_T1w_bet.nii.gz", mask = True)
#---- Form 2
#skullstrip.inputs.in_file = "/home/farahana/Documents/dataset/ds052/sub-01/anat/sub-01_run-2_T1w.nii.gz"
#skullstrip.inputs.out_file = "output/node_T1w_bet.nii"
# Command line execution
# skullstrip.cmdline
# !bet /home/farahana/Documents/dataset/ds052/sub-01/anat/sub-01_run-2_T1w.nii.gz output/node_T1w_bet.nii.gz<jupyter_output><empty_output><jupyter_text>Run the BET<jupyter_code>skullstrip.run()<jupyter_output><empty_output><jupyter_text>Plotting the input and output of BET (Brain Extraction)<jupyter_code>plot_anat('/home/farahana/Documents/dataset/ds052/sub-01/anat/sub-01_run-2_T1w.nii.gz', cut_coords=(36, -27, 30))
plot_anat('output/node_T1w_bet.nii.gz', cut_coords=(36, -27, 30))
# plot_stat_map('output/node_T1w_bet.nii.gz', cut_coords=(36, -27, 30))<jupyter_output><empty_output><jupyter_text>BET with func datas<jupyter_code>skullstrip_func = BET(in_file="/home/farahana/Documents/dataset/ds052/sub-01/func/sub-01_task-reversalweatherprediction_run-1_bold.nii.gz",
                      out_file = "output/func_T1w_bet.nii.gz")
skullstrip_func.run()
plot_anat('output/func_T1w_bet.nii.gz', cut_coords=(36, -27, 30))<jupyter_output><empty_output><jupyter_text>Nodes for main workflow<jupyter_code># ExtractROI - skip dummy scans
extract = Node(ExtractROI(t_min=4, t_size=-1),
               output_type='NIFTI',
               name="extract")

# MCFLIRT - motion correction
mcflirt = Node(MCFLIRT(mean_vol=True,
                       save_plots=True,
                       output_type='NIFTI'),
               name="mcflirt")

# SliceTimer - correct for slice wise acquisition
slicetimer = Node(SliceTimer(index_dir=False,
                             interleaved=True,
                             output_type='NIFTI',
                             time_repetition=TR),
                  name="slicetimer")

# Smooth - image smoothing
smooth = Node(Smooth(), name="smooth")
smooth.iterables = ("fwhm", fwhm)

# Artifact Detection - determines outliers in functional images
art = Node(ArtifactDetect(norm_threshold=2,
                          zintensity_threshold=3,
                          mask_type='spm_global',
                          parameter_source='FSL',
                          use_differences=[True, False],
                          plot_type='svg'),
           name="art")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125262211567276, 'fraction_numerical': 0.03356308061132754, 'mean_word_length': 4.370072405470635, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 47, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '193712', 'license': 'MIT', 'n_tokens_mistral': 2522, 'n_tokens_neox': 2312, 'n_words': 431, 'path': '01-Nipype.ipynb', 'repo_name': 'farahanams/Pytorch-fMRI', 'size': '6674'}