Skip to content

Commit 83deff9

Browse files
mgxdeffigies
andcommitted
ENH: Port over CIFTI interfaces from fmriprep
Co-authored-by: Christopher J. Markiewicz <[email protected]>
1 parent c2d46e1 commit 83deff9

File tree

2 files changed

+864
-0
lines changed

2 files changed

+864
-0
lines changed

niworkflows/interfaces/surf.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,61 @@ def _run_interface(self, runtime):
558558
return runtime
559559

560560

561+
class CreateSurfaceROIInputSpec(TraitedSpec):
562+
subject_id = traits.Str(desc='subject ID')
563+
hemisphere = traits.Enum(
564+
"L",
565+
"R",
566+
mandatory=True,
567+
desc='hemisphere',
568+
)
569+
thickness_file = File(exists=True, mandatory=True, desc='input GIFTI file')
570+
571+
572+
class CreateSurfaceROIOutputSpec(TraitedSpec):
573+
roi_file = File(desc='output GIFTI file')
574+
575+
576+
class CreateSurfaceROI(SimpleInterface):
577+
"""Prepare GIFTI shape file for use in"""
578+
579+
input_spec = CreateSurfaceROIInputSpec
580+
output_spec = CreateSurfaceROIOutputSpec
581+
582+
def _run_interface(self, runtime):
583+
subject, hemi = self.inputs.subject_id, self.inputs.hemisphere
584+
if not isdefined(subject):
585+
subject = 'sub-XYZ'
586+
img = nb.GiftiImage.from_filename(self.inputs.thickness_file)
587+
# wb_command -set-structure
588+
img.meta["AnatomicalStructurePrimary"] = {'L': 'CortexLeft', 'R': 'CortexRight'}[hemi]
589+
darray = img.darrays[0]
590+
# wb_command -set-map-names
591+
meta = darray.meta
592+
meta['Name'] = f"{subject}_{hemi}_ROI"
593+
# wb_command -metric-palette calls have no effect on ROI files
594+
595+
# Compiling an odd sequence of math operations that works out to:
596+
# wb_command -metric-math "abs(var * -1) > 0"
597+
roi = np.abs(darray.data) > 0
598+
599+
darray = nb.gifti.GiftiDataArray(
600+
roi,
601+
intent=darray.intent,
602+
datatype=darray.datatype,
603+
encoding=darray.encoding,
604+
endian=darray.endian,
605+
coordsys=darray.coordsys,
606+
ordering=darray.ind_ord,
607+
meta=meta,
608+
)
609+
610+
out_filename = os.path.join(runtime.cwd, f"{subject}.{hemi}.roi.native.shape.gii")
611+
img.to_filename(out_filename)
612+
self._results["roi_file"] = out_filename
613+
return runtime
614+
615+
561616
def normalize_surfs(in_file, transform_file, newpath=None):
562617
"""
563618
Re-center GIFTI coordinates to fit align to native T1w space.

0 commit comments

Comments
 (0)