straightening package

straightening_tools

class towbintools.straightening.straightening_tools.Warper(length: float, width: float, splines: list[BSpline | None])[source]

Bases: object

Class for generating, storing, and applying splines fit to midlines of oblong objects for use in image/coordinate warping e.g. on worms, worm pharynxes

individual splines are fit to each plane independently to account for movement during stack acquisition

classmethod from_img(img: ndarray[tuple[Any, ...], dtype[_ScalarT]], mask: ndarray[tuple[Any, ...], dtype[bool]]) Warper[source]

Construct a Warper by extracting midlines from a mask and fitting splines.

Extracts the midline for each plane of mask, ensures consistent head/tail orientation across planes, fits splines, and aligns splines using image-based registration.

Parameters:
  • img (NDArray) – Grayscale image (2D or 3D stack) used for spline alignment across planes.

  • mask (NDArray[bool]) – Binary mask with the same shape as img.

Returns:

A Warper instance ready for image warping.

Return type:

Warper

Raises:

ValueError – If img and mask shapes do not match, or if the mask is invalid (see validate_mask()).

classmethod from_pickle(file: PathLike) Warper[source]

load a Warper object from a pickle file WARNING do not load untrusted pickle files as they can execute arbitrary code

rescaled_3D_img(img3D: NDArray, scale_factor: float = 1, spacing: tuple[float, float, float] = array([2., 0.325, 0.325]), normalise_spacing: bool = True, mirror: bool = False, interpolation_order: Literal[0, 1, 2, 3, 4, 5] = 1, preserve_range: bool = True, preserve_dtype: bool = True, return_final_spacing: bool = False)[source]

Warp and rescale raw 3D image according to spacing such that resultant spacing is the same in each axis

spacing is set by microscope acquisition settings and can be found in (raw) image metadata post-experiment default spacing is (2.0, 0.325, 0.325), but note, this is specific for each experiment type

if normalise_spacing is True, spacing is normalised by ‘spacing/spacing.min()’, keeping pixel length in the respective spacing.min() dimension the same size as provided image. Resultant final_spacing will be spacing.min() / scale_factor in each dimension if normalise_spacing is False, pixel lengths in resultant image will be 1/scale_factor units of the underlying physical unit (e.g. micrometers), and so resultant final_spacing will be 1/scale_factor in each dimension

to_pickle(file: PathLike)[source]

save object instace to a pickle file

warp_2D_img(img2D: ndarray[tuple[Any, ...], dtype[_ScalarT]], spline_i: int, scale_factor: float | tuple[float, float] = 1, mirror: bool = False, interpolation_order: Literal[0, 1, 2, 3, 4, 5] = 1, preserve_range: bool = True, preserve_dtype: bool = True) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Warp a single 2D plane using self.splines[i]

self.splines[i] is the spline fit to mask[i] in the provided mask during object instantiation, so usually spline_i should match z of chosen 2D plane from zstack

warp_3D_img(img3D: ndarray[tuple[Any, ...], dtype[_ScalarT]], scale_factor: float | tuple[float, float] = 1, mirror: bool = False, interpolation_order: Literal[0, 1, 2, 3, 4, 5] = 1, preserve_range: bool = True, preserve_dtype: bool = True) ndarray[tuple[Any, ...], dtype[_ScalarT]][source]

Warp full 3D image using generated all generated splines

towbintools.straightening.straightening_tools.extract_midline(mask2D: ndarray[tuple[Any, ...], dtype[bool]], ridge_responce_thresh: float = 90, return_dt: bool = False) ndarray[tuple[Any, ...], dtype[_ScalarT]] | tuple[ndarray[tuple[Any, ...], dtype[_ScalarT]], ndarray[tuple[Any, ...], dtype[_ScalarT]]][source]

Returns a topologically sorted list of pixels making up the midline of the object

mask2D: 2D mask of a single object dt_percent_thresh: % threshold on ridge response. Lower values retain weaker ridge-pixels return_dt: whether to return the calculated distance transform of the mask

towbintools.straightening.straightening_tools.validate_mask(mask: ndarray[tuple[Any, ...], dtype[_ScalarT]]) None[source]
provided mask should be:
  • 2D or 3D

  • not empty

  • contain a single object (connected component) on each 2D plane.