|
20 | 20 | shell, |
21 | 21 | threshold, |
22 | 22 | ) |
23 | | -from joblib import Parallel, cpu_count, delayed |
| 23 | +from joblib import Parallel, cpu_count, delayed, parallel_backend |
24 | 24 | from skimage.transform import resize |
25 | 25 |
|
26 | 26 | logger = utils.get_logger(__name__) |
27 | 27 |
|
| 28 | + |
28 | 29 | def get_base_from_raw(x): |
29 | 30 | """Get base from raw filename.""" |
30 | | - |
31 | 31 | return os.path.basename(x).split(".")[0] |
32 | | - |
| 32 | + |
| 33 | + |
33 | 34 | def resample_reference_to_sections( |
34 | 35 | resolution: float, |
35 | 36 | input_fn: str, |
@@ -239,7 +240,8 @@ def ants_registration_2d_section( |
239 | 240 | else: |
240 | 241 | init_str = f"--initial-moving-transform [{fx_fn},{mv_fn},1]" |
241 | 242 |
|
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} " |
243 | 245 |
|
244 | 246 | if int(verbose) <= 0: |
245 | 247 | command_str += f" &> {output_log_fname}" |
@@ -379,7 +381,6 @@ def align_2d_parallel( |
379 | 381 | y = int(row["sample"]) |
380 | 382 | base = get_base_from_raw(row["raw"]) |
381 | 383 |
|
382 | | - |
383 | 384 | prefix = f"{tfm_dir}/{base}_y-{y}" |
384 | 385 |
|
385 | 386 | fx_fn = row["fx"] |
@@ -490,12 +491,14 @@ def apply_transforms_parallel( |
490 | 491 |
|
491 | 492 | tfm_fn = row["2d_tfm"] |
492 | 493 |
|
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 |
496 | 499 | shutil.copy(img_rsl_fn, out_fn) |
497 | 500 | return out_fn |
498 | | - |
| 501 | + |
499 | 502 | cmd = f"antsApplyTransforms -v {int(verbose)} -d 2 -n {interpolation} -i {img_rsl_fn} -r {fx_fn} -t {tfm_fn} -o {out_fn} " |
500 | 503 |
|
501 | 504 | shell(cmd, True) |
@@ -610,44 +613,43 @@ def align_sections( |
610 | 613 |
|
611 | 614 | tfm_dir = output_dir + os.sep + "tfm" |
612 | 615 |
|
613 | | - |
614 | | - |
615 | 616 | sect_info = get_align_filenames(tfm_dir, sect_info) |
616 | 617 |
|
617 | | - |
618 | 618 | # get lists of files that need to be aligned and resampled |
619 | 619 | to_do_sect_info, to_do_resample_sect_info = get_align_2d_to_do(sect_info) |
620 | 620 |
|
621 | 621 | 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)( |
641 | 625 | tfm_dir, |
642 | 626 | resolution, |
| 627 | + resolution_list, |
643 | 628 | 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, |
646 | 633 | verbose=verbose, |
647 | 634 | ) |
648 | | - for row in to_do_resample_sect_info |
| 635 | + for row in to_do_sect_info |
649 | 636 | ) |
650 | 637 |
|
| 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 | + |
651 | 653 | sect_info["2d_align"] = sect_info["2d_align_out"] |
652 | 654 | sect_info["2d_align_cls"] = sect_info["2d_align_cls_out"] |
653 | 655 |
|
@@ -764,9 +766,7 @@ def align_2d( |
764 | 766 | ) |
765 | 767 |
|
766 | 768 | 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)) |
770 | 770 |
|
771 | 771 | # Align 2D sections to sections from reference volume using ANTs |
772 | 772 | sect_info = align_sections( |
|
0 commit comments