Skip to content

Commit

Permalink
Simple lflip (#335)
Browse files Browse the repository at this point in the history
* initial atempt, not yet wetrun tested

* lint

* moved redundant rules to downloads.smk

* lint

* a bit more brevity

* copy or flip instead of copy then flip

* minor fixed to BBtemplate from wetrun testing

* this refactors the config to put template stuff together

- adds a hemi_wildcards field instead

---------

Co-authored-by: Jordan DeKraker <[email protected]>
Co-authored-by: Ali Khan <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent ca08e47 commit 3d0a60c
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 471 deletions.
64 changes: 30 additions & 34 deletions hippunfold/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -354,41 +354,7 @@ singularity:
xfm_identity: resources/etc/identity_xfm.txt
xfm_identity_itk: resources/etc/identity_xfm_itk.txt

#templates enabled for template-based segmentation are here
# TODO: should also perhaps include modalities avaialble, and any custom crop_native_res settings
template_based_segmentation:
CITI168:
hemi:
- R
- L
dHCP:
hemi:
- R
- L

MBMv2:
hemi:
- R
MBMv3:
hemi:
- R
CIVM:
hemi:
- R
- L
upenn:
hemi:
- R
ABAv3:
hemi:
- R
- L
bigbrain:
hemi:
- R
- L


template_files:
CITI168:
T1w: T1w_head_700um.nii.gz
Expand All @@ -399,6 +365,9 @@ template_files:
Mask_crop: Mask_300umCoronalOblique_hemi-{hemi}.nii.gz
dseg: sub-CITI168_hemi-{hemi}_space-cropT1w_desc-postproc_dseg.nii.gz
coords: sub-CITI168_dir-{dir}_hemi-{hemi}_space-cropT1w_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- L
- R
dHCP:
T1w: tpl-dHCP_cohort-1_res-1_T1w.nii.gz
T2w: tpl-dHCP_cohort-1_res-1_T2w.nii.gz
Expand All @@ -408,6 +377,10 @@ template_files:
Mask_crop: tpl-dHCP_cohort-1_res-1_space-corobl_hemi-{hemi}_desc-hipp_mask.nii.gz
dseg: tpl-dHCP_space-corobl_hemi-{hemi}_desc-tissuemanualseg_dseg.nii.gz
coords: tpl-dHCP_dir-{dir}_hemi-{hemi}_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- L
- R

MBMv2:
T1w: Template_sym_MTR_80um.nii.gz
T2w: Template_sym_T2_80um.nii.gz
Expand All @@ -416,6 +389,9 @@ template_files:
Mask_crop: tpl-MBMv2_hemi-R_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
hemi_wildcards:
- R

MBMv3:
T1w: tpl-MBMv3_T1w.nii.gz
T2w: tpl-MBMv3_T2w.nii.gz
Expand All @@ -424,24 +400,40 @@ template_files:
Mask_crop: tpl-MBMv3_hemi-R_space-corobl_desc-hipp_mask.nii.gz
dseg: tpl-MBMv3_hemi-R_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: tpl-MBMv3_dir-{dir}_hemi-R_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- R


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-{hemi}_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: tpl-CIVM_dir-{dir}_hemi-{hemi}_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- L
- R


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
hemi_wildcards:
- R

ABAv3:
T1w: tpl-ABAv3_T1w.nii.gz
xfm_corobl: tpl-ABAv3_from-native_to-corobl_type-itk_affine.txt
crop_ref: tpl-ABAv3_hemi-{hemi}_space-corobl_T1w.nii.gz
Mask_crop: tpl-ABAv3_hemi-{hemi}_space-corobl_desc-tissuemanual_dseg.nii.gz
dseg: tpl-ABAv3_hemi-{hemi}_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: tpl-ABAv3_dir-{dir}_hemi-{hemi}_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- L
- R

bigbrain:
T1w: tpl-bigbrain_1mm_merker.nii.gz
T2w: tpl-bigbrain_1mm_merker.nii.gz
Expand All @@ -450,6 +442,10 @@ template_files:
Mask_crop: tpl-bigbrain_hemi-{hemi}_space-corobl_desc-tissuemanual_100um_dseg.nii.gz
dseg: tpl-bigbrain_hemi-{hemi}_space-corobl_desc-tissuemanual_100um_dseg.nii.gz
coords: tpl-bigbrain_dir-{dir}_hemi-{hemi}_space-corobl_label-{autotop}_desc-laplace_coords.nii.gz
hemi_wildcards:
- L
- R



atlas_files:
Expand Down
131 changes: 131 additions & 0 deletions hippunfold/workflow/rules/download.smk
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,134 @@ rule download_extract_template:
shell:
"wget https://{params.url} -O temp.zip && "
" unzip -d {output.unzip_dir} temp.zip"


## unpack template
# this is used in both shape_inject.smk and templateseg.smk, so instead of having redundant rules I will simply apply them hemisphere
# Template-based segmentation supports templates that have only a single hemisphere
# by flipping it


def copy_or_flip(wildcards, file_to_process):
if (
wildcards.hemi
in config["template_files"][config["inject_template"]]["hemi_wildcards"]
):
cmd = f"cp {file_to_process}"
else:
cmd = f"c3d {file_to_process} -flip x -o"
return cmd


rule import_template_dseg:
input:
template_dir=Path(download_dir) / "template" / config["inject_template"],
params:
template_seg=lambda wildcards: Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]]["dseg"].format(
**wildcards
),
copy_or_flip_cmd=lambda wildcards: copy_or_flip(
wildcards,
Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]]["dseg"].format(
**wildcards
),
),
output:
template_seg=bids(
root=work,
datatype="anat",
space="template",
**inputs.subj_wildcards,
desc="hipptissue",
hemi="{hemi}",
suffix="dseg.nii.gz"
),
group:
"subj"
container:
config["singularity"]["autotop"]
shell:
"{params.copy_or_flip_cmd} {output.template_seg}"


