|
1 | | -"""Processing steps shared across anatomical and functional streams. |
| 1 | +"""Processing steps shared across anatomical, functional, and metrics streams. |
2 | 2 |
|
3 | 3 | Currently provides: |
4 | 4 | - Deobliquing and RPI reorientation (initial preprocessing for T1w and BOLD). |
5 | 5 | - 4D NIfTI splitting and merging utilities. |
| 6 | +- Spatially smooth a 3D map or 4D timeseries to a target FWHM. |
6 | 7 | """ |
7 | 8 |
|
8 | 9 | from __future__ import annotations |
|
21 | 22 | from rbc.core.nifti import strip_afni_volatile_metadata |
22 | 23 | from rbc.core.niwrap import generate_exec_folder |
23 | 24 |
|
24 | | -__all__ = ["deoblique_and_reorient", "merge_3d_to_4d", "split_4d"] |
| 25 | +__all__ = ["deoblique_and_reorient", "merge_3d_to_4d", "smooth", "split_4d"] |
25 | 26 |
|
26 | 27 |
|
27 | 28 | def deoblique_and_reorient( |
@@ -92,3 +93,33 @@ def merge_3d_to_4d(volumes: Sequence[Path], output: Path) -> Path: |
92 | 93 | merged = nib.funcs.concat_images(imgs, axis=None) |
93 | 94 | nib.save(merged, output) |
94 | 95 | return output |
| 96 | + |
| 97 | + |
| 98 | +def smooth( |
| 99 | + in_file: Path, |
| 100 | + mask_file: Path, |
| 101 | + fwhm: float = 6.0, |
| 102 | +) -> Path: |
| 103 | + """Spatially smooth a 3D map or 4D timeseries to a target FWHM. |
| 104 | +
|
| 105 | + Uses AFNI ``3dBlurToFWHM`` to iteratively blur the input until the |
| 106 | + estimated smoothness reaches the requested FWHM within the brain mask. |
| 107 | + Supports both 3D derivative maps (ALFF, fALFF, ReHo) and 4D BOLD |
| 108 | + timeseries. |
| 109 | +
|
| 110 | + Args: |
| 111 | + in_file: NIfTI image to smooth (3-D map or 4-D timeseries). |
| 112 | + mask_file: Binary brain mask; voxels outside are set to zero. |
| 113 | + fwhm: Target full-width at half-maximum in mm. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + Path to the smoothed image. |
| 117 | + """ |
| 118 | + result = afni.v_3d_blur_to_fwhm( |
| 119 | + in_file=in_file, |
| 120 | + mask=mask_file, |
| 121 | + fwhm=fwhm, |
| 122 | + prefix="smoothed.nii.gz", |
| 123 | + ) |
| 124 | + assert result.out_file is not None # noqa: S101 |
| 125 | + return result.out_file |
0 commit comments