Skip to content

utils

xvr.utils.preprocess

Standardize

Standardize(eps=1e-06)

Rescale each image to the range [0, 1].

Source code in src/xvr/utils/preprocess.py
def __init__(self, eps=1e-6):
    super().__init__()
    self.eps = eps

Identity

Identity()

Pass the image through unchanged, standing in for a disabled transform.

Source code in src/xvr/utils/preprocess.py
def __init__(self):
    super().__init__()

Equalize

Equalize(n_bins: int = 256, tau: float = 0.01, eps: float = 1e-10)

Differentiable histogram equalization.

Soft-assigns pixels to bins with a Gaussian kernel so the histogram, and therefore the equalized image, stays differentiable with respect to the input. tau sets how soft that assignment is.

Source code in src/xvr/utils/preprocess.py
def __init__(self, n_bins: int = 256, tau: float = 0.01, eps: float = 1e-10):
    super().__init__()
    self.n_bins = n_bins
    self.tau = tau
    self.eps = eps

XrayTransforms

XrayTransforms(
    height: int,
    width: int | None = None,
    mean: float = 0.15,
    std: float = 0.1,
    equalize: bool = False,
) -> Compose

Build the resize-and-normalize pipeline shared by training and registration.

PARAMETER DESCRIPTION
height

Output height in pixels.

TYPE: int

width

Output width in pixels. Defaults to height.

TYPE: int | None DEFAULT: None

mean

Mean used to normalize intensities.

TYPE: float DEFAULT: 0.15

std

Standard deviation used to normalize intensities.

TYPE: float DEFAULT: 0.1

equalize

If True, apply differentiable histogram equalization.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Compose

A Compose transform mapping a (B, 1, H, W) image to (B, 1, height, width).

Source code in src/xvr/utils/preprocess.py
def XrayTransforms(
    height: int,
    width: int | None = None,
    mean: float = 0.15,
    std: float = 0.1,
    equalize: bool = False,
) -> Compose:
    """Build the resize-and-normalize pipeline shared by training and registration.

    Args:
        height: Output height in pixels.
        width: Output width in pixels. Defaults to `height`.
        mean: Mean used to normalize intensities.
        std: Standard deviation used to normalize intensities.
        equalize: If True, apply differentiable histogram equalization.

    Returns:
        A `Compose` transform mapping a ``(B, 1, H, W)`` image to ``(B, 1, height, width)``.
    """
    width = height if width is None else width
    return Compose(
        [
            Standardize(),
            Equalize() if equalize else Identity(),
            Resize((height, width)),
            Normalize([mean], [std]),
        ]
    )

xvr.utils.transforms

read_rigid_transform

read_rigid_transform(
    mat: str | Path, img: str | Path, invert: bool = False
) -> RigidTransform

Get the rigid or affine matrix for warping img_warped -> img.

Source code in src/xvr/utils/transforms.py
def read_rigid_transform(mat: str | Path, img: str | Path, invert: bool = False) -> RigidTransform:
    """Get the rigid or affine matrix for warping img_warped -> img."""

    M = _read_transform(mat, invert)

    img = ScalarImage(img)
    D = _read_direction(img)
    T = _read_isocenter(img)

    T = T @ D @ M @ np.linalg.inv(D)
    T = torch.from_numpy(T).to(torch.float32)
    T = RigidTransform(T)

    return _get_nearest_rigid_transform(T)