Skip to content

Commit 00c9add

Browse files
authored
Merge pull request #420 from dPys/development
Development
2 parents 8a3802f + 2a4695b commit 00c9add

File tree

6 files changed

+303
-223
lines changed

6 files changed

+303
-223
lines changed

pynets/core/interfaces.py

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ def _run_interface(self, runtime):
574574
template_tmp_path,
575575
self.inputs.simple,
576576
)
577+
time.sleep(0.5)
577578

578579
if self.inputs.mask:
579580
out_name_mask = fname_presuffix(
@@ -1143,11 +1144,12 @@ class RegisterDWI(SimpleInterface):
11431144

11441145
def _run_interface(self, runtime):
11451146
import gc
1147+
import time
11461148
import glob
11471149
import os.path as op
11481150
from pynets.registration import register
1149-
from pynets.registration import reg_utils as regutils
11501151
from nipype.utils.filemanip import fname_presuffix, copyfile
1152+
from pynets.registration.reg_utils import check_orient_and_dims
11511153

11521154
fa_tmp_path = fname_presuffix(
11531155
self.inputs.fa_path, suffix="_tmp", newpath=runtime.cwd
@@ -1205,6 +1207,9 @@ def _run_interface(self, runtime):
12051207
mask_tmp_path,
12061208
copy=True,
12071209
use_hardlink=False)
1210+
mask_tmp_path = check_orient_and_dims(
1211+
mask_tmp_path, runtime.cwd, self.inputs.vox_size
1212+
)
12081213
else:
12091214
mask_tmp_path = None
12101215

@@ -1218,6 +1223,9 @@ def _run_interface(self, runtime):
12181223
gm_mask,
12191224
copy=True,
12201225
use_hardlink=False)
1226+
gm_mask = check_orient_and_dims(
1227+
gm_mask, runtime.cwd, self.inputs.vox_size
1228+
)
12211229
else:
12221230
gm_mask = None
12231231

@@ -1231,6 +1239,9 @@ def _run_interface(self, runtime):
12311239
wm_mask,
12321240
copy=True,
12331241
use_hardlink=False)
1242+
wm_mask = check_orient_and_dims(
1243+
wm_mask, runtime.cwd, self.inputs.vox_size
1244+
)
12341245
else:
12351246
wm_mask = None
12361247

@@ -1245,6 +1256,9 @@ def _run_interface(self, runtime):
12451256
csf_mask,
12461257
copy=True,
12471258
use_hardlink=False)
1259+
csf_mask = check_orient_and_dims(
1260+
csf_mask, runtime.cwd, self.inputs.vox_size
1261+
)
12481262
else:
12491263
csf_mask = None
12501264

@@ -1270,9 +1284,11 @@ def _run_interface(self, runtime):
12701284

12711285
# Generate T1w brain mask
12721286
reg.gen_mask(mask_tmp_path)
1287+
time.sleep(0.5)
12731288

12741289
# Perform anatomical segmentation
12751290
reg.gen_tissue(wm_mask, gm_mask, csf_mask, self.inputs.overwrite)
1291+
time.sleep(0.5)
12761292

12771293
# Align t1w to mni template
12781294
# from joblib import Memory
@@ -1284,17 +1300,20 @@ def _run_interface(self, runtime):
12841300
# t1w2mni_align = memory.cache(reg.t1w2mni_align)
12851301
# t1w2mni_align()
12861302
reg.t1w2mni_align()
1303+
time.sleep(0.5)
12871304

12881305
if (self.inputs.overwrite is True) or (
12891306
op.isfile(reg.t1w2dwi) is False):
12901307
# Align t1w to dwi
12911308
reg.t1w2dwi_align()
1309+
time.sleep(0.5)
12921310

12931311
if (self.inputs.overwrite is True) or (
12941312
op.isfile(reg.wm_gm_int_in_dwi) is False
12951313
):
12961314
# Align tissue
12971315
reg.tissue2dwi_align()
1316+
time.sleep(0.5)
12981317

12991318
self._results["wm_in_dwi"] = reg.wm_in_dwi
13001319
self._results["gm_in_dwi"] = reg.gm_in_dwi
@@ -1389,6 +1408,7 @@ class RegisterAtlasDWI(SimpleInterface):
13891408

