Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template inject Lflip vs R #310

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions hippunfold/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,21 +384,25 @@ template_based_segmentation:
hemi:
- R
- L
CIVM:
dHCP:
hemi:
- R
- L

MBMv2:
hemi:
- R
MBMv3:
hemi:
- R
ABAv3:
CIVM:
hemi:
- R
- L
dHCP:
upenn:
hemi:
- R
ABAv3:
hemi:
- R
- L
Expand Down
80 changes: 58 additions & 22 deletions hippunfold/workflow/rules/shape_inject.smk
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
# Template-based segmentation supports templates that have only a single hemisphere
# by mapping it to the flipped version of the other hemisphere.
# If a template has both L and R files, then we set hemi_constrained_wildcard to L|R.
# If a hemisphere is missing data, then we set it to flip that, e.g. if L missing, then use Lflip|R

hemi_constraints = []
if config["template"] in config["template_based_segmentation"]:
for hemi in config["hemi"]:
if hemi in config["template_based_segmentation"][config["template"]]["hemi"]:
hemi_constraints.append(hemi)
else:
hemi_constraints.append(f"{hemi}flip")

hemi_constrained_wildcard = "{{hemi,{constraints}}}".format(
constraints="|".join(hemi_constraints)
)


def flipped(wildcards):
"""function to map hemi in wildcards from Lflip to R, or Rflip to L,
for use in rules where e.g. the output wildcard is Lflip, but for the input, R is desired, such as
when mapping a R hemi dseg to the Lflip hemisphere of a subject."""

if wildcards.hemi == "L" or wildcards.hemi == "R":
return wildcards
elif wildcards.hemi == "Lflip":
wildcards.hemi = "R"
return wildcards
elif wildcards.hemi == "Rflip":
wildcards.hemi = "L"
return wildcard


def get_input_for_shape_inject(wildcards):
if config["modality"] == "cropseg":
seg = bids(
Expand All @@ -6,7 +39,7 @@ def get_input_for_shape_inject(wildcards):
**config["subj_wildcards"],
suffix="dseg.nii.gz",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
).format(**wildcards)
elif get_modality_key(config["modality"]) == "seg":
modality_suffix = get_modality_suffix(config["modality"])
Expand All @@ -18,7 +51,7 @@ def get_input_for_shape_inject(wildcards):
suffix="dseg.nii.gz",
space="corobl",
hemi="{hemi}",
from_="{modality_suffix}"
from_="{modality_suffix}",
).format(**wildcards, modality_suffix=modality_suffix),
)
else:
Expand All @@ -29,7 +62,7 @@ def get_input_for_shape_inject(wildcards):
suffix="dseg.nii.gz",
desc="nnunet",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
).format(**wildcards)
return seg

Expand All @@ -42,7 +75,7 @@ def get_input_splitseg_for_shape_inject(wildcards):
**config["subj_wildcards"],
suffix="dsegsplit",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
).format(**wildcards)

elif get_modality_key(config["modality"]) == "seg":
Expand All @@ -54,7 +87,7 @@ def get_input_splitseg_for_shape_inject(wildcards):
suffix="dsegsplit",
space="corobl",
hemi="{hemi}",
from_="{modality_suffix}"
from_="{modality_suffix}",
).format(**wildcards, modality_suffix=modality_suffix)
else:
seg = bids(
Expand All @@ -64,7 +97,7 @@ def get_input_splitseg_for_shape_inject(wildcards):
suffix="dsegsplit",
desc="nnunet",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
).format(**wildcards)
return seg

