Skip to content

Commit f7fa9ce

Browse files
author
dPys
committed
[ENH] Add try/except block to interfaces tracking
1 parent 0a8b95c commit f7fa9ce

File tree

7 files changed

+198
-113
lines changed

7 files changed

+198
-113
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ RUN apt-get update -qq \
4848
libglu1-mesa-dev \
4949
libglib2.0-0 \
5050
libglw1-mesa \
51+
libxkbcommon-x11-0 \
5152
liblapack-dev \
5253
libopenblas-base \
5354
sqlite3 \
@@ -190,6 +191,7 @@ ENV PATH="/opt/conda/bin":$PATH
190191
ENV OPENBLAS_NUM_THREADS=4 \
191192
GOTO_NUM_THREADS=4 \
192193
OMP_NUM_THREADS=4
194+
ENV QT_QPA_PLATFORM=offscreen
193195

194196
# and add it as an entrypoint
195197
#ENTRYPOINT ["pynets"]

pynets/core/interfaces.py

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ def _run_interface(self, runtime):
11541154
import gc
11551155
import time
11561156
import glob
1157+
import os
11571158
import os.path as op
11581159
from pynets.registration import register
11591160
from nipype.utils.filemanip import fname_presuffix, copyfile
@@ -1347,6 +1348,22 @@ def _run_interface(self, runtime):
13471348
self._results["t1w2dwi_xfm"] = reg.t1w2dwi_xfm
13481349
self._results["wm_gm_int_in_dwi"] = reg.wm_gm_int_in_dwi
13491350

1351+
reg_tmp = [
1352+
fa_tmp_path,
1353+
mask_tmp_path,
1354+
reg.warp_t1w2mni,
1355+
reg.t1w_head,
1356+
reg.wm_edge,
1357+
reg.vent_mask_dwi,
1358+
reg.vent_mask_t1w,
1359+
reg.corpuscallosum_mask_t1w,
1360+
reg.corpuscallosum_dwi
1361+
]
1362+
for j in reg_tmp:
1363+
if j is not None:
1364+
if os.path.isfile(j):
1365+
os.system(f"rm -f {j} &")
1366+
13501367
gc.collect()
13511368

13521369
return runtime
@@ -1745,6 +1762,8 @@ def _run_interface(self, runtime):
17451762
t1w2dwi_bbr_xfm_tmp_path,
17461763
t1w2dwi_xfm_tmp_path,
17471764
wm_gm_int_in_dwi_tmp_path,
1765+
aligned_atlas_skull,
1766+
t1w_brain_tmp_path
17481767
]
17491768
for j in reg_tmp:
17501769
if j is not None:
@@ -1922,6 +1941,9 @@ def _run_interface(self, runtime):
19221941
mni2t1w_warp_tmp_path,
19231942
t1wtissue2dwi_xfm_tmp_path,
19241943
mni2t1_xfm_tmp_path,
1944+
template_tmp_path,
1945+
roi_in_t1w,
1946+
roi_file_tmp_path
19251947
]
19261948
for j in reg_tmp:
19271949
if j is not None:
@@ -2238,6 +2260,8 @@ def _run_interface(self, runtime):
22382260
uatlas_tmp_path,
22392261
template_tmp_path,
22402262
t1w2mni_xfm_tmp_path,
2263+
t1w2mni_warp_tmp_path,
2264+
template_mask_tmp_path
22412265
]
22422266

22432267
for j in reg_tmp:
@@ -2612,6 +2636,7 @@ def _run_interface(self, runtime):
26122636
mni2t1w_warp_tmp_path,
26132637
mni2t1_xfm_tmp_path,
26142638
roi_file_tmp_path,
2639+
template_tmp_path
26152640
]
26162641
for j in reg_tmp:
26172642
if j is not None:
@@ -3036,35 +3061,37 @@ def _run_interface(self, runtime):
30363061
print(Style.RESET_ALL)
30373062

30383063
# Commence Ensemble Tractography
3039-
streamlines = track_ensemble(
3040-
self.inputs.target_samples,
3041-
labels_im_file_tmp_path_wm_gm_int,
3042-
labels_im_file_tmp_path,
3043-
recon_path,
3044-
get_sphere(sphere),
3045-
self.inputs.directget,
3046-
self.inputs.curv_thr_list,
3047-
self.inputs.step_list,
3048-
self.inputs.track_type,
3049-
self.inputs.maxcrossing,
3050-
int(roi_neighborhood_tol),
3051-
self.inputs.min_length,
3052-
waymask_tmp_path,
3053-
B0_mask_tmp_path,
3054-
t1w2dwi_tmp_path, gm_in_dwi_tmp_path,
3055-
vent_csf_in_dwi_tmp_path, wm_in_dwi_tmp_path,
3056-
self.inputs.tiss_class,
3057-
runtime.cwd
3058-
)
3059-
3060-
gc.collect()
3064+
try:
3065+
streamlines = track_ensemble(
3066+
self.inputs.target_samples,
3067+
labels_im_file_tmp_path_wm_gm_int,
3068+
labels_im_file_tmp_path,
3069+
recon_path,
3070+
get_sphere(sphere),
3071+
self.inputs.directget,
3072+
self.inputs.curv_thr_list,
3073+
self.inputs.step_list,
3074+
self.inputs.track_type,
3075+
self.inputs.maxcrossing,
3076+
int(roi_neighborhood_tol),
3077+
self.inputs.min_length,
3078+
waymask_tmp_path,
3079+
B0_mask_tmp_path,
3080+
t1w2dwi_tmp_path, gm_in_dwi_tmp_path,
3081+
vent_csf_in_dwi_tmp_path, wm_in_dwi_tmp_path,
3082+
self.inputs.tiss_class,
3083+
runtime.cwd
3084+
)
3085+
gc.collect()
3086+
except BaseException:
3087+
print(UserWarning("Tractography failed..."))
3088+
streamlines = None
30613089

