diff --git a/bip/main.py b/bip/main.py
index b23e0258c35787af1ea8ba34a8cd8c21aeb1fd07..fef7f0edef8ba77b444a85e43cff1339bbcdb6a1 100755
--- a/bip/main.py
+++ b/bip/main.py
@@ -1,4 +1,14 @@
 #!/usr/bin/env python
+#
+# main.py - Main BIP entry.
+#
+# Author: Fidel Alfaro Almagro <fidel.alfaroalmagro@ndcn.ox.ac.uk>
+# Author: Paul McCarthy <pauldmccarthy@gmail.com>
+# Author: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
+#
+# pylint: disable=C0103,E0602,C0114,C0115,C0116,R0913,R0914,R0915
+# pylint: disable=W1201,R0912
+#
 '''
  WIN-FMRIB, Oxford University
  $28-Nov-2022 16:00:00$
@@ -43,7 +53,8 @@ class Context:
         self.FSLDIR     = os.environ['FSLDIR']
         self.gdc        = self.get_data('GDC/UKB.txt')
 
-        with open(self.get_data('dMRI/autoptx/struct_list.json'), 'r') as f:
+        with open(self.get_data('dMRI/autoptx/struct_list.json'), 'r',
+                                encoding="utf-8") as f:
             self.tract_struct = json.load(f)
 
     @property
@@ -56,12 +67,12 @@ class Context:
 
     def get_standard(self, fileName):
         fileName.replace('/', os.sep)
-        basedir = self.FSLDIR + os.sep + op.foin('data', 'standard') + os.sep
+        basedir = self.FSLDIR + os.sep + op.join('data', 'standard') + os.sep
         return basedir + fileName
 
     def get_atlas(self, fileName):
         fileName.replace('/', os.sep)
-        basedir = self.FSLDIR + os.sep + op.foin('data', 'atlases') + os.sep
+        basedir = self.FSLDIR + os.sep + op.join('data', 'atlases') + os.sep
         return basedir + fileName
 
     def get_data(self, fileName):
@@ -98,8 +109,10 @@ def parseArguments(ctx):
                             'Distiortion Correction). \n' +
                               '   Options:  \n' +
                               '    none--> No GDC is performed' +
-                              '    <path to file> --> Uses the user-pecified gradients file' +
-                              '    --> If this option is not used, the default is the UKB',
+                              '    <path to file> --> Uses the user-pecified '+\
+                              'gradients file' +
+                              '    --> If this option is not used, the '+\
+                              'default is the UKB',
                         dest="coeff")
     parser.add_argument("-C", "--coils_SWI",action="store",nargs="?",default=32,
                         help='Number of coils for SWI data. Default: 32. 0 ' +\
@@ -149,9 +162,11 @@ def parseArguments(ctx):
                              '  - 0: Run the whole pipeline with default parameters \n' +
                              '  - 1: Run   (CPU) 1st block (up until eddy) \n' +
                              '  - 2: Run   (GPU) 2nd block (eddy) \n' +
-                             '  - 3: Run   (CPU) 3rd block (after eddy, before bedpostX + probtrackX) \n' +
+                             '  - 3: Run   (CPU) 3rd block (after eddy, '+\
+                             'before bedpostX + probtrackX) \n' +
                              '  - 4: Run   (GPU) 4th block (bedpostX + probtrackX) \n' +
-                             '  - 5: Run   (CPU) 5th block (everything after bedpostX + probtrackX) \n' +
+                             '  - 5: Run   (CPU) 5th block (everything after '+\
+                             'bedpostX + probtrackX) \n' +
                              '  - 20: Run  (CPU) 2nd block (eddy) \n '+
                              '  - 40: Run  (CPU) 4th block (bedpostX + probtrackX) \n ' +
                              '  - 123: Run (CPU) 1st, 2nd, & 3rd block (up until bedpostX) \n ',
diff --git a/bip/pipelines/IDPs_gen/IDP_SWI_T2star.py b/bip/pipelines/IDPs_gen/IDP_SWI_T2star.py
index d030aa850460a7b9e643c3cc73f50d0953641ffe..8b50bbfbada4787de5486c03736f7a76cbc3e224 100755
--- a/bip/pipelines/IDPs_gen/IDP_SWI_T2star.py
+++ b/bip/pipelines/IDPs_gen/IDP_SWI_T2star.py
@@ -28,7 +28,8 @@ def run(ctx,
 
         result = ("NaN " * 14).strip()
 
-        if op.exists(T1_first_all_fast_firstseg):
+        if T1_first_all_fast_firstseg is not None and \
+            op.exists(T1_first_all_fast_firstseg):
             v=wrappers.fslstats(T2star_to_T1, K=T1_first_all_fast_firstseg).p(50).run()
 
             # Check that the outputs are OK
diff --git a/bip/pipelines/IDPs_gen/IDP_T1_FIRST_vols.py b/bip/pipelines/IDPs_gen/IDP_T1_FIRST_vols.py
index 7b270d5bb32ae4d7a81a4f1f319bdc87704f149e..b08ff80782f1fe90c26b48ccb0a777f7454c42ed 100755
--- a/bip/pipelines/IDPs_gen/IDP_T1_FIRST_vols.py
+++ b/bip/pipelines/IDPs_gen/IDP_T1_FIRST_vols.py
@@ -26,7 +26,8 @@ def run(ctx,
     with redirect_logging(job_name(run), outdir=logs_dir):
         result = ("NaN " * 15).strip()
 
-        if op.exists(T1_first_all_fast_firstseg):
+        if T1_first_all_fast_firstseg is not None and \
+            op.exists(T1_first_all_fast_firstseg):
             v=wrappers.fslstats(T1_first_all_fast_firstseg).H(58,0.5,58.5).run()
             # Check that the outputs are OK
             if len(v) == 58:
diff --git a/bip/pipelines/IDPs_gen/IDP_T1_GM_parcellation.py b/bip/pipelines/IDPs_gen/IDP_T1_GM_parcellation.py
index e746cf73b46e0bc9d667f6ac29ce30bc3e03515f..3ea8e84296e063448931f0bb01d8bdea2493f8c5 100755
--- a/bip/pipelines/IDPs_gen/IDP_T1_GM_parcellation.py
+++ b/bip/pipelines/IDPs_gen/IDP_T1_GM_parcellation.py
@@ -26,7 +26,7 @@ def run(ctx,
         IDP_T1_GM_parcellation:  Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         result = ("NaN " * 139).strip()
 
diff --git a/bip/pipelines/IDPs_gen/IDP_T1_SIENAX.py b/bip/pipelines/IDPs_gen/IDP_T1_SIENAX.py
index ec2b6e4dcf22618f8b513a53d795c79fde2f7928..10b177ec35a1ce8ff0bec7599c003dba71c75752 100755
--- a/bip/pipelines/IDPs_gen/IDP_T1_SIENAX.py
+++ b/bip/pipelines/IDPs_gen/IDP_T1_SIENAX.py
@@ -28,7 +28,7 @@ def run(ctx,
 
         result = ("NaN " * 11).strip()
 
-        if op.exists(T1_sienax_report):
+        if T1_sienax_report is not None and op.exists(T1_sienax_report):
             with open(T1_sienax_report, "r",  encoding="utf-8") as f:
                 text = f.readlines()
                 # indices that we are interested in
diff --git a/bip/pipelines/IDPs_gen/IDP_T1_align_to_std.py b/bip/pipelines/IDPs_gen/IDP_T1_align_to_std.py
index b5a51cd0d9fe7cbbbdbcd5ce5fadcddea03c3715..f8af17422ee44870083473f73d845552ea9ab1cc 100755
--- a/bip/pipelines/IDPs_gen/IDP_T1_align_to_std.py
+++ b/bip/pipelines/IDPs_gen/IDP_T1_align_to_std.py
@@ -7,6 +7,7 @@
 # Author: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
 #
 # pylint: disable=C0103,E0602,C0114,C0115,C0116,R0913,R0914,R0915
+# pylint: disable=E1101
 #
 
 import os
@@ -28,7 +29,7 @@ def run(ctx,
         IDP_T1_align_to_std:  Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         tmp_jac = op.join(tmp_dir, 'tmpjac.nii.gz')
         tmp_mat = op.join(tmp_dir, 'tmp_mat.mat')
diff --git a/bip/pipelines/IDPs_gen/IDP_T1_noise_ratio.py b/bip/pipelines/IDPs_gen/IDP_T1_noise_ratio.py
index 90b1b6b1ec8cce86f6ef8587f0bb0d03ff3a9c36..725a125f46d5d171885e5acaf9afde7e04c36530 100755
--- a/bip/pipelines/IDPs_gen/IDP_T1_noise_ratio.py
+++ b/bip/pipelines/IDPs_gen/IDP_T1_noise_ratio.py
@@ -26,7 +26,7 @@ def run(ctx,
         IDP_T1_noise_ratio: Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         tmp_SNR = op.join(tmp_dir, 'tmp_SNR.nii.gz')
 
diff --git a/bip/pipelines/IDPs_gen/IDP_all_align_to_T1.py b/bip/pipelines/IDPs_gen/IDP_all_align_to_T1.py
index f33de1d6c4863ab3f72a97ca049853c6d0690460..0fc2b943277b60581e9996d2bb42be68496551e9 100755
--- a/bip/pipelines/IDPs_gen/IDP_all_align_to_T1.py
+++ b/bip/pipelines/IDPs_gen/IDP_all_align_to_T1.py
@@ -30,7 +30,7 @@ def run(ctx,
         IDP_all_align_to_T1:        Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         tmp_mat = op.join(tmp_dir, 'tmp_mat.mat')
 
@@ -41,7 +41,7 @@ def run(ctx,
         for file_name in [T2_FLAIR_brain, fieldmap_iout_to_T1, SWI_to_T1,
                          rfMRI_example_func2highres,tfMRI_example_func2highres]:
 
-            if op.exists(file_name):
+            if file_name is not None and op.exists(file_name):
                 costs1 = wrappers.flirt(src=file_name, ref=T1_brain,
                                         refweight=T1_brain_mask, schedule=MC,
                                         omat=tmp_mat).stdout[0].strip()
diff --git a/bip/pipelines/IDPs_gen/IDP_diff_autoPtx.py b/bip/pipelines/IDPs_gen/IDP_diff_autoPtx.py
index 33988057a4140a5f562395454d5fba878de69dd3..9627c7cd53ef406f51783584744d022bb12dce72 100755
--- a/bip/pipelines/IDPs_gen/IDP_diff_autoPtx.py
+++ b/bip/pipelines/IDPs_gen/IDP_diff_autoPtx.py
@@ -29,7 +29,7 @@ def run(ctx,
         IDP_diff_autoPtx:   Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         autoPtx_all = op.join(tmp_dir, 'autoPtx_all.nii.gz')
         autoPtx_tmp = op.join(tmp_dir, 'autoPtx_tmp.nii.gz')
diff --git a/bip/pipelines/IDPs_gen/IDP_diff_eddy_outliers.py b/bip/pipelines/IDPs_gen/IDP_diff_eddy_outliers.py
index 063c89292ff73badf53dd73de0d412af88aa9b1e..550db9d1b0917895db52e271a08a3bfa64130f66 100755
--- a/bip/pipelines/IDPs_gen/IDP_diff_eddy_outliers.py
+++ b/bip/pipelines/IDPs_gen/IDP_diff_eddy_outliers.py
@@ -26,7 +26,7 @@ def run(ctx,
 
         num_outliers = 0
 
-        if op.exists(eddy_outlier_report):
+        if eddy_outlier_report is not None and op.exists(eddy_outlier_report):
             with open(eddy_outlier_report, "r", encoding="utf-8") as f:
                 num_outliers = str(len(f.readlines()))
         else:
diff --git a/bip/pipelines/IDPs_gen/IDP_func_TSNR.py b/bip/pipelines/IDPs_gen/IDP_func_TSNR.py
index 047762b1e81a4abcb43a1bcbab9f3647c3533987..a60e9be5386283c033567cb3cd17dc637c22cb6a 100755
--- a/bip/pipelines/IDPs_gen/IDP_func_TSNR.py
+++ b/bip/pipelines/IDPs_gen/IDP_func_TSNR.py
@@ -27,7 +27,7 @@ def run(ctx,
         IDP_func_TSNR:            Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         fMRI_SNR = op.join(tmp_dir, 'fMRI_SNR.nii.gz')
 
@@ -35,7 +35,7 @@ def run(ctx,
 
         for file_name in [filtered_func_data, filtered_func_data_clean,
                           tfMRI_filtered_func_data]:
-            if op.exists(file_name):
+            if file_name is not None and op.exists(file_name):
                 wrappers.fslmaths(file_name).Tstd().run(fMRI_SNR)
                 wrappers.fslmaths(file_name).Tmean().div(fMRI_SNR).run(fMRI_SNR)
                 v = 1 / wrappers.fslstats(fMRI_SNR).l(0.1).p(50).run()
diff --git a/bip/pipelines/IDPs_gen/IDP_func_head_motion.py b/bip/pipelines/IDPs_gen/IDP_func_head_motion.py
index 2685a07e6b824dd76223cd806c4e35948e6cb32a..bd341f569d8d670401ca5f1822cfeb2509a87fed 100755
--- a/bip/pipelines/IDPs_gen/IDP_func_head_motion.py
+++ b/bip/pipelines/IDPs_gen/IDP_func_head_motion.py
@@ -26,13 +26,13 @@ def run(ctx,
 
     with redirect_logging(job_name(run), outdir=logs_dir):
 
-        if op.exists(rfMRI_mc_rel_mean):
+        if rfMRI_mc_rel_mean is not None and op.exists(rfMRI_mc_rel_mean):
             with open(rfMRI_mc_rel_mean, "r", encoding="utf-8") as f:
                 val_1 = json.load(f)
         else:
             val_1 = "NaN"
 
-        if op.exists(tfMRI_mc_rel_mean):
+        if tfMRI_mc_rel_mean is not None and op.exists(tfMRI_mc_rel_mean):
             with open(tfMRI_mc_rel_mean, "r", encoding="utf-8") as f:
                 val_2 = json.load(f)
         else:
diff --git a/bip/pipelines/IDPs_gen/IDP_func_task_activation.py b/bip/pipelines/IDPs_gen/IDP_func_task_activation.py
index c713241c476909a4a10c8b23714d22f5fcefc734..8e47aec886137339ac92aa5fc5e8488d16b0f018 100755
--- a/bip/pipelines/IDPs_gen/IDP_func_task_activation.py
+++ b/bip/pipelines/IDPs_gen/IDP_func_task_activation.py
@@ -43,19 +43,22 @@ def run(ctx,
         IDP_func_task_activation:  Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir):
-        
-        if not op.exists(highres2standard_warp):
-            rel_path = op.relpath(T1_to_MNI_warp, highres2standard_warp)
+
+        if highres2standard_warp is not None and \
+            not op.exists(highres2standard_warp):
+            rel_path = op.relpath(T1_to_MNI_warp, 
+                                  op.dirname(highres2standard_warp))
             os.symlink(src=rel_path, dst=highres2standard_warp)
 
-        if not op.exists(highres2standard_warp_inv):
-            rel_path = op.relpath(T1_to_MNI_warp_coef_inv, 
-                                  highres2standard_warp_inv)
+        if highres2standard_warp_inv is not None and \
+            not op.exists(highres2standard_warp_inv):
+            rel_path = op.relpath(T1_to_MNI_warp_coef_inv,
+                                  op.dirname(highres2standard_warp_inv))
             os.symlink(src=rel_path, dst=highres2standard_warp_inv)
 
         for dir_name in [tfMRI_featquery_1_dir, tfMRI_featquery_2_dir,
                          tfMRI_featquery_5_dir, tfMRI_featquery_5a_dir]:
-            if op.exists(dir_name):
+            if dir_name is not None and op.exists(dir_name):
                 shutil.rmtree(dir_name)
 
         group_mask_1  = ctx.get_data("tfMRI_group/groupMask1.nii.gz")
diff --git a/bip/pipelines/IDPs_gen/IDP_subject_COG_table.py b/bip/pipelines/IDPs_gen/IDP_subject_COG_table.py
index 80cbb70d86cdb313a0ab9658e2e7c951f081911a..00f16892cd9ff00a9e5ef781e54536619ea3fb3f 100755
--- a/bip/pipelines/IDPs_gen/IDP_subject_COG_table.py
+++ b/bip/pipelines/IDPs_gen/IDP_subject_COG_table.py
@@ -39,7 +39,7 @@ def run(ctx,
         for file_name in [T1_dcm_txt, T2_dcm_txt, rfMRI_dcm_txt, SWI_dcm_txt,
                           dMRI_dcm_txt, tfMRI_dcm_txt]:
 
-            if op.exists(file_name):
+            if file_name is not None and op.exists(file_name):
                 with open(file_name, 'rt', encoding="utf-8") as f:
                     cont = f.readlines()
                     for x in cont:
diff --git a/bip/pipelines/IDPs_gen/IDP_subject_centre.py b/bip/pipelines/IDPs_gen/IDP_subject_centre.py
index dbdcbde57d14625adb3c27aedc6ce0b24d43919c..55c29e895a9eb9574062dbb28cbb41ce3f1658bc 100755
--- a/bip/pipelines/IDPs_gen/IDP_subject_centre.py
+++ b/bip/pipelines/IDPs_gen/IDP_subject_centre.py
@@ -35,7 +35,7 @@ def run(ctx,
         for file_name in [T1_dcm_txt, T2_dcm_txt, rfMRI_dcm_txt, SWI_dcm_txt,
                           dMRI_dcm_txt, tfMRI_dcm_txt]:
 
-            if op.exists(file_name):
+            if file_name is not None and op.exists(file_name):
                 with open(file_name, 'rt', encoding="utf-8") as f:
                     cont = f.readlines()
 
diff --git a/bip/pipelines/IDPs_gen/IDPs_generator.py b/bip/pipelines/IDPs_gen/IDPs_generator.py
index cf4c32ad449bd39d323b4dbfdfbff75becda4998..0702b2eae9ca7dfb122b370286fb1d0f3236cc7b 100755
--- a/bip/pipelines/IDPs_gen/IDPs_generator.py
+++ b/bip/pipelines/IDPs_gen/IDPs_generator.py
@@ -57,7 +57,7 @@ def run(ctx,
             num_IDPs = len(IDPs_dict[plain_name])
             result_nans = ("NaN " * num_IDPs).strip()
 
-            if op.exists(IDP_file):
+            if IDP_file is not None and op.exists(IDP_file):
                 with open(IDP_file, "r", encoding="utf-8") as f:
                     IDPs_l = f.read().strip().split()
                 if len(IDPs_l) != num_IDPs:
diff --git a/bip/pipelines/dMRI_diff/__init__.py b/bip/pipelines/dMRI_diff/__init__.py
index 5f7840c8c6270ca87158247ff24a4711e3e7ae91..6db1aff73354bf1150d4d723185d2bc9034053f2 100755
--- a/bip/pipelines/dMRI_diff/__init__.py
+++ b/bip/pipelines/dMRI_diff/__init__.py
@@ -12,7 +12,7 @@
 
 import logging
 from bip.utils.log_utils     import job_name
-from bip.pipelines.dMRI_diff import diff_eddy, diff_dtifit, diff_noddi
+from bip.pipelines.dMRI_diff import diff_eddy, diff_dtifit, diff_noddi, diff_gdc
 from bip.pipelines.dMRI_diff import diff_tbss, diff_bedpostx, diff_autoptx
 
 log = logging.getLogger(__name__)
@@ -36,6 +36,9 @@ def add_to_pipeline(ctx, pipe, tree):
     pipe(diff_eddy.run,
          submit=cuda_eddy_dict,
          kwargs={'ctx' : ctx})
+    pipe(diff_gdc.run,
+         submit=dict(jobtime=200, name=job_name(diff_gdc.run, subj)),
+         kwargs={'ctx' : ctx})
     pipe(diff_dtifit.run,
          submit=dict(jobtime=200, name=job_name(diff_dtifit.run, subj)),
          kwargs={'ctx' : ctx})
diff --git a/bip/pipelines/dMRI_diff/diff_autoptx.py b/bip/pipelines/dMRI_diff/diff_autoptx.py
index adc53b83c8b122068baa8e8ba94f2d021729743b..d2b7072d7158b8dc327eff5e2a737e440bf4fd9c 100755
--- a/bip/pipelines/dMRI_diff/diff_autoptx.py
+++ b/bip/pipelines/dMRI_diff/diff_autoptx.py
@@ -55,9 +55,9 @@ def run(ctx,
 
         # Does the protocol defines a second
         # run with inverted seed / target masks?
-        if op.exists(orig_tract_dir + 'invert'):
+        if orig_tract_dir is not None and op.exists(orig_tract_dir + 'invert'):
             symtrack=True
-            if op.exists(waytotal):
+            if waytotal is not None and op.exists(waytotal):
                 os.remove(waytotal)
         else:
             symtrack=False
@@ -67,7 +67,7 @@ def run(ctx,
         copyfile(orig_tract_dir + 'target.nii.gz',  aptx_target)
         copyfile(orig_tract_dir + 'exclude.nii.gz', aptx_exclude)
 
-        if op.exists(waytotal):
+        if waytotal is not None and op.exists(waytotal):
             os.remove(waytotal)
 
         # Building arguments dictionary for probtrackx
@@ -88,7 +88,7 @@ def run(ctx,
 
         # Is there a stop criterion defined
         # in the protocol for this struct?
-        if op.exists(orig_tract_dir + 'stop.nii.gz'):
+        if orig_tract_dir is not None and op.exists(orig_tract_dir+'stop.nii.gz'):
             kwargs['stop'] = aptx_stop
             copyfile(orig_tract_dir + 'stop.nii.gz', aptx_stop)
 
@@ -116,7 +116,7 @@ def run(ctx,
                 way1 = int(f.read())
             with open(waytotal_inv, 'r', encoding="utf-8") as f:
                 way2 = int(f.read())
-            if op.exists(waytotal):
+            if waytotal is not None and op.exists(waytotal):
                 os.remove(waytotal)
             with open(waytotal, 'wt', encoding="utf-8") as f:
                 way = way1 + way2
diff --git a/bip/pipelines/dMRI_diff/diff_bedpostx.py b/bip/pipelines/dMRI_diff/diff_bedpostx.py
index 0fabdf158e105304f2ec4da31152e3c8ba6736e6..92d3f9783e304e3e414172e82c587796cc447e8a 100755
--- a/bip/pipelines/dMRI_diff/diff_bedpostx.py
+++ b/bip/pipelines/dMRI_diff/diff_bedpostx.py
@@ -83,5 +83,5 @@ def run(ctx,
                                        SubjectDir=w + eddy_dir,
                                        TotalNumParts="1", bindir=ctx.FSLDIR)
 
-        if op.exists(bedpostx_data_0):
+        if bedpostx_data_0 is not None and op.exists(bedpostx_data_0):
             os.remove(bedpostx_data_0)
diff --git a/bip/pipelines/dMRI_diff/diff_dtifit.py b/bip/pipelines/dMRI_diff/diff_dtifit.py
index ea4cc46f2611c0a43e9f5b3ba979cd0f24fb9b0c..64fb4dd10e2b6ea005cea63f5fd3426b6476d825 100755
--- a/bip/pipelines/dMRI_diff/diff_dtifit.py
+++ b/bip/pipelines/dMRI_diff/diff_dtifit.py
@@ -11,12 +11,10 @@
 #
 
 import logging
-from shutil import copyfile
 import numpy as np
 from fsl import wrappers
 from bip.utils.log_utils import redirect_logging, job_name
 from pipe_tree import In, Out, Ref
-from gradunwarp.core.gradient_unwarp_apply import gradient_unwarp_apply
 
 log = logging.getLogger(__name__)
 
@@ -25,11 +23,9 @@ def run(ctx,
         eddy_bvals:                In,
         eddy_bvecs:                In,
         eddy_nodif_brain_mask_ud:  In,
+        eddy_data_ud:              In,
         logs_dir:                  Ref,
-        eddy_data_GDC:             Ref,
         dtifit_output_prefix:      Ref,
-        eddy_data_ud:              Out,
-        eddy_data_ud_warp:         Out,
         eddy_data_ud_1_shell:      Out,
         eddy_data_ud_1_shell_bval: Out,
         eddy_data_ud_1_shell_bvec: Out,
@@ -37,20 +33,6 @@ def run(ctx,
 
     with redirect_logging(job_name(run), outdir=logs_dir):
 
-
-        if ctx.gdc != '':
-            #Calculate and apply the Gradient Distortion Unwarp
-            # TODO: Review the "half=True" in next version
-            gradient_unwarp_apply(WD=eddy_data_GDC,
-                                  infile=eddy_data,
-                                  outfile=eddy_data_ud,
-                                  owarp=eddy_data_ud_warp,
-                                  gradcoeff=ctx.gdc,
-                                  vendor='siemens', nojac=True, half=True)
-        else:
-            copyfile(src=eddy_data, dst=eddy_data_ud)
-
-
         #Correct input for dtifit using one shell
         list_vals = []
         b_vals = np.loadtxt(eddy_bvals)
diff --git a/bip/pipelines/dMRI_diff/diff_eddy.py b/bip/pipelines/dMRI_diff/diff_eddy.py
index 38d160cbc63bb6b27d30ba7d4a9b95d7c65d8044..f72def6f10bc4e7a78e9b3e6424b869072a6c6e7 100755
--- a/bip/pipelines/dMRI_diff/diff_eddy.py
+++ b/bip/pipelines/dMRI_diff/diff_eddy.py
@@ -43,22 +43,24 @@ def run(ctx,
         eddy_data:                      Out,
         eddy_outlier_report:            Out):
 
-    #TODO: Do the __name__ for everything
     with redirect_logging(job_name(run), outdir=logs_dir):
 
         # Creates links
-        # TODO: These links are NOT relative. This may cause future problems.
-        if not op.exists(eddy_AP):
-            rel_path = op.relpath(AP, eddy_AP)
+        if eddy_AP is not None and not op.exists(eddy_AP):
+            rel_path = op.relpath(AP, op.dirname(eddy_AP))
             os.symlink(src=rel_path, dst=eddy_AP)
-        if not op.exists(eddy_nodif):
-            rel_path = op.relpath(fieldmap_iout_mean, eddy_nodif)
+        if eddy_nodif is not None and not op.exists(eddy_nodif):
+            rel_path = op.relpath(fieldmap_iout_mean, op.dirname(eddy_nodif))
             os.symlink(src=rel_path, dst=eddy_nodif)
-        if not op.exists(eddy_nodif_brain_mask):
-            rel_path = op.relpath(fieldmap_mask, eddy_nodif_brain_mask)
+        if eddy_nodif_brain_mask is not None and \
+            not op.exists(eddy_nodif_brain_mask):
+            rel_path = op.relpath(fieldmap_mask, 
+                                  op.dirname(eddy_nodif_brain_mask))
             os.symlink(src=rel_path, dst=eddy_nodif_brain_mask)
-        if not op.exists(eddy_nodif_brain_mask_ud):
-            rel_path = op.relpath(fieldmap_mask_ud, eddy_nodif_brain_mask_ud)
+        if eddy_nodif_brain_mask_ud is not None and \
+            not op.exists(eddy_nodif_brain_mask_ud):
+            rel_path = op.relpath(fieldmap_mask_ud, 
+                                  op.dirname(eddy_nodif_brain_mask_ud))
             os.symlink(src=rel_path, dst=eddy_nodif_brain_mask_ud)
 
         copyfile(src=AP_bval, dst=eddy_bvals)
diff --git a/bip/pipelines/dMRI_diff/diff_gdc.py b/bip/pipelines/dMRI_diff/diff_gdc.py
new file mode 100755
index 0000000000000000000000000000000000000000..0c9147985b25c4cae019c408591bae9c3a848cbc
--- /dev/null
+++ b/bip/pipelines/dMRI_diff/diff_gdc.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+#
+# diff_gdc.py - Sub-pipeline with the Gradient Distortion Correction of the dMRI
+#
+# Author: Fidel Alfaro Almagro <fidel.alfaroalmagro@ndcn.ox.ac.uk>
+# Author: Paul McCarthy <pauldmccarthy@gmail.com>
+# Author: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
+#
+# pylint: disable=C0103,E0602,C0114,C0115,C0116,R0913,R0914,R0915
+# pylint: disable=W0613
+#
+
+import logging
+from shutil import copyfile
+from bip.utils.log_utils import redirect_logging, job_name
+from pipe_tree import In, Out, Ref
+from gradunwarp.core.gradient_unwarp_apply import gradient_unwarp_apply
+
+log = logging.getLogger(__name__)
+
+def run(ctx,
+        eddy_data:                 In,
+        logs_dir:                  Ref,
+        eddy_data_GDC:             Ref,
+        eddy_data_ud:              Out,
+        eddy_data_ud_warp:         Out):
+
+    with redirect_logging(job_name(run), outdir=logs_dir):
+
+        if ctx.gdc != '':
+            #Calculate and apply the Gradient Distortion Unwarp
+            # TODO: Review the "half=True" in next version
+            gradient_unwarp_apply(WD=eddy_data_GDC,
+                                  infile=eddy_data,
+                                  outfile=eddy_data_ud,
+                                  owarp=eddy_data_ud_warp,
+                                  gradcoeff=ctx.gdc,
+                                  vendor='siemens', nojac=True, half=True)
+        else:
+            copyfile(src=eddy_data, dst=eddy_data_ud)
diff --git a/bip/pipelines/dMRI_diff/diff_noddi.py b/bip/pipelines/dMRI_diff/diff_noddi.py
index 95e6c5d9cce63d4258d6b985318fc9def12ec5e9..459a39e1b2559f3c8652b2a865d9d9ee889e1234 100755
--- a/bip/pipelines/dMRI_diff/diff_noddi.py
+++ b/bip/pipelines/dMRI_diff/diff_noddi.py
@@ -40,8 +40,8 @@ def run(ctx,
         NODDI_dir_file:            Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
-         
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
+
         amtmp      = op.join(tmp_dir, 'amtmp.nii')
         amtmp_mask = op.join(tmp_dir, 'amtmp_mask.nii')
 
@@ -50,7 +50,7 @@ def run(ctx,
         # Otherwise, this should not be done
         if not op.exists('./kernels/'):
             if op.exists(ctx.get_data('dMRI/kernels.zip')):
-                with zipfile.ZipFile(ctx.get_data('dMRI/kernels.zip'), 
+                with zipfile.ZipFile(ctx.get_data('dMRI/kernels.zip'),
                                      'r') as zip_ref:
                     zip_ref.extractall("./")
 
@@ -86,5 +86,5 @@ def run(ctx,
 
         # Delete created folders
         for dir_to_delete in [AMICO_dir, NODDI_kernels_dir]:
-            if op.exists(dir_to_delete):
+            if dir_to_delete is not None and op.exists(dir_to_delete):
                 rmtree(dir_to_delete)
diff --git a/bip/pipelines/dMRI_diff/diff_tbss.py b/bip/pipelines/dMRI_diff/diff_tbss.py
index 4d0c0830d3e60c710e5b9ea3e1b9f7837783d53f..12a24c5e62d79e5aa77c1ba4f4ee1301c8e123e6 100755
--- a/bip/pipelines/dMRI_diff/diff_tbss.py
+++ b/bip/pipelines/dMRI_diff/diff_tbss.py
@@ -54,17 +54,17 @@ def run(ctx,
         JHUrois_FA:                  Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         TBSS_FA_tmp            = op.join(tmp_dir, 'TBSS_FA_tmp.nii.gz')
         TBSS_FA_to_MNI_warp_s1 = op.join(tmp_dir, 'FA_to_MNI_warp_s1.nii.gz')
         TBSS_FA_to_MNI_warp_s2 = op.join(tmp_dir, 'FA_to_MNI_warp_s2.nii.gz')
 
         # Creates links
-        if not op.exists(TBSS_FA):
-            rel_path = op.relpath(FA, TBSS_FA)
+        if TBSS_FA is not None and not op.exists(TBSS_FA):
+            rel_path = op.relpath(FA, op.dirname(TBSS_FA))
             os.symlink(src=rel_path, dst=TBSS_FA)
-        if not op.exists(TBSS_MNI):
+        if TBSS_MNI is not None and not op.exists(TBSS_MNI):
             os.symlink(src=ctx.get_standard('FMRIB58_FA_1mm.nii.gz'),
                        dst=TBSS_MNI)
         FA_img     = nib.load(TBSS_FA)
@@ -76,11 +76,11 @@ def run(ctx,
 
         # create mask (for use in FLIRT & FNIRT)
         wrappers.fslmaths(TBSS_FA_dir_FA).bin().run(TBSS_FA_dir_FA_mask)
-        #TODO: investigate how to add the -odt=char option
         wrappers.fslmaths(TBSS_FA_dir_FA_mask).dilD(2).sub(1).abs().\
             add(TBSS_FA_dir_FA_mask).run(TBSS_FA_dir_FA_mask, odt="char")
 
-        if not op.exists(TBSS_MNI_to_dti_FA_warp_msf):
+        if TBSS_MNI_to_dti_FA_warp_msf is not None and \
+            not op.exists(TBSS_MNI_to_dti_FA_warp_msf):
             #New Optimal Registration
             wrappers.flirt(ref=TBSS_MNI,
                            src=TBSS_FA_dir_FA,
diff --git a/bip/pipelines/dMRI_fieldmap/fieldmap_post_topup.py b/bip/pipelines/dMRI_fieldmap/fieldmap_post_topup.py
index 9036ed1d318fed4f899e51dd16ade459f5fe0f6a..e419bc264b1f8dbe79129b95e13f96ca4b79d2b1 100755
--- a/bip/pipelines/dMRI_fieldmap/fieldmap_post_topup.py
+++ b/bip/pipelines/dMRI_fieldmap/fieldmap_post_topup.py
@@ -50,7 +50,7 @@ def run(ctx,
         fieldmap_iout_mean_ud_inv_warp: Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         B0_AP_corr_tmp  = op.join(tmp_dir, 'B0_AP_corr_tmp.nii.gz')
         B0_PA_corr_tmp  = op.join(tmp_dir, 'B0_PA_corr_tmp.nii.gz')
diff --git a/bip/pipelines/dMRI_fieldmap/fieldmap_pre_topup.py b/bip/pipelines/dMRI_fieldmap/fieldmap_pre_topup.py
index 0841b88edc8ac4cd487302f77ba3842e7c3154e8..eecbbe2b6fc50fc4e6bb33f4c69c029742567d1d 100755
--- a/bip/pipelines/dMRI_fieldmap/fieldmap_pre_topup.py
+++ b/bip/pipelines/dMRI_fieldmap/fieldmap_pre_topup.py
@@ -44,7 +44,7 @@ def run(ctx,
         tmp_dir:          Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         AP_tmp = op.join(tmp_dir, 'AP_tmp_')
         PA_tmp = op.join(tmp_dir, 'PA_tmp_')
diff --git a/bip/pipelines/fMRI_rest/rfMRI_fix.py b/bip/pipelines/fMRI_rest/rfMRI_fix.py
index 781af126f17a56ee3c80bb40d4c38d98d78bf013..0a08974f12169760f901af87aaf7a6d816e48d69 100755
--- a/bip/pipelines/fMRI_rest/rfMRI_fix.py
+++ b/bip/pipelines/fMRI_rest/rfMRI_fix.py
@@ -43,7 +43,8 @@ def run(ctx,
     with redirect_logging('rfMRI_fix', outdir=logs_dir):
 
         # Generate files needed to extract features
-        if not op.exists(rfMRI_highres_pveseg):
+        if rfMRI_highres_pveseg is not None and \
+            not op.exists(rfMRI_highres_pveseg):
             copyfile(T1_fast_pveseg, rfMRI_highres_pveseg)
         if not op.exists(rfMRI_standard2example_func_warp):
             wrappers.invwarp(ref=rfMRI_example_func,
@@ -51,7 +52,8 @@ def run(ctx,
                              out=rfMRI_standard2example_func_warp)
 
         # FIX: Extract features for subject
-        if not op.exists(fix_features):
+        if fix_features is not None and \
+            not op.exists(fix_features):
             d = extract.FixData.from_melodic_dir(rfMRI_ica, fixdir = fix_dir)
 
             f = extract.extract_features(data=d,
@@ -86,7 +88,8 @@ def run(ctx,
 
         # Create this directory in case it does not exist. This may
         # happen due to the overwrite function issue in Melodic
-        if not op.exists(rfMRI_reg_standard_dir):
+        if rfMRI_reg_standard_dir is not None and \
+            not op.exists(rfMRI_reg_standard_dir):
             os.mkdir(rfMRI_reg_standard_dir)
 
         # Generate the data in standard space
@@ -95,5 +98,7 @@ def run(ctx,
                            warp=rfMRI_example_func2standard_warp,
                            interp="spline")
         mask = ctx.get_data('MNI/MNI152_T1_2mm_brain_mask_bin.nii.gz')
-        wrappers.fslmaths(filtered_func_data_clean_std).mas(mask).run(filtered_func_data_clean_std)
-        wrappers.fslmaths(filtered_func_data_clean_std).Tstd().bin().run(filtered_func_data_clean_stdmask)
+        wrappers.fslmaths(filtered_func_data_clean_std).mas(mask).\
+            run(filtered_func_data_clean_std)
+        wrappers.fslmaths(filtered_func_data_clean_std).Tstd().bin().\
+            run(filtered_func_data_clean_stdmask)
diff --git a/bip/pipelines/fMRI_rest/rfMRI_melodic.py b/bip/pipelines/fMRI_rest/rfMRI_melodic.py
index 33ae209540f1dd0f9137a6e56d067dfa13e52d21..07e8681a816ff3731e1bb14d416e1073c961826c 100755
--- a/bip/pipelines/fMRI_rest/rfMRI_melodic.py
+++ b/bip/pipelines/fMRI_rest/rfMRI_melodic.py
@@ -11,7 +11,6 @@
 # pylint: disable=W0613
 #
 
-import os
 import os.path as op
 import shutil
 import logging
@@ -35,17 +34,8 @@ def run(ctx,
         rfMRI_mc_rel_mean:                Out):
 
     with redirect_logging('rfMRI_melodic', outdir=logs_dir):
-        wrappers.feat(fsf=rfMRI_fsf)
 
-        # The ending of this script is needed because the overwrite output
-        # function in MELODIC is not working, and therefore, the output is
-        # generated in a folder called "rfMI+.ica". Once this function works,
-        # we can remove these last 8 lines
-        name_dir_alt = rfMRI_ica.split(".")
-        name_dir_alt[-2] = name_dir_alt[-2] + "+"
-        name_dir_alt = ".".join(name_dir_alt)
+        if rfMRI_ica is not None and op.exists(rfMRI_ica):
+            shutil.rmtree(rfMRI_ica)
 
-        if op.exists(name_dir_alt):
-            if op.exists(rfMRI_ica):
-                shutil.rmtree(rfMRI_ica)
-            os.rename(name_dir_alt, rfMRI_ica)
+        wrappers.feat(fsf=rfMRI_fsf)
diff --git a/bip/pipelines/fMRI_rest/rfMRI_netmats.py b/bip/pipelines/fMRI_rest/rfMRI_netmats.py
index 651b83ede834c0a201f7c8cd31264c034cfaf5c6..922af5fcaef8bde03e0854203875344301e55acb 100755
--- a/bip/pipelines/fMRI_rest/rfMRI_netmats.py
+++ b/bip/pipelines/fMRI_rest/rfMRI_netmats.py
@@ -40,7 +40,7 @@ def run(ctx,
 
         # Create this directory in case it does not exist. This may
         # happen due to the overwrite function issue in Melodic
-        if not op.exists(netmats_dir):
+        if netmats_dir is not None and not op.exists(netmats_dir):
             os.mkdir(netmats_dir)
 
 
diff --git a/bip/pipelines/fMRI_rest/rfMRI_prepare.py b/bip/pipelines/fMRI_rest/rfMRI_prepare.py
index d469ff45355f761a03ae83294e9a4f638b3632ee..6f6fe4f6fe950bd2564a6f879a37cf2ffd113e80 100755
--- a/bip/pipelines/fMRI_rest/rfMRI_prepare.py
+++ b/bip/pipelines/fMRI_rest/rfMRI_prepare.py
@@ -48,20 +48,21 @@ def run(ctx,
     with redirect_logging('rfMRI_prepare', outdir=logs_dir):
 
         # Creates links
-        # TODO: These links ARE hard-coded. This may cause future problems.
-        if not op.exists(rfMRI_T1):
-            rel_path = op.relpath(T1, rfMRI_T1)
+        if rfMRI_T1 is not None and not op.exists(rfMRI_T1):
+            rel_path = op.relpath(T1, op.dirname(rfMRI_T1))
             os.symlink(src=rel_path, dst=rfMRI_T1)
-        if not op.exists(rfMRI_T1_brain):
-            rel_path = op.relpath(T1_brain, rfMRI_T1_brain)
+        if rfMRI_T1_brain is not None and not not op.exists(rfMRI_T1_brain):
+            rel_path = op.relpath(T1_brain, op.dirname(rfMRI_T1_brain))
             os.symlink(src=rel_path, dst=rfMRI_T1_brain)
-        if not op.exists(rfMRI_T1_brain2MNI152_T1_2mm_brain_warp):
-            rel_path = op.relpath(T1_to_MNI_warp, 
-                                  rfMRI_T1_brain2MNI152_T1_2mm_brain_warp)
+        if rfMRI_T1_brain2MNI152_T1_2mm_brain_warp is not None and \
+            not op.exists(rfMRI_T1_brain2MNI152_T1_2mm_brain_warp):
+            rel_path = op.relpath(T1_to_MNI_warp,
+                            op.dirname(rfMRI_T1_brain2MNI152_T1_2mm_brain_warp))
             os.symlink(src=rel_path,dst=rfMRI_T1_brain2MNI152_T1_2mm_brain_warp)
-        if not op.exists(rfMRI_T1_brain2MNI152_T1_2mm_brain_mat):
-            rel_path = op.relpath(T1_to_MNI_linear_mat, 
-                                  rfMRI_T1_brain2MNI152_T1_2mm_brain_mat)
+        if rfMRI_T1_brain2MNI152_T1_2mm_brain_mat is not None and \
+            not op.exists(rfMRI_T1_brain2MNI152_T1_2mm_brain_mat):
+            rel_path = op.relpath(T1_to_MNI_linear_mat,
+                             op.dirname(rfMRI_T1_brain2MNI152_T1_2mm_brain_mat))
             os.symlink(src=rel_path, dst=rfMRI_T1_brain2MNI152_T1_2mm_brain_mat)
 
         wrappers.fslmaths(T1_fast_pve_2).thr(0.5).bin().\
diff --git a/bip/pipelines/fMRI_task/tfMRI_feat.py b/bip/pipelines/fMRI_task/tfMRI_feat.py
index 318a710827a031d7a8b5b4f5979556d5b8aec119..a2069e774c535204b28b8b8f921b43b3e4786819 100755
--- a/bip/pipelines/fMRI_task/tfMRI_feat.py
+++ b/bip/pipelines/fMRI_task/tfMRI_feat.py
@@ -10,12 +10,11 @@
 # pylint: disable=W0613
 #
 
-import os
 import os.path as op
 import shutil
 import logging
 from fsl import wrappers
-from bip.utils.log_utils import redirect_logging
+from bip.utils.log_utils import redirect_logging, job_name
 from pipe_tree import In, Out, Ref
 
 log = logging.getLogger(__name__)
@@ -38,19 +37,9 @@ def run(ctx,
         tfMRI_zstat4:               Out,
         tfMRI_zstat5:               Out):
 
-    with redirect_logging('tfMRI_feat', outdir=logs_dir):
+    with redirect_logging(job_name(run), outdir=logs_dir):
 
-        wrappers.feat(fsf=tfMRI_fsf)
-
-        # The ending of this script is needed because the overwrite output
-        # function in MELODIC is not working, and therefore, the output is
-        # generated in a folder called "rfMI+.ica". Once this function works,
-        # we can remove these last 8 lines
-        name_dir_alt = tfMRI_feat.split(".")
-        name_dir_alt[-2] = name_dir_alt[-2] + "+"
-        name_dir_alt = ".".join(name_dir_alt)
+        if tfMRI_feat is not None and op.exists(tfMRI_feat):
+            shutil.rmtree(tfMRI_feat)
 
-        if op.exists(name_dir_alt):
-            if op.exists(tfMRI_feat):
-                shutil.rmtree(tfMRI_feat)
-            os.rename(name_dir_alt, tfMRI_feat)
+        wrappers.feat(fsf=tfMRI_fsf)
diff --git a/bip/pipelines/fMRI_task/tfMRI_prepare.py b/bip/pipelines/fMRI_task/tfMRI_prepare.py
index c309b45ea5513239d50f2d0236f3cdd1ba16f1ee..aed25d01374aa84ba11d0ed4bb7d18ce658a60c8 100755
--- a/bip/pipelines/fMRI_task/tfMRI_prepare.py
+++ b/bip/pipelines/fMRI_task/tfMRI_prepare.py
@@ -48,20 +48,21 @@ def run(ctx,
     with redirect_logging('tfMRI_prepare', outdir=logs_dir):
 
         # Creates links
-        # TODO: These links ARE hard-coded. This may cause future problems.
-        if not op.exists(tfMRI_T1):
-            rel_path = op.relpath(T1, tfMRI_T1)
+        if tfMRI_T1 is not None and not op.exists(tfMRI_T1):
+            rel_path = op.relpath(T1, op.dirname(tfMRI_T1))
             os.symlink(src=rel_path, dst=tfMRI_T1)
-        if not op.exists(tfMRI_T1_brain):
-            rel_path = op.relpath(T1_brain, tfMRI_T1_brain)
+        if tfMRI_T1_brain is not None and not op.exists(tfMRI_T1_brain):
+            rel_path = op.relpath(T1_brain, op.dirname(tfMRI_T1_brain))
             os.symlink(src=rel_path, dst=tfMRI_T1_brain)
-        if not op.exists(tfMRI_T1_brain2MNI152_T1_2mm_brain_warp):
-            rel_path = op.relpath(T1_to_MNI_warp, 
-                                  tfMRI_T1_brain2MNI152_T1_2mm_brain_warp)
+        if tfMRI_T1_brain2MNI152_T1_2mm_brain_warp is not None and \
+            not op.exists(tfMRI_T1_brain2MNI152_T1_2mm_brain_warp):
+            rel_path = op.relpath(T1_to_MNI_warp,
+                            op.dirname(tfMRI_T1_brain2MNI152_T1_2mm_brain_warp))
             os.symlink(src=rel_path,dst=tfMRI_T1_brain2MNI152_T1_2mm_brain_warp)
-        if not op.exists(tfMRI_T1_brain2MNI152_T1_2mm_brain_mat):
-            rel_path = op.relpath(T1_to_MNI_linear_mat, 
-                                  tfMRI_T1_brain2MNI152_T1_2mm_brain_mat)
+        if tfMRI_T1_brain2MNI152_T1_2mm_brain_mat is not None and \
+            not op.exists(tfMRI_T1_brain2MNI152_T1_2mm_brain_mat):
+            rel_path = op.relpath(T1_to_MNI_linear_mat,
+                             op.dirname(tfMRI_T1_brain2MNI152_T1_2mm_brain_mat))
             os.symlink(src=rel_path,dst=tfMRI_T1_brain2MNI152_T1_2mm_brain_mat)
 
         wrappers.fslmaths(T1_fast_pve_2).thr(0.5).bin().\
diff --git a/bip/pipelines/struct_FS/FS_get_IDPs.py b/bip/pipelines/struct_FS/FS_get_IDPs.py
index f1f37129deca8983ad7a4c1902ffc27186cbdef6..fdf336cc172897d525e1d68b21bc8c4e43866897 100755
--- a/bip/pipelines/struct_FS/FS_get_IDPs.py
+++ b/bip/pipelines/struct_FS/FS_get_IDPs.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# asl_get_IDPs.py - Sub-pipeline generating the IDPs of the ASL pipeline.
+# fsl_get_IDPs.py - Sub-pipeline generating the IDPs of the ASL pipeline.
 #
 # Author: Fidel Alfaro Almagro <fidel.alfaroalmagro@ndcn.ox.ac.uk>
 # Author: Paul McCarthy <pauldmccarthy@gmail.com>
diff --git a/bip/pipelines/struct_FS/FS_get_IDPs_fnc.py b/bip/pipelines/struct_FS/FS_get_IDPs_fnc.py
index 273294f6de208aa0c5f18291e1d8655c7e091ada..d4e64f87e8f7e2485bb095bcbd295f2e0512de9b 100755
--- a/bip/pipelines/struct_FS/FS_get_IDPs_fnc.py
+++ b/bip/pipelines/struct_FS/FS_get_IDPs_fnc.py
@@ -44,7 +44,7 @@ def generate_FS_IDP_files(SUBJECTS_DIR, subject_ID, subject, dataDir,
            ' --subjects ' + subject_ID + ' --skip', env=env)
         run_command(log, 'asegstats2table  '+
             ' -m mean --all-segs --tablefile ' + dataDir +
-            'aseg_intensity.txt ' +' --subjects ' + subject_ID + ' --skip', 
+            'aseg_intensity.txt ' +' --subjects ' + subject_ID + ' --skip',
             env=env)
 
     if op.isfile(statsDir + 'lh.w-g.pct.stats'):
@@ -508,7 +508,6 @@ def bb_FS_get_IDPs(ctx, env):
     dataDir    = subjectDir + '/data/'
     headersDir = subjectDir + '/headers/'
 
-    #TODO: Raise an exception
     if not op.isdir(subjectDir):
         raise Exception("Error: FreeSurfer has not been run on this subject")
 
diff --git a/bip/pipelines/struct_FS/FS_proc.py b/bip/pipelines/struct_FS/FS_proc.py
index 3b4f14e922c2792bcd884ae309249e72ccf1dc84..a31d0e3c60e0085fcbea647c85bb36f73563484d 100755
--- a/bip/pipelines/struct_FS/FS_proc.py
+++ b/bip/pipelines/struct_FS/FS_proc.py
@@ -30,7 +30,7 @@ def run(ctx,
     with redirect_logging(job_name(run), outdir=logs_dir):
 
         # We need to delete the folder because otherwise, FreeSurfer complains
-        if op.exists(FreeSurfer_dir):
+        if FreeSurfer_dir is not None and op.exists(FreeSurfer_dir):
             shutil.rmtree(FreeSurfer_dir)
 
         #TODO: SUBJECTS_DIR can be an argument of freesurfer instead of this craps
@@ -46,8 +46,8 @@ def run(ctx,
             cmd += " -FLAIR " + T2_path + " -FLAIRpial"
 
         try:
-            output = run_command(log, cmd, env=env)      
+            output = run_command(log, cmd, env=env)
             log.info(output)
         finally:
-            if op.exists(fsaverage):
+            if fsaverage is not None and op.exists(fsaverage):
                 os.unlink(fsaverage)
diff --git a/bip/pipelines/struct_FS/FS_segm.py b/bip/pipelines/struct_FS/FS_segm.py
index 7f7fde392eb816754078a9adbba8e184f4b6ebb1..537c98aab89158e6a96295dfaa0d7bec017b3d42 100755
--- a/bip/pipelines/struct_FS/FS_segm.py
+++ b/bip/pipelines/struct_FS/FS_segm.py
@@ -36,7 +36,7 @@ def run(ctx,
         output2 = run_command(log, 'segmentBS.sh FreeSurfer', env=env)
         log.info(output2)
 
-        if op.exists(T2_FLAIR_unbiased):
+        if T2_FLAIR_unbiased is not None and op.exists(T2_FLAIR_unbiased):
             output3 = run_command(log,
                      'segmentHA_T2.sh FreeSurfer ' + FreSurfer_FLAIR + '  AN 1',
                       env=env)
diff --git a/bip/pipelines/struct_T1/T1_QC_CNR_corners.py b/bip/pipelines/struct_T1/T1_QC_CNR_corners.py
index 40c686e20e91b6cb42cd3bb2dc7e416fba888352..0d753601cd8112426f3361b85a9d0148c6f64965 100755
--- a/bip/pipelines/struct_T1/T1_QC_CNR_corners.py
+++ b/bip/pipelines/struct_T1/T1_QC_CNR_corners.py
@@ -32,7 +32,7 @@ def run(ctx,
         tmp_dir:                 Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         roi_1_1     = op.join(tmp_dir, 'roi_1_1.nii.gz')
         roi_1_245   = op.join(tmp_dir, 'roi_1_245.nii.gz')
diff --git a/bip/pipelines/struct_T1/T1_QC_CNR_eyes.py b/bip/pipelines/struct_T1/T1_QC_CNR_eyes.py
index 0b9f7824fc57985775f573b41909353341d7c3c8..8680100a70ac4608777fe9d85b19c1cb49e2ff18 100755
--- a/bip/pipelines/struct_T1/T1_QC_CNR_eyes.py
+++ b/bip/pipelines/struct_T1/T1_QC_CNR_eyes.py
@@ -40,7 +40,7 @@ def run(ctx,
         tmp_dir:               Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T1_brain_GM_mask_orig = op.join(tmp_dir, 'T1_brain_GM_mask_orig.nii.gz')
         T1_brain_WM_mask_orig = op.join(tmp_dir, 'T1_brain_WM_mask_orig.nii.gz')
@@ -48,7 +48,7 @@ def run(ctx,
         rmask                 = op.join(tmp_dir, 'rmask.nii.gz')
         T1_tmp_7              = op.join(tmp_dir, 'T1_tmp_7.nii.gz')
 
-        if not op.exists(T1_orig_ud_warp_inv):
+        if T1_orig_ud_warp_inv is not None and not op.exists(T1_orig_ud_warp_inv):
             wrappers.invwarp(ref=T1_orig, warp=T1_orig_ud_warp,
                              out=T1_orig_ud_warp_inv)
 
diff --git a/bip/pipelines/struct_T1/T1_QC_COG.py b/bip/pipelines/struct_T1/T1_QC_COG.py
index 89d82fe63a2c73718eb84a319421108fdc63eb4d..7a24ae7157265275aed3e27d9f66af0b98c39e56 100755
--- a/bip/pipelines/struct_T1/T1_QC_COG.py
+++ b/bip/pipelines/struct_T1/T1_QC_COG.py
@@ -26,7 +26,7 @@ def run(ctx,
         tmp_dir:       Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T1_tmp_5 = op.join(tmp_dir, 'T1_tmp_5.nii.gz')
         T1_tmp_6 = op.join(tmp_dir, 'T1_tmp_6.nii.gz')
diff --git a/bip/pipelines/struct_T1/T1_brain_extract.py b/bip/pipelines/struct_T1/T1_brain_extract.py
index c2aa51d8c17ed6004feff0fc2a76f41cc8c42a03..a8088c1847e8826a43d293b79632db433b77b96b 100755
--- a/bip/pipelines/struct_T1/T1_brain_extract.py
+++ b/bip/pipelines/struct_T1/T1_brain_extract.py
@@ -41,7 +41,7 @@ def run(ctx,
         T1_to_MNI_warp_jac:      Out):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T1_tmp_1                  = op.join(tmp_dir, 'T1_tmp_1.nii.gz')
         T1_tmp_2                  = op.join(tmp_dir, 'T1_tmp_2.nii.gz')
diff --git a/bip/pipelines/struct_T1/T1_defacing.py b/bip/pipelines/struct_T1/T1_defacing.py
index da4ed929f0c7d0f203fd241bb78e6d7e191b5bb3..eb40f4f5913d00ba1521fa545580635fae631575 100755
--- a/bip/pipelines/struct_T1/T1_defacing.py
+++ b/bip/pipelines/struct_T1/T1_defacing.py
@@ -30,7 +30,7 @@ def run(ctx,
         tmp_dir:                           Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T1_tmp_4                  = op.join(tmp_dir, 'T1_tmp_4.nii.gz')
         T1_tmp_mat                = op.join(tmp_dir, 'T1_tmp.mat')
diff --git a/bip/pipelines/struct_T1/T1_first.py b/bip/pipelines/struct_T1/T1_first.py
index 3a94f71abbb56c47cc5da50d39b4a1ac95d443c2..fa0a89a08ee27cc390bd39f1b78d0868e7de2567 100755
--- a/bip/pipelines/struct_T1/T1_first.py
+++ b/bip/pipelines/struct_T1/T1_first.py
@@ -31,8 +31,10 @@ def run(ctx,
     with redirect_logging(job_name(run), outdir=logs_dir):
 
         # Creates a link inside T1_first to T1_unbiased_brain.nii.gz
-        if not op.exists(T1_first_unbiased_brain):
-            rel_path = op.relpath(T1_unbiased_brain, T1_first_unbiased_brain)
+        if T1_first_unbiased_brain is not None and \
+            not op.exists(T1_first_unbiased_brain):
+            rel_path = op.relpath(T1_unbiased_brain, 
+                                  op.dirname(T1_first_unbiased_brain))
             os.symlink(src=rel_path, dst=T1_first_unbiased_brain)
         wrappers.run_first_all(input=T1_first_unbiased_brain,
                                output=T1_first_prefix, b=True)
diff --git a/bip/pipelines/struct_T1/T1_sienax.py b/bip/pipelines/struct_T1/T1_sienax.py
index 5a226bd8c0c9e03bf6110da93e847f1a916e8d5f..d8ad24df19fae07ca460812e0f050ab2df824979 100755
--- a/bip/pipelines/struct_T1/T1_sienax.py
+++ b/bip/pipelines/struct_T1/T1_sienax.py
@@ -41,8 +41,8 @@ def run(ctx,
         tmp_dir:                             Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
-         
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
+
         T1_tmp_mat_1 = op.join(tmp_dir, 'tmp_mat_1.mat')
         T1_tmp_mat_2 = op.join(tmp_dir, 'tmp_mat_2.mat')
         T1_tmp_mat_3 = op.join(tmp_dir, 'tmp_mat_3.mat')
diff --git a/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_brain_extract.py b/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_brain_extract.py
index 79034d2e8b87dfaa63d30dcddd6935fc477a3ae3..6d75ddbbcc9ce8fa6665a4bea5cf7a04d6d52206 100755
--- a/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_brain_extract.py
+++ b/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_brain_extract.py
@@ -40,7 +40,7 @@ def run(ctx,
         tmp_dir:                            Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T2_FLAIR_tmp_1_mat = op.join(tmp_dir, 'T2_FLAIR_tmp_1.mat')
         T2_FLAIR_tmp_2_mat = op.join(tmp_dir, 'T2_FLAIR_tmp_2.mat')
@@ -81,7 +81,7 @@ def run(ctx,
                                 warp2=T1_to_MNI_warp,
                                 out=T2_FLAIR_orig_to_MNI_warp)
         else:
-            wrappers.convertwrap(ref=ctx.MNI,
+            wrappers.convertwarp(ref=ctx.MNI,
                                  premat=T2_FLAIR_orig_ud_to_T2_FLAIR_mat,
                                  warp1=T1_to_MNI_warp,
                                  out=T2_FLAIR_orig_to_MNI_warp)
diff --git a/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_defacing.py b/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_defacing.py
index 9a9a3cd3094f0164ad42cf32337c36421182baf0..e0be6c78999d23f06658595ed53797e943767ca7 100755
--- a/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_defacing.py
+++ b/bip/pipelines/struct_T2_FLAIR/T2_FLAIR_defacing.py
@@ -32,7 +32,7 @@ def run(ctx,
         tmp_dir:                          Ref):
 
     with redirect_logging(job_name(run), outdir=logs_dir),\
-         tempdir(op.join(tmp_dir, job_name(run))):
+         tempdir(op.join(tmp_dir, job_name(run))) as tmp_dir:
 
         T2_FLAIR_tmp_1     = op.join(tmp_dir, 'T2_FLAIR_tmp_1nii.gz')
         T2_FLAIR_tmp_3_mat = op.join(tmp_dir, 'T2_FLAIR_tmp_3.mat')
@@ -53,7 +53,8 @@ def run(ctx,
                           ref=T2_FLAIR_orig, mat=T2_FLAIR_tmp_3_mat,
                           out=T2_FLAIR_orig_defacing_mask,
                           interp="trilinear")
-        wrappers.fslmaths(T2_FLAIR_orig_defacing_mask).binv().mul(T2_FLAIR_orig).run(T2_FLAIR_orig_defaced)
+        wrappers.fslmaths(T2_FLAIR_orig_defacing_mask).binv().\
+            mul(T2_FLAIR_orig).run(T2_FLAIR_orig_defaced)
 
         #Defacing T2_FLAIR
         copyfile(src=T2_FLAIR, dst=T2_FLAIR_tmp_1)
diff --git a/bip/pipelines/struct_asl/asl_proc.py b/bip/pipelines/struct_asl/asl_proc.py
index 9b3fb6ab6e599f6b7370d0ad3191ecb93b9e0c0d..cfcf234947fb622a809f29481e8e4c6f58947583 100755
--- a/bip/pipelines/struct_asl/asl_proc.py
+++ b/bip/pipelines/struct_asl/asl_proc.py
@@ -89,10 +89,10 @@ def run(ctx,
         wrappers.fslmerge("t", ASL_label, *list_fils_label)
         wrappers.fslmerge("t", ASL_DATA_wrongorder, ASL_label, ASL_control)
 
-        rel_path = op.relpath(ASL_M0,CALIB)
+        rel_path = op.relpath(ASL_M0,op.dirname(CALIB))
         os.symlink(rel_path, CALIB)
 
-        rel_path = op.relpath(ASL_M0_json, CALIB_json)
+        rel_path = op.relpath(ASL_M0_json, op.dirname(CALIB_json))
         os.symlink(rel_path, CALIB_json)
 
         TE = read_json_field(fileName=CALIB_json, fieldName="EchoTime",
diff --git a/bip/pipelines/struct_swMRI/swMRI_proc.py b/bip/pipelines/struct_swMRI/swMRI_proc.py
index 7ff4aa5b92b2a227f59523c6a92ccfae7c65be59..5edc200022cbe925be72bb7c8d2dc952173258f7 100755
--- a/bip/pipelines/struct_swMRI/swMRI_proc.py
+++ b/bip/pipelines/struct_swMRI/swMRI_proc.py
@@ -63,13 +63,13 @@ def run(ctx,
                 complex_phase = True
 
         MAG_TE1 = SWI_MAG_TE1_orig
-        if op.exists(SWI_MAG_TE2_orig):
+        if SWI_MAG_TE2_orig is not None and op.exists(SWI_MAG_TE2_orig):
             MAG_TE2 = SWI_MAG_TE2_orig
         else:
             MAG_TE2 = ""
 
         if num_coils > 0:
-            if op.exists(SWI_MAG_TE1_C01):
+            if SWI_MAG_TE1_C01 is not None and op.exists(SWI_MAG_TE1_C01):
                 combine_magnitude_coils(MAG_TE1, MAG_TE2, MAG_TE1_dir,
                                         MAG_TE2_dir, num_coils, SOS_TE1,
                                         SOS_TE2, SOS_ratio, R2star, T2star)
@@ -112,7 +112,7 @@ def run(ctx,
         #TODO: Change name of SWI to venogram?
         for fil in [R2star, T2star, SOS_TE1, SOS_TE2, SOS_ratio, filtered_phase,
                     SWI]:
-            if op.exists(fil):
+            if fil is not None and op.exists(fil):
                 if ctx.gdc != '':
                     wrappers.applywarp(src=fil, ref=fil, out=fil, rel=True,
                                        w=SWI_TOTAL_MAG_orig_ud_warp,
diff --git a/bip/pipelines/struct_swMRI/swMRI_proc_fnc.py b/bip/pipelines/struct_swMRI/swMRI_proc_fnc.py
index 3e957ee0242072ec2e30641dd535d08d8335f953..512592eaa6af7a777359fef14ebdbe1b524184c7 100755
--- a/bip/pipelines/struct_swMRI/swMRI_proc_fnc.py
+++ b/bip/pipelines/struct_swMRI/swMRI_proc_fnc.py
@@ -53,7 +53,8 @@ def combine_magnitude_coils(MAG_TE1, MAG_TE2, MAG_TE1_dir, MAG_TE2_dir,
         wrappers.fslmaths(MAG_TE1).div(MAG_TE2).run(SOS_ratio)
 
     wrappers.fslmaths(SOS_ratio).log().div(TE_DIFF).run(R2star)
-    wrappers.fslmaths(R2star).recip().uthr(80).thr(5).kernel("2D").fmedian().kernel("sphere", "3").dilM().run(T2star)
+    wrappers.fslmaths(R2star).recip().uthr(80).thr(5).kernel("2D").fmedian().\
+        kernel("sphere", "3").dilM().run(T2star)
 
 
 def gen_filtered_phase(ctx, magImgDir, phaImgDir, magImgFileName, fp_fn,SWI_fn):