utils

apply_affine(aff, pts)

Apply affine matrix aff to points pts.

asbytes(s)

get_actor_from_polydata(polydata)

Get vtkActor from a vtkPolyData.

get_actor_from_polymapper(poly_mapper)

Get vtkActor from a vtkPolyDataMapper.

get_bounding_box_sizes(actor)

Get the bounding box sizes of an actor.

get_grid_cells_position(shapes[, …])

Construct a XY-grid based on the cells content shape.

get_polydata_colors(polydata)

Get points color (ndarrays Nx3 int) from a vtk polydata.

get_polydata_lines(line_polydata)

Convert vtk polydata to a list of lines ndarrays.

get_polydata_normals(polydata)

Get vertices normal (ndarrays Nx3 int) from a vtk polydata.

get_polydata_triangles(polydata)

Get triangles (ndarrays Nx3 int) from a vtk polydata.

get_polydata_vertices(polydata)

Get vertices (ndarrays Nx3 int) from a vtk polydata.

get_polymapper_from_polydata(polydata)

Get vtkPolyDataMapper from a vtkPolyData.

line_colors(streamlines[, cmap])

Create colors for streamlines to be used in actor.line.

lines_to_vtk_polydata(lines[, colors])

Create a vtkPolyData with lines and colors.

map_coordinates(input, coordinates[, …])

Map the input array to new coordinates by interpolation.

map_coordinates_3d_4d(input_array, indices)

Evaluate the input_array data at the given indices using trilinear interpolation.

numpy_to_vtk_colors(colors)

Convert Numpy color array to a vtk color array.

numpy_to_vtk_matrix(array)

Convert a numpy array to a VTK matrix.

numpy_to_vtk_points(points)

Convert Numpy points array to a vtk points array.

rotate(actor[, rotation])

Rotate actor around axis by angle.

set_input(vtk_object, inp)

Set Generic input function which takes into account VTK 5 or 6.

set_polydata_colors(polydata, colors)

Set polydata colors with a numpy array (ndarrays Nx3 int).

set_polydata_normals(polydata, normals)

Set polydata normals with a numpy array (ndarrays Nx3 int).

set_polydata_triangles(polydata, triangles)

Set polydata triangles with a numpy array (ndarrays Nx3 int).

set_polydata_vertices(polydata, vertices)

Set polydata vertices with a numpy array (ndarrays Nx3 int).

shallow_copy(vtk_object)

Create a shallow copy of a given vtkObject object.

update_polydata_normals(polydata)

Generate and update polydata normals.

vtk_matrix_to_numpy(matrix)

Convert VTK matrix to numpy array.

apply_affine

fury.utils.apply_affine(aff, pts)[source]

Apply affine matrix aff to points pts.

Returns result of application of aff to the right of pts. The coordinate dimension of pts should be the last. For the 3D case, aff will be shape (4,4) and pts will have final axis length 3 - maybe it will just be N by 3. The return value is the transformed points, in this case:: res = np.dot(aff[:3,:3], pts.T) + aff[:3,3:4] transformed_pts = res.T This routine is more general than 3D, in that aff can have any shape (N,N), and pts can have any shape, as long as the last dimension is for the coordinates, and is therefore length N-1.

Parameters
aff(N, N) array-like

Homogenous affine, for 3D points, will be 4 by 4. Contrary to first appearance, the affine will be applied on the left of pts.

pts(…, N-1) array-like

Points, where the last dimension contains the coordinates of each point. For 3D, the last dimension will be length 3.

Returns
transformed_pts(…, N-1) array

transformed points

Notes

Copied from nibabel to remove dependency.

Examples

>>> aff = np.array([[0,2,0,10],[3,0,0,11],[0,0,4,12],[0,0,0,1]])
>>> pts = np.array([[1,2,3],[2,3,4],[4,5,6],[6,7,8]])
>>> apply_affine(aff, pts) #doctest: +ELLIPSIS
array([[14, 14, 24],
       [16, 17, 28],
       [20, 23, 36],
       [24, 29, 44]]...)