30623090
if streamlines is not None:
30633091
# import multiprocessing
30643092
# from pynets.core.utils import kill_process_family
30653093
# return kill_process_family(int(multiprocessing.current_process().pid))
30663094

3067-
30683095
# Linear Fascicle Evaluation (LiFE)
30693096
if use_life is True:
30703097
print('Using LiFE to evaluate streamline plausibility...')

pynets/core/nodemaker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ def gen_network_parcels(uatlas, network, labels, dir_path):
12031203
)
12041204
out_path = f"{dir_path}" \
12051205
f"/{op.basename(uatlas).split(op.splitext(uatlas)[1])[0]}_" \
1206-
f"{network}{'_parcels.nii.gz'}"
1206+
f"{network}_parcels.nii.gz"
12071207
nib.save(nib.Nifti1Image(net_parcels_sum, affine=np.eye(4)), out_path)
12081208
del net_parcels_concatted, img_list
12091209
gc.collect()

pynets/dmri/estimation.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ def streams2graph(
553553
overlap_thr = hardcoded_params[
554554
"StructuralNetworkWeighting"]["overlap_thr"][0]
555555
roi_neighborhood_tol = \
556-
hardcoded_params['tracking']["roi_neighborhood_tol"][0]
556+
hardcoded_params['tracking']["roi_neighborhood_tol"][0]
557557

558558
start = time.time()
559559

@@ -709,7 +709,7 @@ def streams2graph(
709709
# Adapted from the nnormalized fiber-density estimation routines of
710710
# Sebastian Tourbier.
711711
if fiber_density is True:
712-
print("Weighting edges by fiber density...")
712+
print("Redefining edges on the basis of fiber density...")
713713
# Summarize total fibers and total label volumes
714714
total_fibers = 0
715715
total_volume = 0
@@ -736,7 +736,7 @@ def streams2graph(
736736
ix += 1
737737

738738
if fa_wei is True:
739-
print("Weighting edges by FA...")
739+
print("Re-weighting edges by FA...")
740740
# Add FA attributes for each edge
741741
ix = 0
742742
for u, v, d in g.edges(data=True):
@@ -815,12 +815,6 @@ def streams2graph(
815815
),
816816
)
817817

818-
tmp_files = [streams, warped_fa]
819-
for j in tmp_files:
820-
if j is not None:
821-
if os.path.isfile(j):
822-
os.system(f"rm -f {j} &")
823-
824818
return (
825819
atlas_mni,
826820
streams,

pynets/dmri/track.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def track_ensemble(
480480
while float(stream_counter) < float(target_samples) and float(ix) < 0.50*float(len(all_combs)):
481481
with Parallel(n_jobs=nthreads, backend='loky',
482482
mmap_mode='r+', temp_folder=joblib_dir,
483-
verbose=2, max_nbytes='50000M',
483+
verbose=2, max_nbytes='25000M',
484484
timeout=timeout) as parallel:
485485
out_streams = parallel(
486486
delayed(run_tracking)(

pynets/stats/benchmarking.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,6 @@ def benchmark_reproducibility(comb, modality, alg, par_dict, disc,
562562
print('Empty dataframe!')
563563
return df_summary
564564

565-
566565
shapes = []
567566
for ix, i in enumerate(vect_all):
568567
shapes.append(i.shape[0] * [list(ids)[ix]])
@@ -594,23 +593,23 @@ def benchmark_reproducibility(comb, modality, alg, par_dict, disc,
594593
if __name__ == "__main__":
595594
__spec__ = "ModuleSpec(name='builtins', loader=<class '_" \
596595
"frozen_importlib.BuiltinImporter'>)"
597-
#base_dir = '/scratch/04171/dpisner/HNU/HNU_outs/triple'
598-
base_dir = '/scratch/04171/dpisner/HNU/HNU_outs/outputs_language'
596+
base_dir = '/scratch/04171/dpisner/HNU/HNU_outs/triple'
597+
#base_dir = '/scratch/04171/dpisner/HNU/HNU_outs/outputs_language'
599598
thr_type = "MST"
600-
icc = True
599+
icc = False
601600
disc = True
602-
int_consist = True
603-
target_modality = 'func'
601+
int_consist = False
602+
target_modality = 'dwi'
604603

605604
#embedding_types = ['ASE']
606-
embedding_types = ['topology']
605+
#embedding_types = ['topology']
607606
#embedding_types = ['OMNI']
608-
#embedding_types = ['OMNI', 'ASE']
607+
embedding_types = ['OMNI', 'ASE']
609608
modalities = ['func', 'dwi']
610-
#rsns = ['triple', 'kmeans']
611-
rsns = ['language']
612-
template = 'CN200'
613-
#template = 'MNI152_T1'
609+
rsns = ['kmeans']
610+
#rsns = ['language']
611+
#template = 'CN200'
612+
template = 'MNI152_T1'
614613
mets = ["global_efficiency",
615614
"average_shortest_path_length",
616615
"degree_assortativity_coefficient",
@@ -686,10 +685,6 @@ def tuple_insert(tup, pos, ele):
686685
tup = tup[:pos] + (ele,) + tup[pos:]
687686
return tup
688687

689-
# rsns = ['SalVentAttnA', 'DefaultA', 'ContB']
690-
#rsns = ["triple", "kmeans"]
691-
rsns = ["language"]
692-
693688
for modality in modalities:
694689
print(f"MODALITY: {modality}")
695690
hyperparams = eval(f"hyperparams_{modality}")

0 commit comments

Comments
 (0)