Skip to content

Commit

Permalink
Wrap iso surface as Node and not Function (#268)
Browse files Browse the repository at this point in the history
* IsoSurface instead of wrap_IsoSurface
  • Loading branch information
davidmeunier79 authored Nov 7, 2024
1 parent 4a51021 commit b0466f6
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 44 deletions.
126 changes: 92 additions & 34 deletions macapype/nodes/surface.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from nipype.interfaces.base import (TraitedSpec, SimpleInterface, traits, File)
from nipype.interfaces.base import (
TraitedSpec, SimpleInterface, traits, File, CommandLineInputSpec)

from nipype.interfaces.afni.base import AFNICommandBase


def keep_gcc(nii_file):
Expand Down Expand Up @@ -115,51 +118,106 @@ def wrap_nii2mesh(nii_file):
return stl_file


# ### wrapping afni IsoSurface
def wrap_afni_IsoSurface(nii_file):
# IsoSurface
class IsoSurfaceInputSpec(CommandLineInputSpec):

nii_file = File(
exists=True,
desc='brain_file',
mandatory=True, position=1, argstr="-input %s")

stl_file = File(
name_source=["nii_file"],
name_template="%s.stl",
desc="stl file",
exists=True,
keep_extension=False,
position=2, argstr="-o %s")

isoval = traits.Float(
1.0,
usedefault=True,
desc='isoval',
mandatory=True, argstr="-isoval %f")

KPB = traits.Float(
0.01,
usedefault=True,
desc='Smoothing ',
requires=["NITER"],
mandatory=True, position=-2, argstr="-Tsmooth %f")

NITER = traits.Int(
100,
usedefault=True,
desc='NITER',
requires=["KPB"],
mandatory=True, position=-1, argstr="%d")

remesh = traits.Float(
0.5, usedefault=True,
desc='EDGE_FRACTION',
mandatory=True, argstr="-remesh %f")

import os
from nipype.utils.filemanip import split_filename as split_f
autocrop = traits.Bool(
True,
desc='autocrop',
mandatory=False, argstr="-autocrop")

path, fname, ext = split_f(nii_file)
overwrite = traits.Bool(
True,
desc='overwrite',
mandatory=False, argstr="-overwrite")

stl_file = os.path.abspath(fname + ".stl")

# parameters
isoval = 1
remesh = 0.5
class IsoSurfaceOutputSpec(TraitedSpec):
stl_file = File(
exists=True, desc="stl file")

# Tsmooth
KPB = 0.0001
NITER = 10000

# remesh
remesh = 0.5
class IsoSurface(AFNICommandBase):
"""Description: Wrap of antsIsoSurface.sh
# options
overwrite = True
autocrop = True
Inputs:
# command
cmd = "IsoSurface"
cmd += " -isoval {}".format(isoval)
cmd += " -input {}".format(nii_file)
cmd += " -Tsmooth {} {}".format(KPB, NITER)
cmd += " -remesh {}".format(remesh)
if overwrite:
cmd += " -overwrite"
if autocrop:
cmd += " -autocrop"
cmd += " -o {}".format(stl_file)
Mandatory:
print(cmd)
nii_file
File, 'brain_file',
ret = os.system(cmd)
stl_file:
File, "stl file",
print(ret)
isoval
Float, 1, desc='isoval',
assert ret == 0, "Error, cmd {} did not work".format(cmd)
return stl_file
, KPB
Float, 0.01, 'Smoothing'
NITER
Int, 100 , 'NITER'
remesh
Float, 0.5 'EDGE_FRACTION',
Optional:
autocrop :
Bool, 'autocrop',
overwrite :
Bool, 'overwrite'
Outputs:
stl_file :
File, desc="stl file"
"""
input_spec = IsoSurfaceInputSpec
output_spec = IsoSurfaceOutputSpec

_cmd = 'IsoSurface'


def split_LR_mask(LR_mask_file, left_index=1, right_index=2):
Expand Down
6 changes: 2 additions & 4 deletions macapype/pipelines/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
compute_5tt, fill_list_vol)


from macapype.nodes.surface import wrap_afni_IsoSurface
from macapype.nodes.surface import IsoSurface

from ..utils.misc import (gunzip, merge_3_elem_to_list,
get_pattern, get_list_length, get_index)
Expand Down Expand Up @@ -1047,9 +1047,7 @@ def create_mask_from_seg_pipe(params={}, name="mask_from_seg_pipe"):

# wmgm2mesh
wmgm2mesh = pe.Node(
interface=niu.Function(input_names=["nii_file"],
output_names=["stl_file"],
function=wrap_afni_IsoSurface),
interface=IsoSurface(),
name="wmgm2mesh")

seg_pipe.connect(bin_mask, 'out_file', wmgm2mesh, "nii_file")
Expand Down
9 changes: 3 additions & 6 deletions macapype/pipelines/surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from macapype.nodes.surface import (Meshify, split_LR_mask,
wrap_nii2mesh,
wrap_afni_IsoSurface, merge_tissues,
IsoSurface, merge_tissues,
keep_gcc)

from macapype.utils.utils_nodes import parse_key, NodeParams
Expand Down Expand Up @@ -740,11 +740,8 @@ def create_IsoSurface_brain_pipe(params={},
keep_gcc_bin_mask, "nii_file")

# wmgm2mesh
wmgm2mesh = pe.Node(
interface=niu.Function(input_names=["nii_file"],
output_names=["stl_file"],
function=wrap_afni_IsoSurface),
name="wmgm2mesh")
wmgm2mesh = pe.Node(interface=IsoSurface(),
name="wmgm2mesh")

IsoSurface_brain_pipe.connect(keep_gcc_bin_mask, 'gcc_nii_file',
wmgm2mesh, "nii_file")
Expand Down

0 comments on commit b0466f6

Please sign in to comment.