Skip to content

Resample surfaces to any space/density using Connectome Workbench. #473

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/smriprep/workflows/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,8 @@ def init_anat_preproc_wf(
hcp_morphometrics_wf = init_hcp_morphometrics_wf(omp_nthreads=omp_nthreads)
resample_surfaces_wf = init_resample_surfaces_wf(
surfaces=['white', 'pial', 'midthickness'],
grayord_density=cifti_output,
space='fsLR',
density='32k' if cifti_output == '91k' else '59k',
)
morph_grayords_wf = init_morph_grayords_wf(
grayord_density=cifti_output, omp_nthreads=omp_nthreads
Expand Down
46 changes: 31 additions & 15 deletions src/smriprep/workflows/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"""

import typing as ty
import warnings

from nipype.interfaces import freesurfer as fs
from nipype.interfaces import io as nio
Expand Down Expand Up @@ -1318,11 +1319,13 @@ def init_anat_ribbon_wf(name='anat_ribbon_wf'):

def init_resample_surfaces_wf(
surfaces: list[str],
grayord_density: ty.Literal['91k', '170k'],
density: str | None = None,
grayord_density: str | None = None,
space: str = 'fsLR',
name: str = 'resample_surfaces_wf',
):
"""
Resample subject surfaces surface to specified density.
Resample subject surfaces surface to specified space and density.

Workflow Graph
.. workflow::
Expand All @@ -1332,44 +1335,57 @@ def init_resample_surfaces_wf(
from smriprep.workflows.surfaces import init_resample_surfaces_wf
wf = init_resample_surfaces_wf(
surfaces=['white', 'pial', 'midthickness'],
grayord_density='91k',
space='onavg',
density='10k',
)

Parameters
----------
surfaces : :class:`list` of :class:`str`
Names of surfaces (e.g., ``'white'``) to resample. Both hemispheres will be resampled.
grayord_density : :class:`str`
Either `91k` or `170k`, representing the total of vertices or *grayordinates*.
space : :class:`str`
The space to resample to, e.g., ``'onavg'``, ``'fsLR'``.
density : :class:`str`
The density to resample to, e.g., ``'10k'``, ``'41k'``. Number of vertices per hemisphere.
name : :class:`str`
Unique name for the subworkflow (default: ``"resample_surfaces_wf"``)

Inputs
------
``<surface>``
Left and right GIFTIs for each surface name passed to ``surfaces``
Left and right GIFTIs for each surface name passed to ``surfaces``.
sphere_reg_fsLR
GIFTI surface mesh corresponding to the subject's fsLR registration sphere
GIFTI surface mesh corresponding to the subject's fsLR registration sphere.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
GIFTI surface mesh corresponding to the subject's fsLR registration sphere.
GIFTI surface mesh corresponding to the subject's ``space`` registration sphere.


Outputs
-------
``<surface>``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
``<surface>``
``<surface>_<space>``

Left and right GIFTI surface mesh corresponding to the input surface, resampled to fsLR
Left and right GIFTI surface mesh corresponding to the input surface, resampled to the
specified space and density.
"""
import templateflow.api as tf
from niworkflows.engine.workflows import LiterateWorkflow as Workflow

workflow = Workflow(name=name)
if density is None:
if grayord_density is None:
raise ValueError('No density specified. Set density argument.')
density = '32k' if grayord_density == '91k' else '59k'
warnings.warn(
'Deprecated grayord_density passed. Replace with\n\t'
"density='32k' if grayord_density == '91k' else '59k'",
DeprecationWarning,
stacklevel=2,
)

fslr_density = '32k' if grayord_density == '91k' else '59k'
workflow = Workflow(name=name)

inputnode = pe.Node(
niu.IdentityInterface(fields=[*surfaces, 'sphere_reg_fsLR']),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the goal is to make this generalizable, this should be sphere_reg_{space}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that what @feilong has is registrations from fsLR to onavg.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, by default the sphere.reg generated by MSM is always registered to fsLR.

name='inputnode',
)

outputnode = pe.Node(
niu.IdentityInterface(fields=[f'{surf}_fsLR' for surf in surfaces]), name='outputnode'
niu.IdentityInterface(fields=[f'{surf}_{space}' for surf in surfaces]), name='outputnode'
)

surface_list = pe.Node(
Expand All @@ -1386,11 +1402,11 @@ def init_resample_surfaces_wf(
resampler.inputs.new_sphere = [
str(
tf.get(
template='fsLR',
density=fslr_density,
template=space,
density=density,
suffix='sphere',
hemi=hemi,
space=None,
space=(None if space == 'fsLR' else 'fsLR'),
extension='.surf.gii',
)
)
Comment on lines 1402 to 1412
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

altering space in this templateflow query feels brittle - for instance, it will not be able to handle:

https://github.com/nipreps/nibabies/blob/1e6afd415ec2776a4bf749628fa1a58646d536db/nibabies/workflows/anatomical/surfaces.py#L502-L512

thinking we can either extract this out, or add a parameter tf_space to handle this - don't particularly love either tbh

Copy link
Author

@feilong feilong May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usage of space- here resembles tpl-dhcpAsym_cohort-42_space-fsaverage_hemi-L_den-41k_desc-reg_sphere.surf.gii.
It is supposed to get the sphere file that bridges two spaces (e.g., dhcpAsym and fsaverage, onavg and fsLR).

I'm not familiar with dhcpAsym templates. Do the templates also imply space-fsLR when space is not specified?

I prefer to use atlas to refer to the registration target, because the registration target is mainly about the cortical folding patterns, but atlas is also an overloaded term...

Expand All @@ -1416,7 +1432,7 @@ def init_resample_surfaces_wf(
(surface_list, resampler, [('out', 'surface_in')]),
(resampler, surface_groups, [('surface_out', 'inlist')]),
(surface_groups, outputnode, [
(f'out{i}', f'{surf}_fsLR') for i, surf in enumerate(surfaces, start=1)
(f'out{i}', f'{surf}_{space}') for i, surf in enumerate(surfaces, start=1)
]),
]) # fmt:skip

Expand Down
Loading