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

Make vtk contour take an affine #1165

Merged
merged 20 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
eb7b4ff
Combined contour function with slicer to use affine
kesshijordan Dec 11, 2016
3740094
PEP8 compliance edits
kesshijordan Dec 16, 2016
ccba149
Changed the contour actor to generate a single binary skin; changed n…
kesshijordan Apr 15, 2017
814ba97
added test for surface actor
kesshijordan Apr 15, 2017
6475c1c
added a test that renders a seed ROI with the streamlines produced an…
kesshijordan Apr 16, 2017
a246757
Tutorial for surface actor added
kesshijordan Apr 18, 2017
f9e8f13
Fixed typo in variable name
kesshijordan Apr 18, 2017
f006c13
Fixed tutorial (deleted lines left over from template)
kesshijordan Apr 18, 2017
98ac9c8
updated variable name in test
kesshijordan May 18, 2017
796771c
replaced soon-to-be-deprecated fvtk module
kesshijordan May 18, 2017
653f109
replaced soon-to-be-deprecated fvtk module in tests
kesshijordan May 18, 2017
5f678a9
Merge remote-tracking branch 'upstream/master' into fix_contour3
kesshijordan Sep 15, 2017
ad60b3b
PEP8
kesshijordan Sep 15, 2017
ad9f666
Merge remote-tracking branch 'upstream/master' into fix_contour3
kesshijordan Sep 19, 2017
6d38c10
addressed code review by MarcCote (bugs, lots of PEP8)
kesshijordan Oct 13, 2017
1469f25
Merge branch 'master' of https://github.com/nipy/dipy into fix_contour3
kesshijordan Oct 13, 2017
e1045d4
random pep8
kesshijordan Oct 13, 2017
d6f8172
changed name from surface_actor to contour_from_roi and removed comments
kesshijordan Dec 1, 2017
b6cb09e
connected tutorial to examples_index.rst and valid_examples.txt
kesshijordan Dec 1, 2017
2750fa9
changed filename to viz_roi_contour
kesshijordan Dec 3, 2017
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
121 changes: 121 additions & 0 deletions dipy/viz/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,127 @@ def copy(self):
return image_actor


def surface_actor(data, affine=None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to contour_from_roi

color=np.array([1, 0, 0]), opacity=1):
"""Take a binary roi and generate a surface actor of the specified color
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to PEP257 the summary should fit on one line. I suggest breaking it up like so

"""Generates surface actor from a binary ROI.

The color and the opacity of the surface can be customized.
...
"""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

and opacity

Parameters
----------
data : array, shape (X, Y, Z)
an ROI file that will be binarized and displayed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start the parameter's description with an uppercase and end it with a point.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

affine : array, shape (4, 4)
Grid to space (usually RAS 1mm) transformation matrix. Default is None.
If None then the identity matrix is used.
color : (1, 3) ndarray
RGB values in [0,1].
opacity : float
Opacity of surface between 0 and 1.

Returns
-------
contour_assembly : vtkAssembly
ROI surface object displayed in space
coordinates as calculated by the affine parameter.

"""

if data.ndim != 3:
raise ValueError('Only 3D arrays are currently supported.')
else:
nb_components = 1

data = (data>0)*1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Missing spaces around > and * operators.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

vol = np.interp(data, xp=[data.min(), data.max()], fp=[0, 255])
vol = vol.astype('uint8')

im = vtk.vtkImageData()
if major_version <= 5:
im.SetScalarTypeToUnsignedChar()
di, dj, dk = vol.shape[:3]
im.SetDimensions(di, dj, dk)
voxsz = (1., 1., 1.)
# im.SetOrigin(0,0,0)
im.SetSpacing(voxsz[2], voxsz[0], voxsz[1])
if major_version <= 5:
im.AllocateScalars()
im.SetNumberOfScalarComponents(nb_components)
else:
im.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, nb_components)

# copy data
vol = np.swapaxes(vol, 0, 2)
vol = np.ascontiguousarray(vol)

if nb_components == 1:
vol = vol.ravel()
else:
vol = np.reshape(vol, [np.prod(vol.shape[:3]), vol.shape[3]])

uchar_array = numpy_support.numpy_to_vtk(vol, deep=0)
im.GetPointData().SetScalars(uchar_array)

if affine is None:
affine = np.eye(4)

# Set the transform (identity if none given)
transform = vtk.vtkTransform()
transform_matrix = vtk.vtkMatrix4x4()
transform_matrix.DeepCopy((
affine[0][0], affine[0][1], affine[0][2], affine[0][3],
affine[1][0], affine[1][1], affine[1][2], affine[1][3],
affine[2][0], affine[2][1], affine[2][2], affine[2][3],
affine[3][0], affine[3][1], affine[3][2], affine[3][3]))
transform.SetMatrix(transform_matrix)
transform.Inverse()

# Set the reslicing
image_resliced = vtk.vtkImageReslice()
set_input(image_resliced, im)
image_resliced.SetResliceTransform(transform)
image_resliced.AutoCropOutputOn()

