Skip to content

Commit

Permalink
Merge pull request #329 from mgxd/enh/recon-desc
Browse files Browse the repository at this point in the history
FIX: Clean up MCRIBS outputs
  • Loading branch information
mgxd committed Dec 20, 2023
2 parents 6dda499 + 5359e64 commit f6ad255
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 26 deletions.
6 changes: 6 additions & 0 deletions nibabies/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,12 @@ def parse_args(args=None, namespace=None):
# Ensure the directory is created
config.execution.mcribs_dir.mkdir(exist_ok=True, parents=True)

# While waiting to confirm licensing for TemplateFlow distribution,
# include templates used to bring data to fsLR space
from nibabies.utils.misc import save_fsLR_mcribs

save_fsLR_mcribs(config.execution.mcribs_dir)

# Wipe out existing work_dir
if opts.clean_workdir and work_dir.exists():
from niworkflows.utils.misc import clean_directory
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

19 changes: 18 additions & 1 deletion nibabies/utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Miscellaneous utilities."""
from __future__ import annotations

import shutil
from pathlib import Path
from typing import Union

from .. import __version__
from nibabies import __version__
from nibabies.data import load_resource


def fix_multi_source_name(in_files):
Expand Down Expand Up @@ -135,3 +138,17 @@ def get_file(pkg: str, src_path: Union[str, Path]) -> str:
ref = files(pkg) / str(src_path)
fl = file_manager.enter_context(as_file(ref))
return str(fl)


def save_fsLR_mcribs(mcribs_dir: str | Path) -> None:
template_dir = Path(mcribs_dir) / 'templates_fsLR'
template_dir.mkdir(exist_ok=True)

for src in load_resource('atlases').glob('*sphere.surf.gii'):
if not (dst := (template_dir / src.name)).exists():
try:
shutil.copyfile(src, dst)
except Exception:
import warnings

warnings.warn(f"Could not save {src.name} to MCRIBS outputs")
6 changes: 3 additions & 3 deletions nibabies/workflows/anatomical/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ def init_infant_anat_wf(

# Define output workflows
anat_reports_wf = init_anat_reports_wf(
surface_recon=freesurfer, output_dir=output_dir, sloppy=sloppy
surface_recon=recon_method, output_dir=output_dir, sloppy=sloppy
)

anat_derivatives_wf = init_anat_derivatives_wf(
bids_root=bids_root,
surface_recon=freesurfer,
surface_recon=recon_method,
num_t1w=num_t1w,
num_t2w=num_t2w,
output_dir=output_dir,
Expand Down Expand Up @@ -427,7 +427,7 @@ def init_infant_anat_wf(
])
# fmt:on

if not freesurfer:
if not recon_method:
return wf

if recon_method == 'freesurfer':
Expand Down
4 changes: 2 additions & 2 deletions nibabies/workflows/anatomical/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ def init_anat_derivatives_wf(
"anat_tpms",
"anat2std_xfm",
"std2anat_xfm",
# FS
"anat2fsnative_xfm",
"fsnative2anat_xfm",
# FS
"anat_fs_aseg",
"anat_fs_aparc",
"anat_ribbon",
Expand Down Expand Up @@ -766,7 +766,7 @@ def init_anat_derivatives_wf(
ds_reg_fsLR = pe.MapNode(
DerivativesDataSink(
base_directory=output_dir,
space="fsLR",
space="dHCP" if surface_recon == "mcribs" else "fsLR",
desc="reg",
suffix="sphere",
extension=".surf.gii",
Expand Down
8 changes: 4 additions & 4 deletions nibabies/workflows/anatomical/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def init_anat_fsLR_resampling_wf(
atlases = load_resource('atlases')
# use dHCP 32k fsLR instead
select_surfaces.inputs.template_spheres = [
str(atlases / 'dHCP' / 'dHCP.week42.L.sphere.surf.gii'),
str(atlases / 'dHCP' / 'dHCP.week42.R.sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-L_den-32k_desc-week42_sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-R_den-32k_desc-week42_sphere.surf.gii'),
]
else:
select_surfaces.inputs.template_spheres = [
Expand Down Expand Up @@ -242,8 +242,8 @@ def init_mcribs_morph_grayords_wf(

atlases = load_resource('atlases')
resample.inputs.new_sphere = [ # 32k
str(atlases / 'dHCP' / 'dHCP.week42.L.sphere.surf.gii'),
str(atlases / 'dHCP' / 'dHCP.week42.R.sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-L_den-32k_desc-week42_sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-R_den-32k_desc-week42_sphere.surf.gii'),
] * 3
resample.inputs.out_file = [
f"space-fsLR_hemi-{h}_den-{grayord_density}_{morph}.shape.gii"
Expand Down
8 changes: 4 additions & 4 deletions nibabies/workflows/anatomical/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ def init_mcribs_sphere_reg_wf(*, name="mcribs_sphere_reg_wf"):
name="project_unproject",
)
project_unproject.inputs.sphere_project_to = [
atlases / 'mcribs' / 'lh.sphere.reg2.surf.gii',
atlases / 'mcribs' / 'rh.sphere.reg2.surf.gii',
atlases / 'tpl-fsaverage_hemi-L_den-41k_desc-reg_sphere.surf.gii',
atlases / 'tpl-fsaverage_hemi-R_den-41k_desc-reg_sphere.surf.gii',
]
project_unproject.inputs.sphere_unproject_from = [
atlases / 'mcribs' / 'lh.sphere.reg.dHCP42.surf.gii',
atlases / 'mcribs' / 'rh.sphere.reg.dHCP42.surf.gii',
atlases / 'tpl-dHCP_space-fsaverage_hemi-L_den-41k_desc-reg_sphere.surf.gii',
atlases / 'tpl-dHCP_space-fsaverage_hemi-R_den-41k_desc-reg_sphere.surf.gii',
]

# fmt:off
Expand Down
4 changes: 2 additions & 2 deletions nibabies/workflows/bold/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,8 @@ def init_bold_fsLR_resampling_wf(
atlases = load_resource('atlases')
# use dHCP 32k fsLR instead
select_surfaces.inputs.template_spheres = [
str(atlases / 'dHCP' / 'dHCP.week42.L.sphere.surf.gii'),
str(atlases / 'dHCP' / 'dHCP.week42.R.sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-L_den-32k_desc-week42_sphere.surf.gii'),
str(atlases / 'tpl-dHCP_space-fsLR_hemi-R_den-32k_desc-week42_sphere.surf.gii'),
]
else:
select_surfaces.inputs.template_spheres = [
Expand Down

0 comments on commit f6ad255

Please sign in to comment.