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

deleted - Template-based segmentation, and new templates (marmoset, macaque) #278

Closed
wants to merge 11 commits into from
76 changes: 67 additions & 9 deletions hippunfold/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ parse_args:
--version:
help: 'Print the version of HippUnfold'
action: version
version: "1.4.2-pre.34"
version: "1.4.2-pre.35"


--modality:
Expand All @@ -141,7 +141,40 @@ parse_args:
- segT1w
- segT2w
- cropseg



--template:
choices:
- 'CITI168'
- 'dHCP'
- 'MBMv2'
- 'MBMv3'
- 'CIVM'
default: 'CITI168'
help: 'Set the template to use for registration to coronal oblique (and optionally for template-based segmentation if --use-template-seg is enabled). CITI168 is for adult human data, dHCP is for neonatal human data, MBMv2 is for ex vivo marmoset data, MBMv3 is for in vivo marmoset data, and CIVM is for in vivo macaque data. (default: %(default)s)'




--inject_template:
choices:
- 'upenn'
- 'MBMv2'
- 'MBMv3'
- 'CIVM'
default: 'upenn'
help: 'Set the template to use for shape injection. (default: %(default)s)'

--use_template_seg:
help: 'Use template-based segmentation for hippocampal tissue *instead of* nnUnet and shape injection. This is only to be used if nnUnet models are not trained for the data you are using, e.g. for non-human primate data with the MBMv2 (ex vivo marmoset), MBMv3 (in vivomarmoset), or CIVM (in vivo macaque) template. (default: %(default)s)'
default: False
action: 'store_true'

--template_seg_smoothing_factor:
help: 'Scales the default smoothing sigma for gradient and warp in greedy registration for template-based segmentation. Using a value higher than 1 will use result in a smoother warp. (default: %(default)s)'
default: 2.0


--derivatives:
help: 'Path to the derivatives folder (e.g. for finding manual segs) (default: %(default)s) '
default: False
Expand Down Expand Up @@ -178,13 +211,7 @@ parse_args:
default: False
action: 'store_true'

--template:
choices:
- 'CITI168'
- 'dHCP'
default: 'CITI168'
help: 'Set the template to use for registration to coronal oblique. (default: %(default)s)'


--t1_reg_template:
help: 'Use T1w to register to template space, instead of the segmentation modality. Note: this was the default behavior prior to v1.0.0. (default: %(default)s)'
default: false
Expand Down Expand Up @@ -354,6 +381,33 @@ template_files:
crop_ref: tpl-dHCP_cohort-1_res-1_space-corobl_hemi-{hemi}_T2w.nii.gz
crop_refT1w: tpl-dHCP_cohort-1_res-1_space-corobl_hemi-{hemi}_T1w.nii.gz
Mask_crop: tpl-dHCP_cohort-1_res-1_space-corobl_hemi-{hemi}_desc-hipp_mask.nii.gz
MBMv2:
T1w: Template_sym_MTR_80um.nii.gz
T2w: Template_sym_T2_80um.nii.gz
xfm_corobl: tpl-MBMv2_from-native_to-corobl_type-itk_affine.txt
crop_ref: tpl-MBMv2_hemi-{hemi}_space-corobl_{modality}.nii.gz
Mask_crop: tpl-MBMv2_hemi-{hemi}_space-corobl_desc-hipp_mask.nii.gz
dseg: tpl-MBMv2_hemi-R_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: tpl-MBMv2_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
MBMv3:
T1w: tpl-MBMv3_T1w.nii.gz
T2w: tpl-MBMv3_T2w.nii.gz
xfm_corobl: tpl-MBMv3_from-native_to-corobl_type-itk_affine.txt
crop_ref: tpl-MBMv3_hemi-{hemi}_space-corobl_{modality}.nii.gz
Mask_crop: tpl-MBMv3_hemi-{hemi}_space-corobl_desc-hipp_mask.nii.gz
dseg: tpl-MBMv3_hemi-R_space-corobl_desc-tissue_dseg.nii.gz
coords: tpl-MBMv3_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
CIVM:
T1w: tpl-CIVM_T1w.nii.gz
xfm_corobl: tpl-CIVM_from-native_to-corobl_type-itk_affine.txt
crop_ref: tpl-CIVM_hemi-{hemi}_space-corobl_{modality}.nii.gz
Mask_crop: tpl-CIVM_hemi-{hemi}_space-corobl_desc-hipp_mask.nii.gz
dseg: tpl-CIVM_hemi-R_space-corobl_desc-tissue_dseg.nii.gz
coords: tpl-CIVM_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
upenn:
T1w: tpl-upenn_desc-hipptissue_dseg.nii.gz
dseg: tpl-upenn_desc-hipptissue_dseg.nii.gz
coords: tpl-upenn_dir-{dir}_label-{autotop}_coords.nii.gz

atlas_files:
multihist7:
Expand Down Expand Up @@ -404,6 +458,8 @@ no_reg_template: False

modality: T2w