Just to show that in the simple 3D case, it is equivalent to:
>>> (np.dot(aff[:3,:3], pts.T) + aff[:3,3:4]).T #doctest: +ELLIPSIS
array([[14, 14, 24],
       [16, 17, 28],
       [20, 23, 36],
       [24, 29, 44]]...)
But `pts` can be a more complicated shape:
>>> pts = pts.reshape((2,2,3))
>>> apply_affine(aff, pts) #doctest: +ELLIPSIS
array([[[14, 14, 24],
        [16, 17, 28]],
<BLANKLINE>
       [[20, 23, 36],
        [24, 29, 44]]]...)

asbytes

fury.utils.asbytes(s)[source]

get_actor_from_polydata

fury.utils.get_actor_from_polydata(polydata)[source]

Get vtkActor from a vtkPolyData.

Parameters
polydatavtkPolyData
Returns
actorvtkActor

get_actor_from_polymapper

fury.utils.get_actor_from_polymapper(poly_mapper)[source]

Get vtkActor from a vtkPolyDataMapper.

Parameters
poly_mappervtkPolyDataMapper
Returns
actorvtkActor

get_bounding_box_sizes

fury.utils.get_bounding_box_sizes(actor)[source]

Get the bounding box sizes of an actor.

get_grid_cells_position

fury.utils.get_grid_cells_position(shapes, aspect_ratio=1.7777777777777777, dim=None)[source]

Construct a XY-grid based on the cells content shape.

This function generates the coordinates of every grid cell. The width and height of every cell correspond to the largest width and the largest height respectively. The grid dimensions will automatically be adjusted to respect the given aspect ratio unless they are explicitly specified.

The grid follows a row-major order with the top left corner being at coordinates (0,0,0) and the bottom right corner being at coordinates (nb_cols*cell_width, -nb_rows*cell_height, 0). Note that the X increases while the Y decreases.

Parameters
shapeslist of tuple of int

The shape (width, height) of every cell content.

aspect_ratiofloat (optional)

Aspect ratio of the grid (width/height). Default: 16:9.

dimtuple of int (optional)

Dimension (nb_rows, nb_cols) of the grid, if provided.

Returns
ndarray

3D coordinates of every grid cell.

get_polydata_colors

fury.utils.get_polydata_colors(polydata)[source]

Get points color (ndarrays Nx3 int) from a vtk polydata.

Parameters
polydatavtkPolyData
Returns
outputarray (N, 3)

Colors. None if no normals in the vtk polydata.

get_polydata_lines

fury.utils.get_polydata_lines(line_polydata)[source]

Convert vtk polydata to a list of lines ndarrays.

Parameters
line_polydatavtkPolyData
Returns
lineslist

List of N curves represented as 2D ndarrays

get_polydata_normals

fury.utils.get_polydata_normals(polydata)[source]

Get vertices normal (ndarrays Nx3 int) from a vtk polydata.

Parameters
polydatavtkPolyData
Returns
outputarray (N, 3)

Normals, represented as 2D ndarrays (Nx3). None if there are no normals in the vtk polydata.

get_polydata_triangles

fury.utils.get_polydata_triangles(polydata)[source]

Get triangles (ndarrays Nx3 int) from a vtk polydata.

Parameters
polydatavtkPolyData
Returns
outputarray (N, 3)

triangles

get_polydata_vertices

fury.utils.get_polydata_vertices(polydata)[source]

Get vertices (ndarrays Nx3 int) from a vtk polydata.

Parameters
polydatavtkPolyData
Returns
outputarray (N, 3)

points, represented as 2D ndarrays

get_polymapper_from_polydata

fury.utils.get_polymapper_from_polydata(polydata)[source]

Get vtkPolyDataMapper from a vtkPolyData.

Parameters
polydatavtkPolyData
Returns
poly_mappervtkPolyDataMapper

line_colors

