noether.data.preprocessors

Submodules

Attributes

Classes

PreProcessor

Base class for all data preprocessors.

ComposePreProcess

Compose multiple transforms and support inversion by reversing the sequence and inverting each transform.

Functions

to_tensor(data)

Helper function to convert input data to a PyTorch tensor if it is not already one.

Package Contents

class noether.data.preprocessors.PreProcessor(normalization_key)

Base class for all data preprocessors. Example:

class MyPreProcessor(PreProcessor):
    def __init__(self, normalization_key: "image"):
        super().__init__(normalization_key=normalization_key)

    def __call__(self, x):
        # Example processing: normalize to [0, 1]
        return x / 255.0

    def denormalize(self, x):
        # Example denormalization: scale back to [0, 255]
        return x * 255.0
Parameters:

normalization_key (str) – key to identify on which getitem_ in the dataset/tensor the preprocessor is applied.

Raises:

TypeError – If normalization_key is not a string.

normalization_key
abstractmethod denormalize(x)

Denormalizes the input data. This method should be overridden by subclasses if denormalization is supported. If denormalization is not supported, it raises NotImplementedError or decide to implement the identity function.

Parameters:

x (torch.Tensor) – The input tensor to denormalize.

Return type:

torch.Tensor | numpy.typing.NDArray

class noether.data.preprocessors.ComposePreProcess(normalization_key, preprocessors)

Compose multiple transforms and support inversion by reversing the sequence and inverting each transform.

Example:

from noether.data.preprocessors.compose import ComposePreProcess

normalizer = ComposePreProcess(
    normalization_key="image",
    preprocessors=[
        MyPreProcessor1(),
        MyPreProcessor2(),
    ],
)
processed_data = normalizer(input_data)
original_data = normalizer.inverse(processed_data)
Parameters:
Raises:
  • TypeError – If preprocessors is not a list or if any item in the list is not an instance of PreProcessor.

  • ValueError – If the preprocessors list is empty.

normalization_key
transforms
inverse(x)

Return a transform that applies the inverse transformations in reverse order. :param x: The input to be denormalized.

Parameters:

x (Any)

Return type:

Any

noether.data.preprocessors.ScalarOrSequence
noether.data.preprocessors.to_tensor(data)

Helper function to convert input data to a PyTorch tensor if it is not already one.

Parameters:

data (noether.data.preprocessors.types.ScalarOrSequence) – The input data to convert. Can be a sequence of floats, a torch.Tensor, or None.

Return type:

torch.Tensor

Returns: The input data as a torch.Tensor if it was a sequence, the original tensor if it was already a torch.Tensor, or None if the input was None. :raises TypeError: If the input data is of an unsupported type.