Implementation:Snorkel team Snorkel TransformationFunction Init
| Knowledge Sources | |
|---|---|
| Domains | Data_Augmentation, Data_Programming |
| Last Updated | 2026-02-14 20:00 GMT |
Overview
Concrete tool for defining data transformation functions that generate augmented examples, provided by the Snorkel library.
Description
The TransformationFunction class (extending Mapper) and the transformation_function decorator provide the interface for defining augmentation operations. A TF wraps a function that takes a data point and returns a modified copy or None.
The class inherits from Mapper which provides:
- Automatic deep copying of data points
- Field name mapping
- Preprocessor chaining
- Optional memoization
Usage
Import this class when defining augmentation operations. Use the decorator for simple TFs and the class form for complex transformations requiring field mappings.
Code Reference
Source Location
- Repository: snorkel
- File: snorkel/augmentation/tf.py
- Lines: L16-50
Signature
class TransformationFunction(Mapper):
"""Base class for transformation functions."""
pass
class LambdaTransformationFunction(LambdaMapper):
"""TF defined from a simple function."""
pass
class transformation_function:
"""Decorator to define a TransformationFunction."""
# Returns LambdaTransformationFunction via lambda_mapper
Import
from snorkel.augmentation import transformation_function
# or
from snorkel.augmentation.tf import TransformationFunction
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| f | Callable | Yes | Function taking a data point, returning modified copy or None |
| name | Optional[str] | No | TF identifier (defaults to function name) |
| pre | Optional[List[BaseMapper]] | No | Preprocessing chain |
Outputs
| Name | Type | Description |
|---|---|---|
| TransformationFunction instance | TransformationFunction | Callable that transforms data points |
| __call__ result | Optional[DataPoint] | Modified data point or None if not applicable |
Usage Examples
import random
from snorkel.augmentation import transformation_function
@transformation_function()
def tf_replace_word(x):
"""Replace a random word with a synonym."""
words = x.text.split()
if len(words) == 0:
return None
idx = random.randint(0, len(words) - 1)
words[idx] = "REPLACED"
x.text = " ".join(words)
return x
@transformation_function()
def tf_add_prefix(x):
"""Add a common prefix to text."""
x.text = "FYI: " + x.text
return x
# Test
from types import SimpleNamespace
dp = SimpleNamespace(text="Hello world")
result = tf_replace_word(dp)
print(result.text) # e.g., "REPLACED world"