|
| 1 | +"""Anatomical workflows.""" |
| 2 | + |
| 3 | +from functools import partial |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import niwrap_helper |
| 7 | + |
| 8 | +from rbc.core.anatomical import ants_brain_extraction, ants_registration |
| 9 | +from rbc.core.common import reorient |
| 10 | +from rbc.core.utils import get_base_entities, rename |
| 11 | + |
| 12 | + |
| 13 | +def single_session(in_t1w: Path, output_dir: Path) -> None: |
| 14 | + """Workflow for preprocessing anatomical data. |
| 15 | +
|
| 16 | + Args: |
| 17 | + in_t1w: Input T1w image to process. |
| 18 | + output_dir: Parent output directory to save data to. |
| 19 | +
|
| 20 | + Raises: |
| 21 | + FileNotFoundError: If brain extracted file could not be found. |
| 22 | + """ |
| 23 | + bids_entities = get_base_entities(in_t1w) |
| 24 | + bids = partial(niwrap_helper.bids_path, **bids_entities) |
| 25 | + |
| 26 | + reoriented_t1w = reorient( |
| 27 | + in_file=in_t1w, |
| 28 | + output_fname=str(bids(desc="reoriented", suffix="T1w", ext=".nii.gz")), |
| 29 | + ) |
| 30 | + extracted_t1w = ants_brain_extraction( |
| 31 | + in_file=reoriented_t1w.out_file, output_prefix=str(bids()) |
| 32 | + ) |
| 33 | + transforms = ants_registration( |
| 34 | + extracted_t1w.brain_extracted_image, output_prefix=str(bids()) |
| 35 | + ) |
| 36 | + |
| 37 | + # Prep files to save |
| 38 | + extracted_t1w_outputs = [ |
| 39 | + (extracted_t1w.brain_extracted_image, "brain", "T1w"), |
| 40 | + (extracted_t1w.brain_mask, "T1w", "mask"), |
| 41 | + (extracted_t1w.csf_segmentation, "csf", "dseg"), |
| 42 | + (extracted_t1w.wm_segmentation, "wm", "dseg"), |
| 43 | + (extracted_t1w.gm_segmentation, "gm", "dseg"), |
| 44 | + ] |
| 45 | + renamed_files = [ |
| 46 | + rename(Path(out_file), str(bids(desc=desc, suffix=suffix, ext=".nii.gz"))) |
| 47 | + for out_file, desc, suffix in extracted_t1w_outputs |
| 48 | + ] |
| 49 | + niwrap_helper.save( |
| 50 | + [*renamed_files, transforms.forward, transforms.inverse], |
| 51 | + out_dir=output_dir / bids(datatype="anat", directory=True), |
| 52 | + ) |
0 commit comments