Skip to content

Commit

Permalink
add MBMv3 instead of MBMv2
Browse files Browse the repository at this point in the history
this template is a group average and better suited for template-based
segmentation of new datasets instead of the ex vivo MBMv2.

Note: hipp segmentation might need some additional tweaks..
  • Loading branch information
akhanf committed Oct 2, 2023
1 parent 1fae9c3 commit 05770fe
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 22 deletions.
39 changes: 23 additions & 16 deletions hippunfold/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,20 @@ parse_args:
--inject_template:
choices:
- 'upenn'
- 'MBMv2'
- 'MBMv3'
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 marmoset data with the MBMv2 template). (default: %(default)s)'
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 marmoset data with the MBMv3 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



--skip_inject_template_labels:
help: 'Set this flag to skip post-processing template injection into CNN segmentation. Note this will disable generation of DG surfaces. (default: %(default)s)'
Expand All @@ -195,7 +200,7 @@ parse_args:
choices:
- 'CITI168'
- 'dHCP'
- 'MBMv2'
- 'MBMv3'
default: 'CITI168'
help: 'Set the template to use for registration to coronal oblique. (default: %(default)s)'

Expand Down Expand Up @@ -368,18 +373,18 @@ template_files:
crop_ref: resources/CITI168/T2w_300umCoronalOblique_hemi-{hemi}.nii.gz
crop_refT1w: resources/CITI168/T1w_300umCoronalOblique_hemi-{hemi}.nii.gz
Mask_crop: resources/CITI168/Mask_300umCoronalOblique_hemi-{hemi}.nii.gz
MBMv2:
T1w: resources/tpl-MBMv2/Template_sym_MTR_80um.nii.gz
T2w: resources/tpl-MBMv2/Template_sym_T2_80um.nii.gz
xfm_corobl: resources/tpl-MBMv2/tpl-MBMv2_from-native_to-corobl_type-itk_affine.txt
crop_ref: resources/tpl-MBMv2/tpl-MBMv2_hemi-{hemi}_space-corobl_T2w.nii.gz
crop_refT1w: resources/tpl-MBMv2/tpl-MBMv2_hemi-{hemi}_space-corobl_MTR.nii.gz
Mask_crop: resources/tpl-MBMv2/tpl-MBMv2_hemi-{hemi}_space-corobl_desc-hipp_mask.nii.gz
dseg: resources/tpl-MBMv2/tpl-MBMv2_hemi-R_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: resources/tpl-MBMv2/tpl-MBMv2_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
upenn:
upenn:
dseg: resources/tpl-upenn/tpl-upenn_desc-hipptissue_dseg.nii.gz
coords: resources/tpl-upenn/tpl-upenn_dir-{dir}_label-{autotop}_coords.nii.gz
MBMv3:
T1w: resources/tpl-MBMv3/tpl-MBMv3_T1w.nii.gz
T2w: resources/tpl-MBMv3/tpl-MBMv3_T2w.nii.gz
xfm_corobl: resources/tpl-MBMv3/tpl-MBMv3_from-native_to-corobl_type-itk_affine.txt
crop_ref: resources/tpl-MBMv3/tpl-MBMv3_hemi-{hemi}_space-corobl_T2w.nii.gz
crop_refT1w: resources/tpl-MBMv3/tpl-MBMv3_hemi-{hemi}_space-corobl_T1w.nii.gz
Mask_crop: resources/tpl-MBMv3/tpl-MBMv3_hemi-{hemi}_space-corobl_desc-hipp_mask.nii.gz
dseg: resources/tpl-MBMv3/tpl-MBMv3_hemi-R_space-corobl_desc-tissue_dseg.nii.gz
coords: resources/tpl-MBMv3/tpl-MBMv3_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz



Expand Down Expand Up @@ -411,9 +416,9 @@ inject_template_files:
upenn:
dseg: resources/tpl-upenn/tpl-upenn_desc-hipptissue_dseg.nii.gz
coords: resources/tpl-upenn/tpl-upenn_dir-{dir}_label-{autotop}_coords.nii.gz
MBMv2:
dseg: resources/tpl-MBMv2/tpl-MBMv2_hemi-R_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: resources/tpl-MBMv2/tpl-MBMv2_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
MBMv3:
dseg: resources/tpl-MBMv3/tpl-MBMv3_hemi-R_space-corobl_desc-tissue_dseg.nii.gz
coords: resources/tpl-MBMv3/tpl-MBMv3_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz

#values to use for re-mapping tissue labels when combining with subfields.
# set the SRLM and Cyst labels to always be higher than any label in the atlas (so they override)
Expand Down Expand Up @@ -594,3 +599,5 @@ generate_myelin_map: False
no_unfolded_reg: False
root: results
use_template_seg: False
template_seg_smoothing_factor: 2

58 changes: 58 additions & 0 deletions hippunfold/resources/tpl-MBMv3/Snakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

configfile: "config.yml"


rule all:
input:
coords=expand(
"tpl-{template}_dir-{dir}_hemi-{hemi}_space-corobl_label-{label}_desc-laplace_coords.nii.gz",
template=config["template"],
dir=["AP", "PD", "IO"],
hemi="R",
label=["hipp", "dentate"],
),


ruleorder: laplace_dentate_AP > laplace_coords


