Skip to content

Commit 226867b

Browse files
committed
Add single subject anat workflow
1 parent e4af729 commit 226867b

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

src/rbc/core/anatomical/registration.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ def ants_registration(
124124
),
125125
],
126126
output=ants.ants_apply_transforms_composite_displacement_field_output(
127-
composite_displacement_field=f"{output_prefix}_from-T1w_to-template_mode-image_xfm.nii.gz"
127+
composite_displacement_field=f"{output_prefix}_from-T1w_to-template_mode-image_xfm.nii.gz",
128+
print_out_composite_warp_file=True,
128129
),
129130
)
130131
rev = ants.ants_apply_transforms(
131-
reference_image=str(in_file),
132+
reference_image=in_file,
132133
transform=[
133134
ants.ants_apply_transforms_transform_file_name(
134135
registration.root / f"{output_prefix}_1InverseWarp.nii.gz"
@@ -138,7 +139,8 @@ def ants_registration(
138139
),
139140
],
140141
output=ants.ants_apply_transforms_composite_displacement_field_output(
141-
composite_displacement_field=f"{output_prefix}_from-template_to-T1w_mode-image_xfm.nii.gz"
142+
composite_displacement_field=f"{output_prefix}_from-template_to-T1w_mode-image_xfm.nii.gz",
143+
print_out_composite_warp_file=True,
142144
),
143145
)
144146
return SimpleNamespace(forward=fwd.output, inverse=rev.output)

src/rbc/workflows/anatomical.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)