Skip to content

Commit 2217809

Browse files
committed
Resample longitudinal mask for QC
1 parent 06929ef commit 2217809

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

src/rbc/core/longitudinal/resampling.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,33 @@
1313
from rbc.core.niwrap import generate_exec_folder
1414

1515

16-
def resample_template_to_bold_grid(bold_ref: Path, template: Path) -> Path:
16+
def resample_img_to_bold_grid(bold_ref: Path, img: Path) -> Path:
1717
"""Resample template to BOLD grid if shapes differ.
1818
1919
Args:
2020
bold_ref: BOLD reference volume (used for ITK conversion).
21-
template: Brain template in target space.
21+
img: 3D image in target space to resample.
2222
2323
Returns:
24-
Resampled template image with BOLD grid
24+
Resampled 3D image with BOLD grid
2525
2626
Raises:
2727
FileNotFoundError: No motion .mat files found in the directory.
2828
ValueError: Number of motion matrices does not match STC volumes.
2929
"""
3030
bold_ref_img = nib.nifti1.load(bold_ref)
31-
template_img = nib.nifti1.load(template)
31+
img_obj = nib.nifti1.load(img)
3232

3333
# If 4D, extract first volume
3434
if len(bold_ref_img.shape) > 3:
3535
bold_ref_img = nib.four_to_three(bold_ref_img)[0]
3636
# If same shape, no need to resample
37-
if bold_ref_img.shape == template_img.shape:
38-
return template_img
37+
if bold_ref_img.shape == img_obj.shape:
38+
return img
3939

40-
template_img = resample_from_to(template_img, bold_ref_img)
41-
template_img_path = (
42-
generate_exec_folder("template_resample_to_bold_grid")
43-
/ "template_resampled.nii.gz"
40+
img_resampled = resample_from_to(img_obj, bold_ref_img)
41+
img_resampled_path = (
42+
generate_exec_folder("img_resample_to_bold_grid") / "resampled.nii.gz"
4443
)
45-
nib.save(template_img, template_img_path)
46-
return template_img_path
44+
nib.save(img_resampled, img_resampled_path)
45+
return img_resampled_path

src/rbc/orchestration/longitudinal/qc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
write_longitudinal_qc_tsv,
2222
)
2323
from rbc.bids.session import iter_session_files
24+
from rbc.core.longitudinal.resampling import resample_img_to_bold_grid
2425
from rbc.core.niwrap import generate_exec_folder
2526
from rbc.core.qc.registration import registration_qc_metrics
2627
from rbc.orchestration import Filters, RunnerConfig, init_runner
@@ -67,6 +68,11 @@ def process_qc(
6768
Returns:
6869
QC outputs with overlap metrics and pass/fail flag.
6970
"""
71+
# Resample longitudinal anatomical mask to bold grid for QC purposes.
72+
# Longitudinal processed data are registered to the longitudinal template with
73+
# respective modality's native resolution
74+
anat_mask_img = nib.nifti1.load(anat_brain_mask)
75+
anat_brain_mask = resample_img_to_bold_grid(bold_mask, anat_mask_img)
7076
anat_mask_arr = nib.nifti1.load(anat_brain_mask).get_fdata()
7177
bold_mask_arr = nib.nifti1.load(bold_mask).get_fdata()
7278
reg_metrics = registration_qc_metrics(anat_mask_arr, bold_mask_arr)

src/rbc/workflows/longitudinal/template.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
fs_to_itk_xfm,
1515
generate_robust_template,
1616
)
17-
from rbc.core.longitudinal.resampling import resample_template_to_bold_grid
17+
from rbc.core.longitudinal.resampling import resample_img_to_bold_grid
1818

1919
if TYPE_CHECKING:
2020
from collections.abc import Sequence
@@ -71,7 +71,7 @@ def generate_subject_template(
7171
)
7272

7373
if bold_ref is not None:
74-
bold_ref = resample_template_to_bold_grid(bold_ref, robust.template)
74+
bold_ref = resample_img_to_bold_grid(bold_ref, robust.template)
7575

7676
return LongitudinalTemplateOutputs(
7777
template=robust.template,

0 commit comments

Comments
 (0)