fury.utils.line_colors(streamlines, cmap='rgb_standard')[source]

Create colors for streamlines to be used in actor.line.

Parameters
streamlinessequence of ndarrays
cmap(‘rgb_standard’, ‘boys_standard’)
Returns
colorsndarray

lines_to_vtk_polydata

fury.utils.lines_to_vtk_polydata(lines, colors=None)[source]

Create a vtkPolyData with lines and colors.

Parameters
lineslist

list of N curves represented as 2D ndarrays

colorsarray (N, 3), list of arrays, tuple (3,), array (K,), None

If None then a standard orientation colormap is used for every line. If one tuple of color is used. Then all streamlines will have the same colour. If an array (N, 3) is given, where N is equal to the number of lines. Then every line is coloured with a different RGB color. If a list of RGB arrays is given then every point of every line takes a different color. If an array (K, 3) is given, where K is the number of points of all lines then every point is colored with a different RGB color. If an array (K,) is given, where K is the number of points of all lines then these are considered as the values to be used by the colormap. If an array (L,) is given, where L is the number of streamlines then these are considered as the values to be used by the colormap per streamline. If an array (X, Y, Z) or (X, Y, Z, 3) is given then the values for the colormap are interpolated automatically using trilinear interpolation.

Returns
poly_datavtkPolyData
is_colormapbool, true if the input color array was a colormap

map_coordinates

fury.utils.map_coordinates(input, coordinates, output=None, order=3, mode='constant', cval=0.0, prefilter=True)[source]

Map the input array to new coordinates by interpolation.

The array of coordinates is used to find, for each point in the output, the corresponding coordinates in the input. The value of the input at those coordinates is determined by spline interpolation of the requested order.

The shape of the output is derived from that of the coordinate array by dropping the first axis. The values of the array along the first axis are the coordinates in the input array at which the output value is found.

Parameters
inputarray_like

The input array.

coordinatesarray_like

The coordinates at which input is evaluated.

outputarray or dtype, optional

The array in which to place the output, or the dtype of the returned array. By default an array of the same dtype as input will be created.

orderint, optional

The order of the spline interpolation, default is 3. The order has to be in the range 0-5.

mode{‘reflect’, ‘constant’, ‘nearest’, ‘mirror’, ‘wrap’}, optional

The mode parameter determines how the input array is extended beyond its boundaries. Default is ‘constant’. Behavior for each valid value is as follows:

‘reflect’ (d c b a | a b c d | d c b a)

The input is extended by reflecting about the edge of the last pixel.

‘constant’ (k k k k | a b c d | k k k k)

The input is extended by filling all values beyond the edge with the same constant value, defined by the cval parameter.

‘nearest’ (a a a a | a b c d | d d d d)

The input is extended by replicating the last pixel.

‘mirror’ (d c b | a b c d | c b a)

The input is extended by reflecting about the center of the last pixel.

‘wrap’ (a b c d | a b c d | a b c d)

The input is extended by wrapping around to the opposite edge.

cvalscalar, optional

Value to fill past edges of input if mode is ‘constant’. Default is 0.0.

prefilterbool, optional

Determines if the input array is prefiltered with spline_filter before interpolation. The default is True, which will create a temporary float64 array of filtered values if order > 1. If setting this to False, the output will be slightly blurred if order > 1, unless the input is prefiltered, i.e. it is the result of calling spline_filter on the original input.

Returns
map_coordinatesndarray

The result of transforming the input. The shape of the output is derived from that of coordinates by dropping the first axis.

See also

spline_filter, geometric_transform, scipy.interpolate

Examples

>>> from scipy import ndimage
>>> a = np.arange(12.).reshape((4, 3))
>>> a
array([[  0.,   1.,   2.],
       [  3.,   4.,   5.],
       [  6.,   7.,   8.],
       [  9.,  10.,  11.]])
>>> ndimage.map_coordinates(a, [[0.5, 2], [0.5, 1]], order=1)
array([ 2.,  7.])