# Adding this will allow to support anisotropic voxels
# and also gives the opportunity to slice per voxel coordinates

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Unnecessary empty line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

rzs = affine[:3, :3]
zooms = np.sqrt(np.sum(rzs * rzs, axis=0))
image_resliced.SetOutputSpacing(*zooms)

image_resliced.SetInterpolationModeToLinear()
image_resliced.Update()

# code from fvtk contour function
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment and reduce empty lines


skin_extractor = vtk.vtkContourFilter()
if major_version <= 5:
skin_extractor.SetInput(image_resliced)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got a

Traceback (most recent call last):
  File "doc/examples/viz_surface_actor.py", line 68, in <module>
    surface_color, surface_opacity)
  File "/data/research/src/dipy/dipy/viz/actor.py", line 305, in surface_actor
    skin_extractor.SetInput(image_resliced)
TypeError: argument 1: method requires a vtkDataObject, a vtkImageReslice was provided.

It should be skin_extractor.SetInput(image_resliced.GetOutput()) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

else:
skin_extractor.SetInputData(image_resliced.GetOutput())

skin_extractor.SetValue(0,1)

skin_normals = vtk.vtkPolyDataNormals()
skin_normals.SetInputConnection(skin_extractor.GetOutputPort())
skin_normals.SetFeatureAngle(60.0)

skin_mapper = vtk.vtkPolyDataMapper()
skin_mapper.SetInputConnection(skin_normals.GetOutputPort())
skin_mapper.ScalarVisibilityOff()

skin_actor = vtk.vtkActor()

skin_actor.SetMapper(skin_mapper)
skin_actor.GetProperty().SetOpacity(opacity)

skin_actor.GetProperty().SetColor(color[0], color[1], color[2])

del skin_mapper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to delete those? I'd assumed those would get garbage collected on the function return.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

del skin_extractor

return skin_actor


def streamtube(lines, colors=None, opacity=1, linewidth=0.1, tube_sides=9,
lod=True, lod_points=10 ** 4, lod_points_size=3,
spline_subdiv=None, lookup_colormap=None):
Expand Down
110 changes: 110 additions & 0 deletions dipy/viz/tests/test_fvtk_actors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
if actor.have_vtk:
if actor.major_version == 5 and use_xvfb:
skip_slicer = True
skip_surface = True
else:
skip_slicer = False
skip_surface = False
else:
skip_slicer = False
skip_surface = False


@npt.dec.skipif(skip_slicer)
Expand Down Expand Up @@ -156,6 +159,113 @@ def test_slicer():
npt.assert_array_equal([1, 3, 2] * np.array(data.shape),
np.array(slicer.shape))

@npt.dec.skipif(skip_surface)
@npt.dec.skipif(not run_test)
@xvfb_it
def test_surface():

#Render volume
renderer = window.renderer()
data = np.zeros((50, 50, 50))
data[20:30,25,25]=1.
data[25, 20:30, 25] = 1.
affine = np.eye(4)
surface = actor.surface_actor(data, affine,
colors=np.array([1, 0, 1]),
opacity=.5)
renderer.add(surface)

renderer.reset_camera()
renderer.reset_clipping_range()
#window.show(renderer)

