@@ -65,6 +65,13 @@ def compress_to_gz(input_file, output_file):
6565 f_output .writelines (f_input )
6666
6767
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+
6875def reorganize_into_bids (input_dir , defacing_outputs , mapping_dict , no_clean ):
6976 # make afni_intermediate_files for each session within anat dir
7077 for anat_dir in defacing_outputs .rglob ('anat' ):
@@ -86,10 +93,15 @@ def reorganize_into_bids(input_dir, defacing_outputs, mapping_dict, no_clean):
8693 gz_file = anat_dir / Path (primary_t1 ).name
8794 compress_to_gz (nii_filepath , gz_file )
8895
96+ # copy over corresponding json sidecar
97+ copy_over_sidecar (Path (primary_t1 ), input_dir / anat_dir .relative_to (defacing_outputs ), anat_dir )
98+
8999 elif nii_filepath .name .endswith ('_defaced.nii.gz' ):
90100 new_filename = '_' .join (nii_filepath .name .split ('_' )[:- 1 ]) + '.nii.gz'
91101 shutil .copy2 (nii_filepath , str (anat_dir / new_filename ))
92102
103+ copy_over_sidecar (nii_filepath , input_dir / anat_dir .relative_to (defacing_outputs ), anat_dir )
104+
93105 # move QC images and afni intermediate files to a new directory
94106 intermediate_files_dir = anat_dir / 'afni_intermediate_files'
95107 intermediate_files_dir .mkdir (parents = True , exist_ok = True )
@@ -118,7 +130,9 @@ def create_vqc_id_list(vqc_dir):
118130
119131def vqcdeface_prep (input_dir , defacing_output_dir ):
120132 vqcdeface_dir = defacing_output_dir .parent / 'visualqc_prep' / 'vqcdeface'
121- for defaced_img in defacing_output_dir .rglob ('*.nii.gz' ):
133+ interested_files = [f for f in defacing_output_dir .rglob ('*.nii.gz' ) if
134+ 'afni_intermediate_files' not in str (f ).split ('/' )]
135+ for defaced_img in interested_files :
122136 # please kill me now ughhh
123137 entities = defaced_img .name .split ('.' )[0 ].split ('_' )
124138 vqcd_subj_dir = vqcdeface_dir / f"{ '/' .join (entities )} "
0 commit comments