transforms
ngtools.transforms
Utilities to manipulate transforms (ng.CoordinateSpaceTransform) in
neuroglancer.
Also provide tools to read and convert arbitrary coordinate transforms into neuroglancer format.
Functions:
| Name | Description |
|---|---|
|
Compose two |
|
Convert the units of a transform, while keeping the transform equivalent. |
|
Convert the units of a transform, so that all axes have the same unit and scale, while keeping the transform equivalent. |
|
Get matrix from transform, creating one if needed. |
|
Invert a |
|
Load an affine transform from a file or stream. |
|
Ensure that input and output spaces have SI units. |
|
Keep only axes whose units are of a given kind. |
|
Ensure that an affine matrix is square (with its homogeneous row). |
Note on transforms
A CoordinateSpaceTransform has the following fields
transform : np.ndarray
Homogeneous affine transform that maps input coordinates
(columns) to output coordinates (row)
inputDimensions : CoordinateSpace
A description of the input space.
Sometimes referred to as the "source" space in the ng sourcecode.
outputDimensions : CoordinateSpace
A description of the output space.
Sometimes referred to as the "view" space in the ng sourcecode.
A CoordinateSpace has the following fields:
names : list[str]
The name of each dimension.
scales : list[float]
The scale (voxel size) of each dimension.
units : list[str]
The unit in which scale is expressed.
Two important points must be noted, as they differ from how transforms are encoded in other frameworks:
* The _linear part_ of the transform is ___always___ unit-less.
That is, it maps from/to the same isotropic unit.
* The _translation_ component of the transform is ___always___
expressed in output units.
A nifti-like "voxel to scaled-world" matrix would therefore be constructed via
[ x ] [ 1/vx ] [ Axi Axj Axk ] [ sx ] [ i ] [ Tx ]
[ y ] = [ 1/vy ] * [ Ayi Ayj Ayk ] * [ sy ] * [ j ] + [ Ty ]
[ z ] [ 1/vz ] [ Azi Azj Azk ] [ sz ] [ k ] [ Tz ]
where v is the view/output voxel size and s is the source/input
voxel size. In that context, [x, y, z] is expressed in "scaled world" units
and [i, j, k] is expressed in "voxel" units.
Alternatively, a "scaled-voxel to world" matrix can be constructed via
matrix_to_quaternion
Convert a rotation matrix to a quaternion.
We follow neuroglancer and assume that quaternions are ordered as [v, r] (or again, [i, j, k, r]). This differs form wikipedia which defines them as [r, v] (or [r, i, j, k]).
ras2transform
Convert a RAS2RAS matrix (in mm space) to a neuroglancer transform.
load_affine
Load an affine transform from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fileobj
|
|
Input file. May start with a format protocol such as
|
required |
format
|
(
|
Alternative way to provide a format hint. |
"afni"
|
Returns:
| Name | Type | Description |
|---|---|---|
affine |
(4, 4) np.ndarray
|
A RAS2RAS affine transform. |
save_transform
Save a neuroglancer transform in a neuroimaging format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trf
|
|
Transformation |
required |
out
|
|
Output path. Currently, only local paths are supported. |
required |
format
|
|
Output format (default: guessed from extension -- or LTA) |
None
|
get_matrix
Get (a copy of) the transfomation's matrix -- create one if needed.
compose
Compose two transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*transforms
|
|
Transforms to compose |
()
|
adapt
|
|
Try to adapt midspaces if they are different neurospaces. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
composition |
|
|
convert_transform
Convert units of a transform.
Example
Ensure that all axes have SI unit (without prefix)
Ensure that all axes have unit-scale SI unit (without prefix)
Ensure that input spatial axes have millimeter unit
Ensure that specific input axes have specific unit
Ensure that specific input axes have specific scales
See also: normalize_transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
|
Transform |
required |
input
|
|
Either a coordinate space to use in-place of the existing one, or a new (list of) unit(s), or a mapping from axis name(s) to new unit(s). |
None
|
output
|
|
Either a coordinate space to use in-place of the existing one, or a new (list of) unit(s), or a mapping from axis name(s) to new unit(s). |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
transform |
|
|
normalize_transform
Ensure that input and output spaces have SI units.
See also: convert_transform, ensure_same_scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
|
Transformation |
required |
unit_scale
|
|
If |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
transform |
|
Converted transformation |
ensure_same_scale
Ensure that all axes and both input and output spaces use the same unit and the same scaling.
This function is quite related to normalize_transform(*, unit_scale=True)
except that the units closest to input units are used, instead of
prefixless SI units.
See also: normalize_transform.
subtransform
Generate a subtransform by keeping only axes of a certain unit kind.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform
|
|
Transformation |
required |
unit
|
|
Unit kind to keep. |
None
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
names |
|
Axis names to keep. |
input_names |
|
Names of input axes to keep. |
str |
|
Names of input axes to keep. |
output_names |
|
Names of output axes to keep. |
str |
|
Names of output axes to keep. |
Returns:
| Name | Type | Description |
|---|---|---|
transform |
|
Converted transformation |