Skip to content

Commit bab5e99

Browse files
committed
changing output file names and adding stdout messages for clarity
1 parent e11d0aa commit bab5e99

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ for i in `ls -d <path/to/BIDS/input/dataset>*`; do
7777
done > defacing_parallel_session_level.swarm
7878
```
7979
```bash
80-
swarm -f defacing_parallel_subject_level.swarm --module afni,fsl --merge-output --logdir swarm_log
80+
swarm -f defacing_parallel_session_level.swarm --module afni,fsl --merge-output --logdir swarm_log
8181
```
8282

8383
### Visually inspect defaced scans using VisualQC

src/deface.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def rename_afni_workdir(workdir_path):
2222
new_workdir_path = workdir_path.parent / f'workdir_{default_prefix}'
2323
workdir_path.rename(new_workdir_path)
2424

25-
print(f"Removing unwanted files and renaming AFNI workdirs..")
25+
print(f"Removing unwanted files and renaming AFNI workdirs..\n")
2626

2727
return new_workdir_path
2828

src/dsst_defacing_wf.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,24 @@ def generate_3d_renders(defaced_img, render_outdir):
119119
yaw, pitch, roll = rot[0], rot[1], rot[2]
120120
outfile = render_outdir.joinpath('defaced_render_' + str(idx) + '.png')
121121
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"
122+
print(fsleyes_render_cmd)
122123
run_command(fsleyes_render_cmd)
123124

124125

125-
def create_vqc_id_list(vqc_dir):
126-
rel_paths_to_orig = [re.sub('/orig.nii.gz', '', str(o.relative_to(vqc_dir))) for o in vqc_dir.rglob('orig.nii.gz')]
127-
with open(vqc_dir / 'vqcdeface_id_list.txt', 'w') as f:
126+
def create_defacing_id_list(qc_dir):
127+
rel_paths_to_orig = [re.sub('/orig.nii.gz', '', str(o.relative_to(qc_dir))) for o in qc_dir.rglob('orig.nii.gz')]
128+
with open(qc_dir / 'defacing_id_list.txt', 'w') as f:
128129
f.write('\n'.join(rel_paths_to_orig))
129130

130131

131132
def vqcdeface_prep(input_dir, defacing_output_dir):
132-
vqcdeface_dir = defacing_output_dir.parent / 'visualqc_prep' / 'vqcdeface'
133+
defacing_qc_dir = defacing_output_dir.parent / 'QC_prep' / 'defacing_QC'
133134
interested_files = [f for f in defacing_output_dir.rglob('*.nii.gz') if
134135
'afni_intermediate_files' not in str(f).split('/')]
135136
for defaced_img in interested_files:
136137
# please kill me now ughhh
137138
entities = defaced_img.name.split('.')[0].split('_')
138-
vqcd_subj_dir = vqcdeface_dir / f"{'/'.join(entities)}"
139+
vqcd_subj_dir = defacing_qc_dir / f"{'/'.join(entities)}"
139140
vqcd_subj_dir.mkdir(parents=True, exist_ok=True)
140141

141142
defaced_link = vqcd_subj_dir / 'defaced.nii.gz'
@@ -146,9 +147,9 @@ def vqcdeface_prep(input_dir, defacing_output_dir):
146147
img_link = vqcd_subj_dir / 'orig.nii.gz'
147148
if not img_link.exists(): img_link.symlink_to(img)
148149

149-
create_vqc_id_list(vqcdeface_dir)
150+
create_defacing_id_list(defacing_qc_dir)
150151

151-
vqcdeface_cmd = f"vqcdeface -u {vqcdeface_dir} -i {vqcdeface_dir / 'vqcdeface_id_list.txt'} -m orig.nii.gz -d defaced.nii.gz -r defaced_render"
152+
vqcdeface_cmd = f"vqcdeface -u {defacing_qc_dir} -i {defacing_qc_dir / 'defacing_id_list.txt'} -m orig.nii.gz -d defaced.nii.gz -r defaced_render"
152153

153154
return vqcdeface_cmd
154155

@@ -189,13 +190,15 @@ def main():
189190
f.write('\n'.join(afni_refacer_failures)) # TODO Not very useful when running the pipeline in parallel
190191

191192
# reorganizing the directory with defaced images into BIDS tree
193+
print(f"Reorganizing the directory with defaced images into BIDS tree...\n")
192194
reorganize_into_bids(input_dir, defacing_outputs, mapping_dict, no_clean)
193195

194196
# prep for visual inspection using visualqc deface
197+
print(f"Preparing for QC by visual inspection...\n")
195198
vqcdeface_cmd = vqcdeface_prep(input_dir, defacing_outputs)
196-
print(f"Run the following command to start a VisualQC Deface session:\n{vqcdeface_cmd}")
197-
with open(output / 'visualqc_prep' / 'defacing_qc_cmd', 'w') as f:
198-
f.write(vqcdeface_cmd)
199+
print(f"Run the following command to start a VisualQC Deface session:\n\t{vqcdeface_cmd}\n")
200+
with open(output / 'QC_prep' / 'defacing_qc_cmd', 'w') as f:
201+
f.write(vqcdeface_cmd + '\n')
199202

200203

201204
if __name__ == "__main__":

src/generate_mappings.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def run_command(cmdstr, logfile):
3232
subprocess.run(cmdstr, stdout=logfile, stderr=subprocess.STDOUT, encoding='utf8', shell=True)
3333

3434

35-
def primary_scans_qc_prep(mapping_dict, visualqc_prep):
35+
def primary_scans_qc_prep(mapping_dict, qc_prep):
3636
"""Prepares a directory tree with symbolic links to primary scans and an id_list of primary scans to be used in the
3737
visualqc_t1_mri command.
3838
@@ -57,7 +57,7 @@ def primary_scans_qc_prep(mapping_dict, visualqc_prep):
5757
# remove empty strings from primaries list
5858
primaries = [p for p in primaries if p != '']
5959

60-
vqc_t1_mri = visualqc_prep / 't1_mri'
60+
vqc_t1_mri = qc_prep / 't1_mri_QC'
6161
vqc_t1_mri.mkdir(parents=True, exist_ok=True)
6262

6363
id_list = []
@@ -79,10 +79,10 @@ def primary_scans_qc_prep(mapping_dict, visualqc_prep):
7979
if not primary_link.exists():
8080
primary_link.symlink_to(primary)
8181

82-
with open(visualqc_prep / 'id_list_t1.txt', 'w') as f:
82+
with open(vqc_t1_mri / 't1_mri_id_list.txt', 'w') as f:
8383
f.write('\n'.join([str(i) for i in id_list]))
8484

85-
vqc_t1_mri_cmd = f"visualqc_t1_mri -u {vqc_t1_mri} -i {vqc_t1_mri.parent / 'vqc_t1_id_list.txt'} -m primary.nii.gz"
85+
vqc_t1_mri_cmd = f"visualqc_t1_mri -u {vqc_t1_mri} -i {vqc_t1_mri / 't1_mri_id_list.txt'} -m primary.nii.gz"
8686

8787
return vqc_t1_mri_cmd
8888

@@ -203,7 +203,7 @@ def summary_to_stdout(vqc_t1_cmd, sess_ct, t1s_found, t1s_not_found, no_anat_dir
203203

204204
def crawl(input_dir, output):
205205
# make dir for log files and visualqc prep
206-
dir_names = ['logs', 'visualqc_prep']
206+
dir_names = ['logs', 'QC_prep']
207207
for dir_name in dir_names:
208208
output.joinpath(dir_name).mkdir(parents=True, exist_ok=True)
209209

@@ -234,8 +234,8 @@ def crawl(input_dir, output):
234234
f3.write('\n'.join([str(p) for p in no_anat_dirs]))
235235

236236
# write vqc command to file
237-
vqc_t1_mri_cmd = primary_scans_qc_prep(mapping_dict, output / 'visualqc_prep')
238-
with open(output / 'visualqc_prep' / 't1_mri_qc_cmd', 'w') as f4:
237+
vqc_t1_mri_cmd = primary_scans_qc_prep(mapping_dict, output / 'QC_prep')
238+
with open(output / 'QC_prep' / 't1_mri_qc_cmd', 'w') as f4:
239239
f4.write(f"{vqc_t1_mri_cmd}\n")
240240

241241
summary_to_stdout(vqc_t1_mri_cmd, total_sessions, t1s_found, t1s_not_found, no_anat_dirs, output)

src/register.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def preprocess_facemask(fmask_path, logfile_obj):
1919

2020
# arithmetic on the result from above
2121
c2 = f"fslmaths {prefix}.nii.gz -abs -binv {defacemask}"
22-
print(f"Splitting the facemask volume at {fmask_path} and binarizing the resulting volume... \n ")
22+
print(f"Generating a defacemask... \n ")
2323
run_command('; '.join([c0, c1, c2]), logfile_obj)
2424
try:
2525
if defacemask.exists():
@@ -59,13 +59,16 @@ def register_to_primary_scan(subj_dir, afni_workdir, primary_scan, other_scans_l
5959
matrix, reg_out, other_mask, other_defaced = get_intermediate_filenames(other_outdir, other_prefix)
6060

6161
other_outdir.mkdir(parents=True, exist_ok=True)
62-
cp_cmd = f"cp {other} {other_outdir.joinpath('original.nii.gz')}"
62+
cp_cmd = f"cp {other} {other_outdir / other_prefix}"
6363

64-
flirt_cmd = f"flirt -dof 6 -cost mutualinfo -searchcost mutualinfo -in {primary_scan} "f"-ref {other} -omat {matrix} -out {reg_out}"
64+
flirt_cmd = f"flirt -dof 6 -cost mutualinfo -searchcost mutualinfo -in {primary_scan} -ref {other} -omat {matrix} -out {reg_out}"
6565

6666
# t1 mask can be found in the afni work directory
67-
applyxfm_cmd = f"flirt -interp nearestneighbour -applyxfm -init {matrix} "f"-in {t1_mask} -ref {other} -out {other_mask}"
67+
applyxfm_cmd = f"flirt -interp nearestneighbour -applyxfm -init {matrix} -in {t1_mask} -ref {other} -out {other_mask}"
6868

6969
mask_cmd = f"fslmaths {other} -mas {other_mask} {other_defaced}"
70+
7071
full_cmd = " ; ".join(["module load fsl", cp_cmd, flirt_cmd, applyxfm_cmd, mask_cmd]) + '\n'
72+
73+
print(f"Registering {other.name} to {primary_scan.name} and applying defacemask...")
7174
run_command(full_cmd, log_fileobj)

0 commit comments

Comments
 (0)