Transforms#

Classes#

class BaseTransform[source]#

Bases: ABC, Generic[InArrayCT, OutArrayCT]

Base transformation class that other types of transforms build from.

__call__(x, *, back=False, uncertainty=None)[source]#

Calling function returns the forward, backwards or uncertainty propagation of the transformation

Overloads:

__call__(self, x: InArrayCT, *, back: bool = ...) OutArrayCT
__call__(self, x: Data[InArrayCT], *, back: bool = ...) Data[OutArrayCT]
__call__(self, x: InArrayCT, *, back: bool = ..., uncertainty: InArrayCT) tuple[OutArrayCT, OutArrayCT]
Parameters:
xArrayT | Data[ArrayCT]

Input array or tensor of shape (N,…), where N is the number of elements

backbool, Optional

If the inverse transformation should be applied, default = False

uncertaintyArrayT, Optional

Corresponding uncertainties for the input data for uncertainty propagation of shape (N,…)

Returns:
ArrayT | Data[ArrayCT] | tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and propagated uncertainties of shape (N,…) if provided

abstractmethod backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

abstractmethod backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

abstractmethod forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

abstractmethod forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class BaseTypeTransform[source]#

Bases: BaseTransform[ArrayCT, ArrayCT]

Base transformation class that other types of transforms build from, with the same input and output type.

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class Index(*, dim=-1, in_shape=None, slice_=slice(None, None, None))[source]#

Bases: BaseTypeTransform

Slices the input along a given dimension assuming the input meets the required shape.

Parameters:
dimint, Optional

Dimension to slice over, default = -1

in_shapetuple[int, …] | NoneType, Optional

Target shape ignoring batch size so that the slice only occurs if the input has the same shape to prevent repeated slicing, if any dimension has a shape of -1, then the size of the dimension will be ignored

slice_slice, Optional

Slicing object, default = slice(None)

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class Log(*, base=10, idxs=None)[source]#

Bases: BaseTypeTransform

Logarithm transform.

Parameters:
basefloat, Optional

Base of the logarithm, default = 10

idxslist[int] | NoneType, Optional

Indices to slice the last dimension to perform the log on

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class MinClamp(*, dim=None, idxs=None)[source]#

Bases: BaseTypeTransform

Clamps the minimum value to be the smallest positive value.

Parameters:
dimint | NoneType, Optional

Dimension to take the minimum value over

idxslist[int] | NoneType, Optional

Indices to slice the last dimension to perform the min clamp on

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

class MultiTransform(*args)[source]#

Bases: BaseTransform

Applies multiple transformations.

Attributes:
transformslist[BaseTransform]

List of transformations

Parameters:
*argsBaseTransform

Transformations

__getitem__(item)[source]#

Gets a transform or a slice of transforms from the list of transforms.

Overloads:

__getitem__(self, item: int) BaseTransform
__getitem__(self, item: slice) MultiTransform
Parameters:
itemint | slice

Index or slice of the transform(s) to get

Returns:
BaseTransform

Transform or MultiTransform containing the slice of transforms

append(transform)[source]#

Appends a transform to the list of transforms

Parameters:
transformBaseTransform

Transform to append to the list of transforms

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

extend(*args)[source]#

Extends the list of transforms with another list of transforms

Parameters:
*argsBaseTransform

Transformations to extend MultiTransform transforms list

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class Normalise(*, mean=True, dim=None, offset=None, scale=None, data=None)[source]#

Bases: BaseTypeTransform

Normalises the data to zero mean and unit variance, or between 0 and 1.

Attributes:
offsetndarray

Offset to subtract from the data

scalendarray

Scale to divide the data by

Overloads:

class Normalise(self, *, offset: numpy.ndarray, scale: numpy.ndarray) None
class Normalise(self, *, data: numpy.ndarray | torch.Tensor, mean: bool = ..., dim: int | tuple[int, ...] | None = ...) None
Parameters:
meanbool, Optional

If data should be normalised to zero mean and unit variance, or between 0 and 1, default = True

dimint | tuple[int, …] | NoneType, Optional

Dimensions to normalise over, if None, all dimensions will be normalised over

offsetndarray | NoneType, Optional

Offset to subtract from the data if data argument is None

scalendarray | NoneType, Optional

Scale to divide the data if data argument is None

dataArrayLike | NoneType, Optional

Data to normalise with shape (N,…), where N is the number of elements

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class NumpyTensor(*, dtype=torch.float32)[source]#

Bases: BaseTransform

Converts Numpy arrays to PyTorch tensors.

Attributes:
dtypetorch.dtype

Data type of the tensor

Parameters:
dtypedtype, Optional

Data type of the tensor, default = torch.float32

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

class Reshape(*, in_shape=None, out_shape=None)[source]#

Bases: BaseTypeTransform

Reshapes the data.

Parameters:
in_shapelist[int] | NoneType, Optional

Original shape of the data

out_shapelist[int] | NoneType, Optional

Output shape of the data

backward(x)[source]#

Backwards pass to invert the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Untransformed array or tensor of shape (N,…)

backward_grad(x, uncertainty)[source]#

Backwards pass to invert the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Untransformed array or tensor of shape (N,…) and untransformed uncertainty of shape (N,…)

extra_repr()[source]#

Additional representation of the transformation

Returns:
str

Transform specific representation

forward(x)[source]#

Forward pass of the transformation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

Returns:
ArrayT

Transformed array or tensor of shape (N,…)

forward_grad(x, uncertainty)[source]#

Forward pass of the transformation and uncertainty propagation

Parameters:
xArrayT

Input array or tensor of shape (N,…), where N is the number of elements

uncertaintyArrayT

Uncertainty of the input array or tensor of shape (N,…)

Returns:
tuple[ArrayT, ArrayT]

Transformed array or tensor of shape (N,…) and transformed uncertainty of shape (N,…)

Types#

InArrayCT#

TypeVar[ndarray, Tensor]

OutArrayCT#

TypeVar[ndarray, Tensor]