13901409
def _run_interface(self, runtime):
13911410
import gc
1411+
import time
13921412
import os
13931413
from pynets.registration import reg_utils as regutils
13941414
from pynets.core.nodemaker import \
@@ -1561,6 +1581,9 @@ def _run_interface(self, runtime):
15611581
base_dir_tmp = f"{runtime.cwd}/atlas_{atlas_name}"
15621582
os.makedirs(base_dir_tmp, exist_ok=True)
15631583

1584+
mni2dwi_xfm = f"{base_dir_tmp}{'/'}{atlas_name}" \
1585+
f"{'_mni2dwi_xfm.mat'}"
1586+
15641587
aligned_atlas_t1mni = f"{base_dir_tmp}{'/'}{atlas_name}" \
15651588
f"{'_t1w_mni.nii.gz'}"
15661589
aligned_atlas_skull = f"{base_dir_tmp}{'/'}{atlas_name}" \
@@ -1597,6 +1620,7 @@ def _run_interface(self, runtime):
15971620
dwi_aligned_atlas,
15981621
dwi_aligned_atlas_wmgm_int,
15991622
B0_mask_tmp_path,
1623+
mni2dwi_xfm,
16001624
self.inputs.simple,
16011625
)
16021626

@@ -1637,6 +1661,7 @@ def _run_interface(self, runtime):
16371661
template_tmp_path,
16381662
self.inputs.simple,
16391663
)
1664+
time.sleep(0.5)
16401665
os.remove(waymask_tmp_path)
16411666
else:
16421667
waymask_in_dwi = None
@@ -1739,6 +1764,7 @@ class RegisterROIDWI(SimpleInterface):
17391764
def _run_interface(self, runtime):
17401765
import gc
17411766
import os
1767+
import time
17421768
from pynets.registration import reg_utils as regutils
17431769
from nipype.utils.filemanip import fname_presuffix, copyfile
17441770
import pkg_resources
@@ -1852,6 +1878,7 @@ def _run_interface(self, runtime):
18521878
template_tmp_path,
18531879
self.inputs.simple,
18541880
)
1881+
time.sleep(0.5)
18551882
else:
18561883
roi_in_dwi = None
18571884

@@ -1910,10 +1937,11 @@ class RegisterFunc(SimpleInterface):
19101937
def _run_interface(self, runtime):
19111938
import gc
19121939
import glob
1940+
import time
19131941
import os.path as op
19141942
from pynets.registration import register
1915-
from pynets.registration import reg_utils as regutils
19161943
from nipype.utils.filemanip import fname_presuffix, copyfile
1944+
from pynets.registration.reg_utils import check_orient_and_dims
19171945

19181946
anat_mask_existing = [
19191947
i
@@ -1945,6 +1973,9 @@ def _run_interface(self, runtime):
19451973
mask_tmp_path,
19461974
copy=True,
19471975
use_hardlink=False)
1976+
mask_tmp_path = check_orient_and_dims(
1977+
mask_tmp_path, runtime.cwd, self.inputs.vox_size
1978+
)
19481979
else:
19491980
mask_tmp_path = None
19501981

@@ -1959,6 +1990,9 @@ def _run_interface(self, runtime):
19591990
gm_mask,
19601991
copy=True,
19611992
use_hardlink=False)
1993+
gm_mask = check_orient_and_dims(
1994+
gm_mask, runtime.cwd, self.inputs.vox_size
1995+
)
19621996
else:
19631997
gm_mask = None
19641998

@@ -1973,6 +2007,9 @@ def _run_interface(self, runtime):
19732007
wm_mask,
19742008
copy=True,
19752009
use_hardlink=False)
2010+
wm_mask = check_orient_and_dims(
2011+
wm_mask, runtime.cwd, self.inputs.vox_size
2012+
)
19762013
else:
19772014
wm_mask = None
19782015

@@ -1995,9 +2032,11 @@ def _run_interface(self, runtime):
19952032