Above, the interpolated value of a[0.5, 0.5] gives output[0], while a[2, 1] is output[1].

>>> inds = np.array([[0.5, 2], [0.5, 4]])
>>> ndimage.map_coordinates(a, inds, order=1, cval=-33.3)
array([  2. , -33.3])
>>> ndimage.map_coordinates(a, inds, order=1, mode='nearest')
array([ 2.,  8.])
>>> ndimage.map_coordinates(a, inds, order=1, cval=0, output=bool)
array([ True, False], dtype=bool)

map_coordinates_3d_4d

fury.utils.map_coordinates_3d_4d(input_array, indices)[source]

Evaluate the input_array data at the given indices using trilinear interpolation.

Parameters
input_arrayndarray,

3D or 4D array

indicesndarray
Returns
outputndarray

1D or 2D array

numpy_to_vtk_colors

fury.utils.numpy_to_vtk_colors(colors)[source]

Convert Numpy color array to a vtk color array.

Parameters
colors: ndarray
Returns
vtk_colorsvtkDataArray

Notes

If colors are not already in UNSIGNED_CHAR you may need to multiply by 255.

Examples

>>> import numpy as np
>>> from fury.utils import numpy_to_vtk_colors
>>> rgb_array = np.random.rand(100, 3)
>>> vtk_colors = numpy_to_vtk_colors(255 * rgb_array)

numpy_to_vtk_matrix

fury.utils.numpy_to_vtk_matrix(array)[source]

Convert a numpy array to a VTK matrix.

numpy_to_vtk_points

fury.utils.numpy_to_vtk_points(points)[source]

Convert Numpy points array to a vtk points array.

Parameters
pointsndarray
Returns
vtk_pointsvtkPoints()

rotate

fury.utils.rotate(actor, rotation=(90, 1, 0, 0))[source]

Rotate actor around axis by angle.

Parameters
actorvtkActor or other prop
rotationtuple

Rotate with angle w around axis x, y, z. Needs to be provided in the form (w, x, y, z).

set_input

fury.utils.set_input(vtk_object, inp)[source]

Set Generic input function which takes into account VTK 5 or 6.

Parameters
vtk_object: vtk object
inp: vtkPolyData or vtkImageData or vtkAlgorithmOutput
Returns
vtk_object

Notes

This can be used in the following way::

from fury.utils import set_input poly_mapper = set_input(vtk.vtkPolyDataMapper(), poly_data)

set_polydata_colors

fury.utils.set_polydata_colors(polydata, colors)[source]

Set polydata colors with a numpy array (ndarrays Nx3 int).

Parameters
polydatavtkPolyData
colorscolors, represented as 2D ndarrays (Nx3)

colors are uint8 [0,255] RGB for each points

set_polydata_normals

fury.utils.set_polydata_normals(polydata, normals)[source]

Set polydata normals with a numpy array (ndarrays Nx3 int).

Parameters
polydatavtkPolyData
normalsnormals, represented as 2D ndarrays (Nx3) (one per vertex)

set_polydata_triangles

fury.utils.set_polydata_triangles(polydata, triangles)[source]

Set polydata triangles with a numpy array (ndarrays Nx3 int).

Parameters
polydatavtkPolyData
trianglesarray (N, 3)

triangles, represented as 2D ndarrays (Nx3)

set_polydata_vertices

fury.utils.set_polydata_vertices(polydata, vertices)[source]

Set polydata vertices with a numpy array (ndarrays Nx3 int).

Parameters
polydatavtkPolyData
verticesvertices, represented as 2D ndarrays (Nx3)

shallow_copy

fury.utils.shallow_copy(vtk_object)[source]

Create a shallow copy of a given vtkObject object.

update_polydata_normals

fury.utils.update_polydata_normals(polydata)[source]

Generate and update polydata normals.

Parameters
polydatavtkPolyData

vtk_matrix_to_numpy

fury.utils.vtk_matrix_to_numpy(matrix)[source]

Convert VTK matrix to numpy array.