Skip to content

torchsky.transforms

Stability: core. Names on this page follow the deprecation policy in contributing.

Display stretches, normalisation, cropping, and augmentation transforms.

Guides: encoders


This reference is generated from the package source. Names are grouped by the module that defines them; import every name from the public namespace torchsky.transforms unless the group text says otherwise.

Top-level namespace

Display stretches, normalisation, cropping, and augmentation transforms.

Every transform is a callable object that maps one tensor to one tensor and keeps the last two axes spatial for the geometric transforms. Stretches are monotone display mappings on non-negative data; normalisations are affine and dtype-preserving.

AsinhStretch

AsinhStretch(a: float = 0.1, Q: float = 8.0)

Inverse-hyperbolic-sine stretch asinh(Q x / a) / asinh(Q / a).

Q instance-attribute

Q = Q

a instance-attribute

a = a

__call__

__call__(tensor: Tensor) -> Tensor

CenterCrop

CenterCrop(size: _Size)

Crop a centred size window from the trailing two axes.

size instance-attribute

size = (
    (size, size) if isinstance(size, int) else tuple(size)
)

__call__

__call__(tensor: Tensor) -> Tensor

Compose

Compose(transforms: Sequence[Any])

Apply transforms left to right.

Parameters:

Name Type Description Default
transforms Sequence[Any]

Ordered callables, each accepting the previous tensor.

required

Examples:

>>> import torch
>>> from torchsky.transforms import Compose
>>> Compose([lambda value: value + 1])(torch.tensor(1)).item()
2

transforms instance-attribute

transforms = list(transforms)

__call__

__call__(tensor: Tensor) -> Tensor

__repr__

__repr__() -> str

GaussianNoise

GaussianNoise(std: float = 0.01, snr_based: bool = False)

Additive Gaussian noise, either absolute or scaled to the signal.

snr_based=True sets the noise standard deviation to signal_std / std, so std is then a signal-to-noise ratio.

snr_based instance-attribute

snr_based = snr_based

std instance-attribute

std = std

__call__

__call__(tensor: Tensor) -> Tensor

LogStretch

LogStretch(a: float = 1000.0)

Logarithmic stretch log(a x + 1) / log(a + 1) for x >= 0.

_log_factor instance-attribute

_log_factor = log(a + 1.0)

a instance-attribute

a = a

__call__

__call__(tensor: Tensor) -> Tensor

MinMaxScale

MinMaxScale(min_val: float = 0.0, max_val: float = 1.0)

Affinely map the data range onto [min_val, max_val].

max_val instance-attribute

max_val = max_val

min_val instance-attribute

min_val = min_val

__call__

__call__(tensor: Tensor) -> Tensor

Normalize

Normalize(
    mean: float | None = None, std: float | None = None
)

Subtract the mean and divide by the standard deviation.

mean instance-attribute

mean = mean

std instance-attribute

std = std

__call__

__call__(tensor: Tensor) -> Tensor

PerturbByError

PerturbByError(scale: float = 1.0)

Add scale-weighted Gaussian noise drawn from a companion error map.

scale instance-attribute

scale = scale

__call__

__call__(inputs: tuple[Tensor, Tensor]) -> Tensor

PoissonNoise

PoissonNoise(lam: float = 1.0)

Poisson sample of a non-negative signal, rescaled to the input scale.

lam instance-attribute

lam = lam

__call__

__call__(tensor: Tensor) -> Tensor

PowerStretch

PowerStretch(gamma: float = 0.5)

Power-law stretch x ** gamma for x >= 0.

gamma instance-attribute

gamma = gamma

__call__

__call__(tensor: Tensor) -> Tensor

RandomCrop

RandomCrop(size: _Size)

Crop a random size window from the trailing two axes.

size instance-attribute

size = (
    (size, size) if isinstance(size, int) else tuple(size)
)

__call__

__call__(tensor: Tensor) -> Tensor

RandomFlip

RandomFlip(
    horizontal: bool = True,
    vertical: bool = True,
    p: float = 0.5,
)

Flip the trailing two axes independently with probability p.

horizontal instance-attribute

horizontal = horizontal

p instance-attribute

p = p

vertical instance-attribute

vertical = vertical

__call__

__call__(tensor: Tensor) -> Tensor

RandomRotation

RandomRotation(interpolation: str = 'bilinear')

Rotate the trailing two axes by a uniformly random angle.

interpolation instance-attribute

interpolation = interpolation

__call__

__call__(tensor: Tensor) -> Tensor

rotate

rotate(tensor: Tensor, degrees: float) -> Tensor

Rotate the trailing two axes by degrees about the image centre.

The rotation is rigid in pixel space. affine_grid works in coordinates normalised per axis to [-1, 1], so for a non-square image the pixel-space rotation R becomes diag(2/W, 2/H) R diag(W/2, H/2) there; using R directly would shear and stretch the image.

RobustScale

RobustScale(center: bool = True, scale: bool = True)

Centre on the median and optionally divide by the interquartile range.

center instance-attribute

center = center

scale instance-attribute

scale = scale

__call__

__call__(tensor: Tensor) -> Tensor

ToDevice

ToDevice(device: str | device)

Move a tensor to device.

device instance-attribute

device = device

__call__

__call__(tensor: Tensor) -> Tensor

ZScale

ZScale(contrast: float = 0.25, max_reject: float = 0.5)

Robust display limits mapped to [0, 1] (astropy-style zscale).

contrast instance-attribute

contrast = contrast

max_reject instance-attribute

max_reject = max_reject

__call__

__call__(tensor: Tensor) -> Tensor

create_display_transform

create_display_transform(
    stretch: str = "asinh", **kwargs: Any
) -> Compose

Build a one-step display Compose for a named stretch.

create_inference_transform

create_inference_transform(
    normalize: bool = True,
) -> Compose

Optional zscale only, keeping the input shape.

create_training_transform

create_training_transform(
    crop_size: int = 224,
    normalize: bool = True,
    augment: bool = True,
) -> Compose

Random crop, optional flips/noise, optional zscale.

create_validation_transform

create_validation_transform(
    crop_size: int = 224, normalize: bool = True
) -> Compose

Centre crop and optional zscale.