Skip to content

Commit

Permalink
Address bug with T2w-only processing (PennLINC#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsalo authored Feb 26, 2024
1 parent fcda9f9 commit 1edc27f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
30 changes: 10 additions & 20 deletions xcp_d/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Command-line interface tests."""

import os
import shutil
from glob import glob

import numpy as np
import pandas as pd
Expand All @@ -16,6 +16,7 @@
check_generated_files,
download_test_data,
get_test_data_path,
list_files,
run_command,
)

Expand Down Expand Up @@ -282,28 +283,17 @@ def test_pnc_cifti_t2wonly(data_dir, output_dir, working_dir):
out_dir = os.path.join(output_dir, test_name)
work_dir = os.path.join(working_dir, test_name)

# Simulate a T2w image
# Rename all T1w-related files in anat folder to T2w.
# T1w-related files in func folder should not impact XCP-D.
anat_dir = os.path.join(dataset_dir, "sub-1648798153/ses-PNC1/anat")
files_to_copy = [
"sub-1648798153_ses-PNC1_acq-refaced_desc-preproc_T1w.nii.gz",
"sub-1648798153_ses-PNC1_acq-refaced_desc-preproc_T1w.json",
(
"sub-1648798153_ses-PNC1_acq-refaced_space-MNI152NLin6Asym_res-2_desc-preproc_"
"T1w.nii.gz"
),
(
"sub-1648798153_ses-PNC1_acq-refaced_space-MNI152NLin6Asym_res-2_desc-preproc_"
"T1w.json"
),
"sub-1648798153_ses-PNC1_acq-refaced_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5",
"sub-1648798153_ses-PNC1_acq-refaced_from-T1w_to-MNI152NLin6Asym_mode-image_xfm.h5",
"sub-1648798153_ses-PNC1_acq-refaced_from-MNI152NLin6Asym_to-T1w_mode-image_xfm.h5",
"sub-1648798153_ses-PNC1_acq-refaced_from-MNI152NLin6Asym_to-T1w_mode-image_xfm.h5",
]
files_to_copy = sorted(glob(os.path.join(anat_dir, "*T1w*")))
for file_to_copy in files_to_copy:
t2w_file = os.path.join(anat_dir, file_to_copy.replace("T1w", "T2w"))
t2w_file = file_to_copy.replace("T1w", "T2w")
if not os.path.isfile(t2w_file):
shutil.copyfile(os.path.join(anat_dir, file_to_copy), t2w_file)
os.rename(os.path.join(anat_dir, file_to_copy), t2w_file)

tree = list_files(dataset_dir)
LOGGER.info(f"Tree after adding T2w:\n{tree}")

test_data_dir = get_test_data_path()
filter_file = os.path.join(test_data_dir, "pnc_cifti_t2wonly_filter.json")
Expand Down
14 changes: 14 additions & 0 deletions xcp_d/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,17 @@ def reorder_expected_outputs():

with open(expected_output_file, "w") as fo:
fo.writelines(file_contents)


def list_files(startpath):
"""List files in a directory."""
tree = ""
for root, _, files in os.walk(startpath):
level = root.replace(startpath, "").count(os.sep)
indent = " " * 4 * (level)
tree += f"{indent}{os.path.basename(root)}/\n"
subindent = " " * 4 * (level + 1)
for f in files:
tree += f"{subindent}{f}\n"

return tree
4 changes: 2 additions & 2 deletions xcp_d/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def collect_data(
# "from" entity will be set later
"template_to_anat_xfm": {
"datatype": "anat",
"to": "T1w",
"to": ["T1w", "T2w"],
"suffix": "xfm",
},
# native T1w-space brain mask
Expand All @@ -231,7 +231,7 @@ def collect_data(
# "to" entity will be set later
"anat_to_template_xfm": {
"datatype": "anat",
"from": "T1w",
"from": ["T1w", "T2w"],
"suffix": "xfm",
},
}
Expand Down

0 comments on commit 1edc27f

Please sign in to comment.