19962033
# Generate T1w brain mask
19972034
reg.gen_mask(mask_tmp_path)
2035+
time.sleep(0.5)
19982036

19992037
# Perform anatomical segmentation
20002038
reg.gen_tissue(wm_mask, gm_mask, self.inputs.overwrite)
2039+
time.sleep(0.5)
20012040

20022041
# Align t1w to mni template
20032042
# from joblib import Memory
@@ -2009,6 +2048,7 @@ def _run_interface(self, runtime):
20092048
# t1w2mni_align = memory.cache(reg.t1w2mni_align)
20102049
# t1w2mni_align()
20112050
reg.t1w2mni_align()
2051+
time.sleep(0.5)
20122052

20132053
self._results["reg_fmri_complete"] = True
20142054
self._results["basedir_path"] = runtime.cwd
@@ -2055,6 +2095,7 @@ def _run_interface(self, runtime):
20552095
import gc
20562096
import os
20572097
import pkg_resources
2098+
import time
20582099
from pynets.core.utils import prune_suffices
20592100
from pynets.registration import reg_utils as regutils
20602101
from nipype.utils.filemanip import fname_presuffix, copyfile
@@ -2146,6 +2187,7 @@ def _run_interface(self, runtime):
21462187
t1w2mni_warp_tmp_path,
21472188
self.inputs.simple
21482189
)
2190+
time.sleep(0.5)
21492191

21502192
out_dir = f"{self.inputs.dir_path}/t1w_clustered_parcellations/"
21512193
os.makedirs(out_dir, exist_ok=True)
@@ -2216,6 +2258,7 @@ class RegisterAtlasFunc(SimpleInterface):
22162258
def _run_interface(self, runtime):
22172259
import gc
22182260
import os
2261+
import time
22192262
import glob
22202263
from pynets.registration import reg_utils as regutils
22212264
from pynets.core.nodemaker import \
@@ -2355,6 +2398,7 @@ def _run_interface(self, runtime):
23552398
aligned_atlas_gm,
23562399
self.inputs.simple,
23572400
)
2401+
time.sleep(0.5)
23582402

23592403
# Correct coords and labels
23602404
[aligned_atlas_gm, coords, labels] = \
@@ -2447,6 +2491,7 @@ class RegisterROIEPI(SimpleInterface):
24472491
def _run_interface(self, runtime):
24482492
import gc
24492493
import os
2494+
import time
24502495
from pynets.registration import reg_utils as regutils
24512496
from nipype.utils.filemanip import fname_presuffix, copyfile
24522497
import pkg_resources
@@ -2516,6 +2561,7 @@ def _run_interface(self, runtime):
25162561
template_tmp_path,
25172562
self.inputs.simple,
25182563
)
2564+
time.sleep(0.5)
25192565
else:
25202566
roi_in_t1w = None
25212567

@@ -2688,7 +2734,6 @@ def _run_interface(self, runtime):
26882734
B0_mask_tmp_path,
26892735
copy=True,
26902736
use_hardlink=False)
2691-
B0_mask_data = nib.load(B0_mask_tmp_path, mmap=True).get_fdata()
26922737

26932738
# Fit diffusion model
26942739
# Save reconstruction to .npy
@@ -2744,6 +2789,7 @@ def _run_interface(self, runtime):
27442789
copy=True,
27452790
use_hardlink=False,
27462791
)
2792+
del model
27472793
else:
27482794
print(
27492795
f"Found existing reconstruction with "
@@ -2754,7 +2800,6 @@ def _run_interface(self, runtime):
27542800
copy=True,
27552801
use_hardlink=False,
27562802
)
2757-
model = np.load(recon_path, mmap_mode='r+').astype('float32')
27582803

27592804
dwi_img.uncache()
27602805
del dwi_data
@@ -2780,12 +2825,6 @@ def _run_interface(self, runtime):
27802825
copy=True,
27812826
use_hardlink=False)
27822827

