Skip to content

Commit 16e8b2c

Browse files
feilongeffigies
authored andcommitted
Resample surfaces to any space/density using Connectome Workbench.
1 parent d29e49c commit 16e8b2c

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/smriprep/workflows/surfaces.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,118 @@ def init_anat_ribbon_wf(name='anat_ribbon_wf'):
13191319
return workflow
13201320

13211321

1322+
def init_resample_surfaces_wb_wf(
1323+
surfaces: list[str],
1324+
space: str,
1325+
density: str,
1326+
name: str = 'resample_surfaces_wb_wf',
1327+
):
1328+
"""
1329+
Resample subject surfaces surface to specified space and density.
1330+
1331+
Workflow Graph
1332+
.. workflow::
1333+
:graph2use: colored
1334+
:simple_form: yes
1335+
1336+
from smriprep.workflows.surfaces import init_resample_surfaces_wb_wf
1337+
wf = init_resample_surfaces_wb_wf(
1338+
surfaces=['white', 'pial', 'midthickness'],
1339+
space='onavg',
1340+
density='10k',
1341+
)
1342+
1343+
Parameters
1344+
----------
1345+
surfaces : :class:`list` of :class:`str`
1346+
Names of surfaces (e.g., ``'white'``) to resample. Both hemispheres will be resampled.
1347+
space : :class:`str`
1348+
The space to resample to, e.g., ``'onavg'``, ``'fsLR'``.
1349+
density : :class:`str`
1350+
The density to resample to, e.g., ``'10k'``, ``'41k'``. Number of vertices per hemisphere.
1351+
name : :class:`str`
1352+
Unique name for the subworkflow (default: ``"resample_surfaces_wb_wf"``)
1353+
1354+
Inputs
1355+
------
1356+
``<surface>``
1357+
Left and right GIFTIs for each surface name passed to ``surfaces``.
1358+
sphere_reg_fsLR
1359+
GIFTI surface mesh corresponding to the subject's fsLR registration sphere.
1360+
1361+
Outputs
1362+
-------
1363+
``<surface>``
1364+
Left and right GIFTI surface mesh corresponding to the input surface, resampled to the
1365+
specified space and density.
1366+
"""
1367+
import templateflow.api as tf
1368+
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
1369+
1370+
workflow = Workflow(name=name)
1371+
1372+
inputnode = pe.Node(
1373+
niu.IdentityInterface(fields=[*surfaces, 'sphere_reg_fsLR']),
1374+
name='inputnode',
1375+
)
1376+
1377+
outputnode = pe.Node(
1378+
niu.IdentityInterface(fields=[f'{surf}_resampled' for surf in surfaces]), name='outputnode'
1379+
)
1380+
1381+
surface_list = pe.Node(
1382+
niu.Merge(len(surfaces), ravel_inputs=True),
1383+
name='surface_list',
1384+
run_without_submitting=True,
1385+
)
1386+
1387+
resampler = pe.MapNode(
1388+
SurfaceResample(method='BARYCENTRIC'),
1389+
iterfield=['surface_in', 'current_sphere', 'new_sphere'],
1390+
name='resampler',
1391+
)
1392+
new_sphere = [
1393+
str(
1394+
tf.get(
1395+
template=space,
1396+
density=density,
1397+
suffix='sphere',
1398+
hemi=hemi,
1399+
space=(None if space == 'fsLR' else 'fsLR'),
1400+
extension='.surf.gii',
1401+
)
1402+
)
1403+
# Order matters. Iterate over surfaces, then hemis to get L R L R L R
1404+
for _surf in surfaces
1405+
for hemi in ['L', 'R']
1406+
]
1407+
print(new_sphere)
1408+
resampler.inputs.new_sphere = new_sphere
1409+
1410+
surface_groups = pe.Node(
1411+
niu.Split(splits=[2] * len(surfaces)),
1412+
name='surface_groups',
1413+
run_without_submitting=True,
1414+
)
1415+
1416+
workflow.connect([
1417+
(inputnode, surface_list, [
1418+
((surf, _sorted_by_basename), f'in{i}')
1419+
for i, surf in enumerate(surfaces, start=1)
1420+
]),
1421+
(inputnode, resampler, [
1422+
(('sphere_reg_fsLR', _repeat, len(surfaces)), 'current_sphere'),
1423+
]),
1424+
(surface_list, resampler, [('out', 'surface_in')]),
1425+
(resampler, surface_groups, [('surface_out', 'inlist')]),
1426+
(surface_groups, outputnode, [
1427+
(f'out{i}', f'{surf}_resampled') for i, surf in enumerate(surfaces, start=1)
1428+
]),
1429+
]) # fmt:skip
1430+
1431+
return workflow
1432+
1433+
13221434
def init_resample_surfaces_wf(
13231435
surfaces: list[str],
13241436
grayord_density: ty.Literal['91k', '170k'],

0 commit comments

Comments
 (0)