Skip to content

Commit 2f18074

Browse files
committed
separating generate 3D renders function from defacing pipeline
1 parent 9780a0c commit 2f18074

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/dsst_defacing_wf.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ def get_sess_dirs(subj_dir_path, mapping_dict):
5656
return sess_dirs
5757

5858

59-
def generate_3d_renders(defaced_img, render_outdir):
60-
rotations = [(45, 5, 10), (-45, 5, 10)]
61-
for idx, rot in enumerate(rotations):
62-
yaw, pitch, roll = rot[0], rot[1], rot[2]
63-
outfile = render_outdir.joinpath('defaced_render_' + str(idx) + '.png')
64-
fsleyes_render_cmd = f"export TMP_DISPLAY=`echo $DISPLAY`; unset DISPLAY; module unload fsl; fsleyes render --scene 3d -rot {yaw} {pitch} {roll} --outfile {outfile} {defaced_img} -dr 20 250 -in spline -bf 0.3 -r 100 -ns 500; export DISPLAY=`echo $TMP_DISPLAY`"
65-
print(fsleyes_render_cmd)
66-
run_command(fsleyes_render_cmd)
67-
print(f"Has the render been created? {outfile.exists()}")
68-
69-
7059
def create_defacing_id_list(qc_dir):
7160
rel_paths_to_orig = [re.sub('/orig.nii.gz', '', str(o.relative_to(qc_dir))) for o in qc_dir.rglob('orig.nii.gz')]
7261
with open(qc_dir / 'defacing_id_list.txt', 'w') as f:
@@ -85,8 +74,6 @@ def vqcdeface_prep(input_dir, defacing_output_dir):
8574
defaced_link = vqcd_subj_dir / 'defaced.nii.gz'
8675
if not defaced_link.exists():
8776
defaced_link.symlink_to(defaced_img)
88-
generate_3d_renders(defaced_img, vqcd_subj_dir)
89-
9077
img = list(input_dir.rglob(defaced_img.name))[0]
9178
img_link = vqcd_subj_dir / 'orig.nii.gz'
9279
if not img_link.exists(): img_link.symlink_to(img)

src/generate_renders.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/local/bin/python3
2+
"""
3+
Generates 3D renders of defaced images using FSLeyes. More info on FSLeyes can be found\
4+
https://open.win.ox.ac.uk/pages/fsl/fsleyes/fsleyes/userdoc/install.html#install-from-conda-forge-recommended
5+
"""
6+
import argparse
7+
import subprocess
8+
from pathlib import Path
9+
10+
11+
def get_args():
12+
parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=__doc__)
13+
14+
parser.add_argument('-o', '--output', type=Path, action='store', dest='outdir', metavar='OUTPUT_DIR',
15+
default=Path('.'), help="Path to defacing outputs directory.")
16+
17+
return parser.parse_args()
18+
19+
20+
def run_command(cmdstr):
21+
"""Runs the given command str as shell subprocess. If logfile object is provided, then the stdout and stderr of the
22+
subprocess is written to the log file.
23+
:param str cmdstr: A shell command formatted as a string variable.
24+
:param io.TextIOWrapper logfile: optional, File object to log the stdout and stderr of the subprocess.
25+
"""
26+
p = subprocess.run(cmdstr, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, encoding='utf8', shell=True)
27+
print(p.stdout)
28+
29+
30+
def generate_3d_renders(defaced_img, render_outdir):
31+
rotations = [(45, 5, 10), (-45, 5, 10)]
32+
for idx, rot in enumerate(rotations):
33+
yaw, pitch, roll = rot[0], rot[1], rot[2]
34+
outfile = render_outdir.joinpath('defaced_render_99' + str(idx) + '.png')
35+
fsleyes_render_cmd = f"export TMP_DISPLAY=$DISPLAY; unset DISPLAY; fsleyes render --scene 3d -rot {yaw} {pitch} {roll} --outfile {outfile} {defaced_img} -dr 20 250 -in spline -bf 0.3 -r 100 -ns 500; export DISPLAY=$TMP_DISPLAY"
36+
print(fsleyes_render_cmd)
37+
run_command(fsleyes_render_cmd)
38+
print(f"Has the render been created? {outfile.exists()}")
39+
40+
41+
def main():
42+
args = get_args()
43+
defaced_imgs = list(args.outdir.rglob('defaced.nii.gz'))
44+
for img in defaced_imgs:
45+
generate_3d_renders(img, img.parent)
46+
# print(defaced_imgs)
47+
48+
49+
if __name__ == "__main__":
50+
main()

0 commit comments

Comments
 (0)