Transform API¶
Transformers expose incremental learning and one-event mapping. Pipeline
learning applies their post-update transform; direct transform_one calls do
not learn.
Preprocessing¶
FeatureSchemaGuard ¶
Bases: BaseTransformer
Validate finite numeric values and a stable set of feature names.
Supply features to enforce an application schema from the first event.
If omitted, the first successfully learned event establishes the schema.
The transformer preserves feature names and values while returning them in
the established order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
Sequence[str] | None
|
Expected feature names, or |
None
|
sort_features
|
bool
|
Sort names when learning a schema from the first event. |
True
|
feature_names
property
¶
Return the configured or learned feature order.
learn_one ¶
Validate an event and commit its schema after successful validation.
learning_transaction ¶
Restore the feature schema if a pipeline update fails.
transform_one ¶
Validate and return an ordered, float-valued copy of an event.
MinMaxScaler ¶
Bases: BaseTransformer
Min-max scaler for normalizing features to a specified range.
Scales each feature linearly to a target range, typically [0, 1]. This scaling is useful for algorithms that are sensitive to the scale of input features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_range
|
tuple[float, float]
|
Target range for scaled features. |
(0, 1)
|
clip
|
bool
|
Bound transformed values to |
False
|
Examples:
from aberrant.transform.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
scaler.learn_one({"x": 5.0, "y": 10.0})
transformed = scaler.transform_one({"x": 3.0, "y": 8.0})
Initialize the MinMaxScaler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_range
|
tuple[float, float]
|
The desired range of transformed features (default is (0, 1)). |
(0, 1)
|
clip
|
bool
|
Whether transformed values are clipped to the target range. |
False
|
learning_transaction ¶
Restore extrema if this or a downstream pipeline stage rejects an event.
learn_one ¶
Update the min and max values for each feature in the input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary of feature-value pairs. |
required |
transform_one ¶
Scale the input data to the specified feature range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary of feature-value pairs. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
The scaled feature-value pairs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If feature hasn't been seen during learning. |
StandardScaler ¶
Bases: BaseTransformer
Standard scaler for normalizing features using z-score normalization.
Transforms features to have zero mean and unit variance (when with_std=True). This is useful for algorithms that assume features are normally distributed and on similar scales.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_std
|
bool
|
Whether to scale to unit variance. |
True
|
Examples:
from aberrant.transform.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.learn_one({"x": 5.0, "y": 10.0})
transformed = scaler.transform_one({"x": 3.0, "y": 8.0})
Initialize the StandardScaler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_std
|
bool
|
Whether normalization should be divided by standard deviation. |
True
|
learning_transaction ¶
Restore running moments if a pipeline update fails.
learn_one ¶
Update the mean and standard deviation for each feature incrementally.
Uses Welford's online algorithm for numerical stability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary of feature-value pairs. |
required |
transform_one ¶
Transform input data to z-scores (standard scores).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary of feature-value pairs. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
The standardized feature-value pairs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If feature hasn't been seen during learning. |
RollingRobustScaler ¶
RollingRobustScaler(window_size: int = 256, min_samples: int = 1, fallback_scale: float = 1.0)
Bases: BaseTransformer
Center each feature by its recent median and divide by its recent IQR.
Quartiles use linear interpolation at positions (n - 1) * q. Each
feature retains its last window_size observations independently;
missing features neither advance nor evict that feature's window. A zero
interquartile range uses fallback_scale, preserving novel deviations
from a constant reference instead of mapping every candidate to zero.
transform_one is read-only and uses provisional statistics even before
min_samples observations. Read is_ready to exclude this warm-up
from evaluation. Freeze a ready calibration before fitting a downstream
model when its stored references must share one coordinate system.
Adaptive updates do not re-express downstream points or split boundaries.
For F features in an event and window size W, adaptive learning costs O(F W log W), transformation O(F), and persistent storage O(D W) for D distinct learned features. Use a schema guard to bound D. Frozen learning validates inputs in O(F) and leaves calibration unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_size
|
int
|
Maximum observations retained per feature. |
256
|
min_samples
|
int
|
Minimum retained observations per learned feature for readiness; this does not disable provisional transformation. |
1
|
fallback_scale
|
float
|
Positive finite divisor used only when IQR is zero. |
1.0
|
sample_counts
property
¶
Return an owned snapshot of retained observations per feature.
is_ready
property
¶
Whether every learned feature has at least min_samples values.
An empty scaler is not ready. Learning a new feature can make an adaptive scaler unready again; use a schema guard for fixed inputs.
is_frozen
property
¶
Whether learning validates events without updating calibration.
learning_transaction ¶
Restore this event's windows if a downstream pipeline update fails.
learn_one ¶
Validate and stage all feature windows before committing any update.
transform_one ¶
Scale without learning; reject unseen features or non-finite output.
Select ¶
Rename ¶
Passthrough ¶
Bases: BaseTransformer
Copy a finite feature mapping without changing names or values.
learning_transaction ¶
Stateless operations need no restoration on downstream failure.
Projection¶
IncrementalPCA ¶
IncrementalPCA(n_components: int, n0: int = 50, keys: list[str] | None = None, tol: float = 1e-07, forgetting_factor: float | None = None)
Bases: BaseTransformer
Incremental Principal Component Analysis for online dimensionality reduction.
Implements an uncentered online PCA algorithm that can process data points one at a time, maintaining principal components without storing all historical data. Inputs are projected around the origin; center or standardize them upstream when conventional mean-centered PCA semantics are required.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of principal components to keep. |
required |
n0
|
int
|
Initial number of samples for warm-up phase. Must be at least
|
50
|
keys
|
list[str] | None
|
Feature names. If None, inferred from first sample. |
None
|
tol
|
float
|
Finite, non-negative tolerance for considering residual significance. |
1e-07
|
forgetting_factor
|
float | None
|
Weight for new values (0 < f < 1). If None, uses 1/t. |
None
|
Examples:
from aberrant.transform.projection import IncrementalPCA
pca = IncrementalPCA(n_components=2)
pca.learn_one({"x": 1.0, "y": 2.0, "z": 3.0})
transformed = pca.transform_one({"x": 1.5, "y": 2.5, "z": 3.5})
Initialize the IncrementalPCA transformer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of principal components to keep. |
required |
n0
|
int
|
Initial number of samples for warm-up phase before switching to online mode. Default is 50. |
50
|
keys
|
Optional[list[str]]
|
List of feature names. If None, they will be inferred from the first sample. Default is None. |
None
|
tol
|
float
|
Tolerance for considering whether a new data point contributes significantly to the subspace. Default is 1e-7. |
1e-07
|
forgetting_factor
|
Optional[float]
|
If None (default) it is a stationary process. Larger f will give new values more weight. Must be in the interval ]0, 1[. |
None
|
Implements an online PCA algorithm based on the incremental SVD approach from: - Brand, M. (2002). "Incremental Singular Value Decomposition of Uncertain Data with Missing Values" - Arora, R., Cotter, A., Livescu, K., & Srebro, N. (2012). "Stochastic optimization for PCA and PLS"
The implementation follows the 'incRpca' function from the R package 'onlinePCA: Online Principal Component Analysis' and uses the mathematical framework described in: - Cardot, H. & Degras, D. (2015). "Online Principal Component Analysis in High Dimension: Which Algorithm to Choose?"
Algorithm Steps: 1. Initialization phase: Collect n0 samples and perform uncentered PCA 2. Online phase: For each new sample x_t at time t: - Apply forgetting factor: λ ← (1-f)λ where f = 1/t - Scale new sample: x ← √f * x_t - Project onto current subspace: x̂ = U^T x - Compute residual: r = x - U x̂ - If ||r|| > tol, expand subspace with normalized residual - Update eigendecomposition of diag(λ) + x̂x̂^T - Keep top n_components eigenvalues/vectors
feature_names
property
¶
Established feature names in projection order.
learning_transaction ¶
Restore PCA state if a pipeline update fails; arrays are replaced, not edited.
learn_one ¶
Update PCA components incrementally using a single sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Dict[str, float]
|
A dictionary with feature names as keys and values as the data point dimensions. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
transform_one ¶
Transform a single data point using the learned PCA components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Dict[str, float]
|
A dictionary with feature names as keys and values as the data point dimensions. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Transformed data point as dictionary with component names as keys. |
RandomProjection ¶
Bases: BaseTransformer
Sparse Achlioptas random projection for streaming feature mappings.
A fixed matrix maps the input vector to n_components output values named
component_0, component_1, and so on. Matrix entries are sampled from
{-sqrt(3/k), 0, sqrt(3/k)}, where k is n_components. Learning
establishes feature order but does not adapt the matrix afterward.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_components
|
int
|
Number of projected dimensions. It cannot exceed the number of input features. |
required |
keys
|
list[str] | None
|
Explicit feature order. If omitted, the first learned mapping's insertion order is used. |
None
|
seed
|
int | None
|
Seed for the transformer's local NumPy generator. |
None
|
References
Achlioptas, D. (2003). Database-friendly random projections: Johnson-Lindenstrauss with binary coins. https://doi.org/10.1016/S0022-0000(03)00025-4
Initialize the projection and, when possible, its random matrix.
feature_names
property
¶
Established feature names in projection order.
learn_one ¶
Learn the number of dimensions from the first data point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary with feature names as keys and values as data point dimensions. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is invalid or |
learning_transaction ¶
Restore schema and matrix initialization if a pipeline update fails.
transform_one ¶
Transform a single data point using random projection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
dict[str, float]
|
A dictionary with feature names as keys and values as data point dimensions. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Transformed data point as dictionary with component names as keys. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If called before learning feature names. |
ValueError
|
If values are non-numeric or non-finite, or the feature schema differs from the learned schema. |