enchanter.preprocessing

Signal

Transforms

transforms.Compose

class enchanter.preprocessing.signal.transforms.Compose(transforms: List[Callable])[source]

Bases: object

Composes several transforms together.

Parameters

transforms (list of Transform objects) – list of transforms to compose.

Examples

>>> import torch
>>> x = torch.randn(512, 10)
>>> transform = Compose([
>>>     FixedWindow(128),
>>>     GaussianNoise(),
>>>     RandomScaling()
>>> ])
>>> y = transform(x)

Methods

insert(index, modules)

append(module)

extend(modules)

transforms.FixedWindow

class enchanter.preprocessing.signal.transforms.FixedWindow(window_size: int, start_position: Optional[int] = None)[source]

Bases: object

Cropping the input data with a fixed length window.

Parameters
  • window_size (int) – Length of the series to be trimmed from the input data.

  • start_position (Optional[int]) – Position to start clipping.

Examples

>>> import numpy as np
>>> x = np.random.randn(512, 18)    # [seq_len, features]
>>> fw = FixedWindow(128)
>>> out = fw(x)
>>> out.shape       # [128, 18]

transforms.GaussianNoise

class enchanter.preprocessing.signal.transforms.GaussianNoise(sigma: float = 0.01, mu: float = 0.0)[source]

Bases: object

Apply gaussian noise to input data.

Examples

>>> import torch
>>> x = torch.randn(512, 10)
>>> noise = GaussianNoise()
>>> y = noise(x)
Parameters
  • sigma – normal distribution paramter \(\sigma\)

  • mu – normal distribution paramter \(\mu\)

transforms.RandomScaling

class enchanter.preprocessing.signal.transforms.RandomScaling(start: float = 0.7, end: float = 1.1)[source]

Bases: object

Scaling the original sequence by a random value in the range start and end following Eq.

\[\mathcal{L_r}(\mathbf{ S }) = \mathbf{ S } \cdot ((\mathrm{end} - \mathrm{start}) * rand() + \mathrm{start})\]

References

An End-to-End Multi-Task and Fusion CNN for Inertial-Based Gait Recognition

Examples

>>> import torch
>>> x = torch.randn(512, 10)
>>> scale = RandomScaling()
>>> y = scale(x)
Parameters
  • start (float) – Starting point of scaling range.

  • end (float) – End point of scaling range.

transforms.Pad

class enchanter.preprocessing.signal.transforms.Pad(length: int, value: Optional[Union[int, float]] = None)[source]

Bases: object

Fills the end of the given series with the specified method.

Examples

>>> import torch
>>> x = torch.randn(10, 3)  # [seq_len, features]
>>> pad = Pad(20)
>>> y = pad(x)              # [20, 3]
>>> # OR
>>> pad = Pad(20, 1.0)
>>> y = pad(x)
Parameters
  • length (int) – Length of output series.

  • value (Optional[float]) – Value to fill.

Functions

FixedSlidingWindow

class enchanter.preprocessing.signal.FixedSlidingWindow(window_size: int, overlap_rate: Optional[float], step_size: Optional[int] = None)[source]

Bases: object

Fixed sliding window.

Examples::
>>> import numpy as np
>>> from enchanter.preprocessing import signal
>>> x = np.random.randn(1024, 23)
>>> y = np.random.randint(0, 9, 1024)
>>> sw = signal.FixedSlidingWindow(256, overlap_rate=0.5)
>>> x, y = sw(x, y)
>>> x.shape     # [6, 256, 23]
>>> y.shape     # [6, ]
Parameters
  • window_size (int) – Window size

  • overlap_rate (float) – overrap rate

  • step_size (Optional[int]) – step size

Raises

AssertionError – an error occur when argument overlap_rate under 0.0 or over 1.0.n error occurred.

Methods

transform(inputs[, verbose])

Apply Fixed Sliding Window

clean(labels)

Clean up

static clean(labels: numpy.ndarray) numpy.ndarray[source]

Clean up

Parameters

labels

Returns:

transform(inputs: numpy.ndarray, verbose: bool = False) numpy.ndarray[source]

Apply Fixed Sliding Window

Parameters
  • inputs – 2 or 3 dim of np.ndarray

  • verbose – if True, show progress bar

Returns

np.ndarray

adjust_sequences

enchanter.preprocessing.signal.adjust_sequences(sequences: List[numpy.ndarray], max_len: Optional[Union[int, Callable[[List[int]], int]]] = None, fill: Union[str, int, float] = 'ffill', dtype: Type = <class 'numpy.float32'>) numpy.ndarray[source]

The function to adjust the length of the series data to a certain value. For each sample, if the series is longer than max_len, the length is up to max_len, and the rest is ignored. If it is shorter than max_len, the missing part is filled in with the last value.

Parameters
  • sequences – A Python list whose elements have np.ndarray objects that are not constant in length. Each element is a 2D array, the 0th dimension is the length of the series, the 1st dimension is the number of features in the time series, and all samples must have the same number of features.

  • max_len – Processes all input elements to the specified length. If not specified, the largest sequence length in a given sample will be max_len. Also, given functions such as np.max, np.min, and np.mean, you can use them to generate new length series.

  • fill – If it is shorter than max_len, specify how to fill in the missing parts. If fill ='ffill', it will be filled with the last value. If a number (Python int or Python float) is given, use that value to fill the value. fill=["ffill" or int or float]

  • dtype – Specify the data type of NumPy. The data type of the output series is determined based on this value. dtype must be a float.

Examples

>>> import numpy as np
>>> x = [
>>>         np.array([[i] for i in [1, 2, 3, 4, 5]]),
>>>         np.array([[i] for i in [1, 2, 3, 4, 5, 6, 7, 8]]),
>>>         np.array([[i] for i in [1, 2, 3]]),
>>> ]
>>> out = adjust_sequences(x)
>>> out[-1]
>>> # array([[1.],
>>> #        [2.],
>>> #        [3.],
>>> #        [3.],
>>> #        [3.],
>>> #        [3.],
>>> #        [3.],
>>> #        [3.]])
>>> out = adjust_sequences(x, np.min)
>>> out
>>> # array([[[1],
>>> #         [2],
>>> #         [3]],
>>> #
>>> #        [[1],
>>> #         [2],
>>> #         [3]],
>>> #
>>> #        [[1],
>>> #         [2],
>>> #         [3]]])
Returns

Returns a 3D array of [Sample, Seq_len, Features].