Skip to content

Commit

Permalink
🚧 WIP ♻️ Differentiate longitudinal template images from session imag…
Browse files Browse the repository at this point in the history
…es in longitudinal template space
  • Loading branch information
shnizzedy committed Nov 25, 2024
1 parent 342dae2 commit e6d33f5
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 72 deletions.
68 changes: 38 additions & 30 deletions CPAC/longitudinal/wf/anat.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ def pick_map(
name="mask_longitudinal_T1w_brain",
config=["longitudinal_template_generation"],
switch=["run"],
inputs=["space-longitudinal_desc-brain_T1w"],
outputs=["space-longitudinal_desc-brain_mask"],
inputs=["longitudinal-template_space-longitudinal_desc-brain_T1w"],
outputs=["longitudinal-template_space-longitudinal_desc-brain_mask"],
)
def mask_longitudinal_T1w_brain(
wf, cfg, strat_pool, pipe_num, opt=None
Expand All @@ -103,10 +103,17 @@ def mask_longitudinal_T1w_brain(
)
brain_mask.inputs.args = "-bin"

node, out = strat_pool.get_data("space-longitudinal_desc-brain_T1w")
node, out = strat_pool.get_data(
"longitudinal-template_space-longitudinal_desc-brain_T1w"
)
wf.connect(node, out, brain_mask, "in_file")

outputs = {"space-longitudinal_desc-brain_mask": (brain_mask, "out_file")}
outputs = {
"longitudinal-template_space-longitudinal_desc-brain_mask": (
brain_mask,
"out_file",
)
}

return (wf, outputs)

Expand All @@ -115,11 +122,9 @@ def mask_longitudinal_T1w_brain(
name="warp_longitudinal_T1w_to_template",
config=["longitudinal_template_generation"],
switch=["run"],
option_key="using",
option_val="C-PAC legacy",
inputs=[
(
"space-longitudinal_desc-brain_T1w",
"longitudinal-template_space-longitudinal_desc-brain_T1w",
"from-longitudinal_to-template_mode-image_xfm",
),
"T1w-brain-template",
Expand Down Expand Up @@ -155,7 +160,9 @@ def warp_longitudinal_T1w_to_template(
"anatomical_registration"
]["registration"]["FSL-FNIRT"]["interpolation"]

node, out = strat_pool.get_data("space-longitudinal_desc-brain_T1w")
node, out = strat_pool.get_data(
"longitudinal-template_space-longitudinal_desc-brain_T1w"
)
wf.connect(node, out, apply_xfm, "inputspec.input_image")

node, out = strat_pool.get_data("T1w-brain-template")
Expand All @@ -164,7 +171,12 @@ def warp_longitudinal_T1w_to_template(
node, out = strat_pool.get_data("from-longitudinal_to-template_mode-image_xfm")
wf.connect(node, out, apply_xfm, "inputspec.transform")

outputs = {"space-template_desc-brain_T1w": (apply_xfm, "outputspec.output_image")}
outputs = {
"longitudinal-template_space-template_desc-brain_T1w": (
apply_xfm,
"outputspec.output_image",
)
}

return (wf, outputs)

Expand All @@ -175,7 +187,7 @@ def warp_longitudinal_T1w_to_template(
switch=["run"],
inputs=[
(
"space-longitudinal_desc-brain_T1w",
"longitudinal-template_space-longitudinal_desc-brain_T1w",
[
"from-longitudinal_to-T1w_mode-image_desc-linear_xfm",
"from-T1w_to-longitudinal_mode-image_desc-linear_xfm",
Expand Down Expand Up @@ -213,7 +225,7 @@ def warp_longitudinal_seg_to_T1w(
pipe_num: int,
opt: Optional[str] = None,
) -> NODEBLOCK_RETURN:
"""Transform anatomical images from longitudinal space template space."""
"""Transform anatomical segmentation from longitudinal template to T1w space."""
outputs = {}
if strat_pool.check_rpool("from-longitudinal_to-T1w_mode-image_desc-linear_xfm"):
xfm_prov = strat_pool.get_cpac_provenance(
Expand All @@ -229,7 +241,7 @@ def warp_longitudinal_seg_to_T1w(
)
reg_tool = check_prov_for_regtool(xfm_prov)
# create inverse xfm if we don't have it
invt = pe.Node(interface=fsl.ConvertXFM(), name="convert_xfm")
invt = pe.Node(interface=fsl.ConvertXFM(), name=f"convert_xfm_{pipe_num}")
invt.inputs.invert_xfm = True
wf.connect(
*strat_pool.get_data("from-T1w_to-longitudinal_mode-image_desc-linear_xfm"),
Expand All @@ -246,7 +258,9 @@ def warp_longitudinal_seg_to_T1w(
)
wf.connect(*xfm, warp, "postmat")
wf.connect(
*strat_pool.get_data("space-longitudinal_desc-brain_T1w"), warp, "reference"
*strat_pool.get_data("longitudinal-template_space-longitudinal_desc-brain_T1w"),
warp,
"reference",
)
outputs["from-longitudinal_to-T1w_mode-image_desc-linear_warp"] = warp, "out_file"

Expand Down Expand Up @@ -281,7 +295,9 @@ def warp_longitudinal_seg_to_T1w(
"anatomical_registration"
]["registration"]["FSL-FNIRT"]["interpolation"]

node, out = strat_pool.get_data("space-longitudinal_desc-brain_T1w")
node, out = strat_pool.get_data(
"longitudinal-template_space-longitudinal_desc-brain_T1w"
)
wf.connect(node, out, apply_xfm, "inputspec.input_image")

node, out = strat_pool.get_data("T1w-brain-template")
Expand Down Expand Up @@ -369,7 +385,7 @@ def anat_longitudinal_wf(
name="template_node_brain",
)

config.pipeline_setup["pipeline_name"] = f"longitudinal_{orig_pipe_name}"
config.pipeline_setup["pipeline_name"] = f"longitudinal-template_{orig_pipe_name}"

num_sessions = len(strats_dct["desc-brain_T1w"])
merge_brains = pe.Node(Merge(num_sessions), name="merge_brains")
Expand All @@ -381,9 +397,9 @@ def anat_longitudinal_wf(
)
wf._connect_node_or_path_for_merge(merge_skulls, strats_dct, "desc-head_T1w", i)

long_id = f"longitudinal_{subject_id}_strat-desc-brain_T1w"
long_id = f"longitudinal-template_{subject_id}_desc-brain_T1w"

wf, rpool = initiate_rpool(wf, config, part_id=long_id)
wf, rpool = initiate_rpool(wf, config, part_id=subject_id)

match config["longitudinal_template_generation", "using"]:
case "C-PAC legacy":
Expand Down Expand Up @@ -449,7 +465,7 @@ def anat_longitudinal_wf(

for suffix in ["", "-template"]:
rpool.set_data(
f"space-longitudinal_desc-brain_T1w{suffix}",
f"longitudinal-template_space-longitudinal_desc-brain_T1w{suffix}",
brain_template_node,
brain_output,
{},
Expand All @@ -459,7 +475,7 @@ def anat_longitudinal_wf(

for desc in ["head", "reorient"]:
rpool.set_data(
f"space-longitudinal_desc-{desc}_T1w{suffix}",
f"longitudinal-template_space-longitudinal_desc-{desc}_T1w{suffix}",
wholehead_template_node,
head_output,
{},
Expand All @@ -477,9 +493,9 @@ def anat_longitudinal_wf(
wf,
config,
add_excl=[
"space-longitudinal_desc-brain_T1w",
"space-longitudinal_desc-reorient_T1w",
"space-longitudinal_desc-brain_mask",
"longitudinal-template_space-longitudinal_desc-brain_T1w",
"longitudinal-template_space-longitudinal_desc-reorient_T1w",
"longitudinal-template_space-longitudinal_desc-brain_mask",
],
)
wf = connect_pipeline(wf, config, rpool, pipeline_blocks)
Expand Down Expand Up @@ -580,14 +596,6 @@ def anat_longitudinal_wf(
"",
head_select_sess.name,
)
rpool.set_data(
"from-T1w_to-longitudinal_mode-image_desc-linear_xfm",
head_select_sess,
"warp_path",
{},
"",
head_select_sess.name,
)

rpool.set_data(
"space-longitudinal_desc-brain_T1w",
Expand Down
15 changes: 12 additions & 3 deletions CPAC/pipeline/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def _set_id_parts(self) -> None:
"""Set part_id and ses_id."""
unique_id = self.name
setattr(self, "_part_id", unique_id.split("_")[0])
if "_" not in unique_id:
setattr(self, "_ses_id", None)
return
ses_id = unique_id.split("_")[1]
if "ses-" not in ses_id:
ses_id = f"ses-{ses_id}"
Expand Down Expand Up @@ -1156,9 +1159,15 @@ def gather_pipes(self, wf, cfg, all=False, add_incl=None, add_excl=None):
for pipe_idx in self.rpool[resource]:
out_dir = cfg.pipeline_setup["output_directory"]["path"]
pipe_name = cfg.pipeline_setup["pipeline_name"]
container = os.path.join(
f"pipeline_{pipe_name}", self.part_id, self.ses_id
)
if resource.startswith("longitudinal-template_"):
container = os.path.join(f"pipeline_{pipe_name}", self.part_id)
else:
container = os.path.join(
f"pipeline_{pipe_name}", self.part_id, self.ses_id
)
resource_name = self.get_name()
if resource_name.startswith("longitudinal-template_"):
resource_name = resource_name[22:]
filename = f"{self.get_name()}_{res_in_filename(self.cfg, resource)}"

out_path = os.path.join(out_dir, container, subdir, filename)
Expand Down
Loading

0 comments on commit e6d33f5

Please sign in to comment.