zarrnii.transform
Spatial transform classes used for image registration and resampling.
Transformation classes for spatial transformations in ZarrNii.
Classes
zarrnii.transform.Transform
Bases: ABC
Abstract base class for spatial transformations.
This class defines the interface that all transformation classes must implement to be used with ZarrNii. Transformations convert coordinates from one space to another (e.g., subject space to template space).
Functions
zarrnii.transform.Transform.apply_transform(vecs)
abstractmethod
Apply transformation to coordinate vectors.
Parameters:
-
vecs(ndarray) –Input coordinates as numpy array. Shape can be: - (3,) for single 3D point - (3, N) for N 3D points - (4,) for single homogeneous coordinate - (4, N) for N homogeneous coordinates
Returns:
-
ndarray–Transformed coordinates with same shape as input
Raises:
-
NotImplementedError–If not implemented by subclass
Source code in zarrnii/transform.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
zarrnii.transform.AffineTransform
Bases: Transform
Affine transformation for spatial coordinate mapping.
Represents a 4x4 affine transformation matrix that can be used to transform 3D coordinates between different coordinate systems. Supports various operations including matrix multiplication, inversion, and point transformation.
Attributes:
-
matrix(ndarray) –4x4 affine transformation matrix
Functions
zarrnii.transform.AffineTransform.from_txt(path, invert=False)
classmethod
Create AffineTransform from text file containing matrix.
Parameters:
-
path(Union[str, bytes]) –Path to text file containing 4x4 affine matrix
-
invert(bool, default:False) –Whether to invert the matrix after loading
Returns:
-
'AffineTransform'–AffineTransform instance with loaded matrix
Raises:
-
OSError–If file cannot be read
-
ValueError–If file does not contain valid 4x4 matrix
Source code in zarrnii/transform.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
zarrnii.transform.AffineTransform.from_array(matrix, invert=False)
classmethod
Create AffineTransform from numpy array.
Parameters:
-
matrix(ndarray) –4x4 numpy array representing affine transformation
-
invert(bool, default:False) –Whether to invert the matrix
Returns:
-
'AffineTransform'–AffineTransform instance with the matrix
Raises:
-
ValueError–If matrix is not 4x4
Source code in zarrnii/transform.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
zarrnii.transform.AffineTransform.identity()
classmethod
Create identity transformation.
Returns:
-
'AffineTransform'–AffineTransform representing identity transformation (no change)
Source code in zarrnii/transform.py
100 101 102 103 104 105 106 107 | |
zarrnii.transform.AffineTransform.apply_transform(vecs)
Apply transformation to coordinate vectors.
Parameters:
-
vecs(ndarray) –Input coordinates to transform
Returns:
-
ndarray–Transformed coordinates
Source code in zarrnii/transform.py
198 199 200 201 202 203 204 205 206 207 | |
zarrnii.transform.AffineTransform.invert()
Return the inverse of the matrix transformation.
Returns:
-
'AffineTransform'–New AffineTransform with inverted matrix
Raises:
-
LinAlgError–If matrix is singular and cannot be inverted
Source code in zarrnii/transform.py
209 210 211 212 213 214 215 216 217 218 | |
zarrnii.transform.AffineTransform.update_for_orientation(input_orientation, output_orientation)
Update the matrix to map from input orientation to output orientation.
Parameters:
-
input_orientation(str) –Current anatomical orientation (e.g., 'RPI')
-
output_orientation(str) –Target anatomical orientation (e.g., 'RAS')
Returns:
-
'AffineTransform'–New AffineTransform updated for orientation mapping
Raises:
-
ValueError–If orientations are invalid or cannot be matched
Source code in zarrnii/transform.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
zarrnii.transform.DisplacementTransform
Bases: Transform
Non-linear displacement field transformation.
Represents a displacement field transformation where each point in space has an associated displacement vector. Uses interpolation to compute displacements for arbitrary coordinates.
Attributes:
-
disp_xyz(ndarray) –Displacement vectors at grid points (4D array: x, y, z, vector_component)
-
disp_grid(Tuple[ndarray, ...]) –Grid coordinates for displacement field
-
disp_affine(AffineTransform) –Affine transformation from world to displacement field coordinates
-
zarr_path(str) –If set, the field is stored in this zarr group (see :meth:
to_zarr) instead ofdisp_xyz, and is only read when needed. Block-wise resampling then reads just the part each block needs from the store, instead of putting the field in the dask graph.
Attributes
zarrnii.transform.DisplacementTransform.field_shape
property
Spatial shape (X, Y, Z) of the displacement field.
Functions
zarrnii.transform.DisplacementTransform.from_nifti(path)
classmethod
Create DisplacementTransform from NIfTI file.
Parameters:
-
path(Union[str, bytes]) –Path to NIfTI displacement field file
Returns:
-
'DisplacementTransform'–DisplacementTransform instance loaded from file
Raises:
-
OSError–If file cannot be read
-
ValueError–If file format is invalid
Source code in zarrnii/transform.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | |
zarrnii.transform.DisplacementTransform.apply_transform(vecs)
Apply displacement transformation to coordinate vectors.
Transforms input coordinates by interpolating displacement vectors from the displacement field and adding them to the input coordinates.
Parameters:
-
vecs(ndarray) –Input coordinates as numpy array. Shape should be (3, N) for N points or (3,) for single point
Returns:
-
ndarray–Transformed coordinates with same shape as input
Notes
Points outside the displacement field domain are filled with zero displacement (no transformation).
Source code in zarrnii/transform.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
zarrnii.transform.DisplacementTransform.to_zarr(path, chunk=32)
Save the field to a zarr group and return a transform backed by it.
The field is stored as float32, (3, X, Y, Z) with chunk-voxel
chunks, with the field's voxel-to-world affine in the group attributes.
Use the returned transform with :meth:ZarrNii.apply_transform so that
each output block reads only the part of the field it needs, which keeps
a large field out of the dask graph (important with
dask.distributed, where the graph is sent to the scheduler). The
store must be readable by every worker.
Parameters:
-
path(Union[str, bytes]) –Path of the zarr group to write (overwritten if it exists).
-
chunk(int, default:32) –Chunk size along each spatial axis.
Returns:
-
A('DisplacementTransform') –class:
DisplacementTransformreading frompath.
Source code in zarrnii/transform.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
zarrnii.transform.DisplacementTransform.from_zarr(path)
classmethod
Open a displacement field saved with :meth:to_zarr (lazily).
Source code in zarrnii/transform.py
412 413 414 415 416 417 418 419 | |
zarrnii.transform.DisplacementTransform.read_field_box(v0, v1)
Field values (3, ...) in the voxel box [v0, v1).
Source code in zarrnii/transform.py
432 433 434 435 436 437 438 439 440 | |