inject_template: upenn

template: CITI168

atlas:
Expand Down Expand Up @@ -572,3 +628,5 @@ t1_reg_template: False
generate_myelin_map: False
no_unfolded_reg: False
root: results
use_template_seg: False
template_seg_smoothing_factor: 2
2 changes: 1 addition & 1 deletion hippunfold/pipeline_description.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"GeneratedBy": [
{
"Name": "hippunfold",
"Version": "1.4.2-pre.34",
"Version": "1.4.2-pre.35",
"CodeURL": "https://github.com/khanlab/hippunfold",
"Author": "Jordan DeKraker & Ali Khan",
"AuthorEmail": "ali.khan@uwo.ca"
Expand Down
25 changes: 21 additions & 4 deletions hippunfold/workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ elif "T1w" in config["modality"]:

# only generate hipp surface if skipping template injection
# i.e. drop dentate
if config["skip_inject_template_labels"]:
if config["skip_inject_template_labels"]: # TODO do we need this?
config["autotop_labels"] = ["hipp"]


Expand Down Expand Up @@ -144,9 +144,6 @@ include: "rules/autotop.smk"
include: "rules/warps.smk"


include: "rules/shape_inject.smk"


include: "rules/gifti.smk"


Expand All @@ -162,6 +159,26 @@ include: "rules/qc.smk"
include: "rules/myelin_map.smk"


if config["use_template_seg"]:
if (
"dseg" in config["template_files"][config["template"]]
and "coords" in config["template_files"][config["template"]]
):

include: "rules/templateseg.smk"

else:
print(
"use_template_seg is not possible, template dseg or coords do not exist for the chosen template"
)

include: "rules/shape_inject.smk"

else:

include: "rules/shape_inject.smk"


rule all:
input:
get_final_output(),
Expand Down
27 changes: 14 additions & 13 deletions hippunfold/workflow/rules/common.smk
Original file line number Diff line number Diff line change
Expand Up @@ -293,20 +293,21 @@ def get_final_qc():
)
)
if (config["modality"] == "T1w") or (config["modality"] == "T2w"):
qc.extend(
expand(
bids(
root=root,
datatype="qc",
desc="unetf3d",
suffix="dice.tsv",
hemi="{hemi}",
**config["subj_wildcards"],
),
hemi=config["hemi"],
allow_missing=True,
if not config["use_template_seg"]:
qc.extend(
expand(
bids(
root=root,
datatype="qc",
desc="unetf3d",
suffix="dice.tsv",
hemi="{hemi}",
**config["subj_wildcards"],
),
hemi=config["hemi"],
allow_missing=True,
)
)
)
return qc


Expand Down
14 changes: 7 additions & 7 deletions hippunfold/workflow/rules/gifti.smk
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ rule metric_to_nii:
params:
interp="-nearest-vertex 1",
refflatnii=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"]["multihist7"]["label_nii"],
/ config["atlas_files"]["multihist7"]["label_nii"].format(**wildcards),
output:
metric_nii=bids(
root=work,
Expand Down Expand Up @@ -531,11 +531,11 @@ rule unfolded_registration:
warpfn="tmp1Warp.nii.gz",
invwarpfn="tmp1InverseWarp.nii.gz",
refthickness=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"][wildcards.atlas]["thickness"],
/ config["atlas_files"][wildcards.atlas]["thickness"].format(**wildcards),
refcurvature=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"][wildcards.atlas]["curvature"],
/ config["atlas_files"][wildcards.atlas]["curvature"].format(**wildcards),
refgyrification=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"][wildcards.atlas]["gyrification"],
/ config["atlas_files"][wildcards.atlas]["gyrification"].format(**wildcards),
output:
warp=bids(
root=work,
Expand Down Expand Up @@ -1058,7 +1058,7 @@ rule resample_atlas_to_refvol:
atlas_dir=lambda wildcards: Path(download_dir) / "atlas" / wildcards.atlas,
params:
atlas=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"][wildcards.atlas]["label_nii"],
/ config["atlas_files"][wildcards.atlas]["label_nii"].format(**wildcards),
output:
label_nii=bids(
root=work,
Expand Down Expand Up @@ -1110,7 +1110,7 @@ rule nii_to_label_gii:
atlas_dir=lambda wildcards: Path(download_dir) / "atlas" / wildcards.atlas,
params:
label_list=lambda wildcards, input: Path(input.atlas_dir)
/ config["atlas_files"][wildcards.atlas]["label_list"],
/ config["atlas_files"][wildcards.atlas]["label_list"].format(**wildcards),
structure_type=lambda wildcards: hemi_to_structure[wildcards.hemi],
output:
label_gii=bids(
Expand All @@ -1131,7 +1131,7 @@ rule nii_to_label_gii:
shadow:
"minimal"
shell:
"wb_command -volume-to-surface-mapping {params.label_list} {input.surf} temp.shape.gii -enclosing && "
"wb_command -volume-to-surface-mapping {input.label_nii} {input.surf} temp.shape.gii -enclosing && "
"wb_command -metric-label-import temp.shape.gii {params.label_list} {output.label_gii} && "
"wb_command -set-structure {output.label_gii} {params.structure_type}"

Expand Down
22 changes: 12 additions & 10 deletions hippunfold/workflow/rules/nnunet.smk
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,13 @@ rule unflip_nnunet_nii:
def get_f3d_ref(wildcards, input):

if config["modality"] == "T2w":
nii = (
Path(input.template_dir)
/ config["template_files"][config["template"]]["crop_ref"]
)
nii = Path(input.template_dir) / config["template_files"][config["template"]][
"crop_ref"
].format(**wildcards)
elif config["modality"] == "T1w":
nii = (
Path(input.template_dir)
/ config["template_files"][config["template"]]["crop_refT1w"]
)
nii = Path(input.template_dir) / config["template_files"][config["template"]][
"crop_refT1w"
].format(**wildcards)
else:
raise ValueError("modality not supported for nnunet!")
return nii
Expand Down Expand Up @@ -302,8 +300,12 @@ rule qc_nnunet_dice:
template_dir=Path(download_dir) / "template" / config["template"],
params:
hipp_lbls=[1, 2, 7, 8],
ref=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["Mask_crop"],
ref=lambda wildcards, input: (
Path(input.template_dir)
/ config["template_files"][config["template"]]["Mask_crop"].format(
**wildcards
)
),
output:
dice=report(
bids(
Expand Down
2 changes: 1 addition & 1 deletion hippunfold/workflow/rules/preproc_seg.smk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ rule warp_seg_to_corobl_crop:
template_dir=Path(download_dir) / "template" / config["template"],
params:
ref=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["crop_ref"],
/ config["template_files"][config["template"]]["crop_ref"].format(**wildcards),
output:
nii=bids(
root=work,
Expand Down
14 changes: 9 additions & 5 deletions hippunfold/workflow/rules/preproc_t1.smk
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ else:

def reg_to_template_cmd(wildcards, input, output):

ref = (
ref = str(
Path(input.template_dir)
/ config["template_files"][config["template"]][wildcards.modality],
/ config["template_files"][config["template"]][wildcards.modality].format(
**wildcards
),
)
if config["no_reg_template"]:
cmd = f"reg_resample -flo {input.flo} -ref {ref} -res {output.warped_subj} -aff {input.xfm_identity}; cp {input.xfm_identity} {output.xfm_ras}"
Expand Down Expand Up @@ -183,7 +185,9 @@ rule compose_template_xfm_corobl:
template_dir=Path(download_dir) / "template" / config["template"],
params:
std_to_cor=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["xfm_corobl"],
/ config["template_files"][config["template"]]["xfm_corobl"].format(
**wildcards
),
output:
sub_to_cor=bids(
root=work,
Expand Down Expand Up @@ -241,7 +245,7 @@ rule template_xfm_itk2ras:
datatype="warps",
**config["subj_wildcards"],
suffix="xfm.txt",
from_="{native_modality,T1w|T2w}",
from_="{native_modality}",
to="corobl",
desc="affine",
type_="itk"
Expand Down Expand Up @@ -288,7 +292,7 @@ rule warp_t1_to_corobl_crop:
template_dir=Path(download_dir) / "template" / config["template"],
params:
ref=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["crop_ref"],
/ config["template_files"][config["template"]]["crop_ref"].format(**wildcards),
output:
t1=bids(
root=work,
Expand Down
9 changes: 4 additions & 5 deletions hippunfold/workflow/rules/preproc_t2.smk
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,9 @@ def get_cmd_compose_t2_xfm_corobl(wildcards, input):
# xfm0: t2 to template
xfm0 = input.t2_to_std
# xfm1: template to corobl
xfm1 = (
Path(input.template_dir)
/ config["template_files"][config["template"]]["xfm_corobl"]
)
xfm1 = Path(input.template_dir) / config["template_files"][config["template"]][
"xfm_corobl"
].format(**wildcards)

return "c3d_affine_tool -itk {xfm0} -itk {xfm1} -mult -oitk {output}"

Expand Down Expand Up @@ -394,7 +393,7 @@ rule warp_t2_to_corobl_crop:
template_dir=Path(download_dir) / "template" / config["template"],
params:
ref=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["crop_ref"],
/ config["template_files"][config["template"]]["crop_ref"].format(**wildcards),
output:
nii=bids(
root=work,
Expand Down
8 changes: 6 additions & 2 deletions hippunfold/workflow/rules/qc.smk
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ rule qc_reg_to_template:
),
template_dir=Path(download_dir) / "template" / config["template"],
params:
ref=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]][wildcards.native_modality],
ref=lambda wildcards, input: str(
Path(input.template_dir)
/ config["template_files"][config["template"]][
wildcards.native_modality
].format(**wildcards)
),
output:
png=report(
bids(
Expand Down
Loading
Loading