Skip to content

Commit

Permalink
changes work macaque local test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan DeKraker - B. Bernhardt Lab committed Feb 21, 2024
1 parent 29647cf commit e1b674c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 21 deletions.
1 change: 1 addition & 0 deletions .dryrun_test_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ hippunfold - test_out participant -np --modality cropseg --path_cropseg test_dat
hippunfold - test_out participant -np --modality cropseg --path_cropseg test_data/data_cropseg_1hemi/sub-{subject}_hemi-{hemi}_dseg.nii.gz --hemi L
hippunfold test_data/bids_singleT2w test_out participant -np --modality T2w --t1_reg_template
hippunfold test_data/bids_singleT2w test_out participant -np --modality T2w --output_space T1w
hippunfold test_data/bids_T1w test_out participant -np --modality T1w --use-template-seg
32 changes: 28 additions & 4 deletions hippunfold/config/snakebids.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,29 @@ singularity:

xfm_identity: resources/etc/identity_xfm.txt

#templates enabled for template-based segmentation are here
template_based_segmentation:
CITI168:
hemi:
- R
- L
dHCP:
hemi:
- R
- L
CIVM:
hemi:
- R
- L
MBMv2:
hemi:
- R
- L
MBMv3:
hemi:
- R


template_files:
CITI168:
T1w: T1w_head_700um.nii.gz
Expand Down Expand Up @@ -395,16 +418,16 @@ template_files:
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
crop_ref: tpl-MBMv3_hemi-{hemi}_space-corobl_T1w.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
dseg: tpl-MBMv3_hemi-{hemi}_space-corobl_desc-tissuemanual_dseg.nii.gz
coords: tpl-MBMv3_dir-{dir}_hemi-{hemi}_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
dseg: tpl-CIVM_hemi-R_space-corobl_desc-tissuemanual_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
Expand Down Expand Up @@ -491,6 +514,7 @@ resource_urls:
MBMv2: 'files.ca-1.osf.io/v1/resources/v8acf/providers/osfstorage/65395c0887852d133ca597dd/?zip='
MBMv3: 'files.ca-1.osf.io/v1/resources/v8acf/providers/osfstorage/65395c0e8a28b11240ffc6e9/?zip='
upenn: 'files.ca-1.osf.io/v1/resources/v8acf/providers/osfstorage/65395c1613d27b122a94ca09/?zip='
CIVM: 'files.ca-1.osf.io/v1/resources/v8acf/providers/osfstorage/65395bf62827451220b86e24/?zip='
#to get hash, see https://github.com/CenterForOpenScience/osf.io/issues/8256#issuecomment-379833911


Expand Down
24 changes: 21 additions & 3 deletions hippunfold/workflow/rules/download.smk
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@
download_dir = get_download_dir()


rule download_extract_atlas_or_template:
rule download_extract_atlas:
params:
url=lambda wildcards: config["resource_urls"][wildcards.resource_type][
url=lambda wildcards: config["resource_urls"]["atlas"][
wildcards.atlas
],
output:
unzip_dir=directory(
Path(download_dir) / "{resource_type,atlas|template}" / "{atlas}"
Path(download_dir) / "atlas" / "{atlas}"
),
container:
config["singularity"]["autotop"]
shadow:
"minimal"
shell:
"wget https://{params.url} -O temp.zip && "
" unzip -d {output.unzip_dir} temp.zip"


rule download_extract_template:
params:
url=lambda wildcards: config["resource_urls"]["template"][
wildcards.template
],
output:
unzip_dir=directory(
Path(download_dir) / "template" / "{template}"
),
container:
config["singularity"]["autotop"]
Expand Down
2 changes: 1 addition & 1 deletion hippunfold/workflow/rules/preproc_t1.smk
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ rule warp_t1_to_corobl_crop:
root=work,
datatype="anat",
**config["subj_wildcards"],
suffix="T1w.nii.gz",
suffix="{modality}.nii.gz",
space="corobl",
desc="preproc",
hemi="{hemi,L|R}"
Expand Down
57 changes: 47 additions & 10 deletions hippunfold/workflow/rules/templateseg.smk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@ def get_smoothing_opt(wildcards):
return f"-s {gradient_sigma}vox {warp_sigma}vox"


# 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 wildcards


rule template_reg:
input:
fixed_img=bids(
Expand All @@ -25,7 +58,7 @@ rule template_reg:
moving_img=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]][
get_modality_suffix(config["modality"])
].format(**wildcards),
].format(**flipped(wildcards)),
xfm_corobl=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["xfm_corobl"].format(
**wildcards
Expand All @@ -43,7 +76,7 @@ rule template_reg:
from_="template",
to="subject",
space="corobl",
hemi="{hemi,R|L}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -81,7 +114,9 @@ rule warp_template_dseg:
template_dir=Path(download_dir) / "template" / config["template"],
params:
template_dseg=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["dseg"].format(**wildcards),
/ config["template_files"][config["template"]]["dseg"].format(
**flipped(wildcards)
),
interp_opt="-ri LABEL 0.2vox",
output:
inject_seg=bids(
Expand All @@ -91,7 +126,7 @@ rule warp_template_dseg:
suffix="dseg.nii.gz",
desc="postproc",
space="corobl",
hemi="{hemi,R|L}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -128,7 +163,9 @@ rule warp_template_coords:
params:
interp_opt="-ri NN",
template_coords=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["coords"].format(**wildcards),
/ config["template_files"][config["template"]]["coords"].format(
**flipped(wildcards)
),
output:
init_coords=bids(
root=work,
Expand All @@ -139,7 +176,7 @@ rule warp_template_coords:
suffix="coords.nii.gz",
desc="init",
space="corobl",
hemi="{hemi,R|L|Lflip}"
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -176,7 +213,7 @@ rule warp_template_anat:
params:
template_anat=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]][config["modality"]].format(
**wildcards
**flipped(wildcards)
),
xfm_corobl=lambda wildcards, input: Path(input.template_dir)
/ config["template_files"][config["template"]]["xfm_corobl"].format(
Expand All @@ -190,7 +227,7 @@ rule warp_template_anat:
suffix=f"{config['modality']}.nii.gz",
desc="warpedtemplate",
space="corobl",
hemi="{hemi,L|R}",
hemi=hemi_constrained_wildcard,
),
group:
"subj"
Expand Down Expand Up @@ -228,7 +265,7 @@ rule unflip_template_dseg:
suffix="dseg.nii.gz",
desc="postproc",
space="corobl",
hemi="{hemi,L}",
hemi="{hemi,L|R}",
**config["subj_wildcards"]
),
container:
Expand Down Expand Up @@ -272,7 +309,7 @@ rule unflip_template_coords:
suffix="coords.nii.gz",
desc="init",
space="corobl",
hemi="{hemi,L}"
hemi="{hemi,L|R}",
),
container:
config["singularity"]["autotop"]
Expand Down
4 changes: 1 addition & 3 deletions hippunfold/workflow/scripts/create_warps.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ def summary(name, array):
summary("displace_to_unfold_vec", displace_to_unfold_vec)

# create new shape as 5d vector image in native space
native_map_shape = np.ones(
5,
)
native_map_shape = np.ones(5,)
native_map_shape[:3] = mask.shape
native_map_shape[-1] = 3

Expand Down

0 comments on commit e1b674c

Please sign in to comment.