from fasttransform import Transform, PipelineOn this page
Other Formats
Welcome to fasttransform
fasttransform provides reusable data transformations and pipelines. It is the main building block of fastai’s data pipelines and can also be used independently. A Transform combines a function with optional inverse, setup, and type-handling behaviour. A Pipeline composes transforms.
Installation
Install latest from the GitHub repository:
or from pypi:
Quick start
Transform
Create a Transform by passing a function to its constructor or using it as a decorator. The function becomes the transform’s encodes method.
A transform supports:
- Reversibility: keep a function and its inverse in one object.
- Setup: configure a transform instance using the dataset.
- Type-based multiple dispatch: select a function based on argument types.
- Type conversion and preservation: control the result’s type, including subclasses.
To create a transform with a decorator:
Reversibility
Pass a function and its inverse to make a transform reversible. Use this to normalize and de-normalize numerical values, or to encode categories as indices and decode them again:
Setup
A transform’s setups method can calculate properties from a dataset. This z-score normalization transform stores the mean and standard deviation. Its encodes and decodes methods use those values:
import statistics
class NormalizeMean(Transform):
def setups(self, items):
self.mean = statistics.mean(items)
self.std = statistics.stdev(items)
def encodes(self, x):
return (x - self.mean) / self.std
def decodes(self, x):
return x * self.std + self.mean
normalize = NormalizeMean()
normalize.setup([1, 2, 3, 4, 5])
normalize.mean3
Type-based multiple dispatch
Pass multiple functions with different parameter annotations to select behaviour by input type. This is useful for handling different image formats or numerical types in one transform.
This transform selects a function for an int or a str:
(6, 'ba')
When no type annotation matches an input, the transform returns that input unchanged.
Type conversion and preservation
Transform uses the wrapped function’s return type to control conversion in encodes and decodes. The return type can be explicit or implicit. The rules are:
- The result has the function’s return type, with conversion when needed.
- When the input’s runtime type is a subtype of that return type, the result preserves the input’s type.
- A return annotation of
Nonedisables type conversion and preservation.
Return type
FS is a subclass of float. Normal Python multiplication of an FS and a float returns a float:
float
With Transform, an FS return annotation makes the multiplication return an FS:
Type preservation
Without a return annotation, this multiplication transform preserves the input’s runtime type. Passing an FS returns an FS. The wrapped function alone would return a float:
Disabling conversion
Use a return annotation of None to disable type conversion and preservation:
Pipelines
A Pipeline applies transforms in sequence. This pipeline doubles a value and then normalizes it. decode reverses the transformations:
def double(x): return x*2.0
def halve(x): return x/2.0
dt = Transform(double,halve)
class NormalizeMean(Transform):
def setups(self, items):
self.mean = statistics.mean(items)
self.std = statistics.stdev(items)
def encodes(self, x):
return (x - self.mean) / self.std
def decodes(self, x):
return x * self.std + self.mean
normalize = NormalizeMean()
normalize.setup([1, 2, 3, 4, 5])
p = Pipeline((dt, normalize))
v = p(5)
v4.427188724235731
Documentation
See the documentation for the full API.