rule laplace_coords:
input:
lbl=lambda wildcards: config["laplace_labels"][wildcards.label][wildcards.dir][
"img"
].format(**wildcards),
params:
gm_labels=lambda wildcards: config["laplace_labels"][wildcards.label][
wildcards.dir
]["gm"],
src_labels=lambda wildcards: config["laplace_labels"][wildcards.label][
wildcards.dir
]["src"],
sink_labels=lambda wildcards: config["laplace_labels"][wildcards.label][
wildcards.dir
]["sink"],
convergence_threshold=1e-5,
max_iters=10000,
output:
coords="tpl-{template}_dir-{dir}_hemi-{hemi}_space-corobl_label-{label}_desc-laplace_coords.nii.gz",
log:
"logs/tpl-{template}_dir-{dir}_hemi-{hemi}_space-corobl_label-{label}_desc-laplace_coords.txt",
script:
"../../workflow/scripts/laplace_coords.py"


rule laplace_dentate_AP:
""" we just mask this from the hipp label"""
input:
lbl=lambda wildcards: config["laplace_labels"][wildcards.label][wildcards.dir][
"img"
].format(**wildcards),
coords="tpl-{template}_dir-AP_hemi-{hemi}_space-corobl_label-hipp_desc-laplace_coords.nii.gz",
params:
gm_labels=lambda wildcards: config["laplace_labels"][wildcards.label][
wildcards.dir
]["gm"],
output:
coords="tpl-{template}_dir-{dir,AP}_hemi-{hemi}_space-corobl_label-{label,dentate}_desc-laplace_coords.nii.gz",
shell:
"c3d {input.lbl} -retain-labels {params.gm_labels} -binarize {input.coords} -multiply -o {output.coords}"
63 changes: 63 additions & 0 deletions hippunfold/resources/tpl-MBMv3/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
template: MBMv3

laplace_labels:
hipp:
AP:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissue_dseg.nii.gz'
gm:
- 1
- 8
src:
- 5
sink:
- 6
PD:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissue_dseg.nii.gz'
gm:
- 1
- 8
src:
- 3
sink:
- 8
IO:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissue_dseg.nii.gz'
gm:
- 1
- 8
src:
- 2
- 4
- 7
sink:
- 0
dentate:
AP:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissue_dseg.nii.gz'
gm:
- 8
src:
- 5
sink:
- 6
PD:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissueDG_dseg.nii.gz'
gm:
- 8
src:
- 1
sink:
- 2
IO:
img: 'tpl-{template}_hemi-{hemi}_space-corobl_desc-tissue_dseg.nii.gz'
gm:
- 8
src:
- 1
sink:
- 2
- 4
- 7
- 0


Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Insight Transform File V1.0
#Transform 0
Transform: MatrixOffsetTransformBase_double_3_3
Parameters: 0.967355 -0.004634807426139079 -0.000258298981039498 -0.00140371 0.517115683952737 -0.8054876155601979 -0.0102109 0.7566899552250975 0.5498342828466409 0.06143197478192646 -8.855894869646765 5.349429146692667
FixedParameters: 0 0 0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
28 changes: 22 additions & 6 deletions hippunfold/workflow/rules/templateseg.smk
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
def get_smoothing_opt(wildcards):
"""sets the smoothness of the greedy template shape injection deformation"""

gradient_sigma = 1.732 * float(config["template_seg_smoothing_factor"])
warp_sigma = 0.7071 * float(config["template_seg_smoothing_factor"])

return f"-s {gradient_sigma}vox {warp_sigma}vox"


rule template_reg:
input:
moving_img=lambda wildcards: os.path.join(
workflow.basedir,
"..",
config["template_files"][config["template"]][config["modality"]],
config["template_files"][config["template"]][
get_modality_suffix(config["modality"])
],
),
xfm_corobl=lambda wildcards: os.path.join(
workflow.basedir,
Expand All @@ -14,14 +25,18 @@ rule template_reg:
root=work,
datatype="anat",
**config["subj_wildcards"],
suffix=f"{config['modality']}.nii.gz",
suffix="{modality}.nii.gz".format(
modality=get_modality_suffix(config["modality"])
),
space="corobl",
desc="preproc",
hemi="{hemi}",
hemi="{hemi}"
),
params:
general_opts="-d 3 -m NCC 2x2x2",
greedy_opts="-n 100x50x10 -s 4vox 2vox", #default is 1.732vox, 0.7071vox
smoothing_opts=get_smoothing_opt,
iteration_opts="-n 100x50x10", #default -n 100x100
img_pairs=lambda wildcards, input: "-i {input.fixed_img} {input.moving_img}",
output:
warp=bids(
root=work,
Expand All @@ -40,8 +55,9 @@ rule template_reg:
config["singularity"]["autotop"]
threads: 8
shell:
"greedy -threads {threads} {params.general_opts} {params.greedy_opts} "
" -i {input.fixed_img} {input.moving_img} -it {input.xfm_corobl} -o {output.warp}"
"greedy -threads {threads} {params.general_opts} "
" {params.smoothing_opts} {params.iteration_opts} "
" {params.img_pairs} -it {input.xfm_corobl} -o {output.warp}"


rule warp_template_dseg:
Expand Down

0 comments on commit 05770fe

Please sign in to comment.