milia.features.window#

Per-window features for anomaly detection: shape, rolling statistics and spectrum.

Each row of the input is one window of readings, typically one meter-day. A detector run on raw readings alone misses the shape that separates a genuine anomaly from ordinary daily variation, so each window becomes its peak-normalised shape plus summary statistics and its strongest frequencies.

Functions

names(steps[, nfft])

Column names matching vector()'s layout.

rolling(profile)

Mean, standard deviation, mean absolute step change and range of one window.

spectrum(profile[, n])

The largest non-constant FFT magnitudes of one window, largest first.

vector(profile[, nfft])

Feature vector of one window: peak-normalised shape, rolling() and spectrum().

Classes

WindowFeatures([nfft])

Scikit-learn transformer applying vector() to every row.

milia.features.window.rolling(profile)#

Mean, standard deviation, mean absolute step change and range of one window.

Parameters:

profile (ArrayLike) – Readings of one window, shape (steps,).

Return type:

ndarray

Returns:

The four statistics, in that order.

Examples

>>> rolling(np.array([1.0, 2.0, 1.0, 2.0])).round(2)
array([1.5, 0.5, 1. , 1. ])
milia.features.window.spectrum(profile, n=2)#

The largest non-constant FFT magnitudes of one window, largest first.

Captures periodic structure a reading-by-reading comparison misses, such as a cycling appliance.

Parameters:
  • profile (ArrayLike) – Readings of one window.

  • n (int) – Number of magnitudes kept; zero-padded when the window is too short.

Return type:

ndarray

Returns:

Array of shape (n,).

Examples

>>> t = np.linspace(0, 2 * np.pi, 48, endpoint=False)
>>> spectrum(np.sin(t) + 0.1 * np.sin(4 * t), n=2).round(1)
array([24. ,  2.4])
milia.features.window.vector(profile, nfft=2)#

Feature vector of one window: peak-normalised shape, rolling() and spectrum().

Parameters:
  • profile (ArrayLike) – Readings of one window, shape (steps,).

  • nfft (int) – Number of FFT magnitudes appended.

Return type:

ndarray

Returns:

Array of shape (steps + 4 + nfft,).

Examples

>>> vector(np.array([1.0, 2.0, 1.0, 2.0]), nfft=1).shape
(9,)
milia.features.window.names(steps, nfft=2)#

Column names matching vector()’s layout.

Parameters:
  • steps (int) – Readings per window.

  • nfft (int) – Number of FFT magnitudes.

Return type:

list[str]

Returns:

shape0 ... shape<steps-1>, mean, std, change, range, fft1 ... fft<nfft>.

Examples

>>> names(2, nfft=1)
['shape0', 'shape1', 'mean', 'std', 'change', 'range', 'fft1']
class milia.features.window.WindowFeatures(nfft=2)#

Bases: TransformerMixin, BaseEstimator

Scikit-learn transformer applying vector() to every row.

Parameters:

nfft (int) – Number of FFT magnitudes kept per window.

Examples

>>> WindowFeatures(nfft=3).fit_transform(np.random.default_rng(0).random((5, 24))).shape
(5, 31)
fit(X, y=None)#

Record the window length.

Parameters:
  • X (ArrayLike) – Windows, one per row.

  • y (None) – Ignored; present for API consistency.

Return type:

WindowFeatures

Returns:

The fitted transformer.

transform(X)#

Apply vector() to every row.

Parameters:

X (ArrayLike) – Windows, one per row, of the fitted length.

Return type:

ndarray

Returns:

Array of shape (n, steps + 4 + nfft).

get_feature_names_out(input_features=None)#

Names of the output features (see names()).

Parameters:

input_features (None) – Ignored; present for API consistency.

Return type:

ndarray

Returns:

Feature names, in column order.