@@ -498,11 +498,13 @@ def _run_interface(self, runtime):
498498 from pynets .fmri import clustools
499499 from pynets .registration .reg_utils import check_orient_and_dims
500500 from joblib import Parallel , delayed
501+ from joblib .externals .loky .backend import resource_tracker
501502 from pynets .registration import reg_utils as regutils
502503 from pynets .core .utils import decompress_nifti
503504 import pkg_resources
504505 import shutil
505506 import tempfile
507+ resource_tracker .warnings = None
506508
507509 template = pkg_resources .resource_filename (
508510 "pynets" , f"templates/{ self .inputs .template_name } _brain_"
@@ -625,6 +627,7 @@ def _run_interface(self, runtime):
625627 if float (c_boot ) > 1 :
626628 import random
627629 from joblib import Memory
630+ from joblib .externals .loky import get_reusable_executor
628631 print (
629632 f"Performing circular block bootstrapping with { c_boot } "
630633 f" iterations..."
@@ -701,6 +704,8 @@ def run_bs_iteration(i, ts_data, work_dir, local_corr,
701704 iter_bootedparcels if
702705 i is not None ])
703706 counter = len (boot_parcellations )
707+ del iter_bootedparcels
708+ gc .collect ()
704709
705710 print ('Bootstrapped samples complete:' )
706711 print (boot_parcellations )
@@ -713,6 +718,10 @@ def run_bs_iteration(i, ts_data, work_dir, local_corr,
713718 nib .save (consensus_parcellation , nip .uatlas )
714719 memory .clear (warn = False )
715720 shutil .rmtree (cache_dir , ignore_errors = True )
721+ del parallel , memory , cache_dir
722+ get_reusable_executor ().shutdown (wait = True )
723+ gc .collect ()
724+
716725 for i in boot_parcellations :
717726 if os .path .isfile (i ):
718727 os .remove (i )
@@ -1186,7 +1195,8 @@ def _run_interface(self, runtime):
11861195 copy = True ,
11871196 use_hardlink = False )
11881197 else :
1189- if len (anat_mask_existing ) > 0 and self .inputs .mask is None :
1198+ if len (anat_mask_existing ) > 0 and \
1199+ self .inputs .mask is None and op .isfile (anat_mask_existing [0 ]):
11901200 mask_tmp_path = fname_presuffix (
11911201 anat_mask_existing [0 ], suffix = "_tmp" , newpath = runtime .cwd
11921202 )
@@ -1195,9 +1205,6 @@ def _run_interface(self, runtime):
11951205 mask_tmp_path ,
11961206 copy = True ,
11971207 use_hardlink = False )
1198- mask_tmp_path = regutils .check_orient_and_dims (
1199- mask_tmp_path , runtime .cwd ,
1200- self .inputs .vox_size )
12011208 else :
12021209 mask_tmp_path = None
12031210
@@ -1241,12 +1248,21 @@ def _run_interface(self, runtime):
12411248 else :
12421249 csf_mask = None
12431250
1251+ anat_file_tmp_path = fname_presuffix (
1252+ self .inputs .anat_file , suffix = "_tmp" , newpath = runtime .cwd
1253+ )
1254+ copyfile (
1255+ self .inputs .anat_file ,
1256+ anat_file_tmp_path ,
1257+ copy = True ,
1258+ use_hardlink = False )
1259+
12441260 reg = register .DmriReg (
12451261 basedir_path = runtime .cwd ,
12461262 fa_path = fa_tmp_path ,
12471263 ap_path = ap_tmp_path ,
12481264 B0_mask = B0_mask_tmp_path ,
1249- anat_file = self . inputs . anat_file ,
1265+ anat_file = anat_file_tmp_path ,
12501266 vox_size = self .inputs .vox_size ,
12511267 template_name = self .inputs .template_name ,
12521268 simple = self .inputs .simple ,
@@ -1284,7 +1300,7 @@ def _run_interface(self, runtime):
12841300 self ._results ["gm_in_dwi" ] = reg .gm_in_dwi
12851301 self ._results ["vent_csf_in_dwi" ] = reg .vent_csf_in_dwi
12861302 self ._results ["csf_mask_dwi" ] = reg .csf_mask_dwi
1287- self ._results ["anat_file" ] = self . inputs . anat_file
1303+ self ._results ["anat_file" ] = anat_file_tmp_path
12881304 self ._results ["t1w2dwi" ] = reg .t1w2dwi
12891305 self ._results ["B0_mask" ] = B0_mask_tmp_path
12901306 self ._results ["ap_path" ] = ap_tmp_path
@@ -1894,6 +1910,7 @@ class RegisterFunc(SimpleInterface):
18941910 def _run_interface (self , runtime ):
18951911 import gc
18961912 import glob
1913+ import os .path as op
18971914 from pynets .registration import register
18981915 from pynets .registration import reg_utils as regutils
18991916 from nipype .utils .filemanip import fname_presuffix , copyfile
@@ -1917,7 +1934,8 @@ def _run_interface(self, runtime):
19171934 copy = True ,
19181935 use_hardlink = False )
19191936 else :
1920- if len (anat_mask_existing ) > 0 and self .inputs .mask is None :
1937+ if len (anat_mask_existing ) > 0 and \
1938+ self .inputs .mask is None and op .isfile (anat_mask_existing [0 ]):
19211939 mask_tmp_path = fname_presuffix (
19221940 anat_mask_existing [0 ], suffix = "_tmp" ,
19231941 newpath = runtime .cwd
@@ -1927,9 +1945,6 @@ def _run_interface(self, runtime):
19271945 mask_tmp_path ,
19281946 copy = True ,
19291947 use_hardlink = False )
1930- mask_tmp_path = regutils .check_orient_and_dims (
1931- mask_tmp_path , self .inputs .in_dir ,
1932- self .inputs .vox_size )
19331948 else :
19341949 mask_tmp_path = None
19351950
@@ -1961,9 +1976,18 @@ def _run_interface(self, runtime):
19611976 else :
19621977 wm_mask = None
19631978
1979+ anat_file_tmp_path = fname_presuffix (
1980+ self .inputs .anat_file , suffix = "_tmp" , newpath = runtime .cwd
1981+ )
1982+ copyfile (
1983+ self .inputs .anat_file ,
1984+ anat_file_tmp_path ,
1985+ copy = True ,
1986+ use_hardlink = False )
1987+
19641988 reg = register .FmriReg (
19651989 basedir_path = runtime .cwd ,
1966- anat_file = self . inputs . anat_file ,
1990+ anat_file = anat_file_tmp_path ,
19671991 vox_size = self .inputs .vox_size ,
19681992 template_name = self .inputs .template_name ,
19691993 simple = self .inputs .simple ,
@@ -3063,7 +3087,7 @@ def _run_interface(self, runtime):
30633087 from dipy .io import save_pickle
30643088 from dipy .io import read_bvals_bvecs
30653089 from dipy .core .gradients import gradient_table
3066-
3090+ from nipype . utils . filemanip import copyfile , fname_presuffix
30673091 # from dipy.segment.mask import median_otsu
30683092 from pynets .registration .reg_utils import median
30693093 from pynets .dmri .dmri_utils import normalize_gradients , extract_b0
@@ -3075,8 +3099,26 @@ def _run_interface(self, runtime):
30753099 gtab_file = f"{ runtime .cwd } /gtab.pkl"
30763100 all_b0s_file = f"{ runtime .cwd } /all_b0s.nii.gz"
30773101
3102+ fbval_tmp_path = fname_presuffix (
3103+ self .inputs .fbval , suffix = "_tmp" , newpath = runtime .cwd
3104+ )
3105+ copyfile (
3106+ self .inputs .fbval ,
3107+ fbval_tmp_path ,
3108+ copy = True ,
3109+ use_hardlink = False )
3110+
3111+ fbvec_tmp_path = fname_presuffix (
3112+ self .inputs .fbvec , suffix = "_tmp" , newpath = runtime .cwd
3113+ )
3114+ copyfile (
3115+ self .inputs .fbvec ,
3116+ fbvec_tmp_path ,
3117+ copy = True ,
3118+ use_hardlink = False )
3119+
30783120 # loading bvecs/bvals
3079- bvals , bvecs = read_bvals_bvecs (self . inputs . fbval , self . inputs . fbvec )
3121+ bvals , bvecs = read_bvals_bvecs (fbval_tmp_path , fbvec_tmp_path )
30803122 bvecs_norm , bvals_norm = normalize_gradients (
30813123 bvecs , bvals , b0_threshold = self .inputs .b0_thr
30823124 )
@@ -3103,10 +3145,19 @@ def _run_interface(self, runtime):
31033145 # Save gradient table to pickle
31043146 save_pickle (gtab_file , gtab )
31053147
3148+ dwi_file_tmp_path = fname_presuffix (
3149+ self .inputs .dwi_file , suffix = "_tmp" , newpath = runtime .cwd
3150+ )
3151+ copyfile (
3152+ self .inputs .dwi_file ,
3153+ dwi_file_tmp_path ,
3154+ copy = True ,
3155+ use_hardlink = False )
3156+
31063157 # Extract and Combine all b0s collected, make mean b0
31073158 print ("Extracting b0's..." )
31083159 all_b0s_file = extract_b0 (
3109- self . inputs . dwi_file ,
3160+ dwi_file_tmp_path ,
31103161 b0_thr_ixs ,
31113162 all_b0s_file )
31123163 med_b0_file = median (all_b0s_file )
@@ -3130,11 +3181,16 @@ def _run_interface(self, runtime):
31303181 # Get mean B0 brain mask
31313182 cmd = f"bet { med_b0_file } { B0_bet } -m -f 0.2"
31323183 os .system (cmd )
3133- time .sleep (1 )
3184+ time .sleep (2 )
31343185
31353186 self ._results ["gtab_file" ] = gtab_file
31363187 self ._results ["B0_bet" ] = B0_bet
31373188 self ._results ["B0_mask" ] = B0_mask
31383189 self ._results ["dwi_file" ] = self .inputs .dwi_file
31393190
3191+ tmp_files = [fbval_tmp_path , fbvec_tmp_path , dwi_file_tmp_path ]
3192+ for j in tmp_files :
3193+ if j is not None :
3194+ os .remove (j )
3195+
31403196 return runtime
0 commit comments