Anisotropic arrays and pointsets

Pirt uses two classes to represent anisotropic data and points. These allow taking anisotropy into account throughout the registration process (all the way to the interpolation). The visvis library is “aware” of the Aarray class (well, it checks arrays for a sampling and origin attribute, as duck-typing goes), and the PointSet class is really just a numpy nd array. Therefore (anisotropic) data represented with these classes can be correctly visualized with Visvis without applying any transformations.

class pirt.Aarray(shape_or_array, sampling=None, origin=None, fill=None, dtype='float32', **kwargs)

Anisotropic array; inherits from numpy.ndarray and adds a sampling and origin property which gives the sample distance and offset for each dimension.

Parameters:
shape_or_array : shape-tuple or numpy.ndarray

Specifies the shape of the produced array. If an array instance is given, the returned Aarray is a view of the same data (i.e. no data is copied).

sampling : tuple of ndim elements

Specifies the sample distance (i.e. spacing between elements) for each dimension. Default is all ones.

origin : tuple of ndim elements

Specifies the world coordinate at the first element for each dimension. Default is all zeros.

fill : scalar (optional)

If given, and the first argument is not an existing array, fills the array with this given value.

dtype : any valid numpy data type

The type of the data

All extra arguments are fed to the constructor of numpy.ndarray.
get_end()

Get the end of the array expressed in world coordinates.

get_size()

Get the size (as a vector) of the array expressed in world coordinates.

get_start()

Get the origin of the array expressed in world coordinates. Differs from the property ‘origin’ in that this method returns a point rather than indices z,y,x.

index_to_point(*index)

Given a multidimensional index, get the corresponding point in world coordinates.

origin

A tuple with the origin for each dimension.

point_to_index(point, non_on_index_error=False)

Given a point returns the sample index (z,y,x,..) closest to the given point. Returns a tuple with as many elements as there are dimensions.

If the point is outside the array an IndexError is raised by default, and None is returned when non_on_index_error == True.

sample(point, default=None)

Take a sample of the array, given the given point in world-coordinates, i.e. transformed using sampling. By default raises an IndexError if the point is not inside the array, and returns the value of “default” if it is given.

sampling

A tuple with the sample distance for each dimension.

class pirt.PointSet

The PointSet class can be used to represent sets of points or vectors, as well as singleton points. The dimensionality of the vectors in the pointset can be anything, and the dtype can be any of those supported by numpy.

This class inherits from np.ndarray, which makes it very flexible; you can threat it as a regular array, and also pass it to functions that require a numpy array. The shape of the array is NxD, with N the number of points, and D the dimensionality of each point.

This class has a __repr__ that displays a pointset-aware description. To see the underlying array, use print, or use pointset[…] to convert to a pure numpy array.

Parameters:
input : various

If input is in integer, it specifies the dimensionality of the array, and an empty pointset is created. If input is a list, it specifies a point, with which the pointset is initialized. If input is a numpy array, the pointset is a view on that array (ndim must be 2).

dtype : dtype descrsiption

The data type of the numpy array. If not given, the result will be float32.

angle(self, *p)

Calculate the angle (in radians) between two vectors. For 2D uses the arctan2 method so the angle has a sign. For 3D the angle is the smallest angles between the two vectors.

If no point is given, the angle is calculated relative to the positive x-axis.

angle2(self, *p)

Calculate the angle (in radians) of the vector between two points.

Say we have p1=(3,4) and p2=(2,1). p1.angle(p2) returns the difference of the angles of the two vectors: 0.142 = 0.927 - 0.785

p1.angle2(p2) returns the angle of the difference vector (1,3): p1.angle2(p2) == (p1-p2).angle()

append(self, *p)

Append a point to this pointset. One can give the elements of the points as separate arguments. Alternatively, a tuple or numpy array can be given.

can_resize

Whether points can be appended to/removed from this pointset. This can be False if the array does not own its own data or when it is not contiguous. In that case, one should make a copy first.

contains(self, *p)

Check whether the given point is already in this set.

cross(self, *p)

Calculate the cross product of two 3D vectors. Given two vectors, returns the vector that is orthogonal to both vectors. The right hand rule is applied; this vector is the middle finger, the argument the index finger, the returned vector points in the direction of the thumb.

distance(self, *p)

Calculate the Euclidian distance between two points or pointsets. Use norm() to calculate the length of a vector.

dot(self, *p)

Calculate the dot product of two pointsets. The dot product is the standard inner product of the orthonormal Euclidean space. The sizes of the point sets should match, or one point set should be singular.

extend(self, data)

Extend the point set with more points. The shape[1] of the given data must match with that of this array.

insert(self, index, *p)

Insert a point at the given index.

norm(self)

Calculate the norm (length) of the vector. This is the same as the distance to the origin, but implemented a bit faster.

normal(self)

Calculate the normalized normal of a vector. Use (p1-p2).normal() to calculate the normal of the line p1-p2. Only works on 2D points. For 3D points use cross().

normalize(self)

Return normalized vector (to unit length).

pop(self, index=-1)

Remove and returns a point from the pointset. Removes the last by default (which is more efficient than popping from anywhere else).

ravel([order])

Return a flattened array.

Refer to numpy.ravel for full documentation.

See also

numpy.ravel
equivalent function
ndarray.flat
a flat iterator on the array.
remove(self, *p, **kwargs)

Remove the given point from the point set. Produces an error if such a point is not present. If the keyword argument all is given and True, all occurances of that point are removed. Otherwise only the first occurance is removed.