Skip to content

Transform

One of the core building blocks of AutonFeat is the Transform abstraction. This enables users to define custom featurizers that can be applied to the sliding window intervals and is how we implement the build-in feature extractors.

Bases: object

Represents a transformation to apply to a signal.

__call__(signal_window, *args, **kwargs)

Apply the transformation to the signal window provided.

Parameters:

Name Type Description Default
signal_window ndarray

The signal window to transform.

required
*args Any

Additional arguments to pass to the transformation.

()
**kwargs Any

Additional keyword arguments to pass to the transformation.

{}

Returns:

Type Description
Union[float_, int_]

A scalar value representing the transformation of the signal.

Raises:

Type Description
NotImplementedError

If the transformation is not implemented.

__init__(name='Not specified')

Initialize a new transformation.

__repr__()

Get the string representation of the transformation.

Returns:

Type Description
str

The string representation of the transformation.

__str__()

Get the string representation of the transformation.

Returns:

Type Description
str

The string representation of the transformation.

get_name()

Get the name of the transformation.

Returns:

Type Description
str

The name of the transformation.

set_name(name)

Set the name of the transformation.

Parameters:

Name Type Description Default
name str

The new name of the transformation.

required

Examples

In the below example, we show how to define a custom featurizer that computes the mean of the signal.

Define Featurizer Function

For ease of use, we define a function that computes the mean of the signal.

import numpy as np

def mean_function(x):
    return np.mean(x)

Define Transform

Use the Transform abstraction to define the featurizer.

import numpy as np
from typing import Callable, Union
from autonfeat.core import Transform

class MeanTransform(Transform):
    def __init__(self, name: str = "Mean") -> None:
        super().__init__(name=name)

    def __call__(self, signal_window: np.ndarray, where: Callable[[Union[int, float, np.int_, np.float_]], Union[bool, np.bool_]] = lambda x: not np.isnan(x)) -> Union[np.float_, np.int_]:
        where_fn = np.vectorize(pyfunc=where)
        filtered_signal_window = signal_window[where_fn(signal_window)]
        return mean_function(filtered_signal_window)

Apply Transform

Using the SlidingWindow abstraction, we can apply the transform to the sliding window intervals.

import autonfeat as aft

# Random data
n_samples = 100
x = np.random.rand(n_samples)

# Create sliding window
ws = 10
ss = 10
window = aft.SlidingWindow(window_size=ws, step_size=ss)

# Create transform
tf = MeanTransform()

# Get featurizer
featurizer = window.use(tf)

# Get features
features = featurizer(x)

# Print features
print(window)
print(tf)
print(features)

If you enjoy using AutonFeat, please consider starring the repository ⭐️.