rule import_template_coords:
input:
template_dir=Path(download_dir) / "template" / config["inject_template"],
params:
template_coords=lambda wildcards: Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]]["coords"].format(
**wildcards
),
copy_or_flip_cmd=lambda wildcards: copy_or_flip(
wildcards,
Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]]["coords"].format(
**wildcards
),
),
output:
template_coords=bids(
root=work,
datatype="coords",
**inputs.subj_wildcards,
dir="{dir}",
label="{autotop}",
suffix="coords.nii.gz",
desc="init",
space="template",
hemi="{hemi}",
),
group:
"subj"
container:
config["singularity"]["autotop"]
shell:
"{params.copy_or_flip_cmd} {output.template_coords}"


rule import_template_anat:
input:
template_dir=Path(download_dir) / "template" / config["inject_template"],
params:
template_anat=lambda wildcards: Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]][
get_modality_suffix(config["modality"])
].format(**wildcards),
copy_or_flip_cmd=lambda wildcards: copy_or_flip(
wildcards,
Path(download_dir)
/ "template"
/ config["inject_template"]
/ config["template_files"][config["inject_template"]][
get_modality_suffix(config["modality"])
].format(**wildcards),
),
output:
template_anat=bids(
root=work,
datatype="anat",
space="template",
**inputs.subj_wildcards,
hemi="{hemi}",
suffix="{modality}.nii.gz".format(
modality=get_modality_suffix(config["modality"])
),
),
group:
"subj"
container:
config["singularity"]["autotop"]
shell:
"{params.copy_or_flip_cmd} {output.template_anat}"
2 changes: 1 addition & 1 deletion hippunfold/workflow/rules/gifti.smk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# lookup tables for structure:
hemi_to_structure = {"L": "CORTEX_LEFT", "Lflip": "CORTEX_LEFT", "R": "CORTEX_RIGHT"}
hemi_to_structure = {"L": "CORTEX_LEFT", "R": "CORTEX_RIGHT"}
surf_to_secondary_type = {
"midthickness": "MIDTHICKNESS",
"inner": "PIAL",
Expand Down
46 changes: 2 additions & 44 deletions hippunfold/workflow/rules/nnunet.smk
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,15 @@ rule run_inference:
suffix="dseg.nii.gz",
desc="nnunet",
space="corobl",
hemi="{hemi,Lflip|R}"
hemi="{hemi}"
),
log:
bids(
root="logs",
**inputs.subj_wildcards,
suffix="nnunet.txt",
space="corobl",
hemi="{hemi,Lflip|R}"
hemi="{hemi}"
),
shadow:
"minimal"
Expand Down Expand Up @@ -171,48 +171,6 @@ rule run_inference:
"cp {params.temp_lbl} {output.nnunet_seg}"


rule unflip_nnunet_nii:
"""Unflip the Lflip nnunet seg"""
input:
nnunet_seg=bids(
root=work,
datatype="anat",
**inputs.subj_wildcards,
suffix="dseg.nii.gz",
desc="nnunet",
space="corobl",
hemi="{hemi}flip"
),
unflip_ref=(
bids(
root=work,
datatype="anat",
**inputs.subj_wildcards,
suffix="{modality}.nii.gz".format(modality=config["modality"]),
space="corobl",
desc="preproc",
hemi="{hemi}",
),
),
output:
nnunet_seg=bids(
root=work,
datatype="anat",
**inputs.subj_wildcards,
suffix="dseg.nii.gz",
desc="nnunet",
space="corobl",
hemi="{hemi,L}"
),
container:
config["singularity"]["autotop"]
group:
"subj"
shell:
"c3d {input.nnunet_seg} -flip x -popas FLIPPED "
" {input.unflip_ref} -push FLIPPED -copy-transform -o {output.nnunet_seg} "


def get_f3d_ref(wildcards, input):
if config["modality"] == "T2w":
nii = Path(input.template_dir) / config["template_files"][config["template"]][
Expand Down
27 changes: 0 additions & 27 deletions hippunfold/workflow/rules/preproc_hippb500.smk
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,6 @@ rule resample_hippdwi_to_template:
" -trim 0vox -o {output}"


rule lr_flip_b500:
input:
nii=bids(
root=work,
datatype="dwi",
**inputs.subj_wildcards,
suffix="b500.nii.gz",
space="corobl",
hemi="{hemi}"
),
output:
nii=bids(
root=work,
datatype="dwi",
**inputs.subj_wildcards,
suffix="b500.nii.gz",
space="corobl",
hemi="{hemi,L}flip"
),
container:
config["singularity"]["autotop"]
group:
"subj"
shell:
"c3d {input} -flip x -o {output}"


rule cp_b500_to_anat_dir:
input:
nii=bids(
Expand Down
Loading

0 comments on commit 3d0a60c

Please sign in to comment.