Shifted Arrays

class castalign.ndarray_shifted.ndarray_shifted(a, origin=[0, 0, 0], only_if_necessary=False)[source]

Bases: ndarray

NumPy ndarray with an attached origin offset in 3D space.

This is used to carry array data together with a spatial origin so image coordinates can be interpreted in a shared 3D coordinate system without copying data into a separate wrapper object.

Notes

The origin attribute stores a coordinate offset (typically [z, y, x]). Array behavior is otherwise the same as numpy.ndarray.

See also

castalign.utils.absolute_coords_to_voxel_coords

Convert absolute coordinates to voxel indices using origin.

castalign.utils.voxel_coords_to_absolute_coords

Convert voxel indices back to absolute coordinates.

castalign.utils.crop_to_intersection

Crop two shifted volumes to a shared overlapping field of view.

Examples

Create a shifted 3D array:

>>> arr = ndarray_shifted(np.zeros((4, 5, 6)), origin=[10, 20, 30])
>>> isinstance(arr, ndarray_shifted)
True
>>> isinstance(arr, np.ndarray)
True
>>> arr.origin.tolist()
[10, 20, 30]

Keep a plain ndarray when no shift is needed:

>>> out = ndarray_shifted(np.zeros((3, 3, 3)), origin=[0, 0, 0], only_if_necessary=True)
>>> isinstance(out, np.ndarray)
True
>>> isinstance(out, ndarray_shifted)
False