Preprocess¶
The Preprocess
class is a core building block in AutonFeat
. This enables users to define custom preprocessors that can be applied to the signal before extracting features.
Bases: object
Represents a preprocessor to apply to a signal.
__call__(signal, *args, **kwargs)
¶
Apply the preprocessor to the signal provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signal | ndarray | The signal window to apply the preprocessor to. | required |
*args | Any | Additional arguments to pass to the preprocessor. | () |
**kwargs | Any | Additional keyword arguments to pass to the preprocessor. | {} |
Returns:
Type | Description |
---|---|
ndarray | The preprocessed signal. |
Raises:
Type | Description |
---|---|
NotImplementedError | If the preprocessor is not implemented. |
__init__(name='Not specified')
¶
Initialize a new preprocessor.
__repr__()
¶
Get the string representation of the preprocessor.
Returns:
Type | Description |
---|---|
str | The string representation of the preprocessor. |
__str__()
¶
Get the string representation of the preprocessor.
Returns:
Type | Description |
---|---|
str | The string representation of the preprocessor. |
get_name()
¶
Get the name of the preprocessor.
Returns:
Type | Description |
---|---|
str | The name of the preprocessor. |
set_name(name)
¶
Set the name of the preprocessor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | The new name of the preprocessor. | required |
Examples¶
In this example, we define a custom preprocessor that shifts the signal by some delta
value.
Define Custom Preprocessor¶
We define a custom preprocessor that performs this computation in the following way.
import numpy as np
from typing import Union, Callable
from autonfeat.core import Preprocess
class DeltaPreprocessor(Preprocess):
"""
Preprocess the signal by shifting the `signal` by some `delta` value.
"""
def __init__(self, name: str = "Delta") -> None:
super().__init__(name=name)
def __call__(self, signal: np.ndarray, delta: Union[int, float, np.int_, np.float_], where: Callable[[Union[int, float, np.int_, np.float_]], Union[bool, np.bool_]] = lambda x: not np.isnan(x)) -> np.ndarray:
where_fn = np.vectorize(where)
# Compute mask and multiply by distribution shift along axis
mask = where_fn(x)
shift = mask * delta
return x - shift
Apply Custom Preprocessor¶
The preprocessor can then be applied to a signal \(x\) as follows:
# Define the signal
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
# Define the delta
delta = 1
delta_preprocessor = DeltaPreprocessor()
# Apply the preprocessor
processed_signal = delta_preprocessor(x, delta=delta)
# See the result
print(processed_signal)
If you enjoy using AutonFeat
, please consider starring the repository ⭐️.