11import argparse
2- import gzip
32import json
43import re
5- import shutil
64import subprocess
75from pathlib import Path
86
@@ -58,61 +56,6 @@ def get_sess_dirs(subj_dir_path, mapping_dict):
5856 return sess_dirs
5957
6058
61- def compress_to_gz (input_file , output_file ):
62- if not output_file .exists ():
63- with open (input_file , 'rb' ) as f_input :
64- with gzip .open (output_file , 'wb' ) as f_output :
65- f_output .writelines (f_input )
66-
67-
68- def copy_over_sidecar (scan_filepath , input_anat_dir , output_anat_dir ):
69- prefix = '_' .join ([i for i in re .split ('_|\.' , scan_filepath .name ) if i not in ['defaced' , 'nii' , 'gz' ]])
70- filename = prefix + '.json'
71- json_sidecar = input_anat_dir / filename
72- shutil .copy2 (json_sidecar , output_anat_dir / filename )
73-
74-
75- def reorganize_into_bids (input_dir , defacing_outputs , mapping_dict , no_clean ):
76- # make workdir for each session within anat dir
77- for anat_dir in defacing_outputs .rglob ('anat' ):
78- sess_id = None
79- # extract subject/session IDs
80- subj_id = [i for i in anat_dir .parts if i .startswith ('sub-' )][0 ]
81-
82- # one anat dir associated with one sess dir, if at all
83- for s in anat_dir .parts :
84- if s .startswith ('ses-' ):
85- sess_id = s
86-
87- primary_t1 = mapping_dict [subj_id ][sess_id ]['primary_t1' ] if sess_id else mapping_dict [subj_id ]['primary_t1' ]
88-
89- # iterate over all nii files within an anat dir to rename all primary and "other" scans
90- for nii_filepath in anat_dir .rglob ('*nii*' ):
91- if nii_filepath .name .startswith ('tmp.99.result' ):
92- # convert to nii.gz, rename and copy over to anat dir
93- gz_file = anat_dir / Path (primary_t1 ).name
94- compress_to_gz (nii_filepath , gz_file )
95-
96- # copy over corresponding json sidecar
97- copy_over_sidecar (Path (primary_t1 ), input_dir / anat_dir .relative_to (defacing_outputs ), anat_dir )
98-
99- elif nii_filepath .name .endswith ('_defaced.nii.gz' ):
100- new_filename = '_' .join (nii_filepath .name .split ('_' )[:- 1 ]) + '.nii.gz'
101- shutil .copy2 (nii_filepath , str (anat_dir / new_filename ))
102-
103- copy_over_sidecar (nii_filepath , input_dir / anat_dir .relative_to (defacing_outputs ), anat_dir )
104-
105- # move QC images and afni intermediate files to a new directory
106- intermediate_files_dir = anat_dir / 'workdir'
107- intermediate_files_dir .mkdir (parents = True , exist_ok = True )
108- for dirpath in anat_dir .glob ('*' ):
109- if dirpath .name .startswith ('workdir' ) or dirpath .name .endswith ('QC' ):
110- shutil .move (dirpath , intermediate_files_dir )
111-
112- # if not no_clean:
113- # shutil.rmtree(intermediate_files_dir)
114-
115-
11659def generate_3d_renders (defaced_img , render_outdir ):
11760 rotations = [(45 , 5 , 10 ), (- 45 , 5 , 10 )]
11861 for idx , rot in enumerate (rotations ):
@@ -121,6 +64,7 @@ def generate_3d_renders(defaced_img, render_outdir):
12164 fsleyes_render_cmd = f"fsleyes render --scene 3d -rot { yaw } { pitch } { roll } --outfile { outfile } { defaced_img } -dr 20 250 -in spline -bf 0.3 -r 100 -ns 500"
12265 print (fsleyes_render_cmd )
12366 run_command (fsleyes_render_cmd )
67+ print (f"Has the render been created? { outfile .exists ()} " )
12468
12569
12670def create_defacing_id_list (qc_dir ):
@@ -141,7 +85,7 @@ def vqcdeface_prep(input_dir, defacing_output_dir):
14185 defaced_link = vqcd_subj_dir / 'defaced.nii.gz'
14286 if not defaced_link .exists ():
14387 defaced_link .symlink_to (defaced_img )
144- generate_3d_renders (defaced_img , vqcd_subj_dir )
88+ generate_3d_renders (defaced_img , vqcd_subj_dir )
14589
14690 img = list (input_dir .rglob (defaced_img .name ))[0 ]
14791 img_link = vqcd_subj_dir / 'orig.nii.gz'
@@ -182,7 +126,8 @@ def main():
182126
183127 # calling deface.py script
184128 for subj_sess in subj_sess_list :
185- missing_refacer_out = deface .deface_primary_scan (subj_sess [0 ], subj_sess [1 ], mapping_dict , defacing_outputs )
129+ missing_refacer_out = deface .deface_primary_scan (input_dir , subj_sess [0 ], subj_sess [1 ], mapping_dict ,
130+ defacing_outputs , no_clean )
186131 if missing_refacer_out is not None :
187132 afni_refacer_failures .extend (missing_refacer_out )
188133
@@ -192,10 +137,6 @@ def main():
192137 # unload fsl module and use fsleyes installed on conda env
193138 run_command (f"export TMP_DISPLAY=`echo $DISPLAY`; unset DISPLAY; module unload fsl" )
194139
195- # reorganizing the directory with defaced images into BIDS tree
196- print (f"Reorganizing the directory with defaced images into BIDS tree...\n " )
197- reorganize_into_bids (input_dir , defacing_outputs , mapping_dict , no_clean )
198-
199140 # prep for visual inspection using visualqc deface
200141 print (f"Preparing for QC by visual inspection...\n " )
201142
0 commit comments