Skip to content

Commit 33198ea

Browse files
authored
Merge pull request #28 from nimh-dsst/feature/parallel-renders
Update generate_renders.py
2 parents e8d451c + 9550e2f commit 33198ea

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ scripts_outputs/
88
examples/visualqc_prep/
99
examples/sub-01/
1010
user-testing/*
11-
11+
.vscode/*
1212
src/isolate_fsleyes_render_issue.py

src/generate_renders.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import re
88
import subprocess
99
from pathlib import Path
10+
from multiprocessing.pool import Pool
1011

1112

1213
def get_args():
1314
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=__doc__)
1415

1516
parser.add_argument('-o', '--output', type=Path, action='store', dest='outdir', metavar='OUTPUT_DIR',
1617
default=Path('.'), help="Path to defacing outputs directory.")
18+
parser.add_argument('-n', '--n-cpus', type=int, default=1,
19+
help='Number of parallel processes to run. '
20+
'Defaults to 1, meaning "serial processing" when not provided.')
1721
return parser.parse_args()
1822

1923

@@ -50,8 +54,21 @@ def generate_3d_renders(defaced_img, render_outdir):
5054
def main():
5155
args = get_args()
5256
defaced_imgs = list(args.outdir.rglob('defaced.nii.gz'))
53-
for img in defaced_imgs:
54-
generate_3d_renders(img, img.parent)
57+
58+
if args.n_cpus == 1:
59+
print(f'Running in Serial, one render at a time')
60+
for img in defaced_imgs:
61+
generate_3d_renders(img, img.parent)
62+
63+
elif args.n_cpus > 1:
64+
print(f'Running in Parallel with {args.n_cpus} cores')
65+
# initialize pool
66+
with Pool(processes=args.n_cpus) as p:
67+
p.starmap( generate_3d_renders, zip( defaced_imgs,
68+
[img.parent for img in defaced_imgs] ) )
69+
70+
else:
71+
raise ValueError(f"Provided --n-cpus of {args.n_cpus} is not a valid number of CPUs.")
5572

5673
# prep for visual inspection using visualqc deface
5774
print(f"Preparing for QC by visual inspection...\n")

0 commit comments

Comments
 (0)