Expand Down Expand Up @@ -92,7 +125,7 @@ rule import_template_shape:
params:
template_seg=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["inject_template"]]["dseg"].format(
**wildcards
**flipped(wildcards)
),
output:
template_seg=bids(
Expand All @@ -101,6 +134,7 @@ rule import_template_shape:
space="template",
**config["subj_wildcards"],
desc="hipptissue",
hemi=hemi_constrained_wildcard,
suffix="dseg.nii.gz"
),
group:
Expand Down Expand Up @@ -164,6 +198,7 @@ rule template_shape_reg:
space="template",
**config["subj_wildcards"],
desc="hipptissue",
hemi="{hemi}",
suffix="dsegsplit"
),
subject_seg=get_input_splitseg_for_shape_inject,
Expand All @@ -183,7 +218,7 @@ rule template_shape_reg:
to="subject",
space="corobl",
type_="ras",
hemi="{hemi,Lflip|R}"
hemi=hemi_constrained_wildcard,
),
warp=bids(
root=work,
Expand All @@ -194,7 +229,7 @@ rule template_shape_reg:
from_="template",
to="subject",
space="corobl",
hemi="{hemi,Lflip|R}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand All @@ -205,7 +240,7 @@ rule template_shape_reg:
bids(
root="logs",
**config["subj_wildcards"],
hemi="{hemi,Lflip|R}",
hemi=hemi_constrained_wildcard,
suffix="templateshapereg.txt"
),
shell:
Expand All @@ -222,6 +257,7 @@ rule template_shape_inject:
space="template",
**config["subj_wildcards"],
desc="hipptissue",
hemi="{hemi}",
suffix="dseg.nii.gz"
),
subject_seg=get_input_for_shape_inject,
Expand All @@ -235,7 +271,7 @@ rule template_shape_inject:
to="subject",
space="corobl",
type_="ras",
hemi="{hemi}"
hemi="{hemi}",
),
warp=bids(
root=work,
Expand All @@ -246,7 +282,7 @@ rule template_shape_inject:
from_="template",
to="subject",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
),
params:
interp_opt="-ri LABEL 0.2vox",
Expand All @@ -258,14 +294,14 @@ rule template_shape_inject:
suffix="dseg.nii.gz",
desc="inject",
space="corobl",
hemi="{hemi,Lflip|R}"
hemi=hemi_constrained_wildcard,
),
log:
bids(
root="logs",
**config["subj_wildcards"],
suffix="templateshapeinject.txt",
hemi="{hemi,Lflip|R}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand All @@ -289,7 +325,7 @@ rule inject_init_laplace_coords:
to="subject",
space="corobl",
type_="ras",
hemi="{hemi}"
hemi="{hemi}",
),
warp=bids(
root=work,
Expand All @@ -300,7 +336,7 @@ rule inject_init_laplace_coords:
from_="template",
to="subject",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
),
template_dir=Path(download_dir) / "template" / config["inject_template"],
params:
Expand All @@ -319,7 +355,7 @@ rule inject_init_laplace_coords:
suffix="coords.nii.gz",
desc="init",
space="corobl",
hemi="{hemi,R|Lflip}"
hemi=hemi_constrained_wildcard,
),
log:
bids(
Expand All @@ -329,7 +365,7 @@ rule inject_init_laplace_coords:
label="{autotop}",
suffix="injectcoords.txt",
desc="init",
hemi="{hemi,R|Lflip}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -365,7 +401,7 @@ rule unflip_init_coords:
suffix="coords.nii.gz",
desc="init",
space="corobl",
hemi="{hemi,L}"
hemi="{hemi,L|R}"
),
container:
config["singularity"]["autotop"]
Expand All @@ -385,7 +421,7 @@ rule reinsert_subject_labels:
suffix="dseg.nii.gz",
desc="inject",
space="corobl",
hemi="{hemi}"
hemi="{hemi}",
),
subject_seg=get_input_for_shape_inject,
params:
Expand All @@ -400,7 +436,7 @@ rule reinsert_subject_labels:
suffix="dseg.nii.gz",
desc="postproc",
space="corobl",
hemi="{hemi,Lflip|R}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -429,7 +465,7 @@ rule unflip_postproc:
suffix="dseg.nii.gz",
desc="postproc",
space="corobl",
hemi="{hemi,L}",
hemi="{hemi,L|R}",
**config["subj_wildcards"]
),
container:
Expand Down
Loading