VectorField#
- class voxel.VectorField(tensor, geometry=None, space=None)#
A 3-channel volume of 3D vectors, e.g. displacements or flow velocities, whose values are expressed in either world or voxel units (
space).- save(filename, fmt=None) None#
Save the vector field to a file. A transform file keeps the vector space, a volume format writes the vectors as a 3-channel volume.
- Parameters:
filename (PathLike) – The path to the file to save.
fmt (str, optional) – The format of the file. If None, the format is determined by the file extension.
- new(tensor, geometry=None, keep_labels=True) VectorField | Volume#
Construct a new instance with the provided tensor, preserving unchanged properties. The result remains a VectorField with the same vector space when the tensor still holds 3 channels and decays to a Volume otherwise.
- Parameters:
tensor (Tensor) – The new data tensor replacement.
geometry (VolumeGeometry, optional) – The new geometry. If None, the current geometry is propagated.
keep_labels (bool, optional) – Unused for vector fields. Present to match the Volume interface.
- Returns:
VectorField or Volume – The new instance.
- in_space(space) VectorField#
Convert the vector values to a target coordinate space, rotating and scaling them through the linear part of the geometry.
- Parameters:
space (Space) – Target space of the vector values, either ‘world’ or ‘voxel’.
- Returns:
VectorField –
- The converted field, or the unchanged instance when
already in the target space.
- transform(transform, mode='linear', padding_mode='zeros', like=None) VectorField | Volume#
Apply a spatial transform to the field. An affine matrix moves the field along with the anatomy it deforms: the grid moves and the vectors turn and scale with it, so a world-space field is conjugated by the affine while a voxel-space field keeps its values. Warps, fields, series, and
likebehave as for any volume.- Parameters:
transform (AnyTransform) – Transform to apply.
mode (str, optional) – Interpolation mode when resampling.
padding_mode (str, optional) – Padding mode for outside grid values when resampling.
like (Volume or VolumeGeometry, optional) – Geometry the result is resampled onto.
- Returns:
VectorField – Transformed field.
- as_warp(geometry=None) Warp#
Convert to an absolute world-space coordinate warp, built as the identity grid displaced by the world-space vectors.
- Parameters:
geometry (VolumeGeometry, optional) – Grid to express the warp on. Displacements are sampled there and vanish beyond the field’s extent. Defaults to the field’s own grid.
- Returns:
Warp – The corresponding coordinate warp.
- integrate(coordinates, dt, method='euler', time=1, space=None, exact_gradient=True) Tensor#
Integrate points through the flow field with fixed-step numerical integration. Points beyond the field extent see zero velocity.
- Parameters:
coordinates (Tensor) – Points to integrate, of shape \((..., 3)\).
dt (float) – Integration step size.
method (str, optional) – Integration scheme, either forward ‘euler’ or midpoint ‘rk2’.
time (float, optional) – Total integration time.
space (Space, optional) – Coordinate space of the input and returned points. If None, the space of the field vectors is assumed.
exact_gradient (bool, optional) – When disabled, backprop may use cheaper approximate gradients that do not exactly match the integrated result. For rk2, the midpoint velocity is excluded from the graph, roughly halving backward time and memory at the cost of a small dt-dependent gradient bias. The forward result is unaffected.
- Returns:
Tensor – The integrated points, of shape \((..., 3)\).
- exponentiate(steps) VectorField#
Exponentiate a stationary velocity field by scaling and squaring.
- Parameters:
steps (int) – Number of scaling and squaring iterations.
- Returns:
VectorField –
- The displacement field of the flowed deformation,
expressed in the vector space of this field.