Skip to content

Commit 1ba6f5a

Browse files
committed
Resample long templ to bold grid for ref
1 parent fb22681 commit 1ba6f5a

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Resampling utiltiies for longitudinal templates."""
2+
3+
from __future__ import annotations
4+
5+
from typing import TYPE_CHECKING
6+
7+
import nibabel as nib
8+
from nibabel.processing import resample_from_to
9+
10+
if TYPE_CHECKING:
11+
from pathlib import Path
12+
13+
from rbc.core.niwrap import generate_exec_folder
14+
15+
16+
def resample_template_to_bold(bold_ref: Path, template: Path) -> Path:
17+
"""Resample template to BOLD grid if shapes differ.
18+
19+
Args:
20+
bold_ref: BOLD reference volume (used for ITK conversion).
21+
template: Brain template in target space.
22+
23+
Returns:
24+
Resampled template image with BOLD grid
25+
26+
Raises:
27+
FileNotFoundError: No motion .mat files found in the directory.
28+
ValueError: Number of motion matrices does not match STC volumes.
29+
"""
30+
bold_ref_img = nib.nifti1.load(bold_ref)
31+
template_img = nib.nifti1.load(template)
32+
33+
# If 4D, extract first volume
34+
if len(bold_ref_img.shape) > 3:
35+
bold_ref_img = bold_ref_img[..., 0]
36+
# If same shape, no need to resample
37+
if bold_ref_img.shape == template_img.shape:
38+
return template_img
39+
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"
44+
)
45+
nib.save(template_img, template_img_path)
46+
return template_img_path

src/rbc/workflows/longitudinal/functional.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from typing import TYPE_CHECKING, NamedTuple
1313

1414
from rbc.core.functional import apply_regression, apply_regression_bandpass
15+
from rbc.core.longitudinal.resampling import resample_template_to_bold
1516
from rbc.core.longitudinal.transform import (
1617
compose_transform,
1718
func_transform,
@@ -82,8 +83,10 @@ def longitudinal_process(
8283
:class:`FunctionalLongOutputs` with all inputs transformed to
8384
longitudinal template space and per-regressor regression outputs.
8485
"""
86+
template_resampled = resample_template_to_bold(bold_ref=sbref, template=template)
87+
8588
bold_to_tpl_xfm = compose_transform(
86-
ref=template,
89+
ref=template_resampled,
8790
bold_to_anat_itk=bold_to_anat_itk,
8891
anat_to_tpl_xfm=anat_to_template_xfm,
8992
)

0 commit comments

Comments
 (0)