#Test binarization
renderer2 = window.renderer()
data2 = np.zeros((50, 50, 50))
data2[20:30, 25, 25] = 1.
data2[35:40, 25, 25] = 1.
affine = np.eye(4)
surface2 = actor.surface_actor(data2, affine,
colors=np.array([0, 1, 1]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is failing for me with the following error:

TypeError: surface_actor() got an unexpected keyword argument 'colors'

opacity=.5)
renderer2.add(surface2)

renderer2.reset_camera()
renderer2.reset_clipping_range()
#window.show(renderer2)

arr = window.snapshot(renderer, 'test_surface.png', offscreen=False)
arr2 = window.snapshot(renderer2, 'test_surface2.png', offscreen=False)

report = window.analyze_snapshot(arr, find_objects=True)
report2 = window.analyze_snapshot(arr2, find_objects=True)

npt.assert_equal(report.objects, 1)
npt.assert_equal(report2.objects, 2)

print(report)


#test on real streamlines using tracking example
from dipy.data import read_stanford_labels
from dipy.reconst.shm import CsaOdfModel
from dipy.data import default_sphere
from dipy.direction import peaks_from_model
from dipy.tracking.local import ThresholdTissueClassifier
from dipy.tracking import utils
from dipy.tracking.local import LocalTracking
from dipy.viz import fvtk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using the soon-to-be-deprecated module fvtk, if possible. Here you can use:
from dipy.viz import actor, window

from dipy.viz.colormap import line_colors


hardi_img, gtab, labels_img = read_stanford_labels()
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.get_affine()

white_matter = (labels == 1) | (labels == 2)

csa_model = CsaOdfModel(gtab, sh_order=6)
csa_peaks = peaks_from_model(csa_model, data, default_sphere,
relative_peak_threshold=.8,
min_separation_angle=45,
mask=white_matter)

classifier = ThresholdTissueClassifier(csa_peaks.gfa, .25)

seed_mask = labels == 2
seeds = utils.seeds_from_mask(seed_mask, density=[1, 1, 1], affine=affine)

# Initialization of LocalTracking. The computation happens in the next step.
streamlines = LocalTracking(csa_peaks, classifier, seeds, affine,
step_size=2)

# Compute streamlines and store as a list.
streamlines = list(streamlines)

# Prepare the display objects.
streamlines_actor = fvtk.line(streamlines, line_colors(streamlines))
seedroi_actor = actor.surface_actor(seed_mask, affine, [0, 1, 1], 0.5)

# Create the 3d display.
r = fvtk.ren()
r2 = fvtk.ren()
fvtk.add(r, streamlines_actor)
arr3 = window.snapshot(r, 'test_surface3.png', offscreen=False)
report3 = window.analyze_snapshot(arr3, find_objects=True)
fvtk.add(r2, streamlines_actor)
fvtk.add(r2, seedroi_actor)
arr4 = window.snapshot(r2, 'test_surface4.png', offscreen=False)
report4 = window.analyze_snapshot(arr4, find_objects=True)

#assert that the seed ROI rendering isn't far away from the streamlines (affine error)
npt.assert_equal(report3.objects,report4.objects)
#fvtk.show(r)
#fvtk.show(r2)





@npt.dec.skipif(not run_test)
@xvfb_it
Expand Down
98 changes: 98 additions & 0 deletions doc/examples/viz_surface_actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
==================================
Visualization of 3D Surface Rendered ROI with Streamlines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to change the title to
Visualization of ROI contour or surface..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Visualization of ROI Surface Rendered with Streamlines"

==================================

Here is a simple tutorial following the probabilistic CSA Tracking Example in
which we generate a dataset of streamlines from a corpus callosum ROI, and
then display them with the seed ROI rendered in 3D with 50% transparency.

"""

from dipy.data import read_stanford_labels
from dipy.reconst.shm import CsaOdfModel
from dipy.data import default_sphere
from dipy.direction import peaks_from_model
from dipy.tracking.local import ThresholdTissueClassifier
from dipy.tracking import utils
from dipy.tracking.local import LocalTracking
from dipy.viz import fvtk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using the soon-to-be-deprecated module fvtk, if possible. Instead, use:
from dipy.viz import actor, window

from dipy.viz.colormap import line_colors

"""
First, we need to generate some streamlines. For a more complete description of
these steps, please refer to the CSA Probabilistic Tracking Tutorial.
"""

hardi_img, gtab, labels_img = read_stanford_labels()
data = hardi_img.get_data()
labels = labels_img.get_data()
affine = hardi_img.get_affine()

white_matter = (labels == 1) | (labels == 2)

csa_model = CsaOdfModel(gtab, sh_order=6)
csa_peaks = peaks_from_model(csa_model, data, default_sphere,
relative_peak_threshold=.8,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Wrong alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

min_separation_angle=45,
mask=white_matter)

classifier = ThresholdTissueClassifier(csa_peaks.gfa, .25)

seed_mask = labels == 2
seeds = utils.seeds_from_mask(seed_mask, density=[1, 1, 1], affine=affine)

# Initialization of LocalTracking. The computation happens in the next step.
streamlines = LocalTracking(csa_peaks, classifier, seeds, affine,
step_size=2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Wrong alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


# Compute streamlines and store as a list.
streamlines = list(streamlines)

"""
We will create a streamline actor from the streamlines
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

"""

streamlines_actor = fvtk.line(streamlines, line_colors(streamlines))

"""
Next, we create a surface actor from the corpus callosum seed ROI. We
provide the ROI data, the affine, the color in [R,G,B], and the opacity as
a decimal between zero and one. Here, we set the color as blue/green with
50% opacity.
"""
surface_opacity = 0.5
surface_color = [0,1,1]

seedroi_actor = fvtk.actor.surface_actor(seed_mask, affine,
surface_color, surface_opacity)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8: Wrong alignment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


"""
Next, we initialize a ''Renderer'' object and add both of the actors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ... both actors ... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

to the rendering.
"""

ren = fvtk.ren()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ren = window.ren()
ren.add(streamlines_actor)
ren.add(seedroi_actor)

fvtk.add(ren, streamlines_actor)
fvtk.add(ren, seedroi_actor)

"""
If you uncomment the following line, the rendering will pop up in an interactive
window.
"""

#fvtk.show(ren)

ren.zoom(1.5)
ren.reset_clipping_range()

window.record(ren, out_path='surface_actor_tutorial.png', size=(1200, 900),
reset_camera=False)

"""
.. figure:: surface_actor_tutorial.png
:align: center

**A top view of corpus callosum streamlines with the blue transparent
seed ROI in the center**.
"""