Skip to content

Commit e2dffd0

Browse files
authored
Merge pull request #418 from dPys/development
Development
2 parents f6d870a + d11207a commit e2dffd0

File tree

14 files changed

+26551
-7740
lines changed

14 files changed

+26551
-7740
lines changed

docs/_static/omnetome.png

1.31 MB
Loading

pynets/cli/pynets_run.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,14 @@ def get_parser():
403403
parser.add_argument(
404404
"-em",
405405
metavar="Error margin",
406-
default=12,
406+
default=10,
407407
nargs="+",
408408
help="(Hyperparameter): Distance (in the units of the streamlines, "
409409
"usually mm). If any coordinate in the streamline is within this "
410410
"distance from the center of any voxel in the ROI, the filtering "
411411
"criterion is set to True for this streamline, otherwise False. "
412412
"Defaults to the distance between the center of each voxel and "
413-
"the corner of the voxel.\n",
413+
"the corner of the voxel. Default is 10.\n",
414414
)
415415
parser.add_argument(
416416
"-dg",
@@ -3114,9 +3114,6 @@ def wf_multi_subject(
31143114
config.update_config(cfg_v)
31153115
config.enable_debug_mode()
31163116
config.enable_resource_monitor()
3117-
3118-
import logging
3119-
31203117
callback_log_path = f"{wf_multi.base_dir}/run_stats.log"
31213118
logger = logging.getLogger("callback")
31223119
logger.setLevel(logging.DEBUG)
@@ -3171,7 +3168,7 @@ def wf_multi_subject(
31713168
shutil.rmtree(cnfnd_tmp_dir)
31723169
shutil.rmtree(f"{dir}/reg_fmri", ignore_errors=True)
31733170
for file_ in [i for i in glob.glob(
3174-
f"{dir}/*/*") if os.path.isfile(i)]:
3171+
f"{dir}/func/*") if os.path.isfile(i)]:
31753172
if ("reor-RAS" in file_) or ("res-" in file_):
31763173
try:
31773174
os.remove(file_)
@@ -3181,7 +3178,7 @@ def wf_multi_subject(
31813178
shutil.rmtree(f"{dir}/dmri_tmp", ignore_errors=True)
31823179
shutil.rmtree(f"{dir}/reg_dmri", ignore_errors=True)
31833180
for file_ in [i for i in glob.glob(
3184-
f"{dir}/*/*") if os.path.isfile(i)]:
3181+
f"{dir}/dwi/*") if os.path.isfile(i)]:
31853182
if ("reor-RAS" in file_) or ("res-" in file_):
31863183
try:
31873184
os.remove(file_)
@@ -3319,9 +3316,6 @@ def wf_multi_subject(
33193316
config.update_config(cfg_v)
33203317
config.enable_debug_mode()
33213318
config.enable_resource_monitor()
3322-
3323-
import logging
3324-
33253319
callback_log_path = f"{wf.base_dir}/run_stats.log"
33263320
logger = logging.getLogger("callback")
33273321
logger.setLevel(logging.DEBUG)
@@ -3372,15 +3366,15 @@ def wf_multi_subject(
33723366
print("Cleaning up...")
33733367
if func_file:
33743368
for file_ in [i for i in glob.glob(
3375-
f"{subj_dir}/*/*") if os.path.isfile(i)]:
3369+
f"{subj_dir}/func/*") if os.path.isfile(i)]:
33763370
if ("reor-RAS" in file_) or ("res-" in file_):
33773371
try:
33783372
os.remove(file_)
33793373
except BaseException:
33803374
continue
33813375
if dwi_file:
33823376
for file_ in [i for i in glob.glob(
3383-
f"{subj_dir}/*/*") if os.path.isfile(i)]:
3377+
f"{subj_dir}/dwi/*") if os.path.isfile(i)]:
33843378
if ("reor-RAS" in file_) or ("res-" in file_) or \
33853379
("_bvecs_reor.bvec" in file_):
33863380
try:

pynets/core/interfaces.py

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

pynets/core/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,9 +1569,8 @@ def save_ts_to_file(
15691569
"%s" % ("%s%s%s" % ("hpass-",
15701570
hpass,
15711571
"Hz_") if hpass is not None else ""),
1572-
"%s" % ("%s%s%s" % ("extract-",
1573-
extract_strategy,
1574-
"_") if extract_strategy is not None else ""),
1572+
"%s" % ("%s%s" % ("extract-",
1573+
extract_strategy) if extract_strategy is not None else ""),
15751574
".npy",
15761575
)
15771576

pynets/core/workflows.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,8 @@ def dmri_connectometry(
13921392
base_dirname = f"dmri_connectometry_{ID}"
13931393
dmri_connectometry_wf = pe.Workflow(name=base_dirname)
13941394

1395-
if template_name == "MNI152_T1" or template_name == "colin27" or template_name == "CN200":
1395+
if template_name == "MNI152_T1" or template_name == "colin27" or \
1396+
template_name == "CN200":
13961397
template = pkg_resources.resource_filename(
13971398
"pynets", f"templates/{template_name}_brain_{vox_size}.nii.gz"
13981399
)

pynets/dmri/dmri_utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def evaluate_streamline_plausibility(dwi_data, gtab, mask_data, streamlines,
161161
import dipy.tracking.life as life
162162
import dipy.core.optimize as opt
163163
from dipy.tracking._utils import _mapping_to_voxel
164-
from dipy.data import get_sphere
164+
# from dipy.data import get_sphere
165165
from dipy.tracking import utils
166166
from dipy.tracking.streamline import Streamlines
167167

@@ -202,11 +202,14 @@ def evaluate_streamline_plausibility(dwi_data, gtab, mask_data, streamlines,
202202
dwi_data.shape).astype('bool') * dwi_data)
203203
# ! Remember this 4d masking function !
204204

205-
sphere = get_sphere(sphere)
206205
fiber_model = life.FiberModel(gtab)
207206
fiber_fit = fiber_model.fit(data_in_mask, streamlines_in_brain,
208207
affine=affine,
209-
sphere=sphere)
208+
sphere=False)
209+
# sphere = get_sphere(sphere)
210+
# fiber_fit = fiber_model.fit(data_in_mask, streamlines_in_brain,
211+
# affine=affine,
212+
# sphere=sphere)
210213
streamlines = list(np.array(streamlines_in_brain)[
211214
np.where(fiber_fit.beta > 0)[0]])
212215
pruned_count = len(streamlines)
@@ -220,6 +223,8 @@ def evaluate_streamline_plausibility(dwi_data, gtab, mask_data, streamlines,
220223
'LiFE model. Is your acquisition hemispheric? '
221224
'Also check the gradient table for errors. \n'))
222225
return streamlines_in_brain
226+
else:
227+
del streamlines_in_brain
223228

224229
model_predict = fiber_fit.predict()
225230
model_error = model_predict - fiber_fit.data

0 commit comments

Comments
 (0)