Skip to content

Commit a6b34f8

Browse files
committed
update
1 parent a91c608 commit a6b34f8

13 files changed

Lines changed: 629 additions & 434 deletions

brainbuilder/align/align_2d.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
shell,
2121
threshold,
2222
)
23-
from joblib import Parallel, cpu_count, delayed
23+
from joblib import Parallel, cpu_count, delayed, parallel_backend
2424
from skimage.transform import resize
2525

2626
logger = utils.get_logger(__name__)
2727

28+
2829
def get_base_from_raw(x):
2930
"""Get base from raw filename."""
30-
3131
return os.path.basename(x).split(".")[0]
32-
32+
33+
3334
def resample_reference_to_sections(
3435
resolution: float,
3536
input_fn: str,
@@ -239,7 +240,8 @@ def ants_registration_2d_section(
239240
else:
240241
init_str = f"--initial-moving-transform [{fx_fn},{mv_fn},1]"
241242

242-
command_str = f"antsRegistration -v 1 -d 2 --write-composite-transform {write_composite_transform} {init_str} -o [{prefix}_{transform}_{metric}_,{mv_rsl_fn},/tmp/out_inv.nii.gz] -t {transform}[{step}] -m {metric}[{fx_fn},{mv_fn},1,{bins},Random,{sampling}] -s {s_str} -f {f_str} -c {itr_str} "
243+
inv_out = f"{prefix}_{transform}_{metric}_inv.nii.gz"
244+
command_str = f"antsRegistration -v 1 -d 2 --write-composite-transform {write_composite_transform} {init_str} -o [{prefix}_{transform}_{metric}_,{mv_rsl_fn},{inv_out}] -t {transform}[{step}] -m {metric}[{fx_fn},{mv_fn},1,{bins},Random,{sampling}] -s {s_str} -f {f_str} -c {itr_str} "
243245

244246
if int(verbose) <= 0:
245247
command_str += f" &> {output_log_fname}"
@@ -379,7 +381,6 @@ def align_2d_parallel(
379381
y = int(row["sample"])
380382
base = get_base_from_raw(row["raw"])
381383

382-
383384
prefix = f"{tfm_dir}/{base}_y-{y}"
384385

385386
fx_fn = row["fx"]
@@ -490,12 +491,14 @@ def apply_transforms_parallel(
490491

491492
tfm_fn = row["2d_tfm"]
492493

493-
if not isinstance(tfm_fn, str) : # assume identity transform
494-
print(f"Warning: transform file does not exist, assuming identity transform in:\n{base}")
495-
# copy img_rsl_fn to out_fn
494+
if not isinstance(tfm_fn, str): # assume identity transform
495+
print(
496+
f"Warning: transform file does not exist, assuming identity transform in:\n{base}"
497+
)
498+
# copy img_rsl_fn to out_fn
496499
shutil.copy(img_rsl_fn, out_fn)
497500
return out_fn
498-
501+
499502
cmd = f"antsApplyTransforms -v {int(verbose)} -d 2 -n {interpolation} -i {img_rsl_fn} -r {fx_fn} -t {tfm_fn} -o {out_fn} "
500503

501504
shell(cmd, True)
@@ -610,44 +613,43 @@ def align_sections(
610613

611614
tfm_dir = output_dir + os.sep + "tfm"
612615

613-
614-
615616
sect_info = get_align_filenames(tfm_dir, sect_info)
616617

617-
618618
# get lists of files that need to be aligned and resampled
619619
to_do_sect_info, to_do_resample_sect_info = get_align_2d_to_do(sect_info)
620620

621621
if len(to_do_sect_info) > 0:
622-
Parallel(n_jobs=num_cores, backend="multiprocessing")(
623-
delayed(align_2d_parallel)(
624-
tfm_dir,
625-
resolution,
626-
resolution_list,
627-
row,
628-
base_lin_itr=base_lin_itr,
629-
base_nl_itr=base_nl_itr,
630-
file_to_align=file_to_align,
631-
use_syn=use_syn,
632-
verbose=verbose,
633-
)
634-
for row in to_do_sect_info
635-
)
636-
637-
if len(to_do_resample_sect_info) > 0:
638-
for tissue_str in ["", "_cls"]:
639-
Parallel(n_jobs=num_cores, backend="multiprocessing")(
640-
delayed(apply_transforms_parallel)(
622+
with parallel_backend("loky", inner_max_num_threads=1):
623+
Parallel(n_jobs=num_cores, backend="loky", prefer="processes")(
624+
delayed(align_2d_parallel)(
641625
tfm_dir,
642626
resolution,
627+
resolution_list,
643628
row,
644-
tissue_str=tissue_str,
645-
interpolation=interpolation,
629+
base_lin_itr=base_lin_itr,
630+
base_nl_itr=base_nl_itr,
631+
file_to_align=file_to_align,
632+
use_syn=use_syn,
646633
verbose=verbose,
647634
)
648-
for row in to_do_resample_sect_info
635+
for row in to_do_sect_info
649636
)
650637

638+
if len(to_do_resample_sect_info) > 0:
639+
for tissue_str in ["", "_cls"]:
640+
with parallel_backend("loky", inner_max_num_threads=1):
641+
Parallel(n_jobs=num_cores, backend="loky", prefer="processes")(
642+
delayed(apply_transforms_parallel)(
643+
tfm_dir,
644+
resolution,
645+
row,
646+
tissue_str=tissue_str,
647+
interpolation=interpolation,
648+
verbose=verbose,
649+
)
650+
for row in to_do_resample_sect_info
651+
)
652+
651653
sect_info["2d_align"] = sect_info["2d_align_out"]
652654
sect_info["2d_align_cls"] = sect_info["2d_align_cls_out"]
653655

@@ -764,9 +766,7 @@ def align_2d(
764766
)
765767

766768
logger.info("\t\tStep 4: 2d nl alignment")
767-
sect_info["base"] = sect_info["raw"].apply(
768-
lambda x: get_base_from_raw(x)
769-
)
769+
sect_info["base"] = sect_info["raw"].apply(lambda x: get_base_from_raw(x))
770770

771771
# Align 2D sections to sections from reference volume using ANTs
772772
sect_info = align_sections(

0 commit comments

Comments
 (0)