2783-
atlas_img = nib.load(labels_im_file_tmp_path, mmap=True)
2784-
atlas_data = np.array(atlas_img.dataobj).astype("uint16")
2785-
atlas_data_wm_gm_int = np.asarray(
2786-
nib.load(labels_im_file_tmp_path_wm_gm_int, mmap=True).dataobj
2787-
).astype("uint16")
2788-
27892828
t1w2dwi_tmp_path = fname_presuffix(
27902829
self.inputs.t1w2dwi, suffix="_tmp",
27912830
newpath=runtime.cwd
@@ -2836,22 +2875,8 @@ def _run_interface(self, runtime):
28362875
waymask_tmp_path,
28372876
copy=True,
28382877
use_hardlink=False)
2839-
waymask_data = np.asarray(nib.load(waymask_tmp_path,
2840-
mmap=True).dataobj
2841-
).astype("bool")
28422878
else:
2843-
waymask_data = None
2844-
2845-
# Build mask vector from atlas for later roi filtering
2846-
parcels = []
2847-
i = 0
2848-
intensities = [i for i in np.unique(atlas_data) if i != 0]
2849-
for roi_val in intensities:
2850-
parcels.append(atlas_data == roi_val)
2851-
i += 1
2852-
2853-
del atlas_data
2854-
gc.collect()
2879+
waymask_tmp_path = None
28552880

28562881
# Iteratively build a list of streamlines for each ROI while tracking
28572882
print(
@@ -2889,9 +2914,9 @@ def _run_interface(self, runtime):
28892914
# Commence Ensemble Tractography
28902915
streamlines = track_ensemble(
28912916
self.inputs.target_samples,
2892-
atlas_data_wm_gm_int,
2893-
parcels,
2894-
model,
2917+
labels_im_file_tmp_path_wm_gm_int,
2918+
labels_im_file_tmp_path,
2919+
recon_path,
28952920
get_sphere(sphere),
28962921
self.inputs.directget,
28972922
self.inputs.curv_thr_list,
@@ -2900,14 +2925,14 @@ def _run_interface(self, runtime):
29002925
self.inputs.maxcrossing,
29012926
int(roi_neighborhood_tol),
29022927
self.inputs.min_length,
2903-
waymask_data,
2904-
B0_mask_data,
2928+
waymask_tmp_path,
2929+
B0_mask_tmp_path,
29052930
t1w2dwi_tmp_path, gm_in_dwi_tmp_path,
29062931
vent_csf_in_dwi_tmp_path, wm_in_dwi_tmp_path,
2907-
self.inputs.tiss_class, B0_mask_tmp_path
2932+
self.inputs.tiss_class,
2933+
runtime.cwd
29082934
)
29092935

2910-
del model, parcels, atlas_data_wm_gm_int
29112936
gc.collect()
29122937

29132938
# Save streamlines to trk
@@ -2951,15 +2976,16 @@ def _run_interface(self, runtime):
29512976
if use_life is True:
29522977
print('Using LiFE to evaluate streamline plausibility...')
29532978
from pynets.dmri.dmri_utils import evaluate_streamline_plausibility
2954-
dwi_img = nib.load(dwi_file_tmp_path, mmap=True)
2979+
dwi_img = nib.load(dwi_file_tmp_path)
29552980
dwi_data = dwi_img.get_fdata().astype('float32')
29562981
orig_count = len(streamlines)
29572982

29582983
if self.inputs.waymask:
2959-
mask_data = waymask_data
2984+
mask_data = nib.load(waymask_tmp_path
2985+
).get_fdata().astype('bool').astype('int')
29602986
else:
29612987
mask_data = nib.load(wm_in_dwi_tmp_path
2962-
).get_fdata().astype('float32')
2988+
).get_fdata().astype('bool').astype('int')
29632989
try:
29642990
streamlines = evaluate_streamline_plausibility(
29652991
dwi_data, gtab, mask_data, streamlines,
@@ -2971,9 +2997,7 @@ def _run_interface(self, runtime):
29712997
if len(streamlines) < 0.5*orig_count:
29722998
raise ValueError('LiFE revealed no plausible streamlines in '
29732999
'the tractogram!')
2974-
del dwi_data
2975-
2976-
del B0_mask_data, waymask_data
3000+
del dwi_data, mask_data
29773001

29783002
stf = StatefulTractogram(
29793003
streamlines,

0 commit comments

Comments
 (0)