Skip to content

Commit 268cba5

Browse files
committed
syntactical changes to scripts
1 parent 1529934 commit 268cba5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/dsst_defacing_wf.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
6875
def 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

119131
def 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)}"

src/generate_mappings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def primary_scans_qc_prep(mapping_dict, visualqc_prep):
4242
:return str vqc_t1_mri_cmd: A visualqc T1 MRI command string.
4343
"""
4444

45-
interested_keys = ('primary_t1', 'others')
45+
interested_keys = ['primary_t1', 'others']
4646
primaries = []
4747
for subjid in mapping_dict.keys():
4848

src/register.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def preprocess_facemask(fmask_path, logfile_obj):
3232

3333

3434
def get_intermediate_filenames(outdir, prefix):
35-
mat = f"{outdir.joinpath(prefix)}_reg.mat"
36-
reg_out = f"{outdir.joinpath('registered.nii.gz')}"
37-
mask = f"{outdir.joinpath(prefix)}_mask.nii.gz"
38-
defaced_out = f"{outdir.joinpath(prefix)}_defaced.nii.gz"
35+
mat = f"{outdir / prefix}_reg.mat"
36+
reg_out = f"{outdir / prefix}_registered.nii.gz"
37+
mask = f"{outdir / prefix}_mask.nii.gz"
38+
defaced_out = f"{outdir / prefix}_defaced.nii.gz"
3939
return mat, reg_out, mask, defaced_out
4040

4141

@@ -58,13 +58,14 @@ def register_to_primary_scan(subj_dir, afni_workdir, primary_scan, other_scans_l
5858

5959
matrix, reg_out, other_mask, other_defaced = get_intermediate_filenames(other_outdir, other_prefix)
6060

61-
mkdir_cmd = f"mkdir -p {other_outdir}; cp {other} {other_outdir.joinpath('original.nii.gz')}"
61+
other_outdir.mkdir(parents=True, exist_ok=True)
62+
cp_cmd = f"cp {other} {other_outdir.joinpath('original.nii.gz')}"
6263

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

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

6869
mask_cmd = f"fslmaths {other} -mas {other_mask} {other_defaced}"
69-
full_cmd = " ; ".join(["module load fsl", mkdir_cmd, flirt_cmd, applyxfm_cmd, mask_cmd]) + '\n'
70+
full_cmd = " ; ".join(["module load fsl", cp_cmd, flirt_cmd, applyxfm_cmd, mask_cmd]) + '\n'
7071
run_command(full_cmd, log_fileobj)

0 commit comments

Comments
 (0)