Skip to content

[FIX] Created TVTKBaseInterface. Should fix #1218 #1219

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

Merged
merged 8 commits into from
Dec 9, 2015
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
4 changes: 3 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Next Release
Next release
============

* FIX: VTK version check missing when using tvtk (https://github.com/nipy/nipype/pull/1219)
* ENH: Added an OAR scheduler plugin (https://github.com/nipy/nipype/pull/1259)
* ENH: New ANTs interface: antsBrainExtraction (https://github.com/nipy/nipype/pull/1231)
* API: Default model level for the bedpostx workflow has been set to "2" following FSL 5.0.9 lead
* ENH: New interfaces for interacting with AWS S3: S3DataSink and S3DataGrabber (https://github.com/nipy/nipype/pull/1201)


Release 0.11.0 (September 15, 2015)
============

Expand Down
84 changes: 38 additions & 46 deletions nipype/algorithms/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@
iflogger = logging.getLogger('interface')


class TVTKBaseInterface(BaseInterface):
_redirect_x = True
_vtk_major = 6

def __init__(self, **inputs):
try:
from tvtk.tvtk_classes.vtk_version import vtk_build_version
self._vtk_major = int(vtk_build_version[0])
except ImportError:
iflogger.warning('VTK version-major inspection using tvtk failed.')

super(TVTKBaseInterface, self).__init__(**inputs)


class WarpPointsInputSpec(BaseInterfaceInputSpec):
points = File(exists=True, mandatory=True,
desc=('file containing the point set'))
Expand All @@ -42,7 +56,7 @@ class WarpPointsOutputSpec(TraitedSpec):
out_points = File(desc='the warped point set')


class WarpPoints(BaseInterface):
class WarpPoints(TVTKBaseInterface):

"""
Applies a displacement field to a point set given in vtk format.
Expand All @@ -62,7 +76,6 @@ class WarpPoints(BaseInterface):
"""
input_spec = WarpPointsInputSpec
output_spec = WarpPointsOutputSpec
_redirect_x = True

def _gen_fname(self, in_file, suffix='generated', ext=None):
import os.path as op
Expand All @@ -81,30 +94,15 @@ def _gen_fname(self, in_file, suffix='generated', ext=None):
return op.abspath('%s_%s.%s' % (fname, suffix, ext))

def _run_interface(self, runtime):
vtk_major = 6
try:
import vtk
vtk_major = vtk.VTK_MAJOR_VERSION
except ImportError:
iflogger.warn(('python-vtk could not be imported'))
import nibabel as nb
import numpy as np
from scipy import ndimage

try:
from tvtk.api import tvtk
except ImportError:
raise ImportError('Interface requires tvtk')

try:
from enthought.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'null'
except ImportError:
iflogger.warn(('ETS toolkit could not be imported'))
except ValueError:
iflogger.warn(('ETS toolkit could not be set to null'))

import nibabel as nb
import numpy as np
from scipy import ndimage

r = tvtk.PolyDataReader(file_name=self.inputs.points)
r.update()
mesh = r.output
Expand Down Expand Up @@ -134,7 +132,7 @@ def _run_interface(self, runtime):
newpoints = [p + d for p, d in zip(points, disps)]
mesh.points = newpoints
w = tvtk.PolyDataWriter()
if vtk_major <= 5:
if self._vtk_major <= 5:
w.input = mesh
else:
w.set_input_data_object(mesh)
Expand Down Expand Up @@ -182,7 +180,7 @@ class ComputeMeshWarpOutputSpec(TraitedSpec):
desc='numpy file keeping computed distances and weights')


class ComputeMeshWarp(BaseInterface):
class ComputeMeshWarp(TVTKBaseInterface):

"""
Calculates a the vertex-wise warping to get surface2 from surface1.
Expand All @@ -207,7 +205,6 @@ class ComputeMeshWarp(BaseInterface):

input_spec = ComputeMeshWarpInputSpec
output_spec = ComputeMeshWarpOutputSpec
_redirect_x = True

def _triangle_area(self, A, B, C):
A = np.array(A)
Expand All @@ -223,15 +220,7 @@ def _run_interface(self, runtime):
try:
from tvtk.api import tvtk
except ImportError:
raise ImportError('Interface ComputeMeshWarp requires tvtk')

try:
from enthought.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'null'
except ImportError:
iflogger.warn(('ETS toolkit could not be imported'))
except ValueError:
iflogger.warn(('ETS toolkit is already set'))
raise ImportError('Interface requires tvtk')

r1 = tvtk.PolyDataReader(file_name=self.inputs.surface1)
r2 = tvtk.PolyDataReader(file_name=self.inputs.surface2)
Expand Down Expand Up @@ -280,7 +269,12 @@ def _run_interface(self, runtime):
out_mesh.point_data.vectors.name = 'warpings'
writer = tvtk.PolyDataWriter(
file_name=op.abspath(self.inputs.out_warp))
writer.set_input_data(out_mesh)

if self._vtk_major <= 5:
writer.input = mesh
else:
writer.set_input_data_object(mesh)

writer.write()

self._distance = np.average(errvector, weights=weights)
Expand Down Expand Up @@ -322,7 +316,7 @@ class MeshWarpMathsOutputSpec(TraitedSpec):
desc='vtk with surface warped')


class MeshWarpMaths(BaseInterface):
class MeshWarpMaths(TVTKBaseInterface):

"""
Performs the most basic mathematical operations on the warping field
Expand All @@ -348,21 +342,12 @@ class MeshWarpMaths(BaseInterface):

input_spec = MeshWarpMathsInputSpec
output_spec = MeshWarpMathsOutputSpec
_redirect_x = True

def _run_interface(self, runtime):
try:
from tvtk.api import tvtk
except ImportError:
raise ImportError('Interface ComputeMeshWarp requires tvtk')

try:
from enthought.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'null'
except ImportError:
iflogger.warn(('ETS toolkit could not be imported'))
except ValueError:
iflogger.warn(('ETS toolkit is already set'))
raise ImportError('Interface requires tvtk')

r1 = tvtk.PolyDataReader(file_name=self.inputs.in_surf)
vtk1 = r1.output
Expand Down Expand Up @@ -412,14 +397,21 @@ def _run_interface(self, runtime):
vtk1.point_data.vectors = warping
writer = tvtk.PolyDataWriter(
file_name=op.abspath(self.inputs.out_warp))
writer.set_input_data(vtk1)
if self._vtk_major <= 5:
writer.input = vtk1
else:
writer.set_input_data_object(vtk1)
writer.write()

vtk1.point_data.vectors = None
vtk1.points = points1 + warping
writer = tvtk.PolyDataWriter(
file_name=op.abspath(self.inputs.out_file))
writer.set_input_data(vtk1)

if self._vtk_major <= 5:
writer.input = vtk1
else:
writer.set_input_data_object(vtk1)
writer.write()

return runtime
Expand Down
27 changes: 24 additions & 3 deletions nipype/interfaces/fsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1870,9 +1870,18 @@ def _vtk_to_coords(self, in_file, out_file=None):
except ImportError:
raise ImportError('This interface requires tvtk to run.')

vtk_major = 5
try:
from tvtk.tvtk_classes.vtk_version import vtk_build_version
vtk_major = int(vtk_build_version[0])
except ImportError:
iflogger.warning('VTK version-major inspection using tvtk failed.')

reader = tvtk.PolyDataReader(file_name=in_file + '.vtk')
reader.update()
points = reader.output.points

mesh = reader.output if vtk_major < 6 else reader.get_output()
points = mesh.points

if out_file is None:
out_file, _ = op.splitext(in_file) + '.txt'
Expand All @@ -1887,12 +1896,24 @@ def _coords_to_vtk(self, points, out_file):
except ImportError:
raise ImportError('This interface requires tvtk to run.')

vtk_major = 5
try:
from tvtk.tvtk_classes.vtk_version import vtk_build_version
vtk_major = int(vtk_build_version[0])
except ImportError:
iflogger.warning('VTK version-major inspection using tvtk failed.')

reader = tvtk.PolyDataReader(file_name=self.inputs.in_file)
reader.update()
mesh = reader.output

mesh = reader.output if vtk_major < 6 else reader.get_output()
mesh.points = points

writer = tvtk.PolyDataWriter(file_name=out_file, input=mesh)
writer = tvtk.PolyDataWriter(file_name=out_file)
if vtk_major < 6:
writer.input = mesh
else:
writer.set_input_data_object(mesh)
writer.write()

def _trk_to_coords(self, in_file, out_file=None):
Expand Down