larmor.types#
The core data types: grids, volumes, tractograms, surfaces, gradients and labels.
The diffusion weighting of each volume of a DWI, in the order of the volumes. |
|
The voxel grid an object (Volume, Tractogram) lives on. |
|
One entry of a lookup table: a label's name and colour. |
|
A triangle mesh in world coordinates, with named values per vertex. |
|
A set of streamlines, stored as one flat array of points and their offsets. |
|
Volume from an MRI acquisition. |
- class larmor.types.GradientTable(bvals, bvecs, b0_threshold=50.0, shell_epsilon=80.0)#
The diffusion weighting of each volume of a DWI, in the order of the volumes.
On construction the table is checked, then normalised: each b-value is scaled by the squared length of its vector (the b-value actually applied), and each vector is scaled to unit length. A vector of length 0.5 at b = 2800 therefore becomes a unit vector at b = 700. The caller’s arrays are copied, not changed.
The checks run only on construction: change a table through
flipandpermute, not by assigning its fields.- Parameters:
bvals (ndarray) – (N,) b-values in s/mm^2.
bvecs (ndarray) – (N, 3) gradient directions in world coordinates (RAS+), unitless. A row of NaN means no direction, like (0, 0, 0).
b0_threshold (float) – b-values at or below this, in s/mm^2, count as b=0. Defaults to 50.0.
shell_epsilon (float) – A gap larger than this between sorted b-values, in s/mm^2, starts a new shell. Defaults to 80.0.
- Raises:
ValueError – If the table is empty, the shapes are not (N,) and (N, 3), a b-value is negative, NaN or infinite, a vector has an infinite component, or a volume above
b0_thresholdhas no direction.
- property b0_mask: ndarray#
b-value at or below
b0_threshold.- Returns:
np.ndarray – (N,) bool.
- Type:
Which volumes are b0s
- property b_matrix: ndarray#
The six distinct elements of b * g g^T for each volume.
In the order (gx^2, 2 gx gy, 2 gx gz, gy^2, 2 gy gz, gz^2) times b, with the factor 2 of the off-diagonal elements included (MRIToolkit’s convention).
- Returns:
np.ndarray – (N, 6) in s/mm^2.
- property shells: ndarray#
The b-value of each shell (the mean of its volumes), ascending.
- Returns:
np.ndarray – (K,) in s/mm^2; empty when every volume is a b0.
- count_shells(*, above=0.0)#
The number of distinct shells, as
shellsgroups them, above a b-value.b-values spread around a shell (995, 1000, 1005, …) count as one shell, and b0s never count.
- property shell_index: ndarray#
Each volume’s index in
shells, or -1 for a b0.The b-values of the weighted volumes are sorted, and a gap larger than
shell_epsilonbetween neighbours starts a new shell.- Returns:
np.ndarray – (N,) int.
- flip(axis)#
Negates one component of every direction, in place.
Corrects a table that is wrong for its data (for example a y sign reversed by the scanner export). Reorienting a volume never needs it.
- Parameters:
axis (int) – The world axis to negate: 0 (x), 1 (y) or 2 (z).
- Raises:
ValueError – If
axisis not 0, 1 or 2.
- permute(order)#
Reorders the components of every direction, in place.
New component j is old component order[j]. Like
flip, it corrects a wrong table and is never needed for reorienting a volume.- Parameters:
- Raises:
ValueError – If
orderis not a permutation of (0, 1, 2).
- class larmor.types.Grid(shape, affine)#
The voxel grid an object (Volume, Tractogram) lives on.
Compare grids with
matches, not==: equality on a dataclass holding an array would compare element by element.- affine#
(4, 4) float64, read-only, voxel indices to world coordinates (RAS+) in mm.
- Type:
- property orientation: tuple[str, str, str]#
Closest RAS+ direction of each voxel axis, such as (“R”, “A”, “S”).
- canonical()#
Returns the same grid with its axes permuted and flipped to RAS.
The voxels are the same in the world; only the order and direction of the axes change.
- Returns:
Grid – The grid with RAS axes.
- Return type:
- matches(other, *, atol=0.001)#
Checks whether two grids are the same voxels in the world.
Grids that differ only in the order and direction of their axes (for example a volume and the same volume reoriented) match.
- Parameters:
- Returns:
True if
otheris this grid with its axes permuted and flipped – same shape after the permutation, and affines equal withinatol.- Return type:
- class larmor.types.Label(name, rgb)#
One entry of a lookup table: a label’s name and colour.
- class larmor.types.Surface(vertices, faces, vertex_data=<factory>, reference=None)#
A triangle mesh in world coordinates, with named values per vertex.
Surfaces compare by identity (
==isis); compare the arrays to compare contents.- Parameters:
vertices (ndarray) – (V, 3) float32, the vertices in world coordinates (RAS+) in mm. Converted to float32 if needed.
faces (ndarray) – (F, 3) uint32, the vertex indices of each triangle, counter-clockwise seen from outside, so normals point out. Converted to uint32 if needed.
vertex_data (dict[str, ndarray]) – Named values per vertex, each with first axis V, such as “curv”, “thickness” or label keys. Empty by default.
reference (Grid | None) – The voxel grid of the volume the surface was made on, if known, such as the one in a FreeSurfer file’s footer. Defaults to None (unknown).
- Raises:
ValueError – If
verticesis not (V, 3) orfacesis not (F, 3), the faces are not integers or an index is outside [0, V), or avertex_datavalue does not have V entries along its first axis.
- class larmor.types.Tractogram(points, offsets, point_data=<factory>, streamline_data=<factory>, reference=None)#
A set of streamlines, stored as one flat array of points and their offsets.
Streamline s is
points[offsets[s]:offsets[s + 1]]. Index a tractogram with an integer to get one streamline’s points, or with a slice, a list or array of indices, or a boolean mask to get a new tractogram with those streamlines. Tractograms compare by identity (==isis); compare the arrays to compare contents.- Parameters:
points (ndarray) – (P, 3) float32, all points of all streamlines, in world coordinates (RAS+) in mm. Converted to float32 if needed.
offsets (ndarray) – (S + 1,) int64, where each streamline starts; the last entry is P. Converted to int64 if needed.
point_data (dict[str, ndarray]) – Named values per point, each with first axis P, such as an FA sampled along the streamlines. Empty by default.
streamline_data (dict[str, ndarray]) – Named values per streamline, each with first axis S, such as a length or a cluster label. Empty by default.
reference (Grid | None) – The voxel grid of the image the tractogram was made on, if known. Defaults to None (unknown).
- Raises:
ValueError – If
pointsis not (P, 3),offsetsare not integers that start at 0, never decrease and end at P, or apoint_dataorstreamline_datavalue does not have P or S entries along its first axis.
- subsample(n, *, seed=None)#
Returns a new tractogram with
nstreamlines chosen at random.- Parameters:
- Returns:
Tractogram –
- The selected streamlines with their data and the same
reference.
- Raises:
ValueError – If
nis negative or larger than the number of streamlines.- Return type:
- subsample_indices(n, *, seed=None)#
The streamlines
subsamplekeeps for the samenandseed.- Parameters:
- Returns:
np.ndarray – (n,) int64, the indices of the kept streamlines, ascending.
- Raises:
ValueError – If
nis negative or larger than the number of streamlines.- Return type:
- class larmor.types.Volume(data, affine, slope=1.0, intercept=0.0)#
Volume from an MRI acquisition.
dataandaffineare read-only. Reorientation replaces them with new arrays instead of writing to them. The affine is copied; the data is not (it can be large), so the caller must not change the array it passed in. The checks run only on construction, so assigning new values to the fields afterwards is not supported. Volumes compare by identity:==isis.- Parameters:
data (ndarray) – The stored values, unscaled, as (X, Y, Z) or (X, Y, Z, N). The last axis of a 4D volume is its series (DWI volumes, FOD coefficients).
affine (ndarray) – (4, 4) float64, voxel indices to world coordinates (RAS+) in mm. Its last row must be [0, 0, 0, 1].
slope (float) – Scaling, value = data * slope + intercept. Defaults to 1.0; 0, NaN or infinity means no scaling, as in nibabel.
intercept (float) – Scaling offset. Defaults to 0.0.
- Raises:
ValueError – If
datais not 3D or 4D,affineis not a finite 4x4 matrix with last row [0, 0, 0, 1] and non-zero axes, or the slope is used but the intercept is not finite.
- property orientation: tuple[str, str, str]#
Closest RAS+ direction of each voxel axis, such as (“R”, “A”, “S”).
- as_float(*, dtype=<class 'numpy.float32'>)#
Returns the scaled values, data * slope + intercept, as a float array.
When the data already has
dtypeand the scaling is trivial, this returnsdataitself (read-only), without a copy.Note that the data * slope + intercept operation can cause overflow.
- Parameters:
dtype (DTypeLike) – A floating-point type. Defaults to float32.
- Returns:
The scaled values, with the shape of
data.- Raises:
ValueError – If
dtypeis not a floating-point type.- Return type:
- with_data(data)#
Returns a new volume with the same affine, the given data and no scaling.
- Parameters:
data (ndarray) – The new values, (X, Y, Z) or (X, Y, Z, N), on the same spatial grid.
- Returns:
Volume – The new volume, with slope 1 and intercept 0.
- Raises:
ValueError – If the spatial shape of
datadiffers from this volume’s.- Return type:
- flip_axis(axis)#
Reverses a spatial voxel axis in place, keeping every voxel’s world position.
The affine is updated to match, and the data becomes a flipped view (no copy).
- Parameters:
axis (int) – The voxel axis to flip: 0, 1 or 2.
- Raises:
ValueError – If
axisis not 0, 1 or 2.
- permute_axes(order)#
Reorders the spatial voxel axes in place, keeping world positions.
The affine is updated to match, and the data becomes a transposed view.
- Parameters:
- Raises:
ValueError – If
orderis not a permutation of (0, 1, 2).
- to_orientation(codes)#
Reorients the volume in place to the given orientation, such as “RAS”.
Only permutes and flips axes (no resampling, no copy of the data), so every voxel keeps its world position.
- Parameters:
codes (str | Sequence[str]) – The orientation to reach: one of R/L, A/P and S/I each.
- Raises:
ValueError – If
codesis not a valid orientation.
- to_closest_canonical()#
Reorients the volume in place to its closest RAS orientation.