Skip to content
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

ENH: Port GIFTI/CIFTI interfaces from fMRIPrep #811

Merged
merged 5 commits into from
Jun 9, 2023
Merged
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
1 change: 1 addition & 0 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ furo ~= 2022.4.7
nipype >= 1.5.1
traits < 6.4
packaging
pytest
sphinx ~= 4.2
sphinxcontrib-apidoc
sphinxcontrib-napoleon
Expand Down
40 changes: 40 additions & 0 deletions niworkflows/interfaces/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pathlib import Path
from shutil import copytree

import pytest

try:
from contextlib import chdir as _chdir
except ImportError: # PY310
import os
from contextlib import contextmanager

@contextmanager # type: ignore
def _chdir(path):
cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(cwd)


@pytest.fixture(scope="module")
def data_dir():
return Path(__file__).parent / "tests" / "data"


@pytest.fixture(autouse=True)
def _docdir(request, tmp_path):
# Trigger ONLY for the doctests.
doctest_plugin = request.config.pluginmanager.getplugin("doctest")
if isinstance(request.node, doctest_plugin.DoctestItem):
copytree(Path(__file__).parent / "tests" / "data", tmp_path, dirs_exist_ok=True)

# Chdir only for the duration of the test.
with _chdir(tmp_path):
yield

else:
# For normal tests, we have to yield, since this is a yield-fixture.
yield
60 changes: 60 additions & 0 deletions niworkflows/interfaces/surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,66 @@ def _run_interface(self, runtime):
return runtime


class CreateSurfaceROIInputSpec(TraitedSpec):
subject_id = traits.Str(desc='subject ID')
hemisphere = traits.Enum(
"L",
"R",
mandatory=True,
desc='hemisphere',
)
thickness_file = File(exists=True, mandatory=True, desc='input GIFTI file')


class CreateSurfaceROIOutputSpec(TraitedSpec):
roi_file = File(desc='output GIFTI file')


class CreateSurfaceROI(SimpleInterface):
"""Prepare GIFTI shape file for use in cortical masking

Distilled from the FreeSurfer2CaretConvertAndRegisterNonlinear.sh script in
DCAN-HCP PostFreeSurfer scripts (as of commit 9291324). The relevant lines
are 277-290.
"""

input_spec = CreateSurfaceROIInputSpec
output_spec = CreateSurfaceROIOutputSpec

def _run_interface(self, runtime):
subject, hemi = self.inputs.subject_id, self.inputs.hemisphere
if not isdefined(subject):
subject = 'sub-XYZ'
img = nb.GiftiImage.from_filename(self.inputs.thickness_file)
# wb_command -set-structure (L282)
img.meta["AnatomicalStructurePrimary"] = {'L': 'CortexLeft', 'R': 'CortexRight'}[hemi]
darray = img.darrays[0]
# wb_command -set-map-names (L284)
meta = darray.meta
meta['Name'] = f"{subject}_{hemi}_ROI"
# wb_command -metric-palette calls (L285, L289) have no effect on ROI files

# Compiling an odd sequence of math operations (L283, L288, L290) that work out to:
# wb_command -metric-math "abs(var * -1) > 0"
roi = np.abs(darray.data) > 0

darray = nb.gifti.GiftiDataArray(
roi,
intent=darray.intent,
datatype=darray.datatype,
encoding=darray.encoding,
endian=darray.endian,
coordsys=darray.coordsys,
ordering=darray.ind_ord,
meta=meta,
)

out_filename = os.path.join(runtime.cwd, f"{subject}.{hemi}.roi.native.shape.gii")
img.to_filename(out_filename)
self._results["roi_file"] = out_filename
return runtime


def normalize_surfs(in_file, transform_file, newpath=None):
"""
Re-center GIFTI coordinates to fit align to native T1w space.
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Loading